diff --git a/dash/__init__.py b/dash/__init__.py index 6532cdf..37d8822 100644 --- a/dash/__init__.py +++ b/dash/__init__.py @@ -6,7 +6,7 @@ from flask_moment import Moment from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager from flask_debugtoolbar import DebugToolbarExtension -from flask.ext.principal import Principal +from flask_principal import Principal from config import config diff --git a/dash/admin/forms.py b/dash/admin/forms.py index 082a6fb..751769b 100644 --- a/dash/admin/forms.py +++ b/dash/admin/forms.py @@ -1,5 +1,31 @@ from flask_wtf import Form from flask import flash -from wtforms import StringField, PasswordField, BooleanField, SubmitField, ValidationError +from wtforms import StringField, PasswordField, BooleanField, SubmitField, \ + ValidationError, SelectField from wtforms.validators import Required, Length, Email, Regexp, EqualTo -from ..models import User \ No newline at end of file +from ..models import User, Role + +class EditProfileAdminForm(Form): + email = StringField('Email', validators=[Required(), Length(1, 128), + Email()]) + username = StringField('Username', validators=[ + Required(), Length(1, 64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0, + 'Usernames must have only letters, ' + 'numbers, dots or underscores')]) + full_name = StringField('Full name', validators=[Required(), Length(1, 255)]) + role_id = SelectField('Role', coerce=int) + confirmed = BooleanField('Confirmed') + + def __init__(self, user, *args, **kwargs): + super(EditProfileAdminForm, self).__init__(*args, **kwargs) + self.role_id.choices = [(role.id, role.name) + for role in Role.query.order_by(Role.name).all()] + self.user = user + + def validate_email(self, field): + if User.query.filter_by(email=field.data).first(): + raise ValidationError('Email already registered.') + + def validate_username(self, field): + if User.query.filter_by(username=field.data).first(): + raise ValidationError('Username already in use.') \ No newline at end of file diff --git a/dash/admin/views.py b/dash/admin/views.py index c9d138c..8207732 100644 --- a/dash/admin/views.py +++ b/dash/admin/views.py @@ -8,9 +8,10 @@ from flask_principal import Identity, AnonymousIdentity, \ from . import admin from .. import db -from ..models import User +from ..models import User, Role from ..email import send_email from ..decorators import requires_roles +from .forms import EditProfileAdminForm @admin.route('/') @login_required @@ -23,4 +24,25 @@ def index(): @requires_roles("admin") def list_users(): users = User.query.all() - return render_template('admin/list_users.html', users=users) \ No newline at end of file + return render_template('admin/list_users.html', users=users, + title="List Users", + block_description = "list, edit and delete users") + +@admin.route('/edit-user/', methods=['GET', 'POST']) +@login_required +@requires_roles("admin") +def edit_user_admin(id): + user = User.query.get_or_404(id) + form = EditProfileAdminForm(user=user) + if form.validate_on_submit(): + user.email = form.email.data + user.username = form.username.data + user.full_name = form.full_name.data + user.role_id = Role.query.get(form.role.data) + user.confirmed = form.confirmed.data + db.session.add(user) + flash('The profile has been updated.') + return redirect(url_for('.user', username=user.username)) + return render_template('admin/edit_user.html', user=user, form=form, + title="Edit User", + block_description = "edit and update user info") \ No newline at end of file diff --git a/dash/templates/_formhelpers.html b/dash/templates/_formhelpers.html new file mode 100644 index 0000000..5790894 --- /dev/null +++ b/dash/templates/_formhelpers.html @@ -0,0 +1,12 @@ +{% macro render_field(field) %} +
{{ field.label }} +
{{ field(**kwargs)|safe }} + {% if field.errors %} + + {% endif %} +
+{% endmacro %} \ No newline at end of file diff --git a/dash/templates/admin/content_header.html b/dash/templates/admin/content_header.html index 2cb3f95..81c5326 100644 --- a/dash/templates/admin/content_header.html +++ b/dash/templates/admin/content_header.html @@ -1,6 +1,8 @@ -
-

- Admin Dashboard - Dashboard for Administrators -

-
\ No newline at end of file +

+ {{ title }} + {{ block_description }} +

+ \ No newline at end of file diff --git a/dash/templates/admin/edit_user.html b/dash/templates/admin/edit_user.html new file mode 100644 index 0000000..2951e39 --- /dev/null +++ b/dash/templates/admin/edit_user.html @@ -0,0 +1,96 @@ +{% extends "adminlte/base.html" %} +{% import "adminlte/layout.html" as layout with context %} +{% import "adminlte/widgets.html" as widgets with context %} +{% from "_formhelpers.html" import render_field %} + +{% block title %}Admin - {{ title }}{% endblock %} +{% block description %}{{ block_description }}{% endblock %} + +{% block navbar %} + + {% include "navbar.html" %} + +{%- endblock navbar %} + + +{% block sidebar -%} + + {% include 'sidebar.html' %} + + {% include 'admin/sidebar_menu.html' %} + +{%- endblock sidebar %} + + +{% block content_header -%} + {% include 'admin/content_header.html' %} +{%- endblock content_header %} + +{% block content -%} + +
+
+
+ +
+
+ {{ form.hidden_tag() }} +
+ + + {% if form.email.errors %} + {% for error in form.email.errors %} {{ error }} {% endfor %} + {% endif %} +
+
+ + + {% if form.username.errors %} + {% for error in form.username.errors %} {{ error }} {% endfor %} + {% endif %} +
+
+ + +
+
+ + + + {% if form.role_id.errors %} + {% for error in form.role_id.errors %} {{ error }} {% endfor %} + {% endif %} +
+
+
+
+ + {% if form.confirmed.errors %} +
+ {% for error in form.confirmed.errors %} {{ error }} {% endfor %} + {% endif %} +
+
+ +
+ +
+ +
+
+
+ +
+ +
+ +
+ + +{%- endblock content %} \ No newline at end of file diff --git a/dash/templates/admin/list_users.html b/dash/templates/admin/list_users.html index a39c7c4..f7fb8cb 100644 --- a/dash/templates/admin/list_users.html +++ b/dash/templates/admin/list_users.html @@ -2,6 +2,9 @@ {% import "adminlte/layout.html" as layout with context %} {% import "adminlte/widgets.html" as widgets with context %} +{% block title %}Admin - {{ title }}{% endblock %} +{% block description %}{{ block_description }}{% endblock %} + {% block navbar %} {% include "navbar.html" %} @@ -24,13 +27,9 @@ {% block content -%} -
-
-

User List

-
@@ -52,7 +51,9 @@ - + {% endfor %} @@ -75,7 +76,5 @@ - - {%- endblock content %} \ No newline at end of file diff --git a/dash/templates/content_header.html b/dash/templates/content_header.html index 60e95aa..dcbe3b3 100644 --- a/dash/templates/content_header.html +++ b/dash/templates/content_header.html @@ -1,8 +1,8 @@ -

- Dashboard - Preview page -

- \ No newline at end of file +

+ Dashboard + Preview page +

+ \ No newline at end of file
{{ user.email }} {{ user.created_at }} {{ user.role.name }}Edit + Edit +