split up views.py and moved resulting .py-files into submodule "views"

This commit is contained in:
eclipse 2025-04-22 16:53:54 +02:00
parent f0cc447350
commit cb9cb3df41
8 changed files with 53 additions and 27 deletions

View File

@ -26,8 +26,9 @@ def create_app():
init_db(app)
# register blueprints
from the_works import views
app.register_blueprint(views.bp)
from the_works.views import home, text
app.register_blueprint(text.bp)
app.register_blueprint(home.bp)
# load debug toolbar
toolbar = DebugToolbarExtension(app)

View File

@ -1,5 +1,10 @@
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" style="display: none;">
<!-- "plus" from https://heroicons.com/solid -->
<defs>
<symbol id="create">
<path fill-rule="evenodd" d="M12 3.75a.75.75 0 0 1 .75.75v6.75h6.75a.75.75 0 0 1 0 1.5h-6.75v6.75a.75.75 0 0 1-1.5 0v-6.75H4.5a.75.75 0 0 1 0-1.5h6.75V4.5a.75.75 0 0 1 .75-.75Z" clip-rule="evenodd" />
</symbol>
<!-- "pencil-square" from https://heroicons.com/solid -->
<symbol id="update">
<path d="M21.731 2.269a2.625 2.625 0 0 0-3.712 0l-1.157 1.157 3.712 3.712 1.157-1.157a2.625 2.625 0 0 0 0-3.712ZM19.513 8.199l-3.712-3.712-8.4 8.4a5.25 5.25 0 0 0-1.32 2.214l-.8 2.685a.75.75 0 0 0 .933.933l2.685-.8a5.25 5.25 0 0 0 2.214-1.32l8.4-8.4Z" />

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -1,6 +1,6 @@
<nav>
<ul>
<li><a href="{{ url_for('views.home') }}">Home</a></li>
<li><a href="{{ url_for('views.texte_show') }}">Texte</a></li>
<li><a href="{{ url_for('home.home') }}">Home</a></li>
<li><a href="{{ url_for('text.read') }}">Texte</a></li>
</ul>
</nav>

View File

@ -5,5 +5,5 @@
{% block heading %}Home{% endblock heading %}
{% block content %}
<p><a href="{{ url_for('views.texte_show') }}">Alle Texte</a></p>
<p><a href="{{ url_for('views.text.read') }}">Alle Texte</a></p>
{% endblock content %}

View File

@ -9,7 +9,10 @@
{% include "_icons.svg" %}
<article>
<section>
<button commandfor="textmodal" command="show-modal" onclick="textmodal.showModal()">Neuer Text …</button>
<button onclick="showCreateModal()" title="Text hinzufügen">
<!-- <svg viewbox="0 0 24 24"><use href="#create" /></svg> -->
Neu …
</button>
</section>
<table>
@ -31,8 +34,8 @@
<td title="Reihe">{{ text["reihe"] }}</td>
<td title="Textform">{{ text["textform"] }}</td>
<td title="Sprache">{{ text["sprache"] }}</td>
<td><a onclick="showUpdateModal('{{ text["titel"] }}', '{{ text["untertitel"] }}', '{{ text["r_id"] }}', '{{ text["tf_id"] }}', '{{ text["s_id"] }}', '{{ url_for("views.text_update", id=text["id"]) }}');" title="Eintrag bearbeiten"><svg viewbox="0 0 24 24"><use href="#update" /></svg></a></td>
<td><a onclick="return confirm('Eintrag wirklich löschen?');" href="{{ url_for('views.text_delete', id=text["id"]) }}" title="Eintrag löschen"><svg viewbox="0 0 24 24"><use href="#delete" /></svg></a></td>
<td><a onclick="showUpdateModal('{{ text["titel"] }}', '{{ text["untertitel"] }}', '{{ text["r_id"] }}', '{{ text["tf_id"] }}', '{{ text["s_id"] }}', '{{ url_for("text.update", id=text["id"]) }}');" title="Text bearbeiten"><svg viewbox="0 0 24 24"><use href="#update" /></svg></a></td>
<td><a onclick="return confirm('Eintrag wirklich löschen?');" href="{{ url_for('text.delete', id=text["id"]) }}" title="Text löschen"><svg viewbox="0 0 24 24"><use href="#delete" /></svg></a></td>
</tr>
{% endfor %}
</tbody>
@ -78,7 +81,7 @@
{% endfor %}
</select>
</label>
<button id="text_submit" type="submit" formmethod="post" formaction="{{ url_for('views.text_create') }}">OK</button>
<button id="text_submit" type="submit" formmethod="post" formaction="{{ url_for('text.create') }}">OK</button>
<button aria-label="close" formmethod="dialog" formnovalidate>Abbrechen</button>
</form>
</article>
@ -95,6 +98,17 @@
document.getElementById("text_submit").formAction = formaction;
document.getElementById("textmodal").showModal();
}
function showCreateModal() {
document.getElementById("dialog-heading").textContent = "Text hinzufügen";
document.getElementById("text_titel").value = "";
document.getElementById("text_untertitel").value = "";
document.getElementById('text_reihe').selectedIndex = "";
document.getElementById("text_textform").selectedIndex = "";
document.getElementById("text_sprache").selectedIndex = "";
document.getElementById("text_submit").formAction = "{{ url_for('text.create') }}";
document.getElementById("textmodal").showModal();
}
</script>
{% endblock content %}

