Skip to content

Commit v0.20

kwmccabe edited this page Apr 17, 2018 · 7 revisions

v0.20 - Create decorator for item_list() options


Files changed (3)

File web/app/decorators.py ADDED

  • Create prototype decorator my_decorator().
  • Move session handling from item_list() to a new decorator function get_list_opts().
  • Init session[session_key] and set defaults.
  • Update session for valid request values.
  • The model param is required for S['itemcnt'] = db.session.query(model).count().
+import logging
+from functools import wraps
+
+from flask import request, session
+from . import db
+
+# @my_decorator('myvalue')
+def my_decorator(key='some value'):
+    def _decorator(f):
+        @wraps(f)
+        def _decorated(*args, **kwargs):
+            logging.debug('my_decorator( %s )' % (key))
+            return f(*args, **kwargs)
+        return _decorated
+        #return wraps(f)(_decorated)
+    return _decorator
+
+
+# create session values for list select options
+# from ..decorators import get_list_opts
+# @get_list_opts(ItemModel,'item_list_opts')
+def get_list_opts( model, session_key='list_opts' ):
+    def _decorator(f):
+        @wraps(f)
+        def _decorated(*args, **kwargs):
+
+            # set default values
+            if not session_key in session:
+                logging.debug('create session[%s]' % (session_key))
+                session[session_key] = { \
+                    'itemcnt' : 0, \
+                    'status'  : 'all', \
+                    'sort'    : 'id', \
+                    'order'   : 'asc', \
+                    'offset'  : 0, \
+                    'limit'   : 0, \
+                    }
+
+            # get updates
+            S = session[session_key]
+            status = request.values.get('status', S['status'])
+            sort   = request.values.get('sort',   S['sort'])
+            order  = request.values.get('order',  S['order'])
+            offset = int(request.values.get('offset',  S['offset']))
+            limit  = int(request.values.get('limit', S['limit']))
+
+            S['itemcnt'] = db.session.query(model).count()
+            if status in ['all','active','inactive']:
+                S['status'] = status
+            if len(sort) > 0 and sort != S['sort']:
+                S['sort']  = sort
+                S['order'] = 'asc'
+            elif order in ['asc','desc']:
+                S['order'] = order
+
+            if limit > 0 and limit != S['limit']:
+                S['limit'] = limit
+            if offset > 0 and offset != S['offset']:
+                S['offset'] = offset
+
+            # set result
+            session[session_key] = S
+
+            return f(*args, **kwargs)
+        return _decorated
+    return _decorator

File web/app/item/templates/item_list.html MODIFIED

  • Display and use values preset in session[opts_key].
  • Add links for filter by item status.
  • Add links for sort by column name.
 <td>
-    Showing {{ rowcnt }} Items
+    Showing {{ rowcnt }} of {{ session[opts_key]['itemcnt'] }} Items
 </td>

...

 <td class="text-right">
-    [<a href="{{ url_for('.item_list') }}?status=all">All</a>]
-    [<a href="{{ url_for('.item_list') }}?status=active">Active</a>]
-    [<a href="{{ url_for('.item_list') }}?status=inactive">Inactive</a>]
+    {% if session[opts_key]['status'] == 'active' %}
+        [<a href="{{ url_for('.item_list') }}?status=all">All</a>]
+        [Active]
+        [<a href="{{ url_for('.item_list') }}?status=inactive">Inactive</a>]
+    {% elif session[opts_key]['status'] == 'inactive' %}
+        [<a href="{{ url_for('.item_list') }}?status=all">All</a>]
+        [<a href="{{ url_for('.item_list') }}?status=active">Active</a>]
+        [Inactive]
+    {% else %}
+        [All]
+        [<a href="{{ url_for('.item_list') }}?status=active">Active</a>]
+        [<a href="{{ url_for('.item_list') }}?status=inactive">Inactive</a>]
+    {% endif %}
 </td>

...

 <tr>
 <th></th>
 {% for col in cols %}
-    <th>{{ col }}</th>
+    <th>
+    {% if col == session[opts_key]['sort'] and session[opts_key]['order'] == 'asc' %}
+        <a href="{{ url_for('.item_list') }}?sort={{ col }}&order=desc"><span class="glyphicon glyphicon-sort-by-attributes" aria-hidden="true"></span></a>
+    {% elif col == session[opts_key]['sort'] %}
+        <a href="{{ url_for('.item_list') }}?sort={{ col }}&order=asc"><span class="glyphicon glyphicon-sort-by-attributes-alt" aria-hidden="true"></span></a>
+    {% else %}
+        <a href="{{ url_for('.item_list') }}?sort={{ col }}"><span class="glyphicon glyphicon-sort" aria-hidden="true"></span></a>
+    {% endif %}
+    {{ col }}
+    </th>
 {% endfor %}
 <th></th>
 </tr>

...

     <td>
-        [ <a href="{{ url_for('.item_edit', id=row.id) }}">edit</a> ]
+        <a href="{{ url_for('.item_edit', id=row.id) }}"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
     </td>

File web/app/item/views.py MODIFIED

  • Remove local handling for session values.
  • Add function decorator @get_list_opts(ItemModel,'item_list_opts'), passing the model and session key.
  • Add the session key opts_key to the call render_template().
+from ..decorators import get_list_opts
 
... 
 
 @item.route('/admin/item/list')
+@get_list_opts(ItemModel,'item_list_opts')
 def item_list():
     cols = ItemModel.__table__.columns.keys()
     rows = db.session.query(ItemModel)
 
-    # set default session values
-    session_key = 'item_list_opts'
-    if not session_key in session:
-        logging.debug('create session[%s]' % (session_key))
-        session[session_key] = { \
-            'itemcnt' : 0, \
-            'status'  : 'all', \
-            'sort'    : 'id', \
-            'order'   : 'asc', \
-            'offset'  : 0, \
-            'limit'   : 0, \
-            }
-
-    # get session updates
-    S = session[session_key]
-    status = request.values.get('status', S['status'])
-    sort   = request.values.get('sort',   S['sort'])
-    order  = request.values.get('order',  S['order'])
-    offset = int(request.values.get('offset',  S['offset']))
-    limit  = int(request.values.get('limit', S['limit']))
-
-    S['itemcnt'] = db.session.query(ItemModel).count()
-    if status in ['all','active','inactive']:
-        S['status'] = status
-    if sort in cols and sort != S['sort']:
-        S['sort']  = sort
-        S['order'] = 'asc'
-    elif order in ['asc','desc']:
-        S['order'] = order
-
-    if limit > 0 and limit != S['limit']:
-        S['limit'] = limit
-    if offset > 0 and offset != S['offset']:
-        S['offset'] = offset
-
-    # set session result
-    session[session_key] = S
-
     # use session values to filter/order items
+    opts_key = 'item_list_opts'
+    S = session[opts_key]
+
     if S['status'] in ['active', 'inactive']:
-        rows = rows.filter(ItemModel.active == (status == 'active'))
+        rows = rows.filter(ItemModel.active == (S['status'] == 'active'))
     if S['sort'] in cols:
         if S['order'] == 'desc':
             rows = rows.order_by(getattr( ItemModel, S['sort'] ).desc())
         else:
             rows = rows.order_by(getattr( ItemModel, S['sort'] ).asc())
 
...

-    return render_template('item_list.html', cols=cols,rows=rows,rowcnt=rowcnt)
+    return render_template('item_list.html', cols=cols,rows=rows,rowcnt=rowcnt,opts_key=opts_key)

Commit-v0.19 | Commit-v0.20 | Commit-v0.21

Clone this wiki locally