Skip to content

Commit v0.32

kwmccabe edited this page Apr 11, 2018 · 9 revisions

Commit v0.32

Refactor user.user_role value/display constants

Files changed (9)

+4 -3 M mysql/scripts/seeddata.sql Comments 1 +1 -1 M web/app/decorators.py +8 -4 M web/app/user/forms.py Comments 1 +3 -3 M web/app/user/templates/user_edit.html Comments 1 +19 -5 M web/app/user/templates/user_list.html Comments 1 +3 -7 M web/app/user/templates/user_profile.html Comments 1 +4 -8 M web/app/user/templates/user_view.html Comments 1 +1 -1 M web/app/user/views.py Comments 1 +7 -1 M web/config.py Comments 1 File mysql/scripts/seeddata.sql MODIFIED Side-by-side diff View file Comment More kwmccabe AUTHOR Create test users for each user_role. Reply Edit Delete 5 days ago USE flaskapp;

DELETE FROM user; -INSERT INTO user (user_role,keyname,user_email) VALUES (4,"admin","[email protected]"); -INSERT INTO user (user_role,keyname,user_email) VALUES (1,"user","[email protected]"); -INSERT INTO user (user_role,keyname,user_email) VALUES (0,"user2","[email protected]"); +INSERT INTO user (user_role,keyname,user_email) VALUES (3,"admin","[email protected]"); +INSERT INTO user (user_role,keyname,user_email) VALUES (2,"edit","[email protected]"); +INSERT INTO user (user_role,keyname,user_email) VALUES (1,"view","[email protected]"); +INSERT INTO user (user_role,keyname,user_email) VALUES (0,"none","[email protected]"); OPTIMIZE TABLE user;

DELETE FROM item; File web/app/decorators.py MODIFIED Side-by-side diff View file Comment More

         if status in ['all','active','inactive']:
             S['status'] = status
  •        if user_role in ['-1','0','1','2','4']:
    
  •        if user_role in ['-1','0','1','2','3']:
               S['user_role'] = int(user_role)
           if len(sort) > 0 and sort != S['sort']:
               S['sort']  = sort
    

File web/app/user/forms.py MODIFIED Side-by-side diff View file Comment More kwmccabe AUTHOR Update the initialization of user_role.choices to use current_app.config['USER_ROLE'] and related values. Reply Edit Delete 5 days ago import re

+from flask import current_app from flask_wtf import FlaskForm from wtforms import BooleanField, DecimalField, FloatField, IntegerField,
DateTimeField, DateField,
HiddenField, SubmitField from wtforms.validators import Email, EqualTo, InputRequired, Length from wtforms import ValidationError -#from flask_pagedown.fields import PageDownField from .models import UserModel

def filter_username(data): return re.sub('[^a-z0-9_-]', '', str(data).lower())

class EditUserForm(FlaskForm): id = HiddenField('id')

  • user_role = SelectField('User Role') keyname = StringField('Username', validators=[InputRequired(),Length(2,63),validate_username], filters=[filter_username])
  • user_role = SelectField('User Role') user_email = StringField('Email', validators=[InputRequired(),Length(1,63),Email(),validate_usermail], filters=[filter_useremail]) password = PasswordField('Password', validators=[EqualTo('password2',message="Passwords must match.")]) password2 = PasswordField('Confirm Password')

    def init(self, user, *args, **kwargs): super(EditUserForm, self).init(*args, **kwargs)

  •    self.user_role.choices = [('4','Administrator'),('2','Editor'),('1','User'),('0','Inactive')]
    
  •    self.user_role.choices = [
    
  •        (str(current_app.config['USER_ROLE_ADMIN']),current_app.config['USER_ROLE'][current_app.config['USER_ROLE_ADMIN']]),
    
  •        (str(current_app.config['USER_ROLE_EDIT']), current_app.config['USER_ROLE'][current_app.config['USER_ROLE_EDIT']]),
    
  •        (str(current_app.config['USER_ROLE_VIEW']), current_app.config['USER_ROLE'][current_app.config['USER_ROLE_VIEW']]),
    
  •        (str(current_app.config['USER_ROLE_NONE']), current_app.config['USER_ROLE'][current_app.config['USER_ROLE_NONE']]),
    
  •        ]
       self.user = user
    

File web/app/user/templates/user_edit.html MODIFIED Side-by-side diff View file Comment More kwmccabe AUTHOR Replace user_role constants with config values. Reply Edit Delete 5 days ago

{% block content %}

[ View User ] [ Edit User ] -

User - '{{ form.user.keyname }}'

+

User '{{ form.user.keyname }}'

