-
Notifications
You must be signed in to change notification settings - Fork 8
Commit v0.14
kwmccabe edited this page Apr 17, 2018
·
9 revisions
v0.14 - List Items (/admin/item/list) and View Item (/admin/item/view/int:id)
- +6 -0 [M] web/app/item/models.py
- +72 -0 [A] web/app/item/templates/item_list.html
- +48 -0 [A] web/app/item/templates/item_view.html
- +35 -2 [M] web/app/item/views.py
- +7 -0 [M] web/app/main/templates/base.html
- +20 -1 [M] web/app/static/css/flaskapp.css
- Update string representation of object for debug output.
def __repr__(self):
- return '<Item %r>' % (self.id)
+ return '<ItemModel: id="%r", keyname="%r">' % (self.id, self.keyname)
+
+ def __str__(self):
+ return 'Item: "%r"' % (self.keyname)
- New template for viewing a list of items.
- Rows are in ascending order by
item.id
. - Columns are ordered by the
ItemModel
. - Each
row
is followed by an.item_edit
link.
+{% extends "base.html" %}
+
+
+<!-- BLOCK: title -->
+{% block title %}{{super()}}Items{% endblock %}
+
+
+<!-- BLOCK: breadcrumb -->
+{% block breadcrumb %}
+{{super()}}
+{# <li><a href="{{ url_for('.item_list') }}">List Items</a></li> #}
+{% endblock %}
+
+
+<!-- BLOCK: content -->
+{% block content %}
+
+<div id="item_list_panel" class="panel panel-info">
+ <div class="panel-heading">
+
+<div class="table-responsive">
+<table class="table table-condensed table-borderless">
+<tr>
+<td>
+ Showing {{ rowcnt }} Items
+</td>
+<td class="text-right">
+ <span>Page 1 of 1</span>
+</td>
+</tr>
+</table>
+</div>
+
+ </div> <!-- end class="panel-heading" -->
+ <div class="panel-body">
+
+<div class="table-responsive">
+<table class="table table-condensed table-hover">
+<tr>
+{% for col in cols %}
+ <th>{{ col }}</th>
+{% endfor %}
+<th></th>
+</tr>
+
+{% for row in rows %}
+ <tr>
+ {% for col in cols %}
+ <td onclick="window.location='{{ url_for('.item_view', id=row.id) }}';">{{ row[col] }}</td>
+ {% endfor %}
+ <td>
+ <a href="{{ url_for('.item_edit', id=row.id) }}">edit</a>
+ </td>
+ </tr>
+{% endfor %}
+
+</table>
+</div>
+
+ </div> <!-- end class="panel-body" -->
+ <div class="panel-footer">
+ panel-footer
+ </div> <!-- end class="panel-footer" -->
+</div> <!-- end class="panel" -->
+
+{% endblock content %}
+
+
+<!-- BLOCK: templates -->
+{% block templates %}{{super()}} - item_list.html{% endblock %}
+
+{# end web/app/item/templates/item_list.html #}
- New template for viewing individual items.
- All values output in simple table of column names and column values.
+{% extends "base.html" %}
+
+
+<!-- BLOCK: title -->
+{% block title %}{{super()}}Item - {{ item.keyname }}{% endblock %}
+
+
+<!-- BLOCK: breadcrumb -->
+{% block breadcrumb %}
+{{super()}}
+<li><a href="{{ url_for('.item_view', id=item.id) }}">View Item</a></li>
+{% endblock %}
+
+
+<!-- BLOCK: content -->
+{% block content %}
+<div id="item_list_panel" class="panel panel-info">
+ <div class="panel-heading">
+ <h3 class="condensed">Item : {{ item.keyname }}</h3>
+ </div>
+ <div class="panel-body">
+
+<div class="table-responsive">
+<table class="table table-condensed table-hover">
+<tr>
+ <th>column</th>
+ <th>value</th>
+</tr>
+
+{% for col in cols %}
+ <tr>
+ <td>{{ col }}</td>
+ <td>{{ item[col] }}</td>
+ </tr>
+{% endfor %}
+
+</table>
+</div>
+
+ </div> <!-- end class="panel-body" -->
+</div> <!-- end class="panel" -->
+{% endblock %}
+
+
+<!-- BLOCK: templates -->
+{% block templates %}{{super()}} - item_view.html{% endblock %}
+
+{# end web/app/item/templates/item_view.html #}
- Create placeholder routes for Delete
/admin/item/delete
, Create/admin/item/create
, and Edit/admin/item/edit
. - Create View route
/admin/item/view
for templateitem_view.html
. - Create List route
/admin/item/list
for templateitem_list.html
.
+from flask import render_template
...
+@item.route('/admin/item/delete/<int:id>')
+def item_delete( id ):
+ return 'item_delete - id:%s' % (id)
+
+@item.route('/admin/item/create')
+def item_create():
+ return 'item_create'
+
+@item.route('/admin/item/edit/<int:id>')
+def item_edit( id ):
+ return 'item_edit - id:%s' % (id)
+
+
+@item.route('/admin/item/view/<int:id>')
+def item_view( id ):
+ item = ItemModel.query.get_or_404(id)
+ cols = ItemModel.__table__.columns.keys()
+ return render_template('item_view.html', cols=cols, item=item)
+
+
+@item.route('/admin/item/list')
+def item_list():
+ cols = ItemModel.__table__.columns.keys()
+
+ rows = db.session.query(ItemModel)
+ rows = rows.order_by(getattr( ItemModel, 'id' ).asc())
+ rows = rows.all()
+
+ rowcnt = len(rows)
+
+ logging.debug('item_list - %s' % (rowcnt))
+ return render_template('item_list.html', cols=cols,rows=rows,rowcnt=rowcnt)
- Add block to display debug info onscreen.
- The block
templates
is updated by each sub-template to show the hierarchy.
+ {% if config['DEBUG'] %}
+<div id="debuginfo"><pre>
+DEBUG:
+TEMPLATES: {% block templates %}base.html{% endblock %}
+</pre></div>
+ {% endif %}
- Make some adjustments for mixing bootstrap panels and tables.
- Make the
debuginfo
div look pretty.
#header_hr {
margin-top: 0;
}
+#footer_hr {
+ margin-bottom: 0;
+}
+
+
+/**
+ * generic element attributes
+ */
+h1.condensed, h2.condensed, h3.condensed, h4.condensed { margin: 10px 0 5px 0; }
+
+.table-borderless td,
+.table-borderless th {
+ border: none !important;
+}
+
+#debuginfo { padding: 15px;}
Commit-v0.13 | Commit-v0.14 | Commit-v0.15
- 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