Skip to content
Snippets Groups Projects

Dodělávky manipulace s places

1 file
+ 25
3
Compare changes
  • Side-by-side
  • Inline
+ 124
12
@@ -112,7 +112,7 @@ def org_place_edit(id: int):
# School record removed
mo.util.log(
type=db.LogType.place,
what=g.user.user_id,
what=school.place_id,
details={'action': 'school-delete', 'school': db.row2dict(school)},
)
app.logger.info(f"Deleting school record for place {place.place_id}")
@@ -122,15 +122,15 @@ def org_place_edit(id: int):
schoolChanges = db.get_object_changes(school)
elif request.form.get('type') == 'school':
# School record created
newSchool = db.School()
newSchool.place_id = place.place_id
new_school = db.School()
new_school.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},
what=new_school.place_id,
details={'action': 'school-add'},
)
app.logger.info(f"Creating new school for place {place.place_id}")
db.get_session().add(newSchool)
db.get_session().add(new_school)
# 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)
@@ -139,7 +139,7 @@ def org_place_edit(id: int):
app.logger.info(f"Place {id} modified, changes: {changes}")
mo.util.log(
type=db.LogType.place,
what=g.user.user_id,
what=id,
details={'action': 'edit', 'changes': changes},
)
db.get_session().commit()
@@ -157,6 +157,82 @@ def org_place_edit(id: int):
)
class PlaceMoveForm(FlaskForm):
code = wtforms.StringField(validators=[validators.DataRequired()])
submit = wtforms.SubmitField('Najít místo')
reset = wtforms.HiddenField()
move = wtforms.HiddenField()
class PlaceMoveConfirmForm(FlaskForm):
code = wtforms.HiddenField()
reset = wtforms.SubmitField('Zrušit')
move = wtforms.SubmitField('Přesunout')
@app.route('/org/place/<int:id>/move', methods=('GET', 'POST'))
def org_place_move(id: int):
sess = db.get_session()
# Tests: can move only existing places that we can edit
place = sess.query(db.Place).get(id)
if not place:
raise werkzeug.exceptions.NotFound()
rr = mo.rights.Rights(g.user)
rr.get_for(place)
if not rr.can_edit_place(place):
raise werkzeug.exceptions.Forbidden()
parents = reversed(db.get_place_parents(place)[1:]) # without place itself and from the top
new_parents = None
search_error = None
form = PlaceMoveForm()
form_confirm = None
if form.validate_on_submit():
if form.reset.data:
return redirect(url_for('org_place_move', id=id))
new_parent = db.place_by_code(form.code.data)
if not new_parent:
search_error = 'Místo s tímto kódem se nepovedlo nalézt'
else:
new_parents = reversed(db.get_place_parents(new_parent))
(_, levels) = db.place_type_names_and_levels[place.type]
rr.get_for(new_parent)
if not rr.can_edit_place(new_parent):
search_error = 'Nemáte právo k editaci vybraného nadřazeného místa, přesun nelze uskutečnit'
elif (new_parent.level + 1) not in levels:
search_error = f'Toto místo ({place.type_name()}) nelze přemístit pod vybrané místo ({new_parent.type_name()}), dostalo by se na nepovolený level'
elif new_parent.place_id == place.parent:
search_error = 'Žádná změna, místo je zde již umístěno'
elif form.move.data:
# Everything is OK, if submitted with 'move' do the move
place.parent = new_parent.place_id
place.level = new_parent.level + 1
changes = db.get_object_changes(place)
mo.util.log(
type=db.LogType.place,
what=id,
details={'action': 'move', 'changes': changes},
)
app.logger.info(f"Place {id} moved, changes: {changes}")
db.get_session().commit()
flash('Místo úspěšně přesunuto', 'success')
return redirect(url_for('org_place', id=id))
else:
# OK but not confirmed yet, display the confirm form
form_confirm = PlaceMoveConfirmForm()
form_confirm.code.data = form.code.data
return render_template(
'org_place_move.html',
place=place, form=form, form_confirm=form_confirm, search_error=search_error,
parents=parents, new_parents=new_parents
)
@app.route('/org/place/<int:id>/delete', methods=('POST',))
def org_place_delete(id: int):
sess = db.get_session()
@@ -175,10 +251,28 @@ def org_place_delete(id: int):
flash("Nelze smazat místo s podřízenými místy", "danger")
return redirect(url_for('org_place', id=id))
# Cannot delete place with contests
if sess.query(db.Contest).filter_by(place_id=id).count() > 0:
flash("Nelze smazat místo ke kterému se váže nějaká soutěž ", "danger")
return redirect(url_for('org_place', id=id))
if place.type == db.PlaceType.school:
school = sess.query(db.School).get(place.place_id)
mo.util.log(
type=db.LogType.place,
what=school.place_id,
details={'action': 'school-delete', 'school': db.row2dict(school)},
)
app.logger.info(f"Deleting school record for place {id}")
db.get_session().delete(school)
mo.util.log(
type=db.LogType.place,
what=id,
details={'action': 'delete', 'place': db.row2dict(place)},
)
app.logger.info(f"Deleting place {id}")
parent = place.parent
db.get_session().delete(place)
db.get_session().commit()
@@ -210,18 +304,36 @@ def org_place_new_child(id: int):
form.populate_obj(new_place)
new_place.parent = parent_place.place_id
new_place.level = parent_place.level + 1
db.get_session().add(new_place)
sess.add(new_place)
sess.flush()
app.logger.info(f"New place created: {db.row2dict(new_place)}")
mo.util.log(
type=db.LogType.place,
what=g.user.user_id,
what=new_place.place_id,
details={'action': 'new', 'place': db.row2dict(new_place)},
)
db.get_session().commit()
flash(u'Nové místo uloženo', 'success')
return redirect(url_for('org_place', id=new_place.place_id))
redirect_url = url_for('org_place', id=new_place.place_id)
msg = 'Nové místo uloženo'
if new_place.type == db.PlaceType.school:
new_school = db.School()
new_school.place_id = new_place.place_id
mo.util.log(
type=db.LogType.place,
what=new_school.place_id,
details={'action': 'school-add'},
)
app.logger.info(f"Creating new school for place {new_place.place_id}")
sess.add(new_school)
# 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'
redirect_url = url_for('org_place_edit', id=new_place.place_id)
sess.commit()
flash(msg, 'success')
return redirect(redirect_url)
parents = reversed(db.get_place_parents(parent_place))
Loading