implemented all CRUD operations for Texte
This commit is contained in:
parent
3031a87d5d
commit
759012d218
@ -5,6 +5,8 @@
|
|||||||
{% block heading %}Texte{% endblock heading %}
|
{% block heading %}Texte{% endblock heading %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
|
{% include "_icons.svg" %}
|
||||||
<article>
|
<article>
|
||||||
<section>
|
<section>
|
||||||
<button commandfor="textmodal" command="show-modal" onclick="textmodal.showModal()">Neuer Text …</button>
|
<button commandfor="textmodal" command="show-modal" onclick="textmodal.showModal()">Neuer Text …</button>
|
||||||
@ -22,15 +24,15 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for row in rows %}
|
{% for text in texte %}
|
||||||
<tr>
|
<tr id="text-{{ text['id'] }}">
|
||||||
<td>{{ row.Texte.Titel }}</td>
|
<td title="Titel">{{ text["titel"] }}</td>
|
||||||
<td>{{ row.Texte.Untertitel }}</td>
|
<td title="Untertitel">{{ text["untertitel"] }}</td>
|
||||||
<td>{{ row.Reihen.Reihentitel }}</td>
|
<td title="Reihe">{{ text["reihe"] }}</td>
|
||||||
<td>{{ row.Textformen.Textform }}</td>
|
<td title="Textform">{{ text["textform"] }}</td>
|
||||||
<td>{{ row.Sprachen.Sprache }}</td>
|
<td title="Sprache">{{ text["sprache"] }}</td>
|
||||||
<td><a href="{{ url_for('views.text_update', id=row.Texte.ID) }}">edit</a></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 href="{{ url_for('views.text_delete', id=row.Texte.ID) }}">delete</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>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
@ -46,15 +48,15 @@
|
|||||||
<form>
|
<form>
|
||||||
<label>
|
<label>
|
||||||
Titel (erforderlich)
|
Titel (erforderlich)
|
||||||
<input name="text_titel" placeholder="Titel" required />
|
<input id="text_titel" name="text_titel" placeholder="Titel" required />
|
||||||
</label>
|
</label>
|
||||||
<label>
|
<label>
|
||||||
Untertitel
|
Untertitel
|
||||||
<input name="text_untertitel" placeholder="Untertitel" />
|
<input id="text_untertitel" name="text_untertitel" placeholder="Untertitel" />
|
||||||
</label>
|
</label>
|
||||||
<label>
|
<label>
|
||||||
Reihe
|
Reihe
|
||||||
<select name="text_reihe" aria-label="Der Text gehört zur Reihe …">
|
<select id="text_reihe" name="text_reihe" aria-label="Der Text gehört zur Reihe …">
|
||||||
<option selected value="">keine Reihe</option>
|
<option selected value="">keine Reihe</option>
|
||||||
{% for r in reihen %}<option value="{{ r.ID }}">{{ r.Reihentitel }}</option>
|
{% for r in reihen %}<option value="{{ r.ID }}">{{ r.Reihentitel }}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
@ -62,7 +64,7 @@
|
|||||||
</label>
|
</label>
|
||||||
<label>
|
<label>
|
||||||
Textform (erforderlich)
|
Textform (erforderlich)
|
||||||
<select name="text_textform" aria-label="Textform" required>
|
<select id="text_textform" name="text_textform" aria-label="Textform" required>
|
||||||
<option selected disabled value="">Textform auswählen …</option>
|
<option selected disabled value="">Textform auswählen …</option>
|
||||||
{% for tf in textformen %}<option value="{{ tf.ID }}">{{ tf.Textform }}</option>
|
{% for tf in textformen %}<option value="{{ tf.ID }}">{{ tf.Textform }}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
@ -70,16 +72,31 @@
|
|||||||
</label>
|
</label>
|
||||||
<label>
|
<label>
|
||||||
Sprache (erforderlich)
|
Sprache (erforderlich)
|
||||||
<select name="text_sprache" aria-label="Sprache des Textes" required>
|
<select id="text_sprache" name="text_sprache" aria-label="Sprache des Textes" required>
|
||||||
<option selected disabled value="">Sprache auswählen …</option>
|
<option selected disabled value="">Sprache auswählen …</option>
|
||||||
{% for s in sprachen %}<option value="{{ s.ID }}">{{ s.Sprache }}</option>
|
{% for s in sprachen %}<option value="{{ s.ID }}">{{ s.Sprache }}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
<button type="submit" formmethod="post" formaction="{{ url_for('views.text_create') }}">OK</button>
|
<button id="text_submit" type="submit" formmethod="post" formaction="{{ url_for('views.text_create') }}">OK</button>
|
||||||
<button aria-label="close" formmethod="dialog" formnovalidate>Abbrechen</button>
|
<button aria-label="close" formmethod="dialog" formnovalidate>Abbrechen</button>
|
||||||
</form>
|
</form>
|
||||||
</article>
|
</article>
|
||||||
</dialog>
|
</dialog>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function showUpdateModal(titel, untertitel, reihe, textform, sprache, formaction) {
|
||||||
|
document.getElementById("dialog-heading").textContent = "Text bearbeiten";
|
||||||
|
document.getElementById("text_titel").value = titel;
|
||||||
|
document.getElementById("text_untertitel").value = untertitel;
|
||||||
|
console.log("'reihe' is " + reihe);
|
||||||
|
// document.getElementById("text_reihe").selectedIndex = reihe;
|
||||||
|
document.querySelector('#text_reihe [value="' + reihe + '"]').selected = true;
|
||||||
|
document.getElementById("text_textform").selectedIndex = textform;
|
||||||
|
document.getElementById("text_sprache").selectedIndex = sprache;
|
||||||
|
document.getElementById("text_submit").formAction = formaction;
|
||||||
|
document.getElementById("textmodal").showModal();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
from flask import Blueprint, render_template, request, redirect, flash, url_for
|
from flask import Blueprint, render_template, request, redirect, flash, url_for
|
||||||
from the_works.database import db
|
from the_works.database import db
|
||||||
from the_works.models import Texte, Reihen, Sprachen, Textformen
|
from the_works.models import Texte, Reihen, Sprachen, Textformen
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select, update, insert, delete
|
||||||
|
|
||||||
bp = Blueprint("views", __name__)
|
bp = Blueprint("views", __name__)
|
||||||
|
|
||||||
@ -11,39 +11,60 @@ def home():
|
|||||||
|
|
||||||
@bp.route("/texte")
|
@bp.route("/texte")
|
||||||
def texte_show():
|
def texte_show():
|
||||||
|
# build ORM equivalent of SELECT statement
|
||||||
stmt = (
|
stmt = (
|
||||||
select(Texte, Reihen, Textformen, Sprachen)
|
select(Texte, Reihen, Textformen, Sprachen)
|
||||||
.join(Texte.textform, isouter=True)
|
.join(Texte.textform, isouter=True)
|
||||||
.join(Texte.reihe, isouter=True)
|
.join(Texte.reihe, isouter=True)
|
||||||
.join(Texte.sprache, isouter=True)
|
.join(Texte.sprache, isouter=True)
|
||||||
)
|
)
|
||||||
return render_template("views/texte.html", rows=db.session.execute(stmt), reihen=db.session.scalars(select(Reihen)), textformen=db.session.scalars(select(Textformen)), sprachen=db.session.scalars(select(Sprachen)))
|
# condense result into list of dicts
|
||||||
|
texte = []
|
||||||
|
for row in db.session.execute(stmt):
|
||||||
|
texte.append({
|
||||||
|
"id": row.Texte.ID,
|
||||||
|
"titel": row.Texte.Titel,
|
||||||
|
"untertitel": row.Texte.Untertitel or "",
|
||||||
|
"reihe": row.Reihen.Reihentitel if row.Reihen else "",
|
||||||
|
"r_id": str(row.Reihen.ID) if row.Reihen else "",
|
||||||
|
"textform": row.Textformen.Textform if row.Textformen else "",
|
||||||
|
"tf_id": str(row.Textformen.ID) if row.Textformen else "",
|
||||||
|
"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)))
|
||||||
|
|
||||||
@bp.route("/texte/create", methods=["POST"])
|
@bp.route("/texte/create", methods=["POST"])
|
||||||
def text_create():
|
def text_create():
|
||||||
db = get_db()
|
stmt = insert(Texte).values(form_to_dict(request.form))
|
||||||
try:
|
db.session.execute(stmt)
|
||||||
db.execute(
|
db.session.commit()
|
||||||
"INSERT INTO texte (Titel, Untertitel, Reihe, Textform, Originalsprache) VALUES (?, ?, ?, ?, ?)",
|
flash("Eintrag erfolgreich hinzugefügt")
|
||||||
(
|
return redirect(url_for("views.texte_show"))
|
||||||
request.form["text_titel"],
|
|
||||||
request.form["text_untertitel"] or None,
|
@bp.route("/texte/update/<int:id>", methods=["POST"])
|
||||||
request.form["text_reihe"] or None,
|
def text_update(id):
|
||||||
request.form["text_textform"],
|
stmt = update(Texte).where(Texte.ID == id).values(form_to_dict(request.form))
|
||||||
request.form["text_sprache"]
|
db.session.execute(stmt)
|
||||||
)
|
db.session.commit()
|
||||||
)
|
flash("Eintrag erfolgreich geändert")
|
||||||
db.commit()
|
|
||||||
flash("Neuen Text in Datenbank eingetragen")
|
|
||||||
except:
|
|
||||||
flash("Fehler beim Eintragen in die Datenbank", "error")
|
|
||||||
|
|
||||||
return redirect(url_for("views.texte_show"))
|
return redirect(url_for("views.texte_show"))
|
||||||
|
|
||||||
@bp.route("/texte/update/<int:id>")
|
|
||||||
def text_update():
|
|
||||||
pass
|
|
||||||
|
|
||||||
@bp.route("/texte/delete/<int:id>")
|
@bp.route("/texte/delete/<int:id>")
|
||||||
def text_delete():
|
def text_delete(id):
|
||||||
pass
|
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"))
|
||||||
|
|
||||||
|
|
||||||
|
def form_to_dict(form):
|
||||||
|
return {
|
||||||
|
"Titel": form["text_titel"],
|
||||||
|
"Untertitel": form["text_untertitel"] or None,
|
||||||
|
"Reihe": form["text_reihe"] or None,
|
||||||
|
"Textform": form["text_textform"],
|
||||||
|
"Originalsprache": form["text_sprache"]
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user