-
Notifications
You must be signed in to change notification settings - Fork 8
Commit v0.20
kwmccabe edited this page Apr 17, 2018
·
7 revisions
v0.20 - Create decorator for item_list() options
- +66 -0 [A] web/app/decorators.py
- +25 -6 [M] web/app/item/templates/item_list.html
- +8 -40 [M] web/app/item/views.py
- Create prototype decorator
my_decorator()
. - Move
session
handling fromitem_list()
to a new decorator functionget_list_opts()
. - Init
session[session_key]
and set defaults. - Update
session
for validrequest
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
- 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>
- 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 callrender_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
- 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