File web/app/user/templates/user_list.html MODIFIED Side-by-side diff View file Comment More kwmccabe AUTHOR Replace user_role constants with config values. Translate user_role value to string from config['USER_ROLE']. Reply Edit Delete 5 days ago Role <option value="-1"{% if session[opts_key]['user_role'] == -1 %} selected{% endif %}>All Users

  •        <option value="4"{% if session[opts_key]['user_role'] == 4 %} selected{% endif %}>Administrators</option>
    
  •        <option value="2"{% if session[opts_key]['user_role'] == 2 %} selected{% endif %}>Editors</option>
    
  •        <option value="1"{% if session[opts_key]['user_role'] == 1 %} selected{% endif %}>Users</option>
    
  •        <option value="0"{% if session[opts_key]['user_role'] == 0 %} selected{% endif %}>Inactive</option>
    
  •        <option value="{{ config['USER_ROLE_ADMIN'] }}"{%
    
  •            if session[opts_key]['user_role'] == config['USER_ROLE_ADMIN'] %} selected{% endif
    
  •            %}>{{ config['USER_ROLE'][config['USER_ROLE_ADMIN']] }}</option>
    
  •        <option value="{{ config['USER_ROLE_EDIT'] }}"{%
    
  •            if session[opts_key]['user_role'] == config['USER_ROLE_EDIT'] %} selected{% endif
    
  •            %}>{{ config['USER_ROLE'][config['USER_ROLE_EDIT']] }}</option>
    
  •        <option value="{{ config['USER_ROLE_VIEW'] }}"{%
    
  •            if session[opts_key]['user_role'] == config['USER_ROLE_VIEW'] %} selected{% endif
    
  •            %}>{{ config['USER_ROLE'][config['USER_ROLE_VIEW']] }}</option>
    
  •        <option value="{{ config['USER_ROLE_NONE'] }}"{%
    
  •            if session[opts_key]['user_role'] == config['USER_ROLE_NONE'] %} selected{% endif
    
  •            %}>{{ config['USER_ROLE'][config['USER_ROLE_NONE']] }}</option>
       </select>
    
{% for col in cols %} - {{ row[col] }} + + {% if col == "user_role" %} + {{ config['USER_ROLE'][(row[col]|int)] }} + {% else %} + {{ row[col] }} + {% endif %} + {% endfor %} File web/app/user/templates/user_profile.html MODIFIED Side-by-side diff View file Comment More kwmccabe AUTHOR Replace user_role constants with config values. Reply Edit Delete 5 days ago {% block content %}
{{ col }} - {% if col == "user_role" %}{% - if user[col] == 4 %}Administrator{% - elif user[col] == 2 %}Editor{% - elif user[col] == 1 %}User{% - else %}Inactive{% - endif %} + {% if col == "user_role" %} + {{ config['USER_ROLE'][(user[col]|int)] }} {% else %} {{ user[col] }} {% endif %} File web/app/user/templates/user_view.html MODIFIED Side-by-side diff View file Comment More kwmccabe AUTHOR Replace user_role constants with config values. Reply Edit Delete 5 days ago {% block content %}
{{ col }} - {% if col == "user_role" %}{% - if user[col] == 4 %}Administrator{% - elif user[col] == 2 %}Editor{% - elif user[col] == 1 %}User{% - else %}Inactive{% - endif %} + {% if col == "user_role" %} + {{ config['USER_ROLE'][(user[col]|int)] }} {% else %} {{ user[col] }} {% endif %} File web/app/user/views.py MODIFIED Side-by-side diff View file Comment More kwmccabe AUTHOR ERROR FIX: delete non-editable fields ONLY after valid submit. Reply Edit Delete 5 days ago def user_edit( id ): user = UserModel.query.get_or_404(id) form = EditUserForm(user) - del form.cnt_login, form.mod_login, form.mod_create, form.mod_update if form.validate_on_submit(): + del form.cnt_login, form.mod_login, form.mod_create, form.mod_update if form.password.data == '': del form.password form.populate_obj(user) File web/config.py MODIFIED Side-by-side diff View file Comment More kwmccabe AUTHOR Add USER_ROLE dictionary to config, used to map user.user_role values to display strings. Reply Edit Delete 5 days ago USER_ROLE_NONE = 0 USER_ROLE_VIEW = 1 USER_ROLE_EDIT = 2 - USER_ROLE_ADMIN = 4 + USER_ROLE_ADMIN = 3 + USER_ROLE = { + USER_ROLE_NONE: 'Inactive', + USER_ROLE_VIEW: 'User', + USER_ROLE_EDIT: 'Editor', + USER_ROLE_ADMIN: 'Administrator', + }
 @staticmethod
 def init_app(app):
Clone this wiki locally