Skip to content
Snippets Groups Projects

Place editing

Merged Jiří Setnička requested to merge jirka/schools into master
2 files
+ 52
12
Compare changes
  • Side-by-side
  • Inline

Files

+ 43
11
from flask import render_template, g, redirect, url_for, flash
from flask import render_template, g, redirect, url_for, flash, request
from flask_wtf import FlaskForm
from flask_wtf import FlaskForm
from sqlalchemy.orm import joinedload
from sqlalchemy.orm import joinedload
import werkzeug.exceptions
import werkzeug.exceptions
@@ -83,20 +83,52 @@ def org_place_edit(id: int):
@@ -83,20 +83,52 @@ def org_place_edit(id: int):
if place.type == db.PlaceType.school:
if place.type == db.PlaceType.school:
school = sess.query(db.School).get(place.place_id)
school = sess.query(db.School).get(place.place_id)
form = PlaceSchoolEditForm(obj=school)
# Pass school data as additional dict (data is used after obj)
 
form = PlaceSchoolEditForm(obj=place, data=db.row2dict(school))
else:
else:
form = PlaceEditForm(obj=place)
form = PlaceEditForm(obj=place)
school = None
school = None
if form.validate_on_submit():
if form.validate_on_submit():
 
form.populate_obj(place)
if school:
if school:
obj = school
form.populate_obj(school)
else:
obj = place
msg = 'Změny místa uloženy'
redirectURL = url_for('org_place', id=id)
form.populate_obj(obj)
if sess.is_modified(obj):
if sess.is_modified(place) or school and sess.is_modified(school):
changes = db.get_object_changes(obj)
placeChanges = db.get_object_changes(place)
 
schoolChanges = {}
 
if school:
 
if request.form.get('type') != 'school':
 
# School record removed
 
mo.util.log(
 
type=db.LogType.place,
 
what=g.user.user_id,
 
details={'action': 'school-delete', 'school': db.row2dict(school)},
 
)
 
app.logger.info(f"Deleting school record for place {place.place_id}")
 
db.get_session().delete(school)
 
msg = 'Změny místa uloženy, záznam o škole smazán'
 
else:
 
schoolChanges = db.get_object_changes(school)
 
elif request.form.get('type') == 'school':
 
# School record created
 
newSchool = db.School()
 
newSchool.place_id = place.place_id
 
mo.util.log(
 
type=db.LogType.place,
 
what=g.user.user_id,
 
details={'action': 'school-add', 'place_id': place.place_id},
 
)
 
app.logger.info(f"Creating new school for place {place.place_id}")
 
db.get_session().add(newSchool)
 
# Take org directly to the school edit to fill the data
 
msg = 'Záznam o škole vytvořen, vyplňte prosím všechna data'
 
redirectURL = url_for('org_place_edit', id=id)
 
 
changes = {**placeChanges, **schoolChanges}
app.logger.info(f"Place {id} modified, changes: {changes}")
app.logger.info(f"Place {id} modified, changes: {changes}")
mo.util.log(
mo.util.log(
type=db.LogType.place,
type=db.LogType.place,
@@ -104,11 +136,11 @@ def org_place_edit(id: int):
@@ -104,11 +136,11 @@ def org_place_edit(id: int):
details={'action': 'edit', 'changes': changes},
details={'action': 'edit', 'changes': changes},
)
)
db.get_session().commit()
db.get_session().commit()
flash(u'Změny místa uloženy', 'success')
flash(msg, 'success')
else:
else:
flash(u'Žádné změny k uložení', 'info')
flash(u'Žádné změny k uložení', 'info')
return redirect(url_for('org_place', id=id))
return redirect(redirectURL)
parents = reversed(db.get_place_parents(place)[1:]) # without place itself and from the top
parents = reversed(db.get_place_parents(place)[1:]) # without place itself and from the top
Loading