-
Notifications
You must be signed in to change notification settings - Fork 8
Commit v0.17
kwmccabe edited this page Apr 17, 2018
·
9 revisions
v0.17 - item_action() route and item checkboxes
- +18 -2 [M] web/app/item/templates/item_list.html
- +31 -0 [M] web/app/item/views.py
- Wrap item
<table>
in HTML<form>
. - Add
<checkbox>
for eachrow
. - Add
<select>
menu for Action types. - Add
<submit>
button. - Post selection(s) to route
/admin/item/action
.
<!-- BLOCK: content -->
{% block content %}
+<form class="form-inline" action="{{ url_for('.item_action') }}" method="post">
...
<div class="table-responsive">
<table class="table table-condensed table-hover">
<tr>
+<th></th>
{% for col in cols %}
<th>{{ col }}</th>
{% endfor %}
...
{% for row in rows %}
<tr>
+ <td>
+ <input type="checkbox" name="item_id" value="{{row.id}}">
+ </td>
{% 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> ]
- [ <a data-confirm-link="Delete Item? This action cannot be undone." href="{{ url_for('.item_delete', id=row.id) }}">delete</a> ]
</td>
</tr>
{% endfor %}
...
<div class="panel-footer">
- panel-footer
+<div id="list-actions">
+ <div class="form-group">
+ <label for="action" class="sr-only">Action</label>
+ <select id="action" name="action" class="form-control">
+ <option value="">Selected Items...</option>
+ <option value="active">Set Active</option>
+ <option value="inactive">Set Inactive</option>
+ <option value="delete">Delete</option>
+ </select>
+ </div>
+ <button class="btn btn-default" type="submit">Apply</button>
+</div>
</div> <!-- end class="panel-footer" -->
</div> <!-- end class="panel" -->
+</form>
{% endblock content %}
- Create POST-only route
/admin/item/action
for form at/admin/item/list
. - Apply
action
to all selecteditem_id
. - Redirect to
/item/admin/list
to show results.
+@item.route('/admin/item/action', methods=['POST'])
+def item_action():
+ action = request.values.get('action', '')
+ item_ids = request.form.getlist('item_id')
+ id_str = "["+",".join([str(id) for id in item_ids])+"]"
+
+ if action and item_ids:
+ if action == 'delete':
+ for id in item_ids:
+ item = ItemModel.query.get_or_404(id)
+ db.session.delete(item)
+ db.session.commit()
+ flash('Items Deleted (id='+id_str+')')
+ if action == 'active':
+ for id in item_ids:
+ item = ItemModel.query.get_or_404(id)
+ item.active = True
+ db.session.add(item)
+ db.session.commit()
+ flash('Items Activated (id='+id_str+')')
+ if action == 'inactive':
+ for id in item_ids:
+ item = ItemModel.query.get_or_404(id)
+ item.active = False
+ db.session.add(item)
+ db.session.commit()
+ flash('Items Deactivated (id='+id_str+')')
+ logging.info('item_action - action:%s, item_ids:%s' % (action, id_str))
+ return redirect(url_for('.item_list'))
Commit-v0.16 | Commit-v0.17 | Commit-v0.18
- 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