Skip to content

Commit

Permalink
finishing n2n
Browse files Browse the repository at this point in the history
  • Loading branch information
fifthaccess committed Jun 7, 2022
1 parent ca58175 commit a377584
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 95 deletions.
5 changes: 3 additions & 2 deletions controllers/lied.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def lied_edit():
item_to_edit_details = item_to_edit_details_query.\
filter(LiedKuenstler.LiedId == edit_lied_id).\
order_by(LiedKuenstler.KuenstlerId).all()

if request.method == 'POST':
print("Post")
if edit_lied_form.validate_on_submit():
Expand All @@ -133,8 +133,9 @@ def lied_edit():
edit_lied_form.Kuenstleranzahl.data = item_to_edit.Kuenstleranzahl
edit_lied_form.Liedname.data = item_to_edit.Liedname
edit_lied_form.Erscheinungsdatum.data = item_to_edit.Erscheinungsdatum

return render_template(
"lieder/editlieder.html",
headline="Edit Lieder",
lieder=item_to_edit_details,
kuenstlers_lied=item_to_edit_details,
form=edit_lied_form)
127 changes: 45 additions & 82 deletions controllers/liedkuenstler.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from unittest import expectedFailure
from flask import redirect, request
from flask.templating import render_template
from flask import Blueprint
import sqlalchemy
import sqlalchemy.orm
from forms.lied_kuenstler import LiedKuenstlerForm ,deleteLiedKuenstlerForm
from model.models import Kuenstler, db, LiedKuenstler
from forms.lied_kuenstler import LiedKuenstlerForm, deleteLiedKuenstlerForm
from model.models import Kuenstler, db, LiedKuenstler


lied_kuenstler_blueprint = Blueprint('lied_kuenstler_blueprint', __name__)
Expand All @@ -25,63 +24,72 @@ def Lied_kuenstler_add():
session: sqlalchemy.orm.scoping.scoped_session = db.session
item_to_attach_id = request.args["itemid"]
add_lied_kuenstler_form = LiedKuenstlerForm()
connected_kuenstler = session.query(LiedKuenstler).filter(LiedKuenstler.LiedId == item_to_attach_id).all()
all_kuenstler = session.query(Kuenstler).order_by(Kuenstler.KuenstlerId).all()
connected_kuenstler = session.query(LiedKuenstler).filter(
LiedKuenstler.LiedId == item_to_attach_id).all()
all_kuenstler = session.query(Kuenstler).order_by(
Kuenstler.KuenstlerId).all()
kuenstler = []
try:
try:
for n in all_kuenstler:
is_connected = 0
for i in connected_kuenstler:
if n == i:
kuenstler.append(session.query(Kuenstler).filter(not(Kuenstler.KuenstlerId == i.KuenstlerId)).first())
if n.KuenstlerId == i.KuenstlerId:
is_connected = 1

if is_connected == 0:
kuenstler.append(n)

except AttributeError:
kuenstler = session.query(Kuenstler).order_by(Kuenstler.KuenstlerId).all()
kuenstler = session.query(Kuenstler).order_by(
Kuenstler.KuenstlerId).all()
if request.method == 'POST':
print("f")
if add_lied_kuenstler_form.validate_on_submit():
print(add_lied_kuenstler_form.CheckedCheckboxes.data)
kuenstler_ids_to_add = add_lied_kuenstler_form.CheckedCheckboxes.data

new_Lied_Kuenstler = LiedKuenstler()

kuenstler_ids_to_add = list(add_lied_kuenstler_form.CheckedCheckboxes.data.split(","))
print("data is valid")
# new_manager.ManagerId = add_manager_form.ManagerID.data
for i in kuenstler_ids_to_add:
new_Lied_Kuenstler.KuenstlerId = int(i)
new_Lied_Kuenstler.LiedId = item_to_attach_id
db.session.add(new_Lied_Kuenstler)

db.session.commit()

try:
for i in kuenstler_ids_to_add:
new_Lied_Kuenstler = LiedKuenstler()
new_Lied_Kuenstler.KuenstlerId = int(i)
new_Lied_Kuenstler.LiedId = item_to_attach_id
db.session.add(new_Lied_Kuenstler)
db.session.commit()
except ValueError:
print("value error")
return redirect("/lieder/edit?itemid=" + item_to_attach_id)

else:
return render_template(
"liedkuenstler/liedkuenstleradd.html",
headline="Add Lieder",
kuenstlers = kuenstler,
form = add_lied_kuenstler_form)
"liedkuenstler/liedkuenstleradd.html",
headline="Add Lieder",
kuenstlers=kuenstler,
form=add_lied_kuenstler_form)

else:
return render_template(
"liedkuenstler/liedkuenstleradd.html",
headline="Add Lieder",
kuenstlers = kuenstler,
form = add_lied_kuenstler_form)
kuenstlers=kuenstler,
form=add_lied_kuenstler_form)


@lied_kuenstler_blueprint.route('/lied_kuenstler/delete', methods=["Get", "Post"])
def lied_kuenstler_delete():