View File

@ -0,0 +1 @@
pass

8
the_works/views/home.py Normal file
View File

@ -0,0 +1,8 @@
from flask import Blueprint, render_template
bp = Blueprint("home", __name__)
@bp.route("/")
def home():
return render_template("views/home.html")

View File

@ -1,16 +1,13 @@
from flask import Blueprint, render_template, request, redirect, flash, url_for
from sqlalchemy import select, insert, update, delete
from the_works.database import db
from the_works.models import Texte, Reihen, Sprachen, Textformen
from sqlalchemy import select, update, insert, delete
bp = Blueprint("views", __name__)
bp = Blueprint("text", __name__)
@bp.route("/")
def home():
return render_template("views/home.html")
@bp.route("/texte")
def texte_show():
@bp.route("/text")
@bp.route("/text/all")
def read():
# build ORM equivalent of SELECT statement
stmt = (
select(Texte, Reihen, Textformen, Sprachen)
@ -32,32 +29,32 @@ def texte_show():
"sprache": row.Sprachen.Sprache,
"s_id": str(row.Sprachen.ID) if row.Sprachen else ""
})
return render_template("views/texte.html", texte=texte, reihen=db.session.scalars(select(Reihen)), textformen=db.session.scalars(select(Textformen)), sprachen=db.session.scalars(select(Sprachen)))
return render_template("views/text.html", texte=texte, reihen=db.session.scalars(select(Reihen)), textformen=db.session.scalars(select(Textformen)), sprachen=db.session.scalars(select(Sprachen)))
@bp.route("/texte/create", methods=["POST"])
def text_create():
@bp.route("/text/create", methods=["POST"])
def create():
stmt = insert(Texte).values(form_to_dict(request.form))
db.session.execute(stmt)
db.session.commit()
flash("Eintrag erfolgreich hinzugefügt")
return redirect(url_for("views.texte_show"))
return redirect(url_for("views.text.read"))
@bp.route("/texte/update/<int:id>", methods=["POST"])
def text_update(id):
@bp.route("/text/update/<int:id>", methods=["POST"])
def update(id):
stmt = update(Texte).where(Texte.ID == id).values(form_to_dict(request.form))
db.session.execute(stmt)
db.session.commit()
flash("Eintrag erfolgreich geändert")
return redirect(url_for("views.texte_show"))
return redirect(url_for("views.text.read"))
@bp.route("/texte/delete/<int:id>")
def text_delete(id):
@bp.route("/text/delete/<int:id>")
def delete(id):
stmt = delete(Texte).where(Texte.ID == id)
db.session.execute(stmt)
db.session.commit()
flash("Eintrag erfolgreich gelöscht")
return redirect(url_for("views.texte_show"))
return redirect(url_for("views.text.read"))
def form_to_dict(form):