diff options
-rw-r--r-- | ishtar_common/static/js/ishtar.js | 74 | ||||
-rw-r--r-- | ishtar_common/templates/base.html | 11 | ||||
-rw-r--r-- | ishtar_common/templates/blocks/bs_compact_form_snippet.html | 21 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/forms/qa_base.html | 8 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/forms/qa_new_item.html | 27 | ||||
-rw-r--r-- | ishtar_common/templatetags/table_form.py | 9 | ||||
-rw-r--r-- | ishtar_common/views.py | 11 | ||||
-rw-r--r-- | ishtar_common/views_item.py | 29 | ||||
-rw-r--r-- | ishtar_common/widgets.py | 12 | ||||
-rw-r--r-- | version.py | 4 |
10 files changed, 177 insertions, 29 deletions
diff --git a/ishtar_common/static/js/ishtar.js b/ishtar_common/static/js/ishtar.js index 66087b1ff..2ec398e99 100644 --- a/ishtar_common/static/js/ishtar.js +++ b/ishtar_common/static/js/ishtar.js @@ -688,16 +688,48 @@ function open_window(url){ function save_and_close_window(name_label, name_pk, item_name, item_pk){ var main_page = opener.document; - jQuery(main_page).find("#"+name_label).val(item_name); - jQuery(main_page).find("#"+name_pk).val(item_pk); + save_and_close_window_data(main_page, name_label, + name_pk, item_name, item_pk); opener.focus(); self.close(); } +function save_and_close_window_data(main_page, name_label, + name_pk, item_name, item_pk){ + var cur_item; + var cur_item_pk; + if (main_page){ + cur_item = $(main_page).find("#"+name_label); + cur_item_pk = $(main_page).find("#"+name_pk); + } else { + cur_item = $("#"+name_label); + cur_item_pk = $("#"+name_pk); + } + cur_item.val(item_name); + cur_item_pk.val(item_pk); +} + function save_and_close_window_many(name_label, name_pk, item_name, item_pk){ var main_page = opener.document; - var lbl_ = jQuery(main_page).find("#" + name_label); - var val_ = jQuery(main_page).find("#" + name_pk); + save_and_close_window_many_data(main_page, name_label, + name_pk, item_name, item_pk); + opener.focus(); + self.close(); +} + +function save_and_close_window_many_data(main_page, name_label, + name_pk, item_name, item_pk){ + var cur_item; + var cur_item_pk; + if (main_page){ + cur_item = $(main_page).find("#"+name_label); + cur_item_pk = $(main_page).find("#"+name_pk); + } else { + cur_item = $("#"+name_label); + cur_item_pk = $("#"+name_pk); + } + var lbl_ = cur_item; + var val_ = cur_item_pk; if (val_.val()){ var v = lbl_.val(); v = v.slice(0, v.lastIndexOf(",")); @@ -705,11 +737,9 @@ function save_and_close_window_many(name_label, name_pk, item_name, item_pk){ val_.val(val_.val() + ", " + item_pk); lbl_.change(); } else { - jQuery(main_page).find("#" + name_label).val(item_name); - jQuery(main_page).find("#" + name_pk).val(item_pk); + cur_item.val(item_name); + cur_item_pk.val(item_pk); } - opener.focus(); - self.close(); } function multiRemoveItem(selItems, name, idx){ @@ -986,7 +1016,8 @@ var dt_generate_qa_url = function (table, url){ return url; }; -var dt_qa_open = function (url){ +var dt_qa_open = function (url, modal_id){ + if (!modal_id) modal_id = "modal-dynamic-form"; short_wait(); $.ajax({ url: url, @@ -994,14 +1025,14 @@ var dt_qa_open = function (url){ success: function(data, textStatus, jqXHR) { close_wait(); var text = jqXHR.responseText; - $('#modal-dynamic-form').html(text); + $('#' + modal_id).html(text); var response = $(text); var responseScript = response.find("script"); $.each(responseScript, function(idx, val){ eval(val.text); } ); - $('#modal-dynamic-form').modal("show"); + $('#' + modal_id).modal("show"); }, error: function() { close_wait(); @@ -1032,16 +1063,25 @@ var ajax_post = function(url, data, target, callback, error_callback){ }); }; -var qa_action_register = function(url) { - $('#qa-action').on('submit', function(event){ +var qa_action_register = function(url, slug) { + var qa_id; + var modal_id; + if (!slug){ + qa_id = "qa-action"; + modal_id = "modal-dynamic-form"; + } else { + qa_id = "qa-new-" + slug; + modal_id = "modal-dynamic-form-" + slug; + } + $("#" + qa_id).on('submit', function(event){ event.preventDefault(); var fn = function(){ - $('#modal-dynamic-form').modal("show"); - } ; - $('#modal-dynamic-form').modal("hide"); + $('#' + modal_id).modal("show"); + } ; + $('#' + modal_id).modal("hide"); short_wait(); ajax_post( - url, $(this).serialize(), "#modal-dynamic-form", fn, fn + url, $(this).serialize(), "#" + modal_id, fn, fn ); }); }; diff --git a/ishtar_common/templates/base.html b/ishtar_common/templates/base.html index 3bfe95141..4854c43d0 100644 --- a/ishtar_common/templates/base.html +++ b/ishtar_common/templates/base.html @@ -193,6 +193,14 @@ {% include 'ishtar/blocks/footer.html' %} </div> {% endblock %} + <div class="modal" id="modal-dynamic-form" tabindex="-1" role="dialog" + data-backdrop="static" data-keyboard="true" aria-hidden="true"> + </div> + {% for extra_form in extra_form_modals %} + <div class="modal" id="modal-dynamic-form-{{extra_form}}" tabindex="-1" role="dialog" + data-backdrop="static" data-keyboard="true" aria-hidden="true"> + </div> + {% endfor %} <div class="modal modal-progress" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false" aria-hidden="true"> <div class="modal-dialog modal-sm modal-dialog-centered"> @@ -219,9 +227,6 @@ </div> </div> </div> - <div class="modal" id="modal-dynamic-form" tabindex="-1" role="dialog" - data-backdrop="static" data-keyboard="true" aria-hidden="true"> - </div> <div id='message'> <div class='information'><i class="fa fa-info-circle" aria-hidden="true"></i> <span class='content'></span></div> </div> diff --git a/ishtar_common/templates/blocks/bs_compact_form_snippet.html b/ishtar_common/templates/blocks/bs_compact_form_snippet.html new file mode 100644 index 000000000..6857c0343 --- /dev/null +++ b/ishtar_common/templates/blocks/bs_compact_form_snippet.html @@ -0,0 +1,21 @@ +{% load i18n from_dict %} +{% if form.non_field_errors %} +<div class="alert alert-danger" role="alert"> + {{form.non_field_errors}} +</div> +{% endif %} + +{% for hidden in form.hidden_fields %} +{{hidden}} +{% if hidden.errors %}<div class="invalid-feedback"> + {{ hidden.errors }} +</div>{% endif %} +{% endfor %} + +{% csrf_token %} +<div class="form-row"> +{% for field in form.visible_fields %} + {% include "blocks/bs_field_snippet.html" %} +{% endfor %} +</div> + diff --git a/ishtar_common/templates/ishtar/forms/qa_base.html b/ishtar_common/templates/ishtar/forms/qa_base.html index 367acfcd8..57e2d3979 100644 --- a/ishtar_common/templates/ishtar/forms/qa_base.html +++ b/ishtar_common/templates/ishtar/forms/qa_base.html @@ -9,7 +9,7 @@ </button> </div> <form enctype="multipart/form-data" action="{{url}}" method="post" - id="qa-action">{% csrf_token %} + id="qa-{% if slug %}new-{{slug}}{% else %}action{% endif %}">{% csrf_token %} <div class="modal-body body-scroll"> <div class='form'> {% block main_form %} @@ -69,7 +69,13 @@ {% block js %} {% endblock %} $(document).ready(function(){ + {% if slug %} + qa_action_register("{{url}}", "{{slug}}"); + {% else %}} qa_action_register("{{url}}"); + {% endif %} + {% block js_ready %} + {% endblock %} }); </script> diff --git a/ishtar_common/templates/ishtar/forms/qa_new_item.html b/ishtar_common/templates/ishtar/forms/qa_new_item.html new file mode 100644 index 000000000..78162d759 --- /dev/null +++ b/ishtar_common/templates/ishtar/forms/qa_new_item.html @@ -0,0 +1,27 @@ +{% extends "ishtar/forms/qa_base.html" %} +{% load i18n inline_formset table_form %} + +{% block main_form %} +{% if new_item_label %} +<p>{{new_item_label}} {% trans "created." %}</p> +{% else %} + {% bs_compact_form form %} +{% endif %} +{% endblock %} + +{% block footer %} +{% if new_item_label %} +<button type="button" data-dismiss="modal" + aria-label="Close" class="btn btn-secondary"> + {% trans "Close" %} +</button> +{% else %} + {{ block.super }} +{% endif %} +{% endblock %} + +{% block js_ready %} +{% if new_item_label %} +save_and_close_window{% if many %}_many{% endif %}_data(null, "{{parent_name}}", "{{parent_pk}}", "{{new_item_label|safe}}", "{{new_item_pk}}"); +{% endif %} +{% endblock %}
\ No newline at end of file diff --git a/ishtar_common/templatetags/table_form.py b/ishtar_common/templatetags/table_form.py index 9fcfdfd4f..5da2171d0 100644 --- a/ishtar_common/templatetags/table_form.py +++ b/ishtar_common/templatetags/table_form.py @@ -15,6 +15,15 @@ def bs_form(context, form, position=0): 'show_field_number': show_field_number} +@register.inclusion_tag('blocks/bs_compact_form_snippet.html', + takes_context=True) +def bs_compact_form(context, form, position=0): + user = context['user'] + show_field_number = user.ishtaruser and user.ishtaruser.show_field_number() + return {'form': form, 'odd': position % 2, + 'show_field_number': show_field_number} + + @register.inclusion_tag('blocks/table_form_snippet.html') def table_form(form): return {'form': form} diff --git a/ishtar_common/views.py b/ishtar_common/views.py index acf7eb70c..d1924accc 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -69,7 +69,7 @@ from ishtar_common.utils import clean_session_cache, CSV_OPTIONS, \ from ishtar_common.widgets import JQueryAutoComplete from .views_item import CURRENT_ITEM_KEYS, CURRENT_ITEM_KEYS_DICT, \ - check_permission, display_item, get_item, new_item, show_item + check_permission, display_item, get_item, new_item, show_item, new_qa_item logger = logging.getLogger(__name__) @@ -734,13 +734,13 @@ def autocomplete_author(request): return HttpResponse(data, content_type='text/plain') -new_person = new_item(models.Person, forms.PersonForm) +new_person = new_qa_item(models.Person, forms.PersonForm) new_person_noorga = new_item(models.Person, forms.NoOrgaPersonForm) new_organization = new_item(models.Organization, forms.OrganizationForm) show_organization = show_item(models.Organization, 'organization') get_organization = get_item(models.Organization, 'get_organization', 'organization') -new_author = new_item(models.Author, forms.AuthorForm) +new_author = new_qa_item(models.Author, forms.AuthorForm) show_person = show_item(models.Person, 'person') get_person = get_item(models.Person, 'get_person', 'person') @@ -1881,6 +1881,11 @@ class DocumentSelectView(IshtarMixin, LoginRequiredMixin, class DocumentEditView(DocumentFormMixin, UpdateView): page_name = _(u"Document modification") + def get_context_data(self, **kwargs): + data = super(DocumentEditView, self).get_context_data(**kwargs) + data["extra_form_modals"] = ['author', 'person'] + return data + def get_form_kwargs(self): kwargs = super(DocumentEditView, self).get_form_kwargs() try: diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index c40b2c75f..b0ec0bdce 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -166,6 +166,35 @@ def new_item(model, frm, many=False): return func +def new_qa_item(model, frm, many=False): + def func(request, parent_name, limits=''): + template = "ishtar/forms/qa_new_item.html" + model_name = model._meta.object_name + if not check_permission(request, 'add_' + model_name.lower()): + not_permitted_msg = ugettext(u"Operation not permitted.") + return HttpResponse(not_permitted_msg) + dct = {'page_name': str(_(u'New %s' % model_name.lower())), + 'url': reverse("new-" + model.SLUG, args=[parent_name]), + 'slug': model.SLUG, + 'parent_name': parent_name, + 'many': many} + if request.method == 'POST': + dct['form'] = frm(request.POST, limits=limits) + if dct['form'].is_valid(): + new_item = dct['form'].save(request.user) + dct['new_item_label'] = str(new_item) + dct['new_item_pk'] = new_item.pk + dct['parent_pk'] = parent_name + if dct['parent_pk'] and '_select_' in dct['parent_pk']: + parents = dct['parent_pk'].split('_') + dct['parent_pk'] = "_".join([parents[0]] + parents[2:]) + return render(request, template, dct) + else: + dct['form'] = frm(limits=limits) + return render(request, template, dct) + return func + + def display_item(model, extra_dct=None, show_url=None): def func(request, pk, **dct): if show_url: diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index 14d153651..79a705e43 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -703,6 +703,9 @@ class JQueryAutoComplete(forms.TextInput): new = '' html = u"" if self.new: + # WARNING: the modal for the form must be in the main template + # "extra_form_modals" list is used for that in view or wizard + html = u"<div class='input-group'>" model_name = self.associated_model._meta.object_name.lower() limits = [] @@ -716,9 +719,12 @@ class JQueryAutoComplete(forms.TextInput): if self.url_new: url_new = self.url_new url_new = reverse(url_new, args=args) - new = u'<span class="input-group-append">'\ - u'<a href="#" class="add-button input-group-text" '\ - u'onclick="open_window(\'%s\');">+</a></span></div>' % url_new + new = """ + <span class="input-group-append"> + <a href="#" class="add-button input-group-text" + onclick="dt_qa_open('{}', 'modal-dynamic-form-{}');">+</a> + </span></div> + """.format(url_new, model_name, model_name) old_value = "" if 'value' in attrs_select and attrs_select['value']: diff --git a/version.py b/version.py index 755885b9c..c2df0e9e2 100644 --- a/version.py +++ b/version.py @@ -1,5 +1,5 @@ -# 3.0.dev.6 -VERSION = (3, 0, 'dev', 6) +# 3.0.dev.7 +VERSION = (3, 0, 'dev', 7) def get_version(): |