session: sqlalchemy.orm.scoping.scoped_session = db.session
item_to_attach_id = request.args["itemid"]
connected_kuenstler = session.query(LiedKuenstler).filter(LiedKuenstler.LiedId == item_to_attach_id).all()
connected_kuenstler = session.query(LiedKuenstler).filter(
LiedKuenstler.LiedId == item_to_attach_id).all()
kuenstler = []
try:
try:
for i in connected_kuenstler:
kuenstler.append(session.query(Kuenstler).filter(Kuenstler.KuenstlerId == i.KuenstlerId).first())
kuenstler.append(session.query(Kuenstler).filter(
Kuenstler.KuenstlerId == i.KuenstlerId).first())
except AttributeError:
kuenstler = session.query(Kuenstler).order_by(Kuenstler.KuenstlerId).all()
kuenstler = session.query(Kuenstler).order_by(
Kuenstler.KuenstlerId).all()

del_form = deleteLiedKuenstlerForm()

Expand All @@ -94,67 +102,22 @@ def lied_kuenstler_delete():
print(del_form.CheckedCheckboxes.data)
for i in delete_id_list:
print("deleting now data with id " + i)
itemToDelete = db.session.query(Lied).filter(Lied.LiedId == i)
itemToDelete = db.session.query(LiedKuenstler).filter(sqlalchemy.and_(LiedKuenstler.LiedId == item_to_attach_id, LiedKuenstler.KuenstlerId == i))
itemToDelete.delete()
db.session.commit()
print("deleted data with id " + i)

lied = session.query(Lied).order_by(Lied.LiedId).all()
return render_template(
"lieder/viewlieder.html",
lieder=lied,
headline="Lieder",
form=del_form)
return redirect("/lieder/edit?itemid=" + item_to_attach_id)
else:
print("invalide Form")
return render_template(
"lieder/deletelieder.html",
lieder=lied,
headline="Delete Lieder",
"liedkuenstler/liedkuenstlerdelete.html",
headline="Add Lieder",
kuenstlers=kuenstler,
form=del_form)

else:
return render_template(
"liedkuenstler/liedkuenstlerdelete.html",
headline="Add Lieder",
kuenstlers = kuenstler,
form = del_form)


@lied_kuenstler_blueprint.route('/lied_kuenstler/edit', methods=["Get", "Post"])
def lied_edit():
session: sqlalchemy.orm.scoping.scoped_session = db.session
edit_lied_id = request.args["itemid"]
edit_lied_form = EditLiedForm()

item_to_edit = db.session.query(Lied).filter(
Lied.LiedId == edit_lied_id).first()

item_to_edit_details_query: sqlalchemy.orm.query.Query = session.query(LiedKuenstler)

item_to_edit_details = item_to_edit_details_query.\
filter(LiedKuenstler.LiedId == edit_lied_id).\
order_by(LiedKuenstler.KuenstlerId).all()

if request.method == 'POST':
print("Post")
if edit_lied_form.validate_on_submit():
print("is validate")

item_to_edit.LiedId = edit_lied_form.LiedId.data
item_to_edit.Kuenstleranzahl = edit_lied_form.Kuenstleranzahl.data
item_to_edit.Liedname = edit_lied_form.Liedname.data
item_to_edit.Erscheinungsdatum = edit_lied_form.Erscheinungsdatum.data
db.session.commit()
return redirect("/lieder")
else:

edit_lied_form.LiedId.data = item_to_edit.LiedId
edit_lied_form.Kuenstleranzahl.data = item_to_edit.Kuenstleranzahl
edit_lied_form.Liedname.data = item_to_edit.Liedname
edit_lied_form.Erscheinungsdatum.data = item_to_edit.Erscheinungsdatum
return render_template(
"lieder/editlieder.html",
headline="Edit Lieder",
lieder=item_to_edit_details,
form=edit_lied_form)
kuenstlers=kuenstler,
form=del_form)
23 changes: 13 additions & 10 deletions templates/lieder/editlieder.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@
<table class="table">
<thead>
<tr>
<th scope="col">LiedId</th>
<th scope="col">Kuenstleranzahl</th>
<th scope="col">Liedname</th>
<th scope="col">Erscheinungsdatum</th>
<th scope="col">KuenstlerId</th>
<th scope="col">ManagerID</th>
<th scope="col">Vorname</th>
<th scope="col">Nachname</th>
<th scope="col">Herkunftsland</th>
<th scope="col">Gehalt</th>


</tr>
</thead>
<tbody>
{% for lied in lieder %}
{% for kuenstler_lied in kuenstlers_lied %}
<tr>
<td>{{lied.LiedId}}</td>
<td>{{lied.Kuenstleranzahl}}</td>
<td>{{lied.Liedname}}</td>
<td>{{lied.Erscheinungsdatum}}</td>

<td>{{kuenstler_lied.kuenstler.KuenstlerId}}</td>
<td>{{kuenstler_lied.kuenstler.ManagerId}}</td>
<td>{{kuenstler_lied.kuenstler.Vorname}}</td>
<td>{{kuenstler_lied.kuenstler.Nachname}}</td>
<td>{{kuenstler_lied.kuenstler.Herkunftsland}}</td>
<td>{{kuenstler_lied.kuenstler.Gehalt}}</td>
</tr>
{% endfor %}

Expand Down
2 changes: 1 addition & 1 deletion templates/liedkuenstler/liedkuenstleradd.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@

<input type ="hidden" name = "CheckedCheckboxes" id ="CheckedCheckboxes" value ="">

<button type="submit" class="btn btn-primary" onclick="Try()">Delete</button>
<button type="submit" class="btn btn-primary" onclick="Try()">Add</button>

{%endblock%}

0 comments on commit a377584

Please sign in to comment.