From 23fb7114d46ffae071891fb0bbdd6521dd4d5265 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 6 Sep 2018 18:18:07 +0200 Subject: Quick action: manage ajax form --- archaeological_finds/forms.py | 91 +++++++++++----------- archaeological_finds/views.py | 3 +- ishtar_common/forms.py | 5 ++ ishtar_common/static/js/ishtar.js | 69 ++++++++++++++-- ishtar_common/templates/base.html | 3 +- .../templates/blocks/bs_form_snippet.html | 1 + ishtar_common/templates/ishtar/qa_form.html | 31 +++++++- ishtar_common/views.py | 18 +++-- scss/custom.scss | 1 + 9 files changed, 156 insertions(+), 66 deletions(-) diff --git a/archaeological_finds/forms.py b/archaeological_finds/forms.py index 5fb77433e..1605c4d6a 100644 --- a/archaeological_finds/forms.py +++ b/archaeological_finds/forms.py @@ -319,79 +319,79 @@ class FindForm(CustomForm, ManageOldType): class QAFindFormMulti(QAForm): form_admin_name = _(u"Find - Quick action - Modify") form_slug = "find-quickaction-modify" - base_models = ['get_first_base_find', 'object_types', 'material_types', - 'communicabilities'] + base_models = ['get_first_base_find', 'qa_object_types', + 'qa_material_types', 'qa_communicabilities'] associated_models = { - 'material_types': models.MaterialType, - 'object_types': models.ObjectType, - 'communicabilities': models.CommunicabilityType, - 'checked_type': models.CheckedType, + 'qa_material_types': models.MaterialType, + 'qa_object_types': models.ObjectType, + 'qa_communicabilities': models.CommunicabilityType, + 'qa_checked_type': models.CheckedType, } MULTI = True REPLACE_FIELDS = [ - 'manufacturing_place', 'checked_type', 'check_date' + 'qa_manufacturing_place', 'qa_checked_type', 'qa_check_date' ] HEADERS = { - 'label': FormHeader(_(u"Identification")), - 'description': FormHeader(_(u"Description")), - 'checked_type': FormHeader(_(u"Sheet")) + 'qa_label': FormHeader(_(u"Identification")), + 'qa_description': FormHeader(_(u"Description")), + 'qa_checked_type': FormHeader(_(u"Sheet")) } SINGLE_FIELDS = [ - 'label', 'denomination', 'previous_id', - 'get_first_base_find__excavation_id', 'museum_id', 'seal_number', - 'mark' + 'qa_label', 'qa_denomination', 'qa_previous_id', + 'qa_get_first_base_find__excavation_id', 'qa_museum_id', + 'qa_seal_number', 'qa_mark' ] - label = forms.CharField( + qa_label = forms.CharField( label=_(u"Free ID"), validators=[validators.MaxLengthValidator(60)]) - denomination = forms.CharField(label=_(u"Denomination"), required=False) - previous_id = forms.CharField(label=_("Previous ID"), required=False) - get_first_base_find__excavation_id = forms.CharField( + qa_denomination = forms.CharField(label=_(u"Denomination"), required=False) + qa_previous_id = forms.CharField(label=_("Previous ID"), required=False) + qa_get_first_base_find__excavation_id = forms.CharField( label=_(u"Excavation ID"), required=False) - museum_id = forms.CharField(label=_(u"Museum ID"), required=False) - seal_number = forms.CharField(label=_(u"Seal number"), required=False) - mark = forms.CharField(label=_(u"Mark"), required=False) + qa_museum_id = forms.CharField(label=_(u"Museum ID"), required=False) + qa_seal_number = forms.CharField(label=_(u"Seal number"), required=False) + qa_mark = forms.CharField(label=_(u"Mark"), required=False) - description = forms.CharField(label=_(u"Description"), + qa_description = forms.CharField(label=_(u"Description"), widget=forms.Textarea, required=False) - material_types = widgets.Select2MultipleField( + qa_material_types = widgets.Select2MultipleField( label=_(u"Material types"), required=False ) - object_types = widgets.Select2MultipleField( + qa_object_types = widgets.Select2MultipleField( label=_(u"Object types"), required=False, ) - decoration = forms.CharField( + qa_decoration = forms.CharField( label=_(u"Decoration"), widget=forms.Textarea, required=False) - inscription = forms.CharField( + qa_inscription = forms.CharField( label=_(u"Inscription"), widget=forms.Textarea, required=False) - manufacturing_place = forms.CharField( + qa_manufacturing_place = forms.CharField( label=_(u"Manufacturing place"), required=False) - communicabilities = widgets.Select2MultipleField( + qa_communicabilities = widgets.Select2MultipleField( label=_(u"Communicability"), required=False ) - comment = forms.CharField( + qa_comment = forms.CharField( label=_(u"Comment"), required=False, widget=forms.Textarea) - dating_comment = forms.CharField( + qa_dating_comment = forms.CharField( label=_(u"Comment on dating"), required=False, widget=forms.Textarea) - checked_type = forms.ChoiceField(label=_(u"Check"), required=False) - check_date = forms.DateField( - initial=get_now, label=_(u"Check date"), widget=DatePicker) + qa_checked_type = forms.ChoiceField(label=_(u"Check"), required=False) + qa_check_date = forms.DateField( + label=_(u"Check date"), widget=DatePicker, required=False) TYPES = [ - FieldType('material_types', models.MaterialType, is_multiple=True), - FieldType('object_types', models.ObjectType, is_multiple=True), - FieldType('communicabilities', models.CommunicabilityType, + FieldType('qa_material_types', models.MaterialType, is_multiple=True), + FieldType('qa_object_types', models.ObjectType, is_multiple=True), + FieldType('qa_communicabilities', models.CommunicabilityType, is_multiple=True), - FieldType('checked_type', models.CheckedType, is_multiple=True), + FieldType('qa_checked_type', models.CheckedType), ] @@ -405,7 +405,7 @@ class QAFindFormSingle(QAFindFormMulti): if not self.items or \ not self.items[0].get_first_base_find( ).context_record.operation.operation_type.judiciary: - self.fields.pop('seal_number') + self.fields.pop('qa_seal_number') class PreservationForm(CustomForm, ManageOldType): @@ -414,14 +414,15 @@ class PreservationForm(CustomForm, ManageOldType): form_slug = "find-030-preservation" base_models = ['alteration', 'alteration_cause', 'preservation_to_consider', 'integritie', 'remarkabilitie'] - associated_models = {'alteration': models.AlterationType, - 'alteration_cause': models.AlterationCauseType, - 'treatment_emergency': models.TreatmentEmergencyType, - 'conservatory_state': models.ConservatoryState, - 'preservation_to_consider': models.TreatmentType, - 'remarkabilitie': models.RemarkabilityType, - 'integritie': models.IntegrityType, - } + associated_models = { + 'alteration': models.AlterationType, + 'alteration_cause': models.AlterationCauseType, + 'treatment_emergency': models.TreatmentEmergencyType, + 'conservatory_state': models.ConservatoryState, + 'preservation_to_consider': models.TreatmentType, + 'remarkabilitie': models.RemarkabilityType, + 'integritie': models.IntegrityType, + } integritie = forms.MultipleChoiceField( label=_(u"Integrity / interest"), choices=[], widget=widgets.Select2Multiple, required=False) diff --git a/archaeological_finds/views.py b/archaeological_finds/views.py index ca575a889..850890894 100644 --- a/archaeological_finds/views.py +++ b/archaeological_finds/views.py @@ -598,5 +598,4 @@ def reset_wizards(request): class QAFindForm(QAItemEditForm): model = models.Find - form_class = QAFindFormSingle - form_class_multi = QAFindFormMulti + form_class = QAFindFormMulti 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("
Oops! We have encountered an error: " + + errmsg + "
"); + 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?" %} {% trans "Time to take another coffee?" %} - 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 @@ {% 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 %}