Commit 3499208d authored by Petrus Janse van Rensburg's avatar Petrus Janse van Rensburg

Merge pull request #933 from pawl/add_fileadmin_modals

Add modals to FileAdmin, minor changes to forms
parents 1a883db4 e6baed0c
......@@ -136,6 +136,19 @@ class FileAdmin(BaseView, ActionsMixin):
"""
# Modals
rename_modal = False
"""Setting this to true will display the rename view as a modal dialog."""
upload_modal = False
"""Setting this to true will display the upload view as a modal dialog."""
mkdir_modal = False
"""Setting this to true will display the mkdir view as a modal dialog."""
edit_modal = False
"""Setting this to true will display the edit view as a modal dialog."""
def __init__(self, base_path, base_url=None,
name=None, category=None, endpoint=None, url=None,
verify_path=True, menu_class_name=None, menu_icon_type=None, menu_icon_value=None):
......@@ -422,7 +435,7 @@ class FileAdmin(BaseView, ActionsMixin):
Additional arguments
"""
if not path:
return self.get_url(endpoint)
return self.get_url(endpoint, **kwargs)
else:
if self._on_windows:
path = path.replace('\\', '/')
......@@ -431,7 +444,7 @@ class FileAdmin(BaseView, ActionsMixin):
return self.get_url(endpoint, **kwargs)
def _get_file_url(self, path):
def _get_file_url(self, path, **kwargs):
"""
Return static file url
......@@ -443,7 +456,7 @@ class FileAdmin(BaseView, ActionsMixin):
else:
route = '.download'
return self.get_url(route, path=path)
return self.get_url(route, path=path, **kwargs)
def _normalize_path(self, path):
"""
......@@ -641,11 +654,15 @@ class FileAdmin(BaseView, ActionsMixin):
if self.validate_form(form):
try:
self._save_form_files(directory, path, form)
flash(gettext('Successfully saved file: %(name)s',
name=form.upload.data.filename))
return redirect(self._get_dir_url('.index', path))
except Exception as ex:
flash(gettext('Failed to save file: %(error)s', error=ex), 'error')
return self.render(self.upload_template, form=form)
return self.render(self.upload_template, form=form,
header_text=gettext('Upload File'),
modal=request.args.get('modal'))
@expose('/download/<path:path>')
def download(self, path=None):
......@@ -696,15 +713,17 @@ class FileAdmin(BaseView, ActionsMixin):
try:
os.mkdir(op.join(directory, form.name.data))
self.on_mkdir(directory, form.name.data)
flash(gettext('Successfully created directory: %(directory)s',
directory=form.name.data))
return redirect(dir_url)
except Exception as ex:
flash(gettext('Failed to create directory: %(error)s', error=ex), 'error')
else:
helpers.flash_errors(form, message='Failed to create directory: %(error)s')
return self.render(self.mkdir_template,
form=form,
dir_url=dir_url)
return self.render(self.mkdir_template, form=form, dir_url=dir_url,
header_text=gettext('Create Directory'),
modal=request.args.get('modal'))
@expose('/delete/', methods=('POST',))
def delete(self):
......@@ -789,8 +808,8 @@ class FileAdmin(BaseView, ActionsMixin):
os.rename(full_path, op.join(dir_base, filename))
self.on_rename(full_path, dir_base, filename)
flash(gettext('Successfully renamed "%(src)s" to "%(dst)s"',
src=op.basename(path),
dst=filename))
src=op.basename(path),
dst=filename))
except Exception as ex:
flash(gettext('Failed to rename: %(error)s', error=ex), 'error')
......@@ -802,7 +821,8 @@ class FileAdmin(BaseView, ActionsMixin):
form=form,
path=op.dirname(path),
name=op.basename(path),
dir_url=return_url)
dir_url=return_url,
modal=request.args.get('modal'))
@expose('/edit/', methods=('GET', 'POST'))
def edit(self):
......@@ -870,7 +890,8 @@ class FileAdmin(BaseView, ActionsMixin):
form.content.data = content
return self.render(self.edit_template, dir_url=dir_url, path=path,
form=form, error=error)
form=form, error=error,
modal=request.args.get('modal'))
@expose('/action/', methods=('POST',))
def action_view(self):
......
......@@ -1611,7 +1611,8 @@ class BaseModelView(BaseView, ActionsMixin):
return self.render(self.create_template,
form=form,
form_opts=form_opts,
return_url=return_url)
return_url=return_url,
modal=request.args.get('modal'))
@expose('/edit/', methods=('GET', 'POST'))
def edit_view(self):
......@@ -1654,7 +1655,8 @@ class BaseModelView(BaseView, ActionsMixin):
model=model,
form=form,
form_opts=form_opts,
return_url=return_url)
return_url=return_url,
modal=request.args.get('modal'))
@expose('/delete/', methods=('POST',))
def delete_view(self):
......
{% extends 'admin/master.html' %}
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
<h3>{{ _gettext('You are editing %(path)s', path=path) }}</h3>
{% macro check_error(error) %}
{% if error %}
<span>This file cannot be edited for now.</span>
<span>{{ _gettext('This file cannot be edited for now.') }}</span>
{% else %}
{{ lib.render_form(form, dir_url) }}
{{ caller() }}
{% endif %}
{% endmacro %}
{% block body %}
{% call check_error(error) %}
{%- if modal -%}
{# content added to modal-content #}
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
{%- else -%}
{% block header_text -%}
<h3>{{ _gettext('Editing %(path)s', path=path) }}<h3>
{%- endblock %}
{{ lib.render_form(form, dir_url) }}
{%- endif -%}
{% endcall %}
{% endblock %}
{% block tail %}
{%- if modal -%}
<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{{ self.header_text() }}');
// fixes "remote modal shows same content every time"
$('.modal').on('hidden', function() {
$(this).removeData('modal');
});
</script>
{%- endif -%}
{% endblock %}
{% extends 'admin/master.html' %}
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
{{ lib.render_form(form, dir_url) }}
{% endblock %}
\ No newline at end of file
{%- 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 -%}
{% endblock %}
......@@ -50,9 +50,15 @@
<td>
{% block list_row_actions scoped %}
{% if admin_view.can_rename and path and name != '..' %}
<a class="icon" href="{{ get_url('.rename', path=path) }}">
<i class="fa fa-pencil icon-pencil"></i>
</a>
{%- if admin_view.rename_modal -%}
{{ lib.add_modal_button(url=get_url('.rename', path=path, modal=True),
title=_gettext('Rename File'),
content='<i class="fa fa-pencil icon-pencil"></i>') }}
{% else %}
<a class="icon" href="{{ get_url('.rename', path=path) }}" title="{{ _gettext('Rename File') }}">
<i class="fa fa-pencil icon-pencil"></i>
</a>
{%- endif -%}
{% endif %}
{%- if admin_view.can_delete and path -%}
{% if is_dir %}
......@@ -86,9 +92,14 @@
{% else %}
<td>
{% if admin_view.can_download %}
<a href="{{ get_file_url(path)|safe }}">{{ name }}</a>
{%- if admin_view.edit_modal and admin_view.is_file_editable(path) -%}
{{ lib.add_modal_button(url=get_file_url(path, modal=True)|safe,
btn_class='', content=name) }}
{% else %}
<a href="{{ get_file_url(path)|safe }}">{{ name }}</a>
{%- endif -%}
{% else %}
{{ name }}
{{ name }}
{% endif %}
</td>
<td>
......@@ -104,12 +115,24 @@
<div class="btn-toolbar">
{% if admin_view.can_upload %}
<div class="btn-group">
<a class="btn btn-large" href="{{ get_dir_url('.upload', path=dir_path) }}">{{ _gettext('Upload File') }}</a>
{%- if admin_view.upload_modal -%}
{{ lib.add_modal_button(url=get_dir_url('.upload', path=dir_path, modal=True),
btn_class="btn btn-large",
content=_gettext('Upload File')) }}
{% else %}
<a class="btn btn-large" href="{{ get_dir_url('.upload', path=dir_path) }}">{{ _gettext('Upload File') }}</a>
{%- endif -%}
</div>
{% endif %}
{% if admin_view.can_mkdir %}
<div class="btn-group">
<a class="btn btn-large" href="{{ get_dir_url('.mkdir', path=dir_path) }}">{{ _gettext('Create Directory') }}</a>
{%- if admin_view.mkdir_modal -%}
{{ lib.add_modal_button(url=get_dir_url('.mkdir', path=dir_path, modal=True),
btn_class="btn btn-large",
content=_gettext('Create Directory')) }}
{% else %}
<a class="btn btn-large" href="{{ get_dir_url('.mkdir', path=dir_path) }}">{{ _gettext('Create Directory') }}</a>
{%- endif -%}
</div>
{% endif %}
{% if actions %}
......@@ -119,14 +142,20 @@
{% endif %}
</div>
{% endblock %}
{% block actions %}
{{ actionslib.form(actions, get_url('.action_view')) }}
{% endblock %}
{%- if admin_view.rename_modal or admin_view.mkdir_modal
or admin_view.upload_modal or admin_view.edit_modal -%}
{{ lib.add_modal_window() }}
{%- endif -%}
{% endblock %}
{% block tail %}
{{ super() }}
{{ actionslib.script(_gettext('Please select at least one file.'),
actions,
actions_confirmation) }}
actions,
actions_confirmation) }}
{% endblock %}
{% extends 'admin/master.html' %}
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
<h3>{{ _gettext('Please provide new name for %(name)s', name=name) }}</h3>
{%- if modal -%}
{# content added to modal-content #}
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
{%- else -%}
<h3>{% block header_text -%}
{{ _gettext('Rename %(name)s', name=name) }}
{%- endblock %}</h3>
{{ lib.render_form(form, dir_url) }}
{% endblock %}
\ No newline at end of file
{%- endif -%}
{% endblock %}
{% block tail %}
{%- if modal -%}
<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{{ self.header_text() }}');
// fixes "remote modal shows same content every time"
$('.modal').on('hidden', function() {
$(this).removeData('modal');
});
</script>
{%- endif -%}
{% endblock %}
......@@ -113,8 +113,8 @@
</div>
{% endmacro %}
{% macro add_modal_button(url='', title='', content='', modal_window_id='fa_modal_window') %}
<a class="icon" href="#" data-toggle="modal" title="{{ title }}" data-target="#{{ modal_window_id }}" data-remote="{{ url }}">
{% macro add_modal_button(url='', title='', content='', modal_window_id='fa_modal_window', btn_class='icon') %}
<a class="{{ btn_class }}" href="#" data-toggle="modal" title="{{ title }}" data-target="#{{ modal_window_id }}" data-remote="{{ url }}">
{{ content|safe }}
</a>
{% endmacro %}
......
{%- if not request.args.get('modal') -%}
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
......@@ -8,17 +8,16 @@
{% endmacro %}
{% block head %}
{%- if not request.args.get('modal') -%}
{%- if not modal -%}
{{ super() }}
{{ lib.form_css() }}
{%- endif -%}
{% endblock %}
{% block body %}
{%- if request.args.get('modal') -%}
{%- if modal -%}
{{ lib.render_form(form, return_url, extra=None, form_opts=form_opts,
action=url_for('.create_view', url=return_url),
is_modal=request.args.get('modal')) }}
action=request.url, is_modal=True) }}
{%- else -%}
{% block navlinks %}
<ul class="nav nav-tabs">
......@@ -31,17 +30,15 @@
</ul>
{% endblock %}
{{ lib.render_form(form, return_url, extra(), form_opts=form_opts,
action=url_for('.create_view', url=return_url),
is_modal=request.args.get('modal')) }}
{{ lib.render_form(form, return_url, extra(), form_opts=form_opts) }}
{%- endif -%}
{% endblock %}
{% block tail %}
{%- if request.args.get('modal') -%}
{%- if modal -%}
<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{% block modal_header %}<h3>{{ _gettext('Create New Record') }}</h3>{% endblock %}');
$('.modal-header h3').html('{% block header_text %}<h3>{{ _gettext('Create New Record') }}</h3>{% endblock %}');
// fixes "remote modal shows same content every time"
$('.modal').on('hidden', function() {
......
{%- if not request.args.get('modal') -%}
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
......@@ -8,30 +8,43 @@
{% endmacro %}
{% block head %}
{%- if not request.args.get('modal') -%}
{%- if not modal -%}
{{ super() }}
{{ lib.form_css() }}
{%- endif -%}
{% endblock %}
{% block body %}
{%- if request.args.get('modal') -%}
{%- if modal -%}
{# remove save and continue button for modal (it won't function properly) #}
{{ lib.render_form(form, return_url, extra=None, form_opts=form_opts,
action=url_for('.edit_view', id=request.args.get('id'), url=return_url),
is_modal=request.args.get('modal')) }}
action=request.url, is_modal=True) }}
{%- else -%}
{{ lib.render_form(form, return_url, extra(), form_opts,
action=url_for('.edit_view', id=request.args.get('id'), url=return_url),
is_modal=request.args.get('modal')) }}
{% block navlinks %}
<ul class="nav nav-tabs">
<li>
<a href="{{ return_url }}">{{ _gettext('List') }}</a>
</li>
<li>
<a href="{{ get_url('.create_view', url=return_url) }}">{{ _gettext('Create') }}</a>
</li>
<li class="active">
<a href="javascript:void(0)">{{ _gettext('Edit') }}</a>
</li>
</ul>
{% endblock %}
{{ lib.render_form(form, return_url, extra(), form_opts) }}
{%- endif -%}
{% endblock %}
{% block tail %}
{%- if request.args.get('modal') -%}
{%- if modal -%}
<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{% block modal_header %}<h3>{{ _gettext('Edit Record') + ' #' + request.args.get('id') }}</h3>{% endblock %}');
$('.modal-header h3').html('{% block header_text -%}
{{ _gettext('Edit Record') + ' #' + request.args.get('id') }}
{%- endblock %}');
// fixes "remote modal shows same content every time"
$('.modal').on('hidden', function() {
......@@ -47,4 +60,4 @@
{{ super() }}
{{ lib.form_js() }}
{%- endif -%}
{% endblock %}
\ No newline at end of file
{% endblock %}
{% extends 'admin/master.html' %}
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
<h3>{{ _gettext('You are editing %(path)s', path=path) }}</h3>
{% macro check_error(error) %}
{% if error %}
<span>This file cannot be edited for now.</span>
<span>{{ _gettext('This file cannot be edited for now.') }}</span>
{% else %}
{{ lib.render_form(form, dir_url) }}
{{ caller() }}
{% endif %}
{% endmacro %}
{% 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>
{% block header_text %}
<h3>{{ _gettext('Editing %(path)s', path=path) }}</h3>
{% endblock %}
</div>
<div class="modal-body">
{% call check_error(error) %}
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
{% endcall %}
</div>
{%- else -%}
{{ self.header_text() }}
{% call check_error(error) %}
{{ lib.render_form(form, dir_url) }}
{% endcall %}
{%- 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 -%}
{% endblock %}
{% extends 'admin/master.html' %}
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
{{ lib.render_form(form, dir_url) }}
{% endblock %}
\ No newline at end of file
{%- 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 -%}
{% endblock %}
......@@ -50,9 +50,15 @@
<td>
{% block list_row_actions scoped %}
{% if admin_view.can_rename and path and name != '..' %}
<a class="icon" href="{{ get_url('.rename', path=path) }}">
<i class="fa fa-pencil glyphicon glyphicon-pencil"></i>
</a>
{%- if admin_view.rename_modal -%}
{{ lib.add_modal_button(url=get_url('.rename', path=path, modal=True),
title=_gettext('Rename File'),
content='<i class="fa fa-pencil glyphicon glyphicon-pencil"></i>') }}
{% else %}
<a class="icon" href="{{ get_url('.rename', path=path) }}" title="{{ _gettext('Rename File') }}">
<i class="fa fa-pencil glyphicon glyphicon-pencil"></i>
</a>
{%- endif -%}
{% endif %}
{%- if admin_view.can_delete and path -%}
{% if is_dir %}
......@@ -86,9 +92,14 @@
{% else %}
<td>
{% if admin_view.can_download %}
<a href="{{ get_file_url(path)|safe }}">{{ name }}</a>
{%- if admin_view.edit_modal and admin_view.is_file_editable(path) -%}
{{ lib.add_modal_button(url=get_file_url(path, modal=True)|safe,
btn_class='', content=name) }}
{% else %}
<a href="{{ get_file_url(path)|safe }}">{{ name }}</a>
{%- endif -%}
{% else %}
{{ name }}
{{ name }}
{% endif %}
</td>
<td>
......@@ -104,12 +115,24 @@
<div class="btn-toolbar">
{% if admin_view.can_upload %}
<div class="btn-group">
<a class="btn btn-default btn-large" href="{{ get_dir_url('.upload', path=dir_path) }}">{{ _gettext('Upload File') }}</a>
{%- if admin_view.upload_modal -%}
{{ lib.add_modal_button(url=get_dir_url('.upload', path=dir_path, modal=True),
btn_class="btn btn-default btn-large",
content=_gettext('Upload File')) }}
{% else %}
<a class="btn btn-default btn-large" href="{{ get_dir_url('.upload', path=dir_path) }}">{{ _gettext('Upload File') }}</a>
{%- endif -%}
</div>
{% endif %}
{% if admin_view.can_mkdir %}
<div class="btn-group">
<a class="btn btn-default btn-large" href="{{ get_dir_url('.mkdir', path=dir_path) }}">{{ _gettext('Create Directory') }}</a>
{%- if admin_view.mkdir_modal -%}
{{ lib.add_modal_button(url=get_dir_url('.mkdir', path=dir_path, modal=True),
btn_class="btn btn-default btn-large",
content=_gettext('Create Directory')) }}
{% else %}
<a class="btn btn-default btn-large" href="{{ get_dir_url('.mkdir', path=dir_path) }}">{{ _gettext('Create Directory') }}</a>
{%- endif -%}
</div>
{% endif %}
{% if actions %}
......@@ -119,14 +142,20 @@
{% endif %}
</div>
{% endblock %}
{% block actions %}
{{ actionslib.form(actions, get_url('.action_view')) }}
{% endblock %}
{%- if admin_view.rename_modal or admin_view.mkdir_modal
or admin_view.upload_modal or admin_view.edit_modal -%}
{{ lib.add_modal_window() }}
{%- endif -%}
{% endblock %}
{% block tail %}
{{ super() }}
{{ actionslib.script(_gettext('Please select at least one file.'),
actions,
actions_confirmation) }}
actions,
actions_confirmation) }}
{% endblock %}
{% extends 'admin/master.html' %}
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
<h3>{{ _gettext('Please provide new name for %(name)s', name=name) }}</h3>
{{ lib.render_form(form, dir_url) }}
{% endblock %}
\ No newline at end of file
{%- 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>
{% block header_text %}
<h3>{{ _gettext('Rename %(name)s', name=name) }}</h3>
{% endblock %}
</div>
<div class="modal-body">
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
</div>
{%- else -%}
<h3>{{ self.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 -%}
{% endblock %}
......@@ -108,8 +108,8 @@
</div>
{% endmacro %}
{% macro add_modal_button(url='', title='', content='', modal_window_id='fa_modal_window') %}
<a class="icon" data-target="#{{ modal_window_id }}" title="{{ title }}" href="{{ url }}" data-toggle="modal">
{% macro add_modal_button(url='', title='', content='', modal_window_id='fa_modal_window', btn_class='icon') %}
<a class="{{ btn_class }}" data-target="#{{ modal_window_id }}" title="{{ title }}" href="{{ url }}" data-toggle="modal">
{{ content|safe }}
</a>
{% endmacro %}
......
{%- if not request.args.get('modal') -%}
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
......@@ -8,23 +8,22 @@
{% endmacro %}
{% block head %}
{%- if not request.args.get('modal') -%}
{%- if not modal -%}
{{ super() }}
{{ lib.form_css() }}
{%- endif -%}
{% endblock %}
{% block body %}
{%- if request.args.get('modal') -%}
{%- if modal -%}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{% block modal_header %}<h3>{{ _gettext('Create New Record') }}</h3>{% endblock %}
{% block header_text %}<h3>{{ _gettext('Create New Record') }}</h3>{% endblock %}
</div>
<div class="modal-body">
{# remove save and continue button for modal (it won't function properly) #}
{{ lib.render_form(form, return_url, extra=None, form_opts=form_opts,
action=url_for('.create_view', url=return_url),
is_modal=request.args.get('modal')) }}
{{ lib.render_form(form, return_url, extra(), form_opts=form_opts,
action=request.url, is_modal=True) }}
</div>
{%- else -%}
{% block navlinks %}
......@@ -38,14 +37,12 @@
</ul>
{% endblock %}
{{ lib.render_form(form, return_url, extra(), form_opts=form_opts,
action=url_for('.create_view', url=return_url),
is_modal=request.args.get('modal')) }}
{{ lib.render_form(form, return_url, extra(), form_opts=form_opts) }}
{%- endif -%}
{% endblock %}
{% block tail %}
{%- if request.args.get('modal') -%}
{%- if modal -%}
<script>
// fixes "remote modal shows same content every time", avoiding the flicker
$('body').on('hidden.bs.modal', '.modal', function () {
......
{%- if not request.args.get('modal') -%}
{%- if not modal -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
......@@ -8,34 +8,47 @@
{% endmacro %}
{% block head %}
{%- if not request.args.get('modal') -%}
{%- if not modal -%}
{{ super() }}
{{ lib.form_css() }}
{%- endif -%}
{% endblock %}
{% block body %}
{%- if request.args.get('modal') -%}
{%- 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>
{% block modal_header %}<h3>{{ _gettext('Edit Record') + ' #' + request.args.get('id') }}</h3>{% endblock %}
{% block header_text %}
<h3>{{ _gettext('Edit Record') + ' #' + request.args.get('id') }}</h3>
{% endblock %}
</div>
<div class="modal-body">
{# remove save and continue button for modal (it won't function properly) #}
{{ lib.render_form(form, return_url, extra=None, form_opts=form_opts,
action=url_for('.edit_view', id=request.args.get('id'), url=return_url),
is_modal=request.args.get('modal')) }}
action=request.url, is_modal=True) }}
</div>
{%- else -%}
{{ lib.render_form(form, return_url, extra(), form_opts,
action=url_for('.edit_view', id=request.args.get('id'), url=return_url),
is_modal=request.args.get('modal')) }}
{% block navlinks %}
<ul class="nav nav-tabs">
<li>
<a href="{{ return_url }}">{{ _gettext('List') }}</a>
</li>
<li>
<a href="{{ get_url('.create_view', url=return_url) }}">{{ _gettext('Create') }}</a>
</li>
<li class="active">
<a href="javascript:void(0)">{{ _gettext('Edit') }}</a>
</li>
</ul>
{% endblock %}
{{ lib.render_form(form, return_url, extra(), form_opts) }}
{%- endif -%}
{% endblock %}
{% block tail %}
{%- if request.args.get('modal') -%}
{%- if modal -%}
<script>
// fixes "remote modal shows same content every time", avoiding the flicker
$('body').on('hidden.bs.modal', '.modal', function () {
......
......@@ -168,7 +168,9 @@
{% endblock %}
{% endblock %}
{% block actions %}
{{ actionlib.form(actions, get_url('.action_view')) }}
{% endblock %}
{%- if admin_view.edit_modal or admin_view.create_modal -%}
{{ lib.add_modal_window() }}
......
......@@ -3,6 +3,8 @@ import os.path as op
from nose.tools import eq_, ok_
from flask_admin.contrib import fileadmin
from flask_admin import Admin
from flask import Flask
from . import setup
......@@ -131,3 +133,58 @@ def test_file_admin():
eq_(rv.status_code, 200)
ok_('path=dummy_renamed_dir' not in rv.data.decode('utf-8'))
ok_('path=dummy.txt' in rv.data.decode('utf-8'))
def test_modal_edit():
# bootstrap 2 - test edit_modal
app_bs2 = Flask(__name__)
admin_bs2 = Admin(app_bs2, template_mode="bootstrap2")
class EditModalOn(fileadmin.FileAdmin):
edit_modal = True
editable_extensions = ('txt',)
class EditModalOff(fileadmin.FileAdmin):
edit_modal = False
editable_extensions = ('txt',)
path = op.join(op.dirname(__file__), 'files')
edit_modal_on = EditModalOn(path, '/files/', endpoint='edit_modal_on')
edit_modal_off = EditModalOff(path, '/files/', endpoint='edit_modal_off')
admin_bs2.add_view(edit_modal_on)
admin_bs2.add_view(edit_modal_off)
client_bs2 = app_bs2.test_client()
# bootstrap 2 - ensure modal window is added when edit_modal is enabled
rv = client_bs2.get('/admin/edit_modal_on/')
eq_(rv.status_code, 200)
data = rv.data.decode('utf-8')
ok_('fa_modal_window' in data)
# bootstrap 2 - test edit modal disabled
rv = client_bs2.get('/admin/edit_modal_off/')
eq_(rv.status_code, 200)
data = rv.data.decode('utf-8')
ok_('fa_modal_window' not in data)
# bootstrap 3
app_bs3 = Flask(__name__)
admin_bs3 = Admin(app_bs3, template_mode="bootstrap3")
admin_bs3.add_view(edit_modal_on)
admin_bs3.add_view(edit_modal_off)
client_bs3 = app_bs3.test_client()
# bootstrap 3 - ensure modal window is added when edit_modal is enabled
rv = client_bs3.get('/admin/edit_modal_on/')
eq_(rv.status_code, 200)
data = rv.data.decode('utf-8')
ok_('fa_modal_window' in data)
# bootstrap 3 - test modal disabled
rv = client_bs3.get('/admin/edit_modal_off/')
eq_(rv.status_code, 200)
data = rv.data.decode('utf-8')
ok_('fa_modal_window' not in data)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment