diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-29 18:50:24 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-29 18:51:53 +0100 |
commit | c7ad0854b7e8d95fcd1d48287063eac2db8bff39 (patch) | |
tree | 0ca065af2ce3b7afbc682cceb9999a680870015e /archaeological_finds | |
parent | d1ccd3fe6f240882b2158e046b4e7908a814930f (diff) | |
download | Ishtar-c7ad0854b7e8d95fcd1d48287063eac2db8bff39.tar.bz2 Ishtar-c7ad0854b7e8d95fcd1d48287063eac2db8bff39.zip |
QA packaging: allow to change ref and current containers
Diffstat (limited to 'archaeological_finds')
-rw-r--r-- | archaeological_finds/forms_treatments.py | 87 | ||||
-rw-r--r-- | archaeological_finds/models_treatments.py | 14 | ||||
-rw-r--r-- | archaeological_finds/templates/ishtar/forms/qa_find_treatment.html | 39 | ||||
-rw-r--r-- | archaeological_finds/tests.py | 113 |
4 files changed, 220 insertions, 33 deletions
diff --git a/archaeological_finds/forms_treatments.py b/archaeological_finds/forms_treatments.py index 18c0697fc..5471cea23 100644 --- a/archaeological_finds/forms_treatments.py +++ b/archaeological_finds/forms_treatments.py @@ -357,15 +357,21 @@ class QAFindTreatmentForm(IshtarForm): reverse_lazy('autocomplete-container'), associated_model=Container, new=True), validators=[valid_id(Container)]) - reference_container = forms.BooleanField( - label=_(u"Change the reference container"), required=False, - widget=widgets.CheckboxInput, initial=True, - help_text=_(u"If unchecked the current container will be changed") + container_to_change = forms.ChoiceField( + label=_(u"Change "), required=True, + choices=( + ('current-and-reference', _(u"current and reference containers")), + ('reference', _(u"the reference container")), + ('current', _(u"the current container")), + ) ) create_treatment = forms.BooleanField( label=_(u"Create a treatment"), required=False, widget=widgets.CheckboxInput ) + treatment_type = forms.ChoiceField( + label=_(u"Treatment type"), choices=[], required=False + ) year = forms.IntegerField( label=_("Year"), initial=lambda: datetime.datetime.now().year, validators=[validators.MinValueValidator(1000), @@ -394,10 +400,36 @@ class QAFindTreatmentForm(IshtarForm): self.user = self.user.ishtaruser self.items = kwargs.pop('items') + tt_change_ref_loca = list( + models.TreatmentType.objects.filter( + available=True, change_reference_location=True).all()) + + self.treatment_type_ref_choices = u"".join([ + u"<option value='{}'>{}</option>".format(tt.pk, unicode(tt)) + for tt in tt_change_ref_loca + ]) + + tt_change_current_loca = list( + models.TreatmentType.objects.filter( + available=True, change_current_location=True).all()) + + self.treatment_type_current_choices = u"".join([ + u"<option value='{}'>{}</option>".format(tt.pk, unicode(tt)) + for tt in tt_change_current_loca + ]) + self.treatment_type_all_choices = self.treatment_type_ref_choices + \ + self.treatment_type_current_choices + super(QAFindTreatmentForm, self).__init__(*args, **kwargs) if not self.user: return + # treatment type is dynamic put all for check + self.fields['treatment_type'].choices = [ + (tt.pk, unicode(tt)) for tt in ( + tt_change_ref_loca + tt_change_current_loca) + ] + q = Person.objects.filter(ishtaruser__pk=self.user.pk) if q.count(): person = q.all()[0] @@ -413,16 +445,22 @@ class QAFindTreatmentForm(IshtarForm): self.cleaned_data['year'] = self.cleaned_data['start_date'].year else: raise forms.ValidationError(_(u"At least a year is required.")) + if not self.cleaned_data.get('treatment_type', None): + raise forms.ValidationError(_(u"Treatment type is required.")) + return self.cleaned_data def save(self, items, user): container = Container.objects.get(pk=self.cleaned_data['container']) + container_to_change = self.cleaned_data.get('container_to_change', '') if self.cleaned_data['create_treatment']: - packaging, created = models.TreatmentType.objects.get_or_create( - txt_idx='packaging', - defaults={"label": _(u"Packaging"), - "change_reference_location": True} - ) + treat_type = models.TreatmentType.objects.get( + pk=self.cleaned_data['treatment_type']) + treat_state, __ = models.TreatmentState.objects.get_or_create( + txt_idx='completed', + defaults={ + 'label': _(u"Completed"), 'executed': True, + 'available': True}) t = models.Treatment.objects.create( container=container, year=self.cleaned_data['year'], @@ -430,19 +468,28 @@ class QAFindTreatmentForm(IshtarForm): location=container.location, person_id=self.cleaned_data['person'], organization_id=self.cleaned_data['organization'], - history_modifier=user + history_modifier=user, + treatment_state=treat_state ) - t.treatment_types.add(packaging) - t.save(items=items) - return - container_attr = 'container' - if self.cleaned_data.get('reference_container', False): - container_attr = 'container_ref' + t.treatment_types.add(treat_type) + new_items = t.save(items=items, return_new=True) + if new_items: + items = new_items + container_attrs = [] + if container_to_change in ('reference', 'current-and-reference'): + container_attrs.append('container_ref') + if container_to_change in ('current', 'current-and-reference'): + container_attrs.append('container') for find in items: - if getattr(find, container_attr) == container: - continue - setattr(find, container_attr, container) - find.save() + changed = False + for container_attr in container_attrs: + if getattr(find, container_attr) == container: + continue + setattr(find, container_attr, container) + changed = True + if changed: + find.history_modifier = user + find.save() SLICING = (("month", _(u"months")), ('year', _(u"years")),) diff --git a/archaeological_finds/models_treatments.py b/archaeological_finds/models_treatments.py index 0a04cefb9..a8c48a3ad 100644 --- a/archaeological_finds/models_treatments.py +++ b/archaeological_finds/models_treatments.py @@ -434,7 +434,7 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem, def save(self, *args, **kwargs): items, user, extra_args_for_new, resulting_find = [], None, [], None upstream_items, upstream_item, resulting_finds = [], None, None - treatment_types = [] + treatment_types, return_new = [], False if "items" in kwargs: items = kwargs.pop('items') if "resulting_find" in kwargs: @@ -451,6 +451,8 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem, extra_args_for_new = kwargs.pop('extra_args_for_new') if "treatment_type_list" in kwargs: treatment_types = kwargs.pop('treatment_type_list') + if "return_new" in kwargs: + return_new = kwargs.pop('return_new') self.pre_save() super(Treatment, self).save(*args, **kwargs) to_be_executed = not self.executed and self.treatment_state.executed @@ -491,6 +493,7 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem, create_new_find = bool([tp for tp in treatment_types if tp.create_new_find]) + new_items = [] for item in items: if not create_new_find or not to_be_executed: self.finds.add(item) @@ -504,6 +507,7 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem, setattr(new, k, extra_args_for_new[k]) new.save() updated.append(new.pk) + new_items.append(new) # update baskets for basket in \ FindBasket.objects.filter(items__pk=item.pk).all(): @@ -511,6 +515,8 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem, basket.items.add(new) if not to_be_executed: + if return_new: + return new_items return if create_new_find: @@ -534,6 +540,8 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem, # manage containers if not self.container: + if return_new: + return new_items return container_attrs = [] @@ -549,6 +557,8 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem, if not container_attrs: # non consistent treatment + if return_new: + return new_items return for find in q.all(): @@ -561,6 +571,8 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem, find.save() self.executed = True self.save() + if return_new: + return new_items @property def associated_filename(self): diff --git a/archaeological_finds/templates/ishtar/forms/qa_find_treatment.html b/archaeological_finds/templates/ishtar/forms/qa_find_treatment.html index f20f0cb65..38db02ff4 100644 --- a/archaeological_finds/templates/ishtar/forms/qa_find_treatment.html +++ b/archaeological_finds/templates/ishtar/forms/qa_find_treatment.html @@ -27,9 +27,9 @@ </div> <div class="form-row"> - {{ form.reference_container }} <label for="{{form.reference_container.auto_id}}"> - {% trans "Change the reference container" %} - </label> + {% with form.container_to_change as field %} + {% include "blocks/bs_field_snippet.html" %} + {% endwith %} </div> <div class="form-row"> @@ -38,15 +38,13 @@ </label> </div> <div id="new-treatment"> - {% with force_large_col=True %}{% for field in form %} - {% if field.name != 'reference_container' and field.name != 'container' and field.name != 'create_treatment' %} - {% if forloop.counter|divisibleby:2 %} - <div class="form-row">{% endif %} + <div class="form-row"> + {% with force_large_col=false %}{% for field in form %} + {% if field.name != 'container_to_change' and field.name != 'container' and field.name != 'create_treatment' %} {% include "blocks/bs_field_snippet.html" %} - {% if not forloop.counter0|divisibleby:2 %} - </div>{% endif %} {% endif %} {% endfor %}{% endwith %} + </div> </div> {% endblock %} @@ -57,11 +55,32 @@ var update_form_display = function(){ } else { $("#new-treatment").hide(); } -} +}; + +var treatment_type_ref_choices = "{{form.treatment_type_ref_choices|safe}}"; +var treatment_type_current_choices = "{{form.treatment_type_current_choices|safe}}"; +var treatment_type_all_choices = "{{form.treatment_type_all_choices|safe}}"; + +var update_treatment_type_choices = function(){ + var container_to_change = $("#id_qa-packaging-container_to_change").val(); + var tt_sel = $("#id_qa-packaging-treatment_type") + tt_sel.empty() + if (container_to_change == 'reference'){ + tt_sel.append(treatment_type_ref_choices); + } + if (container_to_change == 'current'){ + tt_sel.append(treatment_type_current_choices); + } + if (container_to_change == 'current-and-reference'){ + tt_sel.append(treatment_type_all_choices); + } +}; $(document).ready(function(){ $("#{{form.create_treatment.auto_id}}").click(update_form_display); + $("#{{form.container_to_change.auto_id}}").change(update_treatment_type_choices); update_form_display(); + update_treatment_type_choices(); }); {% endblock %} diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index a86890839..fc51cea70 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -34,7 +34,8 @@ from ishtar_common.models import Person, get_current_profile, UserProfile, \ from archaeological_context_records.models import Period, Dating, \ ContextRecord, DatingType, DatingQuality from archaeological_finds import models, views -from archaeological_warehouse.models import Warehouse, WarehouseType +from archaeological_warehouse.models import Warehouse, WarehouseType, \ + ContainerType, Container from ishtar_common import forms_common @@ -759,7 +760,7 @@ class FindPermissionTest(FindInit, TestCase): class FindQATest(FindInit, TestCase): - fixtures = FIND_FIXTURES + fixtures = WAREHOUSE_FIXTURES model = models.Find def setUp(self): @@ -816,6 +817,114 @@ class FindQATest(FindInit, TestCase): self.assertEqual(models.Find.objects.get(pk=find_1.pk).description, base_desc_1 + u"\n" + extra_desc) + def test_packaging(self): + c = Client() + find_0 = self.finds[0] + find_1 = self.finds[1] + pks = u"{}-{}".format(find_0.pk, find_1.pk) + url = reverse('find-qa-packaging', args=[pks]) + response = c.get(url) + self.assertRedirects(response, '/') + + c = Client() + c.login(username=self.username, password=self.password) + response = c.get(url) + # warehouse profile must be activated + self.assertEqual(response.status_code, 404) + + profile, created = IshtarSiteProfile.objects.get_or_create( + slug='default', active=True) + profile.warehouse = True + profile.save() + response = c.get(url) + self.assertEqual(response.status_code, 200) + + c = Client() + c.login(username=self.alt_username, password=self.alt_password) + response = c.get(url) + self.assertEqual(response.status_code, 200) + + main_warehouse = Warehouse.objects.create( + name="Main", + warehouse_type=WarehouseType.objects.all()[0] + ) + + container = Container.objects.create( + reference="Test", responsible=main_warehouse, + location=main_warehouse, + container_type=ContainerType.objects.all()[0] + ) + container2 = Container.objects.create( + reference="Test2", responsible=main_warehouse, + location=main_warehouse, + container_type=ContainerType.objects.all()[0] + ) + packaging = models.TreatmentType.objects.get(txt_idx='packaging') + packaging.change_reference_location = True + packaging.save() + + data_check_lst = [ + ({ + 'qa-packaging-container': container.pk, + 'qa-packaging-container_to_change': 'reference', + }, + { + 'container_ref': container, + 'container': None + }, 0), + ({ + 'qa-packaging-container': container2.pk, + 'qa-packaging-container_to_change': 'current', + }, + { + 'container_ref': None, + 'container': container2 + }, 0), + ({ + 'qa-packaging-container': container.pk, + 'qa-packaging-container_to_change': 'current-and-reference', + }, + { + 'container_ref': container, + 'container': container + }, 0), + ({ + 'qa-packaging-container': container2.pk, + 'qa-packaging-container_to_change': 'reference', + 'qa-packaging-create_treatment': True, + 'qa-packaging-year': 2019, + 'qa-packaging-treatment_type': packaging.pk + }, + { + 'container_ref': container2, + 'container': None + }, 1), + ] + + for data, check, nb_treat in data_check_lst: + # reinit + find_0.container, find_0.container_ref = None, None + find_0.skip_history_when_saving = True + find_0.save() + find_1.container, find_1.container_ref = None, None + find_1.skip_history_when_saving = True + find_1.save() + + init_nb_treat = models.Treatment.objects.count() + + response = c.post( + reverse('find-qa-packaging', args=[pks]), + data) + self.assertRedirects(response, '/success/') + for k in check: + find = models.Find.objects.get(pk=find_0.pk) + self.assertEqual(getattr(find, k), check[k]) + find = models.Find.objects.get(pk=find_1.pk) + self.assertEqual(getattr(find, k), check[k]) + + final_nb_treat = models.Treatment.objects.count() + self.assertEqual(init_nb_treat + nb_treat, final_nb_treat) + class FindHistoryTest(FindInit, TestCase): fixtures = FIND_FIXTURES |