fix comment handling

This commit is contained in:
cellarspoon 2021-11-29 12:06:55 +01:00
parent 5d2ba100cf
commit 216c7e110d
No known key found for this signature in database
GPG Key ID: 03789458B3D0C410

View File

@ -1,5 +1,7 @@
"""Generates PDF cards from a calibre metadata.db.""" """Generates PDF cards from a calibre metadata.db."""
from textwrap import shorten
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
@ -21,44 +23,48 @@ def get_fields(db_path, fields):
session = init_session(db_path) session = init_session(db_path)
for book in session.query(Book).all(): for book in session.query(Book).all():
for comment in session.query(Comment).all(): if "title" in fields:
tag = "<font size=12>{}</font>".format(book.title)
ptitle = Paragraph(tag, styles["Italic"])
content.append(ptitle)
content.append(Spacer(1, 12))
if "title" in fields: if "uploaded_at" in fields:
tag = "<font size=12>{}</font>".format(book.title) tag = "<font size=10>Uploaded: {}</font>".format(book.timestamp)
ptitle = Paragraph(tag, styles["Italic"]) ptime = Paragraph(tag, styles["Normal"])
content.append(ptitle) content.append(ptime)
content.append(Spacer(1, 12)) content.append(Spacer(1, 12))
if "uploaded_at" in fields: if "comments" in fields:
tag = "<font size=10>Uploaded: {}</font>".format(book.timestamp) comments = ", ".join(
ptime = Paragraph(tag, styles["Normal"]) [
content.append(ptime) shorten(comment.text, width=50, placeholder="...")
content.append(Spacer(1, 12)) for comment in book.comments
]
)
tag = "<font size=10>{}</font>".format(comments)
pcomments = Paragraph(tag)
content.append(pcomments)
if "comments" in fields: if "authors" in fields:
tag = "<font size=10>{}</font>".format(comment.text) format_string = "<font size=12>{}</font>"
pcomments = Paragraph(tag) all_authors = [author.name for author in book.authors]
content.append(pcomments) glued_together = format_string.format(", ".join(all_authors))
if "authors" in fields: p = Paragraph(glued_together, styles["Normal"])
format_string = "<font size=12>{}</font>" content.append(p)
all_authors = [author.name for author in book.authors] content.append(PageBreak())
glued_together = format_string.format(", ".join(all_authors)) content.append(Spacer(6, 12))
p = Paragraph(glued_together, styles["Normal"]) if "tags" in fields:
content.append(p) format_string = "<font size=10>{}</font>"
content.append(PageBreak()) all_tags = [tag.name for tag in book.tags]
content.append(Spacer(6, 12)) tags_glued_together = format_string.format(", ".join(all_tags))
if "tags" in fields: p = Paragraph(tags_glued_together, styles["Normal"])
format_string = "<font size=10>{}</font>" content.append(p)
all_tags = [tag.name for tag in book.tags] content.append(PageBreak())
tags_glued_together = format_string.format(", ".join(all_tags)) content.append(Spacer(6, 12))
p = Paragraph(tags_glued_together, styles["Normal"])
content.append(p)
content.append(PageBreak())
content.append(Spacer(6, 12))
return content return content