added support for DB table "Reihe"

This commit is contained in:
eclipse 2025-05-02 17:46:54 +02:00
parent 4f58b076b6
commit 6c07f6653a
4 changed files with 173 additions and 1 deletions

View File

@ -19,7 +19,7 @@ def create_app():
init_db(app)
# register blueprints
from the_works.views import home, text, werk, verlag, sprache, textform, werksform, genre, pseudonym
from the_works.views import home, text, werk, verlag, sprache, textform, werksform, genre, pseudonym, reihe
app.register_blueprint(text.bp)
app.register_blueprint(home.bp)
app.register_blueprint(werk.bp)
@ -29,6 +29,7 @@ def create_app():
app.register_blueprint(werksform.bp)
app.register_blueprint(genre.bp)
app.register_blueprint(pseudonym.bp)
app.register_blueprint(reihe.bp)
### DEBUG
toolbar = DebugToolbarExtension(app)

View File

@ -18,5 +18,6 @@
<li><a href="{{ url_for('werksform.all') }}">Werksformen</a></li>
<li><a href="{{ url_for('genre.all') }}">Genres</a></li>
<li><a href="{{ url_for('pseudonym.all') }}">Pseudonyme</a></li>
<li><a href="{{ url_for('reihe.all') }}">Reihen</a></li>
</ul>
</nav>

View File

@ -0,0 +1,114 @@
{% extends 'base.html' %}
{% block title %}Reihen{% endblock title %}
{% block script %}
<script>
const SCRIPT_ROOT = {{ request.script_root | tojson }};
function showCreateModal() {
// set modal heading
document.getElementById("dialog-heading").textContent = "Reihe hinzufügen";
// empty inputs
document.getElementById("form_Titel").value = "";
document.getElementById("form_Verlag").selectedIndex = "";
// set form action
document.getElementById("form_submit").formAction = "{{ url_for('reihe.create') }}";
// show modal
document.getElementById("reihenmodal").showModal();
}
function showUpdateModal() {
// set modal heading
document.getElementById("dialog-heading").textContent = "Reihe bearbeiten";
// populate text input
document.getElementById("form_Titel").value = this.dataset.reihe;
document.getElementById("form_Verlag").selectedIndex = this.dataset.verlag;
// set form action
document.getElementById("form_submit").formAction = `${SCRIPT_ROOT}/reihe/update/${this.dataset.id}`;
// show modal
document.getElementById("reihenmodal").showModal();
}
window.onload = function () {
// initialise DataTable
let table = new DataTable('#reihentable', {
paging: false,
order: []
});
deRole("#reihentable");
// create and append "New"-button to
let button = document.createElement("button");
button.id = "create-button";
button.setAttribute("title", "Reihe hinzufügen");
button.innerHTML = "Neu …";
button.addEventListener("click", showCreateModal, false);
document.getElementById("reihentable_wrapper").firstElementChild.firstElementChild.appendChild(button);
// add event listeners
document.getElementById ("create-button").addEventListener("click", showCreateModal, false);
for (const el of document.querySelectorAll('.action-update') ) {
el.addEventListener("click", showUpdateModal, false);
}
}
</script>{% endblock script %}
{% block heading %}Reihen{% endblock heading %}
{% block content %}
{% include "_icons.svg" %}
<table id="reihentable">
<thead>
<tr>
<th>Reihe</th>
<th>Verlag</th>
<th colspan="2">Aktionen</th>
</tr>
</thead>
<tbody>
{% for reihe in reihen %}
<tr id="reihe-{{ reihe['id'] }}">
<td title="Reihe">{{ reihe["Titel"] }}</td>
<td title="Verlag">{{ reihe["Verlag"] }}</td>
<td class="action action-update" data-id="{{ reihe['id'] }}" data-reihe="{{reihe['Titel'] }}" data-verlag="{{reihe['v_id'] }}"><a href="#" title="Reihe bearbeiten"><svg viewbox="0 0 24 24"><use href="#update" /></svg></a></td>
<td id="delete-{{ reihe['id'] }}" class="action"><a onclick="return confirm('Eintrag wirklich löschen?');" href="{{ url_for('reihe.delete', id=reihe['id']) }}" title="Reihe löschen"><svg viewbox="0 0 24 24"><use href="#delete" /></svg></a></td>
</tr>
{% endfor %}
</tbody>
</table>
<dialog aria-labelledby="dialog-heading" id="reihenmodal">
<article>
<form id="reihe_detail_form" method="post" >
<header>
<button aria-label="close" rel="prev" onclick="reihenmodal.close()"></button>
<h1 id="dialog-heading">#</h1>
</header>
<fieldset>
<article>
<label>
Reihentitel (erforderlich)
<input id="form_Titel" name="form_Titel" aria-Label="Reihentitel" placeholder="Titel" required autofocus />
</label>
<label>
Verlag
<select id="form_Verlag" name="form_Verlag" aria-label="Verlag">
<option selected value="">kein Verlag</option>
{% for v in verlage %}<option value="{{ v.ID }}">{{ v.Verlag }}</option>{% endfor %}
</select>
</label>
</article>
</fieldset>
<footer class="grid">
<button id="form_submit" type="submit" formmethod="post" formaction="{{ url_for('reihe.create') }}">OK</button>
<button class="secondary" aria-label="close" formmethod="dialog" formnovalidate>Abbrechen</button>
</footer>
</form>
</article>
</dialog>
{% endblock content %}

56
the_works/views/reihe.py Normal file
View File

@ -0,0 +1,56 @@
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 Reihe, Verlag
bp = Blueprint("reihe", __name__)
@bp.route("/reihe")
@bp.route("/reihe/all")
def all():
rows = db.session.execute(select(Reihe, Verlag).join(Reihe.verlag, isouter=True))
reihen = []
for row in rows:
reihen.append({
"id": row.Reihe.ID,
"Titel": row.Reihe.Titel,
"Verlag": row.Verlag.Verlag if row.Verlag else "",
"v_id": row.Reihe.Verlag or ""
})
return render_template("views/reihe.html", reihen=reihen, verlage=db.session.scalars(select(Verlag)))
@bp.route("/text/reihe/<int:id>")
def read(id):
reihe = db.session.get(Genre, id)
return {
"Titel": reihe.Titel,
"Verlag": reihe.Verlag or ""
}
@bp.route("/reihe/create", methods=["POST"])
def create():
db.session.add(Reihe(
Titel = request.form["form_Titel"],
Verlag = request.form["form_Verlag"]
))
db.session.commit()
flash("Eintrag erfolgreich hinzugefügt")
return redirect(url_for("reihe.all"), code=303)
@bp.route("/reihe/update/<int:id>", methods=["POST"])
def update(id):
reihe = db.session.get(Reihe, id)
reihe.Titel = request.form["form_Titel"]
reihe.Verlag = request.form["form_Verlag"]
db.session.commit()
flash("Eintrag erfolgreich geändert")
return redirect(url_for("reihe.all"), code=303)
@bp.route("/reihe/delete/<int:id>")
def delete(id):
reihe = db.session.get(Reihe, id)
db.session.delete(reihe)
db.session.commit()
flash("Eintrag erfolgreich gelöscht")
return redirect(url_for("reihe.all"))