Skip to content

Commit v0.27

kwmccabe edited this page Apr 13, 2018 · 9 revisions

@app.context_processor: current_datetime(), debug_queries()


Files changed (7)

File web/app/item/models.py MODIFIED

  • Reorder columns.
 class ItemModel(db.Model):
     __tablename__ = 'item'
     id         = db.Column(db.BigInteger, autoincrement=True, primary_key=True)
-    keyname    = db.Column(db.String(63), nullable=False, index=True, unique=True, default='')
     active     = db.Column(db.Boolean, nullable=False, index=True, default=1)
+    keyname    = db.Column(db.String(63), nullable=False, index=True, unique=True, default='')
     item_title = db.Column(db.String(255))

File web/app/item/views.py MODIFIED

  • Remove item_text from columns at /admin/item/list.
  • Remove data prefetch in rows = rows.all().
  • Replace rowcnt = len(rows) with rowcnt = rows.count().
 @login_required
 def item_list():
     cols = ItemModel.__table__.columns.keys()
+    cols_filtered = list(filter(lambda x: x not in ['item_text'], cols))
     rows = db.session.query(ItemModel)

...
     if S['sort'] == 'owner_id':
         from ..user.models import UserModel
         rows = rows.outerjoin(UserModel)
+        #rows = rows.options( db.joinedload(ItemModel.owner_id).load_only("keyname", "user_email") )
+        rows = rows.options( \
+            db.Load(ItemModel).defer("item_text"), \
+            db.Load(UserModel).load_only("keyname", "user_email"), \
+        )

...

-    elif S['sort'] in cols:
+    elif S['sort'] in cols_filtered:

...

-    rows = rows.all()
-    rowcnt = len(rows)
+    #rows = rows.all()
+    #rowcnt = len(rows)
+    rowcnt = rows.count()
 
     logging.debug('item_list - %s' % (rowcnt))
-    return render_template('item_list.html', cols=cols,rows=rows,rowcnt=rowcnt,opts_key=opts_key)
+    return render_template('item_list.html', cols=cols_filtered,rows=rows,rowcnt=rowcnt,opts_key=opts_key)

File web/app/main/templates/base.html MODIFIED

  • Add calls to new @app.context_processor: debug_queries() and current_datetime().
 <div id="debuginfo"><pre>
 DEBUG:
-{% if current_user.user_email %}LOGGED IN: {{ current_user.user_email }}{% else %}LOGGED OUT{% endif %}
-TEMPLATES: {% block templates %}base.html{% endblock %}
-REQUEST.ARGS: {{ request.args }}
-REQUEST.COOKIES: {{ request.cookies }}
-SESSION: {{ session }}
+{% if current_user.user_email %}<b>LOGGED IN:</b> {{ current_user.user_email }}{% else %}<b>LOGGED OUT</b>{% endif %}
+<b>TEMPLATES:</b> {% block templates %}base.html{% endblock %}
+<b>REQUEST.ARGS:</b> {{ request.args }}
+<b>REQUEST.COOKIES:</b> {{ request.cookies }}
+<b>SESSION:</b> {{ session }}
+<b>QUERIES :</b> {{ debug_queries() }}
+<b>DATETIME :</b> {{ current_datetime() }}
 </pre></div>

File web/app/static/css/flaskapp.css MODIFIED

  • Reorder sections
 @charset "utf-8";
 
 /**
- * layout
- * @see base.html
+ * generic element attributes
+ */
+h1.condensed, h2.condensed, h3.condensed, h4.condensed { margin: 10px 0 5px 0; }
+
+form { padding: 15px; }
+.form-submit-buttons { padding-left: 15px; }
+
+.table-condensed { margin-bottom: 0; }
+.table-borderless td,
+.table-borderless th {
+    border: none !important;
+}
+
+/**
+ * layout elements defined in base.html
  */
 #header { min-height: 80px; }

...

-/**
- * generic element attributes
- */
-h1.condensed, h2.condensed, h3.condensed, h4.condensed { margin: 10px 0 5px 0; }
-
-form { padding: 15px; }
-.form-submit-buttons { padding-left: 15px; }
-
-.table-condensed { margin-bottom: 0; }
-.table-borderless td,
-.table-borderless th {
-    border: none !important;
-}

File web/app/user/models.py MODIFIED

  • Reorder columns.
-    keyname    = db.Column(db.String(63), nullable=False, unique=True, index=True, default='')
+    keyname    = db.Column(db.String(63), nullable=False, index=True, unique=True, default='')

File web/app/user/views.py MODIFIED

  • Remove data prefetch in rows = rows.all().
  • Replace rowcnt = len(rows) with rowcnt = rows.count().
     if S['limit'] > 0:
         rows = rows.limit(S['limit'])
 
-    rows = rows.all()
-    rowcnt = len(rows)
-
+    rowcnt = rows.count()
     logging.debug('user_list - %s' % (rowcnt))
     return render_template('user_list.html', cols=cols_filtered,rows=rows,rowcnt=rowcnt,opts_key=opts_key)
 

File web/flaskapp.py MODIFIED

  • Add @app.context_processor to create template functions.
  • Define function current_datetime().
  • Define function debug_queries().
+# Utility functions available within templates
+@app.context_processor
+def utility_processor():
+    def current_datetime():
+        import datetime
+        return datetime.datetime.now().strftime("%Y/%m/%d @ %H:%M:%S")
+    def debug_queries():
+        from flask_sqlalchemy import get_debug_queries
+        return get_debug_queries()
+    return dict(\
+        current_datetime=current_datetime, \
+        debug_queries=debug_queries, \
+        )

Commit-v0.26 < Commit-v0.27 > Commit-v0.28

Clone this wiki locally