Skip to content

Commit

Permalink
Merge pull request pallets-eco#1046 from pawl/move_file_admin_templates
Browse files Browse the repository at this point in the history
File-admin - combine mkdir, rename, edit, and upload templates
  • Loading branch information
mrjoes committed Sep 11, 2015
2 parents 4c0bf96 + 92a20bd commit 66874b8
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 213 deletions.
68 changes: 54 additions & 14 deletions flask_admin/contrib/fileadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,41 @@ class MyAdmin(FileAdmin):
File upload template
"""

upload_modal_template = 'admin/file/modals/form.html'
"""
File upload template for modal dialog
"""

mkdir_template = 'admin/file/form.html'
"""
Directory creation (mkdir) template
"""

rename_template = 'admin/file/rename.html'
mkdir_modal_template = 'admin/file/modals/form.html'
"""
Directory creation (mkdir) template for modal dialog
"""

rename_template = 'admin/file/form.html'
"""
Rename template
"""

edit_template = 'admin/file/edit.html'
rename_modal_template = 'admin/file/modals/form.html'
"""
Rename template for modal dialog
"""

edit_template = 'admin/file/form.html'
"""
Edit template
"""

edit_modal_template = 'admin/file/modals/form.html'
"""
Edit template for modal dialog
"""

form_base_class = form.BaseForm
"""
Base form class. Will be used to create the upload, rename, edit, and delete form.
Expand Down Expand Up @@ -660,7 +680,12 @@ def upload(self, path=None):
except Exception as ex:
flash(gettext('Failed to save file: %(error)s', error=ex), 'error')

return self.render(self.upload_template, form=form,
if self.upload_modal and request.args.get('modal'):
template = self.upload_modal_template
else:
template = self.upload_template

return self.render(template, form=form,
header_text=gettext('Upload File'),
modal=request.args.get('modal'))

Expand Down Expand Up @@ -721,9 +746,13 @@ def mkdir(self, path=None):
else:
helpers.flash_errors(form, message='Failed to create directory: %(error)s')

return self.render(self.mkdir_template, form=form, dir_url=dir_url,
header_text=gettext('Create Directory'),
modal=request.args.get('modal'))
if self.mkdir_modal and request.args.get('modal'):
template = self.mkdir_modal_template
else:
template = self.mkdir_template

return self.render(template, form=form, dir_url=dir_url,
header_text=gettext('Create Directory'))

@expose('/delete/', methods=('POST',))
def delete(self):
Expand Down Expand Up @@ -817,12 +846,15 @@ def rename(self):
else:
helpers.flash_errors(form, message='Failed to rename: %(error)s')

return self.render(self.rename_template,
form=form,
path=op.dirname(path),
name=op.basename(path),
dir_url=return_url,
modal=request.args.get('modal'))
if self.rename_modal and request.args.get('modal'):
template = self.rename_modal_template
else:
template = self.rename_template

return self.render(template, form=form, path=op.dirname(path),
name=op.basename(path), dir_url=return_url,
header_text=gettext('Rename %(name)s',
name=op.basename(path)))

@expose('/edit/', methods=('GET', 'POST'))
def edit(self):
Expand Down Expand Up @@ -889,9 +921,17 @@ def edit(self):
else:
form.content.data = content

return self.render(self.edit_template, dir_url=dir_url, path=path,
if error:
return redirect(next_url)

if self.edit_modal and request.args.get('modal'):
template = self.edit_modal_template
else:
template = self.edit_template

return self.render(template, dir_url=dir_url, path=path,
form=form, error=error,
modal=request.args.get('modal'))
header_text=gettext('Editing %(path)s', path=path))

@expose('/action/', methods=('POST',))
def action_view(self):
Expand Down
40 changes: 0 additions & 40 deletions flask_admin/templates/bootstrap2/admin/file/edit.html

This file was deleted.

26 changes: 3 additions & 23 deletions flask_admin/templates/bootstrap2/admin/file/form.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% extends 'admin/master.html' %}
{% import 'admin/lib.html' as lib with context %}

{% block body %}
{%- if modal -%}
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
{%- else -%}
<h3>{{ header_text }}</h3>
{{ lib.render_form(form, dir_url) }}
{%- endif -%}
{% endblock %}

{% block tail %}
{%- if modal -%}
<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{{ header_text }}');

// fixes "remote modal shows same content every time"
$('.modal').on('hidden', function() {
$(this).removeData('modal');
});
</script>
{%- endif -%}
{% block header %}<h3>{{ header_text }}</h3>{% endblock %}
{{ lib.render_form(form, dir_url) }}
{% endblock %}
16 changes: 16 additions & 0 deletions flask_admin/templates/bootstrap2/admin/file/modals/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% import 'admin/static.html' as admin_static with context %}
{% import 'admin/lib.html' as lib with context %}

{% block body %}
{# content added to modal-content #}
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
{% endblock %}

{% block tail %}
<script src="{{ admin_static.url(filename='admin/js/bs2_modal.js', v='1.0.0') }}"></script>

<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{% block header %}{{ header_text }}{% endblock %}');
</script>
{% endblock %}
30 changes: 0 additions & 30 deletions flask_admin/templates/bootstrap2/admin/file/rename.html

This file was deleted.

45 changes: 0 additions & 45 deletions flask_admin/templates/bootstrap3/admin/file/edit.html

This file was deleted.

31 changes: 3 additions & 28 deletions flask_admin/templates/bootstrap3/admin/file/form.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% extends 'admin/master.html' %}
{% import 'admin/lib.html' as lib with context %}

{% block body %}
{%- if modal -%}
{# content added to modal-content #}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h3>{{ header_text }}</h3>
</div>
<div class="modal-body">
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
</div>
{%- else -%}
<h3>{{ header_text }}</h3>
{{ lib.render_form(form, dir_url) }}
{%- endif -%}

{% endblock %}

{% block tail %}
{%- if modal -%}
<script>
// fixes "remote modal shows same content every time", avoiding the flicker
$('body').on('hidden.bs.modal', '.modal', function() {
$(this).removeData('bs.modal').find(".modal-content").empty();
});
</script>
{%- endif -%}
{% block header %}<h3>{{ header_text }}</h3>{% endblock %}
{{ lib.render_form(form, dir_url) }}
{% endblock %}
17 changes: 17 additions & 0 deletions flask_admin/templates/bootstrap3/admin/file/modals/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% import 'admin/static.html' as admin_static with context %}
{% import 'admin/lib.html' as lib with context %}

{% block body %}
{# content added to modal-content #}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{% block header %}<h3>{{ header_text }}</h3>{% endblock %}
</div>
<div class="modal-body">
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
</div>
{% endblock %}

{% block tail %}
<script src="{{ admin_static.url(filename='admin/js/bs3_modal.js', v='1.0.0') }}"></script>
{% endblock %}
33 changes: 0 additions & 33 deletions flask_admin/templates/bootstrap3/admin/file/rename.html

This file was deleted.

0 comments on commit 66874b8

Please sign in to comment.