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 wtforms import StringField, BooleanField, SelectField, SubmitField
from wtforms.validators import DataRequired, Length, Email, Regexp
from wtforms import ValidationError
from wtforms import (BooleanField, SelectField, StringField, SubmitField,
ValidationError)
from wtforms.validators import DataRequired, Email, Length, Regexp
from ..models import Role, User
class EditProfileAdminForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Length(1, 64),
Email()])
username = StringField('Username', validators=[
DataRequired(), Length(1, 64),
Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
email = StringField('Email',
validators=[DataRequired(), Length(1, 64), Email()])
username = StringField(
'Username',
validators=[
DataRequired(),
Length(1, 64),
Regexp(
'^[A-Za-z][A-Za-z0-9_.]*$',
0,
'Usernames must have only letters, numbers, dots or '
'underscores')])
'underscores'
)
]
)
confirmed = BooleanField('Confirmed')
role = SelectField('Role', coerce=int)
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 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'])
@ -16,39 +15,52 @@ from app.utils import background_delete_user
@admin_required
def for_admins_only():
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]
table = AdminUserTable(items).__html__() # converts table object to html string
table = table.replace('tbody', 'tbody class="list"', 1) # add class list to tbody element. Needed by list.js
return render_template('admin/admin.html.j2', title='Administration tools',
table=table)
items = [AdminUserItem(u.username, u.email, u.role_id, u.confirmed, u.id)
for u in users]
# Convert table object to html string
table = AdminUserTable(items).__html__()
# 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
@admin_required
def admin_user_page(user_id):
selected_user = User.query.filter_by(id=user_id).first()
title = 'Administration of user {} with ID: {}'.format(selected_user.username,
selected_user.id)
registration_date = selected_user.registration_date.strftime('%A, %e %B %H:%M')
title = 'Administration of user {} with ID: {}'.format(
selected_user.username,
selected_user.id
)
registration_date = selected_user.registration_date.strftime(
'%A, %e %B %H:%M'
)
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
@admin_required
def admin_delete_user(user_id):
delete_thread = threading.Thread(target=background_delete_user,
args=(current_app._get_current_object(),
user_id))
delete_thread = threading.Thread(
target=background_delete_user,
args=(current_app._get_current_object(), user_id)
)
delete_thread.start()
flash('User {} has been deleted!'.format(user_id))
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
@admin_required
def edit_profile_admin(user_id):
@ -67,9 +79,11 @@ def edit_profile_admin(user_id):
form.username.data = user.username
form.confirmed.data = user.confirmed
form.role.data = user.role_id
title = 'Edit profile of user {} with ID {}'.format(user.username,
user.id)
title = 'Edit profile of user {} with ID {}'.format(
user.username,
user.id
)
return render_template('admin/edit_profile_admin.html.j2',
form=form,
user=user,
title=title)
title=title,
user=user)