diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-09-06 18:18:07 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-10-24 12:06:08 +0200 |
commit | 23fb7114d46ffae071891fb0bbdd6521dd4d5265 (patch) | |
tree | 7752e884cbbd4d1b696ad252961fa2e815c216c3 /ishtar_common | |
parent | 51a031f2f708e6ab890b2a506c0cdcccdfbe47fb (diff) | |
download | Ishtar-23fb7114d46ffae071891fb0bbdd6521dd4d5265.tar.bz2 Ishtar-23fb7114d46ffae071891fb0bbdd6521dd4d5265.zip |
Quick action: manage ajax form
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/forms.py | 5 | ||||
-rw-r--r-- | ishtar_common/static/js/ishtar.js | 69 | ||||
-rw-r--r-- | ishtar_common/templates/base.html | 3 | ||||
-rw-r--r-- | ishtar_common/templates/blocks/bs_form_snippet.html | 1 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/qa_form.html | 31 | ||||
-rw-r--r-- | ishtar_common/views.py | 18 |
6 files changed, 108 insertions, 19 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 106b68c14..2e249e472 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -455,6 +455,8 @@ class IshtarForm(forms.Form): if not getattr(profile, profile_key): for field_key in self.PROFILE_FILTER[profile_key]: self.fields.pop(field_key) + if getattr(self, 'confirm', True): + return for field in self.TYPES: self._init_type(field) for k in self.fields: @@ -646,11 +648,14 @@ class QAForm(CustomForm, ManageOldType): def __init__(self, *args, **kwargs): self.items = kwargs.pop('items') + self.confirm = kwargs.pop('confirm') super(QAForm, self).__init__(*args, **kwargs) for k in self.fields.keys(): if self.MULTI and k in self.SINGLE_FIELDS: self.fields.pop(k) continue + if self.confirm: + self.fields[k].widget = forms.HiddenInput() if self.MULTI and k not in self.REPLACE_FIELDS: self.fields[k].label = unicode(self.fields[k].label) + \ unicode(u" - append to existing") diff --git a/ishtar_common/static/js/ishtar.js b/ishtar_common/static/js/ishtar.js index 483da495c..2750efcc2 100644 --- a/ishtar_common/static/js/ishtar.js +++ b/ishtar_common/static/js/ishtar.js @@ -413,7 +413,23 @@ $(document).on("click", '#progress-content', function(){ $('.modal-progress').hide(); }); -function long_wait(){ + +var closed_wait = true; + +function short_wait(check_wait){ + if (check_wait && closed_wait){ + return + } + closed_wait = false; + $('.modal-progress').modal('show'); + $('.progress-1').show('slow'); +} + +function long_wait(check_wait){ + if (check_wait && closed_wait){ + return + } + closed_wait = false; $('.modal-progress').modal('show'); $('.progress-1').show('slow'); setTimeout(function(){ @@ -430,11 +446,12 @@ function long_wait(){ }, 180000); setTimeout(function(){ $('.progress-4').hide('slow'); - long_wait(); + long_wait(true); }, 240000); } function close_wait(){ + closed_wait = true; $('.modal-progress').modal('hide'); if (current_modal){ $('body').addClass('modal-open'); @@ -771,12 +788,48 @@ var dt_generate_qa_url = function (table, url){ } url += value + "/"; return url; -} +}; var dt_qa_open = function (url){ - long_wait(); - $('#modal-dynamic-form').load(url, function(){ - close_wait(); - $('#modal-dynamic-form').modal("show"); + short_wait(); + $.ajax({ + url: url, + cache: false, + success: function(data, textStatus, jqXHR) { + close_wait(); + var text = jqXHR.responseText; + $('#modal-dynamic-form').html(text); + var response = $(text); + var responseScript = response.find("script"); + $.each(responseScript, function(idx, val){ + eval(val.text); + } + ); + $('#modal-dynamic-form').modal("show"); + }, + error: function() { + close_wait(); + } }); -} +}; + +var ajax_post = function(url, data, target, callback){ + $.ajax({ + url : url, + type : "POST", + data : data, + success : function(data) { + close_wait(); + $(target).html(data); + if(callback) callback(); + }, + error : function(xhr,errmsg,err) { + close_wait(); + $(target).html("<div class='alert-box alert'>Oops! We have encountered an error: " + + errmsg + "</div>"); + console.log(xhr.status + ": " + xhr.responseText); + if(callback) callback(); + } + }); + +}; diff --git a/ishtar_common/templates/base.html b/ishtar_common/templates/base.html index b9270d910..bce02f61d 100644 --- a/ishtar_common/templates/base.html +++ b/ishtar_common/templates/base.html @@ -141,7 +141,8 @@ {% trans "Time to take a coffee?" %} <i class="fa fa-coffee" aria-hidden="true"></i></span> <span class='progress-detail progress-4'> {% trans "Time to take another coffee?" %} <i class="fa fa-coffee" aria-hidden="true"></i></span> - <button type="button" class="close" data-dismiss="modal" aria-label="Close"> + <button type="button" class="close" data-dismiss="modal" aria-label="Close" + onclick="closed_wait = true;return true;"> <span aria-hidden="true">×</span> </button> </div> diff --git a/ishtar_common/templates/blocks/bs_form_snippet.html b/ishtar_common/templates/blocks/bs_form_snippet.html index f122b33f2..0f5f9d83d 100644 --- a/ishtar_common/templates/blocks/bs_form_snippet.html +++ b/ishtar_common/templates/blocks/bs_form_snippet.html @@ -12,6 +12,7 @@ </div>{% endif %} {% endfor %} +{% csrf_token %} {% for field in form.visible_fields %} {% if form.SEARCH_AND_SELECT %} {{field}} diff --git a/ishtar_common/templates/ishtar/qa_form.html b/ishtar_common/templates/ishtar/qa_form.html index 2df3775ea..f3a4cf559 100644 --- a/ishtar_common/templates/ishtar/qa_form.html +++ b/ishtar_common/templates/ishtar/qa_form.html @@ -1,30 +1,57 @@ {% load i18n inline_formset table_form %} <div class="modal-dialog modal-lg modal-dialog-centered"> - <div class="modal-content" id='progress-content'> + <div class="modal-content"> <div class="modal-header"> <h2>{{page_name|safe}}</h2> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> - <form enctype="multipart/form-data" action="." method="post">{% csrf_token %} + <form enctype="multipart/form-data" action="{{url}}" method="post" + id="qa-action">{% csrf_token %} <div class="modal-body body-scroll"> <div class='form'> {% for error in form.non_field_errors %} <p>{{ error }}</p> {% endfor %} {% bs_form form %} + {% if confirm %}{% for field in form %} + {% if field.value %} + <label>{{field.label}}</label> {{ field.value }} + {% endif %} + {% endfor %}{% endif %} </div> </div> <div class="modal-footer"> <button type="submit" id="submit_form" name='validate' value="validate" class="btn btn-success"> + {% if confirm %} + {% trans "Confirm" %} + {% else %} {% trans "Modify" %} + {% endif %} + </button> + <button type="button" data-dismiss="modal" + aria-label="Close" class="btn btn-secondary"> + {% trans "Cancel" %} </button> </div> </form> </div> </div> +<script type="text/javascript"> + $('#qa-action').on('submit', function(event){ + event.preventDefault(); + $('#modal-dynamic-form').modal("hide"); + short_wait(); + ajax_post( + "{{url}}", $(this).serialize(), "#modal-dynamic-form", + function(){ + $('#modal-dynamic-form').modal("show"); + } + ); + }); +</script> diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 8b22048f2..3704106d2 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -1817,31 +1817,33 @@ class QAItemEditForm(IshtarMixin, LoginRequiredMixin, FormView): if not self.model.QA_EDIT.is_available( user=request.user, session=request.session, obj=item): raise Http404() - + self.confirm = kwargs.get('confirm', False) and True + self.url = request.get_full_path() return super(QAItemEditForm, self).dispatch(request, *args, **kwargs) def get_form_class(self): - if len(self.items) > 1: + if len(self.items) > 1 and self.form_class_multi: return self.form_class_multi return self.form_class def get_form_kwargs(self): kwargs = super(QAItemEditForm, self).get_form_kwargs() kwargs['items'] = self.items + kwargs['confirm'] = self.confirm return kwargs def get_context_data(self, **kwargs): data = super(QAItemEditForm, self).get_context_data(**kwargs) data['page_name'] = u"{} – {}".format( self.model._meta.verbose_name, self.model.QA_EDIT.text) + data['url'] = self.url + data['confirm'] = self.confirm return data - - - def get_success_url(self, basket): - return reverse('select_itemsinbasket', - kwargs={'pk': basket}) - def form_valid(self, form): + if not self.confirm: + self.confirm = True + return self.render_to_response( + self.get_context_data(form=self.get_form())) return HttpResponseRedirect(self.get_success_url( form.cleaned_data['basket'])) |