Codestyle for admin package.

This commit is contained in:
Patrick Jentsch 2019-09-23 16:25:26 +02:00
parent 783b8c7e82
commit 5749c94bca
2 changed files with 62 additions and 40 deletions

View File

@ -1,18 +1,26 @@
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, BooleanField, SelectField, SubmitField from wtforms import (BooleanField, SelectField, StringField, SubmitField,
from wtforms.validators import DataRequired, Length, Email, Regexp ValidationError)
from wtforms import ValidationError from wtforms.validators import DataRequired, Email, Length, Regexp
from ..models import Role, User from ..models import Role, User
class EditProfileAdminForm(FlaskForm): class EditProfileAdminForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Length(1, 64), email = StringField('Email',
Email()]) validators=[DataRequired(), Length(1, 64), Email()])
username = StringField('Username', validators=[ username = StringField(
DataRequired(), Length(1, 64), 'Username',
Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0, validators=[
DataRequired(),
Length(1, 64),
Regexp(
'^[A-Za-z][A-Za-z0-9_.]*$',
0,
'Usernames must have only letters, numbers, dots or ' 'Usernames must have only letters, numbers, dots or '
'underscores')]) 'underscores'
)
]
)
confirmed = BooleanField('Confirmed') confirmed = BooleanField('Confirmed')
role = SelectField('Role', coerce=int) role = SelectField('Role', coerce=int)
name = StringField('Real name', validators=[Length(0, 64)]) name = StringField('Real name', validators=[Length(0, 64)])

View File

@ -1,14 +1,13 @@
from flask import (abort, current_app, flash, redirect, request,
render_template, url_for, send_from_directory)
from flask_login import current_user, login_required
from .forms import EditProfileAdminForm
from ..models import Corpus, User, Role, Job
from ..tables import AdminUserTable, AdminUserItem
from . import admin
from ..decorators import admin_required
from .. import db
import threading
from app.utils import background_delete_user from app.utils import background_delete_user
from flask import current_app, flash, redirect, render_template, url_for
from flask_login import login_required
from . import admin
from .forms import EditProfileAdminForm
from .. import db
from ..decorators import admin_required
from ..models import Role, User
from ..tables import AdminUserTable, AdminUserItem
import threading
@admin.route('/overview', methods=['GET', 'POST']) @admin.route('/overview', methods=['GET', 'POST'])
@ -16,39 +15,52 @@ from app.utils import background_delete_user
@admin_required @admin_required
def for_admins_only(): def for_admins_only():
users = User.query.order_by(User.username).all() users = User.query.order_by(User.username).all()
items = [AdminUserItem(u.username, u.email, u.role_id, u.confirmed, u.id) for u in users] items = [AdminUserItem(u.username, u.email, u.role_id, u.confirmed, u.id)
table = AdminUserTable(items).__html__() # converts table object to html string for u in users]
table = table.replace('tbody', 'tbody class="list"', 1) # add class list to tbody element. Needed by list.js # Convert table object to html string
return render_template('admin/admin.html.j2', title='Administration tools', table = AdminUserTable(items).__html__()
table=table) # Add class "list" to tbody element. Needed for "List.js"
table = table.replace('tbody', 'tbody class="list"', 1)
return render_template('admin/admin.html.j2',
table=table,
title='Administration tools')
@admin.route('/overview/admin_user_page/<int:user_id>', methods=['GET', 'POST']) @admin.route('/overview/admin_user_page/<int:user_id>',
methods=['GET', 'POST'])
@login_required @login_required
@admin_required @admin_required
def admin_user_page(user_id): def admin_user_page(user_id):
selected_user = User.query.filter_by(id=user_id).first() selected_user = User.query.filter_by(id=user_id).first()
title = 'Administration of user {} with ID: {}'.format(selected_user.username, title = 'Administration of user {} with ID: {}'.format(
selected_user.id) selected_user.username,
registration_date = selected_user.registration_date.strftime('%A, %e %B %H:%M') selected_user.id
)
registration_date = selected_user.registration_date.strftime(
'%A, %e %B %H:%M'
)
return render_template('admin/admin_user_page.html.j2', return render_template('admin/admin_user_page.html.j2',
title=title, selected_user=selected_user, registration_date=registration_date,
registration_date=registration_date) selected_user=selected_user,
title=title)
@admin.route('/overview/admin_user_page/delete/<int:user_id>', methods=['GET', 'POST']) @admin.route('/overview/admin_user_page/delete/<int:user_id>',
methods=['GET', 'POST'])
@login_required @login_required
@admin_required @admin_required
def admin_delete_user(user_id): def admin_delete_user(user_id):
delete_thread = threading.Thread(target=background_delete_user, delete_thread = threading.Thread(
args=(current_app._get_current_object(), target=background_delete_user,
user_id)) args=(current_app._get_current_object(), user_id)
)
delete_thread.start() delete_thread.start()
flash('User {} has been deleted!'.format(user_id)) flash('User {} has been deleted!'.format(user_id))
return redirect(url_for('admin.for_admins_only')) return redirect(url_for('admin.for_admins_only'))
@admin.route('/overview/admin_user_page/edit_profile_admin/<int:user_id>', methods=['GET', 'POST']) @admin.route('/overview/admin_user_page/edit_profile_admin/<int:user_id>',
methods=['GET', 'POST'])
@login_required @login_required
@admin_required @admin_required
def edit_profile_admin(user_id): def edit_profile_admin(user_id):
@ -67,9 +79,11 @@ def edit_profile_admin(user_id):
form.username.data = user.username form.username.data = user.username
form.confirmed.data = user.confirmed form.confirmed.data = user.confirmed
form.role.data = user.role_id form.role.data = user.role_id
title = 'Edit profile of user {} with ID {}'.format(user.username, title = 'Edit profile of user {} with ID {}'.format(
user.id) user.username,
user.id
)
return render_template('admin/edit_profile_admin.html.j2', return render_template('admin/edit_profile_admin.html.j2',
form=form, form=form,
user=user, title=title,
title=title) user=user)