diff --git a/mo/web/org_users.py b/mo/web/org_users.py index 956362a84b98dfcba3d77a18eb6b213a10973325..683c0f792e14eb1bed26454724905c16a9ba0dbd 100644 --- a/mo/web/org_users.py +++ b/mo/web/org_users.py @@ -321,7 +321,7 @@ def org_org(id: int): ) sess.commit() app.logger.info(f"New role for user #{id} added: {db.row2dict(new_role)}") - flash(f'Role "{new_role}" úspěšně přidána', 'success') + flash(f'Role "{new_role}" úspěšně přidána.', 'success') return redirect(url_for('org_user', id=id)) if form_remove_role.remove_role_id.data and form_remove_role.validate_on_submit(): @@ -340,7 +340,7 @@ def org_org(id: int): ) sess.commit() app.logger.info(f"Role for user #{id} removed: {db.row2dict(role)}") - flash(f'Role "{role}" úspěšně odebrána', 'success') + flash(f'Role "{role}" úspěšně odebrána.', 'success') return redirect(url_for('org_user', id=id)) return render_template( @@ -451,7 +451,7 @@ def org_user_edit(id: int): if hasattr(form, 'email') and form.email is not None: other_user = mo.users.user_by_email(form.email.data) if other_user is not None and other_user != user: - flash('Zadaný e-mail nelze použít, existuje jiný účet s tímto e-mailem', 'danger') + flash('Zadaný e-mail nelze použít, existuje jiný účet s tímto e-mailem.', 'danger') check = False if check: @@ -466,9 +466,9 @@ def org_user_edit(id: int): details={'action': 'edit', 'changes': changes}, ) sess.commit() - flash('Změny uživatele uloženy', 'success') + flash('Změny uživatele uloženy.', 'success') else: - flash('Žádné změny k uložení', 'info') + flash('Žádné změny k uložení.', 'info') return redirect(url_for('org_user', id=id)) @@ -546,7 +546,7 @@ def org_user_new(): token = mo.users.make_activation_token(new_user) sess.commit() - flash('Nový uživatel vytvořen', 'success') + flash('Nový uživatel vytvořen.', 'success') if mo.email.send_new_account_email(new_user, token): flash('E-mail s odkazem na aktivaci účtu odeslán na {}.'.format(new_user.email), 'success') @@ -572,6 +572,40 @@ class ParticipantEditForm(FlaskForm): submit = wtforms.SubmitField("Uložit") +@app.route('/org/user/<int:user_id>/participant/<int:year>/delete', methods=('POST',)) +def org_user_participant_delete(user_id: int, year: int): + sess = db.get_session() + user = mo.users.user_by_uid(user_id) + if not user: + raise werkzeug.exceptions.NotFound() + + rr = g.gatekeeper.rights_generic() + if not rr.can_edit_user(user): + raise werkzeug.exceptions.Forbidden() + + participant = sess.query(db.Participant).filter_by(user_id=user.user_id).filter_by(year=year).one_or_none() + if participant is None: + raise werkzeug.exceptions.NotFound() + + if sess.query(db.Participation).filter_by(user_id=user.user_id).filter(db.Participation.contest.has(db.Contest.round.has(year=year))).count() != 0: + flash('Registraci není možná smazat, soutěžící se v daném ročníku účastní nějakého kola.', 'danger') + else: + sess.delete(participant) + + app.logger.info(f"Participant id {user_id} year {year} deleted") + mo.util.log( + type=db.LogType.participant, + what=user_id, + details={'action': 'delete-participant', 'year': year, 'participant': db.row2dict(participant)}, + ) + + sess.commit() + + flash('Registrace smazána.', 'success') + + return redirect(url_for('org_user', id=user_id)) + + @app.route('/org/user/<int:user_id>/participant/<int:year>/edit', methods=('GET', 'POST')) def org_user_participant_edit(user_id: int, year: int): sess = db.get_session() @@ -593,16 +627,16 @@ def org_user_participant_edit(user_id: int, year: int): if sess.is_modified(participant): changes = db.get_object_changes(participant) - app.logger.info(f"Participant id {id} year {year} modified, changes: {changes}") + app.logger.info(f"Participant id {user_id} year {year} modified, changes: {changes}") mo.util.log( type=db.LogType.participant, what=user_id, details={'action': 'edit-participant', 'year': year, 'changes': changes}, ) sess.commit() - flash('Změny registrace uloženy', 'success') + flash('Změny registrace uloženy.', 'success') else: - flash('Žádné změny k uložení', 'info') + flash('Žádné změny k uložení.', 'info') return redirect(url_for('org_user', id=user_id)) diff --git a/mo/web/templates/org_user.html b/mo/web/templates/org_user.html index 56f9559e656c7761db9348c32a600fd0d2a01493..edf1f7684ee37d97e80886c2064c33a45a2b7c7d 100644 --- a/mo/web/templates/org_user.html +++ b/mo/web/templates/org_user.html @@ -61,7 +61,12 @@ <td><a href="{{ url_for('org_place', id=participant.school) }}">{{ participant.school_place.name }}</a> <td>{{ participant.grade }} <td>{{ participant.birth_year }} - <td><a class="btn btn-xs btn-primary" href="{{ url_for('org_user_participant_edit', user_id=user.user_id, year=participant.year) }}">Editovat</a> + <td><div class="btn-group"> + <a class="btn btn-xs btn-primary" href="{{ url_for('org_user_participant_edit', user_id=user.user_id, year=participant.year) }}">Editovat</a> + <form class="btn-group" method="POST" onsubmit="return confirm('Opravdu nenávratně smazat?')" action='{{ url_for('org_user_participant_delete', user_id=user.user_id, year=participant.year) }}'> + <input class="btn btn-xs btn-danger" type=submit value='Smazat'> + </form> + </div> </tr> {% endfor %} </table>