From aca6c40ace11642bb64b0d01cf599d8e0801c2a4 Mon Sep 17 00:00:00 2001 From: tiyn Date: Tue, 2 Aug 2022 16:44:34 +0200 Subject: [PATCH] templates: changed to dynamic building in jinja --- src/app.py | 39 +++++--- src/content.py | 179 ---------------------------------- src/templates/archive.html | 20 +++- src/templates/index.html | 21 +++- src/templates/rss.xml | 29 ++++-- src/templates/standalone.html | 27 ++++- src/templates/user.html | 17 ++++ 7 files changed, 123 insertions(+), 209 deletions(-) diff --git a/src/app.py b/src/app.py index 64c1b15..755ea61 100644 --- a/src/app.py +++ b/src/app.py @@ -6,7 +6,7 @@ from flask_wtf import CSRFProtect import os import config -import content as con_gen +from content import rating_to_star from database import Database from forms import LoginForm, RegisterForm, WriteForm @@ -26,7 +26,8 @@ login.login_view = "login" @app.context_processor def inject_title(): return dict(title=config.TITLE, style=config.STYLE, - description=config.DESCRIPTION, registration=config.ALLOW_REGISTRATION) + description=config.DESCRIPTION, \ + registration=config.ALLOW_REGISTRATION, r_to_star=rating_to_star) @app.errorhandler(404) @@ -36,36 +37,46 @@ def page_not_found(e): @app.route("/") def index(): - content = con_gen.gen_index_string() - return render_template("index.html", content_string=content) + entries = db.get_entries() + entries.reverse() + return render_template("index.html", entries=entries) @app.route("/archive") def archive(): - content = con_gen.gen_arch_string() - return render_template("archive.html", content_string=content) + entries = db.get_entries() + entries.sort(key=lambda y: y.item.name) + entries.reverse() + entries.sort(key=lambda y: y.item.date) + entries.reverse() + return render_template("archive.html", entries=entries) @app.route("/user/") def user(name): - content = con_gen.gen_user_string(name) - if content != "": - return render_template("user.html", name=name, content_string=content) + entries = db.get_entries_by_user(name) + entries.sort(key=lambda y: y.item.name) + entries.reverse() + entries.sort(key=lambda y: y.item.date) + entries.reverse() + if entries != []: + return render_template("user.html", name=name, entries=entries) abort(404) @app.route("/entry/") def entry(ident): - content = con_gen.gen_stand_string(ident) - if content != "": - return render_template("standalone.html", content_string=content) + entry = db.get_entry_by_id(ident) + if entry is not None: + return render_template("standalone.html", entry=entry) abort(404) @app.route("/feed") def feed(): - content = con_gen.get_rss_string() - rss_xml = render_template("rss.xml", content_string=content) + entries = db.get_entries() + entries.reverse() + rss_xml = render_template("rss.xml", entries=entries) return rss_xml diff --git a/src/content.py b/src/content.py index 4543adc..f77bbaf 100644 --- a/src/content.py +++ b/src/content.py @@ -1,5 +1,3 @@ -from flask import url_for - from database import Database db = Database() @@ -19,180 +17,3 @@ def rating_to_star(rating): if rating/20 % 1 >= 0.5: res += u"\u00BD" return res - - -def gen_arch_string(): - """ - Creates and returns a archive string of every file in ENTRY_DIR. - - Returns: - string: html-formatted archive-string. - """ - content_string = "" - last_year = "" - entries = db.get_entries() - if entries is None: - return "" - entries.sort(key=lambda y: y.item.name) - entries.reverse() - entries.sort(key=lambda y: y.item.date) - entries.reverse() - for entry in entries: - ident = entry.id - title = entry.item.name - print("title", title) - year = entry.item.date - rating = entry.rating - username = entry.user.name - if year != last_year: - if last_year != "": - content_string += "\n" - content_string += "

" + year + "

