diff --git a/app/admin/forms.py b/app/admin/forms.py new file mode 100644 index 00000000..b0f80d9e --- /dev/null +++ b/app/admin/forms.py @@ -0,0 +1,35 @@ +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 ..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, + 'Usernames must have only letters, numbers, dots or ' + 'underscores')]) + confirmed = BooleanField('Confirmed') + role = SelectField('Role', coerce=int) + name = StringField('Real name', validators=[Length(0, 64)]) + submit = SubmitField('Update Profile') + + def __init__(self, user, *args, **kwargs): + super(EditProfileAdminForm, self).__init__(*args, **kwargs) + self.role.choices = [(role.id, role.name) + for role in Role.query.order_by(Role.name).all()] + self.user = user + + def validate_email(self, field): + if field.data != self.user.email and \ + User.query.filter_by(email=field.data).first(): + raise ValidationError('Email already registered.') + + def validate_username(self, field): + if field.data != self.user.username and \ + User.query.filter_by(username=field.data).first(): + raise ValidationError('Username already in use.') diff --git a/app/admin/views.py b/app/admin/views.py index 04ca640d..6358167f 100644 --- a/app/admin/views.py +++ b/app/admin/views.py @@ -1,12 +1,14 @@ 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 ..models import Corpus, User +from .forms import EditProfileAdminForm +from ..models import Corpus, User, Role from ..tables import AdminUserTable, AdminUserItem from . import admin from ..decorators import admin_required from .. import db import os +import datetime @admin.route('/overview', methods=['GET', 'POST']) @@ -28,8 +30,10 @@ 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') return render_template('admin/admin_user_page.html.j2', - title=title, selected_user=selected_user) + title=title, selected_user=selected_user, + registration_date=registration_date) @admin.route('/overview/admin_user_page/delete/', methods=['GET', 'POST']) @@ -41,3 +45,30 @@ def admin_delete_user(user_id): db.session.commit() 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/', methods=['GET', 'POST']) +@login_required +@admin_required +def edit_profile_admin(user_id): + user = User.query.get_or_404(user_id) + form = EditProfileAdminForm(user=user) + if form.validate_on_submit(): + user.email = form.email.data + user.username = form.username.data + user.confirmed = form.confirmed.data + user.role = Role.query.get(form.role.data) + db.session.add(user) + db.session.commit() + flash('The profile has been updated.') + return redirect(url_for('admin.edit_profile_admin', user_id=user.id)) + form.email.data = user.email + 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) + return render_template('admin/edit_profile_admin.html.j2', + form=form, + user=user, + title=title) diff --git a/app/models.py b/app/models.py index 0f28882f..59f2bcd3 100644 --- a/app/models.py +++ b/app/models.py @@ -67,7 +67,7 @@ class Role(db.Model): def has_permission(self, perm): """ - Checks if a Role has a specific Permission. Does this wit hthe bitwise + Checks if a Role has a specific Permission. Does this with the bitwise operator. """ return self.permissions & perm == perm diff --git a/app/templates/admin/admin_user_page.html.j2 b/app/templates/admin/admin_user_page.html.j2 index 6f892a0a..35246937 100644 --- a/app/templates/admin/admin_user_page.html.j2 +++ b/app/templates/admin/admin_user_page.html.j2 @@ -5,6 +5,19 @@
User information +
    +
  • Username: {{selected_user.username}}
  • +
  • Email: {{selected_user.email}}
  • +
  • ID: {{selected_user.id}}
  • +
  • Registration date: {{registration_date}}
  • +
  • Confirmed status: {{selected_user.confirmed}}
  • +
  • Role ID: {{selected_user.role_id}}
  • +
  • Permissions as Int: {{selected_user.role.permissions}}
  • +
  • Role name: {{selected_user.role.name}}
  • +
+
@@ -16,7 +29,7 @@
search - +
{% for job in selected_user.jobs.all() %} diff --git a/app/templates/admin/edit_profile_admin.html.j2 b/app/templates/admin/edit_profile_admin.html.j2 new file mode 100644 index 00000000..af27c067 --- /dev/null +++ b/app/templates/admin/edit_profile_admin.html.j2 @@ -0,0 +1,59 @@ +{% extends "base.html.j2" %} + +{% block page_content %} +
+
+
+
+ {{ form.hidden_tag() }} +
+ account_circle + {{ form.username() }} + {{ form.username.label }} + {% for error in form.username.errors %} + {{ error }} + {% endfor %} +
+
+ mail + {{ form.email() }} + {{ form.email.label }} + {% for error in form.email.errors %} + {{ error }} + {% endfor %} +
+
+ swap_vert + {{ form.role() }} + {{ form.role.label }} + {% for error in form.role.errors %} + {{ error }} + {% endfor %} +
+
+ check + + {% for error in form.confirmed.errors %} + {{ error }} + {% endfor %} +
+
+
+ {{ form.submit(class='btn') }} +
+
+ +
+
+ +{% endblock %} diff --git a/app/templates/auth/edit_profile.html.j2 b/app/templates/auth/edit_profile.html.j2 index 669bc08f..ab992065 100644 --- a/app/templates/auth/edit_profile.html.j2 +++ b/app/templates/auth/edit_profile.html.j2 @@ -51,7 +51,7 @@
{{ change_profile_form.hidden_tag() }} -
+
mail {{ change_profile_form.email() }} {{ change_profile_form.email.label }}