Skip to content

Commit

Permalink
'Add another' button added.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoes committed Mar 26, 2012
1 parent cadf5bc commit 83f66df
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
2 changes: 0 additions & 2 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
- Custom CSS/JS in admin interface
- SQLA Model Admin
- Built-in filtering support
- Built-in search support
- Support for related models
- Many2Many support
- Verify if it is working properly
- WYSIWYG editor support?
Expand Down
23 changes: 16 additions & 7 deletions flask_adminex/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import request, url_for, redirect
from flask import request, url_for, redirect, flash

from .base import BaseView, expose

Expand Down Expand Up @@ -37,7 +37,7 @@ class BaseModelView(BaseView):
edit_template = 'admin/model/edit.html'
"""Default edit template"""

create_template = 'admin/model/edit.html'
create_template = 'admin/model/create.html'
"""Default create template"""

# Customizations
Expand Down Expand Up @@ -419,6 +419,9 @@ def _get_url(self, view=None, page=None, sort=None, sort_desc=None, search=None)
if not search:
search = None

if not page:
page = None

return url_for(view,
page=page,
sort=sort,
Expand Down Expand Up @@ -501,7 +504,7 @@ def create_view(self):
"""
Create model view
"""
return_url = request.args.get('return')
return_url = request.args.get('url')

if not self.can_create:
return redirect(return_url or url_for('.index_view'))
Expand All @@ -510,16 +513,22 @@ def create_view(self):

if form.validate_on_submit():
if self.create_model(form):
return redirect(return_url or url_for('.index_view'))
if '_add_another' in request.form:
flash('Model was successfully created.')
return redirect(url_for('.create_view', url=return_url))
else:
return redirect(return_url or url_for('.index_view'))

return self.render(self.create_template, form=form)
return self.render(self.create_template,
form=form,
return_url=return_url)

@expose('/edit/<int:id>/', methods=('GET', 'POST'))
def edit_view(self, id):
"""
Edit model view
"""
return_url = request.args.get('return')
return_url = request.args.get('url')

if not self.can_edit:
return redirect(return_url or url_for('.index_view'))
Expand All @@ -544,7 +553,7 @@ def delete_view(self, id):
"""
Delete model view. Only POST method is allowed.
"""
return_url = request.args.get('return')
return_url = request.args.get('url')

# TODO: Use post
if not self.can_delete:
Expand Down
5 changes: 4 additions & 1 deletion flask_adminex/templates/admin/lib.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
{% endif %}
{%- endmacro %}

{% macro render_form(form, cancel_url) -%}
{% macro render_form(form, cancel_url, extra=None) -%}
<form action="" method="POST" class="form-horizontal"{% if form.has_file_field %} enctype="multipart/form-data"{% endif %}>
<fieldset>
{{ form.hidden_tag() }}
Expand Down Expand Up @@ -103,6 +103,9 @@
<div class="control-group">
<div class="controls">
<input type="submit" class="btn btn-primary btn-large" />
{% if extra %}
{{ extra }}
{% endif %}
{% if cancel_url %}
<a href="{{ cancel_url }}" class="btn btn-large">Cancel</a>
{% endif %}
Expand Down
20 changes: 20 additions & 0 deletions flask_adminex/templates/admin/model/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'admin/master.html' %}
{% import 'admin/lib.html' as lib %}

{% block head %}
<link href="{{ url_for('admin.static', filename='chosen/chosen.css') }}" rel="stylesheet">
<link href="{{ url_for('admin.static', filename='css/datepicker.css') }}" rel="stylesheet">
{% endblock %}

{% block body %}
{% macro extra() %}
<input name="_add_another" type="submit" class="btn btn-primary btn-large" value="Save and Add" />
{% endmacro %}

{{ lib.render_form(form, return_url, extra()) }}
{% endblock %}

{% block tail %}
<script src="{{ url_for('admin.static', filename='js/bootstrap-datepicker.js') }}"></script>
<script src="{{ url_for('admin.static', filename='js/form.js') }}"></script>
{% endblock %}
6 changes: 3 additions & 3 deletions flask_adminex/templates/admin/model/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
<tr>
<td>
{%- if admin_view.can_edit -%}
<a class="icon" href="{{ url_for('.edit_view', id=row.id, return=return_url) }}">
<a class="icon" href="{{ url_for('.edit_view', id=row.id, url=return_url) }}">
<i class="icon-pencil"></i>
</a>
{%- endif -%}
{%- if admin_view.can_delete -%}
<form class="icon" method="POST" action="{{ url_for('.delete_view', id=row.id, return=return_url) }}">
<form class="icon" method="POST" action="{{ url_for('.delete_view', id=row.id, url=return_url) }}">
<button onclick="return confirm('You sure you want to delete this item?')">
<i class="icon-remove"></i>
</button>
Expand All @@ -72,6 +72,6 @@
</table>
{{ lib.pager(page, num_pages, pager_url) }}
{% if admin_view.can_create %}
<a class="btn btn-primary btn-large" href="{{ url_for('.create_view', return=return_url) }}">Create New</a>
<a class="btn btn-primary btn-large" href="{{ url_for('.create_view', url=return_url) }}">Create New</a>
{% endif %}
{% endblock %}

0 comments on commit 83f66df

Please sign in to comment.