Skip to content

Commit v0.32

kwmccabe edited this page Apr 17, 2018 · 9 revisions

v0.32 - Refactor user.user_role value/display constants


Files changed (9)

File mysql/scripts/seeddata.sql MODIFIED

  • 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`;

File web/app/decorators.py MODIFIED

-            if user_role in ['-1','0','1','2','4']:
+            if user_role in ['-1','0','1','2','3']:
                 S['user_role'] = int(user_role)

File web/app/user/forms.py MODIFIED

  • Update the initialization of user_role.choices to use current_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

File web/app/user/templates/user_edit.html MODIFIED

  • Replace user_role constants with config 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>

File web/app/user/templates/user_list.html MODIFIED

  • Replace user_role constants with config values.
  • Translate user_role value to string from config['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 %}

File web/app/user/templates/user_profile.html MODIFIED

  • Replace user_role constants with config 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 %}

File web/app/user/templates/user_view.html MODIFIED

  • Replace user_role constants with config 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 %}

File web/app/user/views.py MODIFIED

  • 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)

File web/config.py MODIFIED

  • Add USER_ROLE dictionary to config, used to map user.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

Clone this wiki locally