templates: changed to dynamic building in jinja

master
tiyn 2 years ago
parent d9f2e0f62c
commit aca6c40ace

@ -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/<name>")
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/<ident>")
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

@ -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 += "</ul>\n"
content_string += "<h2>" + year + "</h2>\n"
content_string += "<ul>\n"
last_year = year
content_string += "<li>"
content_string += title + "<a href=\"" + \
url_for("entry", ident=str(ident)) + "\"> " + \
rating_to_star(rating) + " by " + username
content_string += "</a><br></li>\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 += "</ul>\n"
content_string += "<h2>" + year + "</h2>\n"
content_string += "<ul>\n"
last_year = year
content_string += "<li>"
content_string += title + "<a href=\"" + \
url_for("entry", ident=str(ident)) + "\"> " + \
rating_to_star(rating)
content_string += "</a><br></li>\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 += "<div class=\"entry\">\n"
content_string += "<h1 id=\"" + str(ident) + "\"><a href=\"" + \
url_for("entry", ident=str(ident)) + "\">" + title + \
" (" + year + ") " + rating_to_star(rating) + "</a></h1>\n"
content_string += "<small>rated " + str(rating) + \
"/100 by <a href=\"" + url_for("user", name=username) + "\">" \
+ username + "</a> on " + str(reviewed) + "</small><br>"
content_string += text
content_string += "<br>"
content_string += "</div>"
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 += "<h1>" + title + \
" (" + year + ") "
content_string += rating_to_star(rating)
content_string += "</h1>\n"
content_string += "<small>rated " + str(rating) + \
"/100 by <a href=\"" + url_for("user", name=username) + "\">" + \
username + "</a> on <a href=\"" + \
url_for("index", _anchor=str(ident)) + "\">" + str(reviewed) + \
"</a></small><br>\n"
content_string += "<small>[<a href=\"" + \
url_for("delete_entry", ident=ident) + \
"\">delete entry</a>]</small>"
content_string += text + "<br>\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 += "<item>\n"
content_string += "<title>" + title + "(" + year + ") " + \
rating_to_star(rating) + " by " + username + "</title>\n"
content_string += "<guid>" + \
url_for("index", _anchor=str(ident), _external=True) + \
"</guid>\n"
content_string += "<pubDate>" + reviewed + "</pubDate>\n"
content_string += "<description>" + text + "</description>\n"
content_string += "</item>\n"
return content_string

@ -4,9 +4,23 @@
<div class="container">
<div class="archive">
<h1>Archive</h1><br>
{% 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 != "" %}
</ul>
{% endif %}
<h2> {{ entry.item.date }} </h2>
<ul>
{% endif %}
{% set ns.last_year = entry.item.date %}
<li>
{{ entry.item.name }}
<a href="{{ url_for('entry', ident=entry.id) }}">
{{ r_to_star(entry.rating) }} by username
</a>
</li><br>
{% endfor %}
</div>
</div>
{% endblock %}

@ -4,9 +4,24 @@
<div class="container">
<div class="blog">
<h1>Index</h1><br>
{% autoescape off %}
{{ content_string }}
{% endautoescape %}
{% for entry in entries %}
<div class="entry">
<h1 id="{{ entry.id }}">
<a href="{{ url_for('entry', ident=entry.id) }}">
{{ entry.item.name }} ({{ entry.item.date }}) {{ r_to_star(entry.rating) }}
</a>
</h1>
<small>
rated {{ entry.rating }}/100 by
<a href="{{ url_for('user', name=entry.user.name) }}">
{{ entry.user.name }}
</a>
</small><br>
{% autoescape off %}
{{ entry.text }}
{% endautoescape %}
</div><br>
{% endfor %}
</div>
</div>
{% endblock %}

@ -4,13 +4,26 @@
<channel>
<title>{{ title }}</title>
<description>{{ description }}</description>
<language>en-us</language>
<link>{{ url_for("feed", _external=True) }}</link>
<atom:link href="{{ url_for('feed', _external=True) }}" rel="self" type="application/rss+xml" />
{% autoescape off %}
{{ content_string }}
{% endautoescape %}
<language>en-us</language>
<link>{{ url_for("feed", _external=True) }}</link>
<atom:link href="{{ url_for('feed', _external=True) }}" rel="self" type="application/rss+xml" />
{% for entry in entries %}
<item>
<title>
{{ entry.item.name }} ({{ entry.item.date }}) {{ r_to_star(entry.rating) }} by {{ entry.user.name }}
</title>
<guid>
{{ url_for("index", _anchor=entry.id, _external=True) }}
</guid>
<pubDate>
{{ entry.reviewed }}
</pubDate>
<description>
{% autoescape off %}
text
{% endautoescape %}
</description>
</item>
{% endfor %}
</channel>
</rss>

@ -3,9 +3,32 @@
{% block content %}
<div class="container">
<div class="standalone">
<h1>
{{ entry.item.name }} ({{ entry.item.date }})
{{ r_to_star(entry.rating) }}
</h1>
<small>
rated {{ entry.rating }}/100 by
<a href="{{ url_for('user', name=entry.user.name) }}">
{{ entry.user.name }}
</a>
on
<a href="{{ url_for('index', _anchor=entry.id) }}">
{{ entry.reviewed }}
</a>
</small><br>
{% if current_user.id == entry.user.id %}
<small>
[
<a href="{{ url_for('delete_entry', ident=entry.id) }}">
delete entry
</a>
]
</small><br>
{% endif %}
{% autoescape off %}
{{ content_string }}
{% endautoescape %}
{{ entry.text }}
{% endautoescape %}<br>
</div>
</div>
{% endblock %}

@ -4,6 +4,23 @@
<div class="container">
<div class="archive">
<h1>User: {{ User }}</h1><br>
{% set ns = namespace(last_year="") %}
{% for entry in entries %}
{% if entry.item.date != ns.last_year %}
{% if ns.last_year != "" %}
</ul>
{% endif %}
<h2> {{ entry.item.date }} </h2>
<ul>
{% endif %}
{% set ns.last_year = entry.item.date %}
<li>
{{ entry.item.name }}
<a href="{{ url_for('entry', ident=entry.id) }}">
{{ r_to_star(entry.rating) }} by username
</a>
</li><br>
{% endfor %}
{% autoescape off %}
{{ content_string }}
{% endautoescape %}

Loading…
Cancel
Save