-
Notifications
You must be signed in to change notification settings - Fork 8
Commit v0.32
kwmccabe edited this page Apr 17, 2018
·
9 revisions
v0.32 - Refactor user.user_role value/display constants
- +4 -3 [M] mysql/scripts/seeddata.sql
- +1 -1 [M] web/app/decorators.py
- +8 -4 [M] web/app/user/forms.py
- +3 -3 [M] web/app/user/templates/user_edit.html
- +19 -5 [M] web/app/user/templates/user_list.html
- +3 -7 [M] web/app/user/templates/user_profile.html
- +4 -8 [M] web/app/user/templates/user_view.html
- +1 -1 [M] web/app/user/views.py
- +7 -1 [M] web/config.py
- Create test users for each
user_role
.
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`;
- if user_role in ['-1','0','1','2','4']:
+ if user_role in ['-1','0','1','2','3']:
S['user_role'] = int(user_role)
- Update the initialization of
user_role.choices
to usecurrent_app.config['USER_ROLE']
and related values.
+from flask import current_app
...
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
- Replace
user_role
constants withconfig
values.
<!-- BLOCK: content -->
{% block content %}
<div id="item_edit_panel" class="panel {%
- if form.user_role.data == '4' %}panel-success{%
- elif form.user_role.data == '0' %}panel-danger{%
+ if form.user_role.data == config['USER_ROLE_ADMIN']|string %}panel-success{%
+ elif form.user_role.data == config['USER_ROLE_NONE']|string %}panel-danger{%
else %}panel-info{%
endif %}">
...
- <h3 class="condensed">User - '{{ form.user.keyname }}'</h3>
+ <h3 class="condensed">User '{{ form.user.keyname }}'</h3>
- Replace
user_role
constants withconfig
values. - Translate
user_role
value to string fromconfig['USER_ROLE']
.
<select id="user_role" name="user_role" class="form-control" onchange="window.location='{{ url_for('.user_list') }}?user_role='+this.value;">
<option value="-1"{% if session[opts_key]['user_role'] == -1 %} selected{% endif %}>All Users</option>
- <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 %}
- <td onclick="window.location='{{ url_for('.user_view', id=row.id) }}';">{{ row[col] }}</td>
+ <td onclick="window.location='{{ url_for('.user_view', id=row.id) }}';">
+ {% if col == "user_role" %}
+ {{ config['USER_ROLE'][(row[col]|int)] }}
+ {% else %}
+ {{ row[col] }}
+ {% endif %}
+ </td>
{% endfor %}
- Replace
user_role
constants withconfig
values.
<!-- BLOCK: content -->
{% block content %}
<div id="item_list_panel" class="panel {%
- if user.user_role == 4 %}panel-success{%
+ if user.user_role == config['USER_ROLE_ADMIN'] %}panel-success{%
else %}panel-info{%
endif %}">
...
- {% 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 %}
- Replace
user_role
constants withconfig
values.
<!-- BLOCK: content -->
{% block content %}
<div id="item_list_panel" class="panel {%
- if user.user_role == 4 %}panel-success{%
- elif user.user_role == 0 %}panel-danger{%
+ if user.user_role == config['USER_ROLE_ADMIN'] %}panel-success{%
+ elif user.user_role == config['USER_ROLE_NONE'] %}panel-danger{%
else %}panel-info{%
endif %}">
...
- {% 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 %}
- ERROR FIX: delete non-editable fields ONLY after valid submit.
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)
- Add
USER_ROLE
dictionary toconfig
, used to mapuser.user_role
values to display strings.
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',
+ }
Commit-v0.31 | Commit-v0.32 | Commit-v0.33
- FlaskApp Tutorial
- Table of Contents
- About
- Application Setup
- Modules, Templates, and Layouts
- Database Items, Forms, and CRUD
- List Filter, Sort, and Paginate
- Users and Login
- Database Relationships
- API Module, HTTPAuth and JSON
- Refactoring User Roles and Item Status
- AJAX and Public Pages