Restructure project 2/2

This commit is contained in:
Patrick Jentsch 2022-09-02 13:08:01 +02:00
parent 8e1d94bb5d
commit dedccad70a
2 changed files with 46 additions and 47 deletions

View File

@ -10,7 +10,9 @@ class AdminEditUserForm(FlaskForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.role.choices = [
(role.hashid, role.name)
for role in Role.query.order_by(Role.name).all()
]
self.role.choices = [(x.hashid, x.name) for x in Role.query.all()]
def prefill(self, user):
''' Pre-fill the form with data of an exististing user '''
self.confirmed.data = user.confirmed
self.role.data = user.role.hashid

View File

@ -1,14 +1,14 @@
from flask import current_app, flash, redirect, render_template, url_for
from flask_login import login_required
from threading import Thread
from app import db, hashids
from app.decorators import admin_required
from app.models import Role, User, UserSettingJobStatusMailNotificationLevel
from app.settings import tasks as settings_tasks
from app.settings.forms import (
EditGeneralSettingsForm,
EditInterfaceSettingsForm,
EditNotificationSettingsForm
)
from flask import flash, redirect, render_template, url_for
from flask_login import login_required
from . import bp
from .forms import AdminEditUserForm
@ -24,20 +24,17 @@ def before_request():
pass
@bp.route('/')
@bp.route('')
def index():
return redirect(url_for('.users'))
@bp.route('/users')
def users():
dict_users = {
user.id: user.to_dict(backrefs=True, relationships=False)
for user in User.query.all()
}
json_users = [x.to_json(backrefs=True) for x in User.query.all()]
return render_template(
'admin/users.html.j2',
dict_users=dict_users,
json_users=json_users,
title='Users'
)
@ -48,59 +45,45 @@ def user(user_id):
return render_template('admin/user.html.j2', title='User', user=user)
@bp.route('/users/<hashid:user_id>/delete')
def delete_user(user_id):
settings_tasks.delete_user(user_id)
flash('User has been marked for deletion')
return redirect(url_for('.users'))
@bp.route('/users/<hashid:user_id>/edit', methods=['GET', 'POST'])
def edit_user(user_id):
user = User.query.get_or_404(user_id)
admin_edit_user_form = AdminEditUserForm(
prefix='admin_edit_user_form'
prefix='admin-edit-user-form'
)
edit_general_settings_form = EditGeneralSettingsForm(
user,
prefix='edit_general_settings_form'
prefix='edit-general-settings-form'
)
edit_interface_settings_form = EditInterfaceSettingsForm(
prefix='edit_interface_settings_form'
prefix='edit-interface-settings-form'
)
edit_notification_settings_form = EditNotificationSettingsForm(
prefix='edit_notification_settings_form'
prefix='edit-notification-settings-form'
)
if (
admin_edit_user_form.submit.data
and admin_edit_user_form.validate()
):
if (admin_edit_user_form.submit.data
and admin_edit_user_form.validate()):
user.confirmed = admin_edit_user_form.confirmed.data
role_id = hashids.decode(admin_edit_user_form.role.data)
user.role = Role.query.get(role_id)
db.session.commit()
flash('Your changes have been saved')
return redirect(url_for('.edit_user', user_id=user.id))
if (
edit_general_settings_form.submit.data
and edit_general_settings_form.validate()
):
if (edit_general_settings_form.submit.data
and edit_general_settings_form.validate()):
user.email = edit_general_settings_form.email.data
user.username = edit_general_settings_form.username.data
db.session.commit()
flash('Your changes have been saved')
return redirect(url_for('.edit_user', user_id=user.id))
if (
edit_interface_settings_form.submit.data
and edit_interface_settings_form.validate()
):
if (edit_interface_settings_form.submit.data
and edit_interface_settings_form.validate()):
user.setting_dark_mode = edit_interface_settings_form.dark_mode.data
db.session.commit()
flash('Your changes have been saved')
return redirect(url_for('.edit_user', user_id=user.id))
if (
edit_notification_settings_form.submit.data
and edit_notification_settings_form.validate()
):
if (edit_notification_settings_form.submit.data
and edit_notification_settings_form.validate()):
user.setting_job_status_mail_notification_level = \
UserSettingJobStatusMailNotificationLevel[
edit_notification_settings_form.job_status_mail_notification_level.data # noqa
@ -108,13 +91,10 @@ def edit_user(user_id):
db.session.commit()
flash('Your changes have been saved')
return redirect(url_for('.edit_user', user_id=user.id))
admin_edit_user_form.confirmed.data = user.confirmed
admin_edit_user_form.role.data = user.role.hashid
edit_general_settings_form.email.data = user.email
edit_general_settings_form.username.data = user.username
edit_interface_settings_form.dark_mode.data = user.setting_dark_mode
edit_notification_settings_form.job_status_mail_notification_level.data = \
user.setting_job_status_mail_notification_level.name
admin_edit_user_form.prefill(user)
edit_general_settings_form.prefill(user)
edit_interface_settings_form.prefill(user)
edit_notification_settings_form.prefill(user)
return render_template(
'admin/edit_user.html.j2',
admin_edit_user_form=admin_edit_user_form,
@ -124,3 +104,20 @@ def edit_user(user_id):
title='Edit user',
user=user
)
@bp.route('/users/<hashid:user_id>/delete', methods=['DELETE'])
def delete_user(user_id):
def _delete_user(app, user_id):
with app.app_context():
user = User.query.get(user_id)
user.delete()
db.session.commit()
User.query.get_or_404(user_id)
thread = Thread(
target=_delete_user,
args=(current_app._get_current_object(), user_id)
)
thread.start()
return {}, 202