Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable custom actions. #113

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions flask_peewee/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,19 @@ def get_query(self):
def get_object(self, pk):
return self.get_query().where(self.pk==pk).get()

def get_actions(self):
return (
('delete', {'url': '/delete/', 'title': 'Delete'}),
('export', {'url': '/export/', 'title': 'Export'}),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not (self.delete, {'url': '/delete/', 'title': 'Delete'}) ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, since the "string" name of the view is pretty important, a 3-tuple:

(self.delete, 'delete', {'url': '/delete/', 'title': 'Delete'})

)

def get_urls(self):
actions_urls = [(v['url'], getattr(self, k))
for k, v in self.get_actions()]
return (
('/', self.index),
('/add/', self.add),
('/delete/', self.delete),
('/export/', self.export),
) + tuple(actions_urls) + (
('/<pk>/', self.edit),
('/_ajax/', self.ajax_list),
)
Expand Down Expand Up @@ -219,12 +226,12 @@ def index(self):
# create a paginated query out of our filtered results
pq = PaginatedQuery(query, self.paginate_by)

# actions
if request.method == 'POST':
id_list = request.form.getlist('id')
if request.form['action'] == 'delete':
return redirect(url_for(self.get_url_name('delete'), id=id_list))
else:
return redirect(url_for(self.get_url_name('export'), id=id_list))
action = request.form['action']
if action in [k for k, v in self.get_actions()]:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[k.__name__ for k, v in self.get_actions()]?

id_list = request.form.getlist('id')
return redirect(url_for(self.get_url_name(action), id=id_list))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we raise a 404 if the action is not recognized?


return render_template(self.templates['index'],
model_admin=self,
Expand Down
8 changes: 6 additions & 2 deletions flask_peewee/templates/admin/models/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
{% block tab_index_class %}active{% endblock %}

{% block extra_tabs %}
{% set model_admin_actions = model_admin.get_actions() %}
{% if model_admin_actions %}
<li class="dropdown" style="float:right">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">With selected... <span class="caret" /></a>
<ul class="dropdown-menu pull-right">
<li><a href="#" onclick="Admin.index_submit('export');">Export</a></li>
<li><a href="#" onclick="Admin.index_submit('delete');">Delete</a></li>
{% for action, info in model_admin_actions %}
<li><a href="#" onclick="Admin.index_submit('{{ action }}');">{{ info.title }}</a></li>
{% endfor %}
</ul>
</li>
{% endif %}
{% include "admin/includes/filter_dropdown.html" %}
{% endblock %}

Expand Down