|
@ -1,45 +1,35 @@ |
|
|
|
|
|
"""Generates PDF cards from a calibre metadata.db.""" |
|
|
|
|
|
|
|
|
from calibrestekje import Book, Comment, Publisher, init_session |
|
|
from calibrestekje import Book, Comment, Publisher, init_session |
|
|
from reportlab.lib.pagesizes import * |
|
|
from reportlab.lib.pagesizes import * |
|
|
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet |
|
|
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet |
|
|
from reportlab.pdfgen import canvas |
|
|
from reportlab.pdfgen import canvas |
|
|
from reportlab.platypus import PageBreak, Paragraph, SimpleDocTemplate, Spacer |
|
|
from reportlab.platypus import PageBreak, Paragraph, SimpleDocTemplate, Spacer |
|
|
|
|
|
|
|
|
pagewidth, pageheight = landscape(A6) |
|
|
|
|
|
|
|
|
|
|
|
doc = SimpleDocTemplate( |
|
|
def make_cards(): |
|
|
"text.pdf", |
|
|
"""The main entrypoint for card generation.""" |
|
|
pagesize=landscape(A6), |
|
|
# TODO: thread arguments into this logic |
|
|
rightMargin=18, |
|
|
doc = create_doc("text.pdf") |
|
|
leftMargin=18, |
|
|
content = get_fields(doc) |
|
|
topMargin=0, |
|
|
doc.build(content) |
|
|
bottomMargin=18, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_fields(): |
|
|
|
|
|
"""Retrieve fields from the metadata.""" |
|
|
content = [] |
|
|
content = [] |
|
|
styles = getSampleStyleSheet() |
|
|
styles = getSampleStyleSheet() |
|
|
|
|
|
|
|
|
session = init_session("sqlite:///metadata.db") |
|
|
session = init_session("sqlite:///metadata.db") |
|
|
|
|
|
|
|
|
# if book id from book table is the same as book id from comment table, show me that |
|
|
|
|
|
for book in session.query(Book).all(): |
|
|
for book in session.query(Book).all(): |
|
|
for comment in session.query(Comment).all(): |
|
|
ptitle = Paragraph( |
|
|
if (book.id) == (comment.book): |
|
|
"<font size=12>{}</font>".format(book.title), styles["Italic"] |
|
|
print(book.id) |
|
|
) |
|
|
print(book.title) |
|
|
|
|
|
print(book.authors) |
|
|
|
|
|
print(book.timestamp) |
|
|
|
|
|
print(book.path) |
|
|
|
|
|
print(book.tags) |
|
|
|
|
|
print(comment.text) |
|
|
|
|
|
|
|
|
|
|
|
# create a paragraph and append content to it - e.g. book.title, book.authors etc |
|
|
|
|
|
ptitle = Paragraph("<font size=12>{}</font>".format(book.title), styles["Italic"]) |
|
|
|
|
|
ptime = Paragraph( |
|
|
ptime = Paragraph( |
|
|
"<font size=10>Uploaded: {}</font>".format(book.timestamp), styles["Normal"] |
|
|
"<font size=10>Uploaded: {}</font>".format(book.timestamp), styles["Normal"] |
|
|
) |
|
|
) |
|
|
pcomments = Paragraph("<font size=10>{}</font>".format(comment.text)) |
|
|
pcomments = Paragraph("<font size=10>{}</font>".format(comment.text)) |
|
|
|
|
|
|
|
|
# list comprehensions for authors and tags |
|
|
|
|
|
format_string = "<font size=12>{}</font>" |
|
|
format_string = "<font size=12>{}</font>" |
|
|
all_authors = [author.name for author in book.authors] |
|
|
all_authors = [author.name for author in book.authors] |
|
|
glued_together = format_string.format(", ".join(all_authors)) |
|
|
glued_together = format_string.format(", ".join(all_authors)) |
|
@ -48,30 +38,12 @@ for book in session.query(Book).all(): |
|
|
all_tags = [tag.name for tag in book.tags] |
|
|
all_tags = [tag.name for tag in book.tags] |
|
|
tags_glued_together = format_string.format(", ".join(all_tags)) |
|
|
tags_glued_together = format_string.format(", ".join(all_tags)) |
|
|
|
|
|
|
|
|
# format_string = '<font size=10>{}</font>' |
|
|
|
|
|
# all_comments = [comment.name for comment in comment.text] |
|
|
|
|
|
# comments_split_apart = format_string.format("".split(all_comments)[:50]) |
|
|
|
|
|
|
|
|
|
|
|
# import ipdb; ipdb.set_trace() |
|
|
|
|
|
|
|
|
|
|
|
# append the other content |
|
|
|
|
|
content.append(ptitle) |
|
|
content.append(ptitle) |
|
|
content.append(Spacer(1, 12)) |
|
|
content.append(Spacer(1, 12)) |
|
|
content.append(ptime) |
|
|
content.append(ptime) |
|
|
content.append(Spacer(1, 12)) |
|
|
content.append(Spacer(1, 12)) |
|
|
content.append(pcomments) |
|
|
content.append(pcomments) |
|
|
|
|
|
|
|
|
# alternative way to list multiple authors... (without list comprehensions) |
|
|
|
|
|
# first = True |
|
|
|
|
|
# author_text = "" |
|
|
|
|
|
# for author in book.authors: |
|
|
|
|
|
# if not first: |
|
|
|
|
|
# author_text += ", " |
|
|
|
|
|
# author_text += author.name |
|
|
|
|
|
# first = False |
|
|
|
|
|
# author_text = "<font size=12>{}</font>".format(author_text) |
|
|
|
|
|
|
|
|
|
|
|
# gluing together authors, tags |
|
|
|
|
|
p = Paragraph(glued_together, styles["Normal"]) |
|
|
p = Paragraph(glued_together, styles["Normal"]) |
|
|
content.append(p) |
|
|
content.append(p) |
|
|
content.append(PageBreak()) |
|
|
content.append(PageBreak()) |
|
@ -82,4 +54,16 @@ for book in session.query(Book).all(): |
|
|
content.append(PageBreak()) |
|
|
content.append(PageBreak()) |
|
|
content.append(Spacer(6, 12)) |
|
|
content.append(Spacer(6, 12)) |
|
|
|
|
|
|
|
|
doc.build(content) |
|
|
return content |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_doc(filename): |
|
|
|
|
|
"""Build the Report Lab document template.""" |
|
|
|
|
|
return SimpleDocTemplate( |
|
|
|
|
|
filename, |
|
|
|
|
|
pagesize=landscape(A6), |
|
|
|
|
|
rightMargin=18, |
|
|
|
|
|
leftMargin=18, |
|
|
|
|
|
topMargin=0, |
|
|
|
|
|
bottomMargin=18, |
|
|
|
|
|
) |
|
|