-
Notifications
You must be signed in to change notification settings - Fork 8
Commit v0.19
kwmccabe edited this page Apr 17, 2018
·
8 revisions
v0.19 - Move item_list() options to session
- +49 -17 [M] web/app/item/views.py
- +1 -0 [M] web/app/main/templates/base.html
- +17 -1 [M] web/app/main/views.py
- Move locally-scoped variables
status
,sort
,order
,offset
, andlimit
into thesession
. - Values updated via the
request
.
-from flask import flash, redirect, render_template, request, url_for
+from flask import flash, redirect, render_template, request, session, url_for
...
- status = 'all' # in [all, active, inactive]
- sort = 'id' # in [cols]
- order = 'asc' # in [asc, desc]
- offset = 0 # [none, (page-1)*perpage]
- limit = 0 # [unlimited, per page]
-
- if status in ['active', 'inactive']:
+ # 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
+ if S['status'] in ['active', 'inactive']:
rows = rows.filter(ItemModel.active == (status == 'active'))
- if sort in cols:
- if order == 'desc':
- rows = rows.order_by(getattr( ItemModel, sort ).desc())
+ 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, sort ).asc())
- if offset > 0:
- rows = rows.offset(offset)
- if limit > 0:
- rows = rows.limit(limit)
+ rows = rows.order_by(getattr( ItemModel, S['sort'] ).asc())
+ if S['offset'] > 0:
+ rows = rows.offset(S['offset'])
+ if S['limit'] > 0:
+ rows = rows.limit(S['limit'])
rows = rows.all()
rowcnt = len(rows)
- Add debug output to display current
session
values.
DEBUG:
TEMPLATES: {% block templates %}base.html{% endblock %}
REQUEST.ARGS: {{ request.args }}
+SESSION: {{ session }}
</pre></div>
- Create utility route
/info/session
to display currentsession
values. - Create utility route
/info/session_clear
to reset the ``session.
-from flask import abort, current_app, escape, redirect, render_template, url_for
+from flask import abort, current_app, escape, redirect, render_template, request, session, url_for
...
result += '<br/><a href="'+url_for('.info_config')+'">show app.config</a>'
result += '<br/><a href="'+url_for('.info_url_map')+'">show url_map</a>'
result += '<br/><a href="'+url_for('.info_request')+'">show request</a>'
+ result += '<br/><a href="'+url_for('.info_session')+'">show session</a>'
+ result += '<br/><a href="'+url_for('.info_session_clear')+'">clear session</a>'
return result
...
+@main.route('/info/session')
+def info_session():
+ result = ''
+ for key in session.keys():
+ result += "<br/>"+key+" : "+str(session[key])
+ return "Session : "+result
+
+
+@main.route('/info/session_clear')
+def info_session_clear():
+ session.clear()
+ return redirect(url_for('.info_session'))
Commit-v0.18 | Commit-v0.19 | Commit-v0.20
- 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