\n" - content_string += "
    \n" - last_year = year - content_string += "
  • " - content_string += title + " " + \ - rating_to_star(rating) + " by " + username - content_string += "
  • \n" - - return content_string - - -def gen_user_string(name): - """ - Creates and returns a archive string of every file in ENTRY_DIR. - - Returns: - string: html-formatted archive-string. - """ - content_string = "" - last_year = "" - entries = db.get_entries_by_user(name) - if entries is None: - return "" - entries.sort(key=lambda y: y.item.name) - entries.reverse() - entries.sort(key=lambda y: y.item.date) - entries.reverse() - for entry in entries: - ident = entry.id - title = entry.item.name - year = entry.item.date - rating = entry.rating - username = entry.user.name - if year != last_year: - if last_year != "": - content_string += "
\n" - content_string += "

" + year + "

\n" - content_string += "
    \n" - last_year = year - content_string += "
  • " - content_string += title + " " + \ - rating_to_star(rating) - content_string += "
  • \n" - - return content_string - - -def gen_index_string(): - """ - Create and returns a string including every file in the database as an - index. - - Returns: - string: html-formatted index string. - """ - content_string = "" - entries = db.get_entries() - if entries is None: - return "" - entries.reverse() - for entry in entries: - ident = entry.id - title = entry.item.name - year = entry.item.date - text = entry.text - rating = entry.rating - username = entry.user.name - reviewed = entry.reviewed - content_string += "
    \n" - content_string += "

    " + title + \ - " (" + year + ") " + rating_to_star(rating) + "

    \n" - content_string += "rated " + str(rating) + \ - "/100 by " \ - + username + " on " + str(reviewed) + "
    " - content_string += text - content_string += "
    " - content_string += "
    " - return content_string - - -def gen_stand_string(ident): - """ - Creates a html-string for an entry. - - Parameters: - ident: ident of an entry. - - Returns: - string: html-formatted string string equivalent to the file. - """ - entry = db.get_entry_by_id(ident) - content_string = "" - if entry is not None: - ident = entry.id - title = entry.item.name - year = entry.item.date - text = entry.text - rating = entry.rating - username = entry.user.name - reviewed = entry.reviewed - content_string += "

    " + title + \ - " (" + year + ") " - content_string += rating_to_star(rating) - content_string += "

    \n" - content_string += "rated " + str(rating) + \ - "/100 by " + \ - username + " on " + str(reviewed) + \ - "
    \n" - content_string += "[delete entry]" - content_string += text + "
    \n" - return content_string - - -def get_rss_string(): - """ - Create a rss-string of the blog and return it. - - Returns: - string: rss-string of everything that is in the ENTRY_DIR. - """ - content_string = "" - entries = db.get_entries() - if entries is None: - return "" - entries.reverse() - for entry in entries: - ident = entry.id - title = entry.item.name - year = entry.item.date - text = entry.text - rating = entry.rating - username = entry.user.name - reviewed = entry.reviewed - content_string += "\n" - content_string += "" + title + "(" + year + ") " + \ - rating_to_star(rating) + " by " + username + "\n" - content_string += "" + \ - url_for("index", _anchor=str(ident), _external=True) + \ - "\n" - content_string += "" + reviewed + "\n" - content_string += "" + text + "\n" - content_string += "\n" - return content_string diff --git a/src/templates/archive.html b/src/templates/archive.html index feab7e5..6410996 100644 --- a/src/templates/archive.html +++ b/src/templates/archive.html @@ -4,9 +4,23 @@

    Archive


    - {% autoescape off %} - {{ content_string }} - {% endautoescape %} + {% set ns = namespace(last_year="") %} + {% for entry in entries %} + {% if entry.item.date != ns.last_year %} + {% if ns.last_year != "" %} +
+ {% endif %} +

{{ entry.item.date }}

+
    + {% endif %} + {% set ns.last_year = entry.item.date %} +
  • + {{ entry.item.name }} + + {{ r_to_star(entry.rating) }} by username + +

  • + {% endfor %} {% endblock %} diff --git a/src/templates/index.html b/src/templates/index.html index 3587204..19d2c20 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -4,9 +4,24 @@

    Index


    - {% autoescape off %} - {{ content_string }} - {% endautoescape %} + {% for entry in entries %} +
    +

    + + {{ entry.item.name }} ({{ entry.item.date }}) {{ r_to_star(entry.rating) }} + +

    + + rated {{ entry.rating }}/100 by + + {{ entry.user.name }} + +
    + {% autoescape off %} + {{ entry.text }} + {% endautoescape %} +

    + {% endfor %}
    {% endblock %} diff --git a/src/templates/rss.xml b/src/templates/rss.xml index 7916e05..95ab4bf 100644 --- a/src/templates/rss.xml +++ b/src/templates/rss.xml @@ -4,13 +4,26 @@ {{ title }} {{ description }} -en-us -{{ url_for("feed", _external=True) }} - - -{% autoescape off %} -{{ content_string }} -{% endautoescape %} - + en-us + {{ url_for("feed", _external=True) }} + + {% for entry in entries %} + + + {{ entry.item.name }} ({{ entry.item.date }}) {{ r_to_star(entry.rating) }} by {{ entry.user.name }} + + + {{ url_for("index", _anchor=entry.id, _external=True) }} + + + {{ entry.reviewed }} + + + {% autoescape off %} + text + {% endautoescape %} + + + {% endfor %} diff --git a/src/templates/standalone.html b/src/templates/standalone.html index 81944bc..38368c2 100644 --- a/src/templates/standalone.html +++ b/src/templates/standalone.html @@ -3,9 +3,32 @@ {% block content %}
    +

    + {{ entry.item.name }} ({{ entry.item.date }}) + {{ r_to_star(entry.rating) }} +

    + + rated {{ entry.rating }}/100 by + + {{ entry.user.name }} + + on + + {{ entry.reviewed }} + +
    + {% if current_user.id == entry.user.id %} + + [ + + delete entry + + ] +
    + {% endif %} {% autoescape off %} - {{ content_string }} - {% endautoescape %} + {{ entry.text }} + {% endautoescape %}
    {% endblock %} diff --git a/src/templates/user.html b/src/templates/user.html index be8e08e..0e04128 100644 --- a/src/templates/user.html +++ b/src/templates/user.html @@ -4,6 +4,23 @@

    User: {{ User }}


    + {% set ns = namespace(last_year="") %} + {% for entry in entries %} + {% if entry.item.date != ns.last_year %} + {% if ns.last_year != "" %} +
+ {% endif %} +

{{ entry.item.date }}

+