diff options
Diffstat (limited to 'archaeological_context_records/forms.py')
-rw-r--r-- | archaeological_context_records/forms.py | 105 |
1 files changed, 86 insertions, 19 deletions
diff --git a/archaeological_context_records/forms.py b/archaeological_context_records/forms.py index 2061ecc1f..87cad2c66 100644 --- a/archaeological_context_records/forms.py +++ b/archaeological_context_records/forms.py @@ -147,6 +147,27 @@ class RecordFormMultiSelection(LockForm, MultiSearchForm): validators=[valid_ids(models.ContextRecord)]) +def get_init_parcel(form, operation, prefix=""): + parcels = operation.parcels.all() + sort = lambda x: (x.town.name, x.section) + parcels = sorted(parcels, key=sort) + for key, gparcels in groupby(parcels, sort): + form.fields[prefix + 'parcel'].choices.append( + (" - ".join([k for k in key if k]), + [(parcel.pk, parcel.short_label) for parcel in gparcels]) + ) + if len(form.fields[prefix + 'parcel'].choices) == 1 and \ + (prefix + 'town') in form.fields: + # only the empty choice is available + form.fields.pop(prefix + 'parcel') + form.fields[prefix + 'town'].required = True + if (prefix + 'town') in form.fields: + if form.fields[prefix + 'town'].required: + form.fields[prefix + 'town'].choices = [] # remove the empty choice + form.fields[prefix + 'town'].choices += [ + (t.pk, str(t)) for t in operation.towns.all()] + + class RecordFormGeneral(CustomForm, ManageOldType): HEADERS = {} form_label = _("General") @@ -279,25 +300,7 @@ class RecordFormGeneral(CustomForm, ManageOldType): if operation: self.fields['operation_id'].initial = operation.pk - parcels = operation.parcels.all() - sort = lambda x: (x.town.name, x.section) - parcels = sorted(parcels, key=sort) - for key, gparcels in groupby(parcels, sort): - self.fields['parcel'].choices.append( - (" - ".join([k for k in key if k]), - [(parcel.pk, parcel.short_label) for parcel in gparcels]) - ) - if len(self.fields['parcel'].choices) == 1 and \ - 'town' in self.fields: - # only the empty choice is available - self.fields.pop('parcel') - self.fields['town'].required = True - if 'town' in self.fields: - if self.fields['town'].required: - self.fields['town'].choices = [] # remove the empty choice - self.fields['town'].choices += [(t.pk, str(t)) - for t in operation.towns.all()] - + get_init_parcel(self, operation) self.fields['archaeological_site'].choices += [ (site.pk, str(site)) for site in operation.archaeological_sites.all() @@ -478,3 +481,67 @@ class QAOperationCR(IshtarForm): else: data["town_id"] = self.cleaned_data['town'] models.ContextRecord.objects.create(**data) + + +class QAContextRecordDuplicateForm(IshtarForm): + qa_label = forms.CharField(label=_("ID"), max_length=None, required=True) + qa_parcel = forms.ChoiceField(label=_(u"Parcel"), choices=[]) + qa_town = forms.ChoiceField(label=_(u"Town"), choices=[], required=False) + qa_unit = forms.ChoiceField(label=_(u"Context record type"), required=False, + choices=[]) + + TYPES = [ + FieldType('qa_unit', models.Unit), + ] + + def __init__(self, *args, **kwargs): + self.user = None + if 'user' in kwargs: + self.user = kwargs.pop('user') + if hasattr(self.user, 'ishtaruser'): + self.user = self.user.ishtaruser + self.context_record = kwargs.pop('items')[0] + super(QAContextRecordDuplicateForm, self).__init__(*args, **kwargs) + + profile = IshtarSiteProfile.get_current_profile() + self.fields['qa_parcel'].choices = [('', '--')] + if not profile.parcel_mandatory: + self.fields['qa_parcel'].required = False + self.fields['qa_town'].choices = [('', '--')] + else: + self.fields.pop('qa_town') + + get_init_parcel(self, self.context_record.operation, prefix="qa_") + + if "qa_town" in self.fields and self.context_record.town: + self.fields["qa_town"].initial = self.context_record.town.pk + if "qa_parcel" in self.fields and self.context_record.parcel: + self.fields["qa_parcel"].initial = self.context_record.parcel.pk + + self.fields['qa_label'].initial = ( + self.context_record.label or "") + str(_(" - duplicate")) + if self.context_record.unit: + self.fields["qa_unit"].initial = self.context_record.unit.pk + + def save(self): + data = {"label": self.cleaned_data["qa_label"]} + if self.cleaned_data.get("qa_unit", None): + try: + data["unit"] = models.Unit.objects.get(pk=int( + self.cleaned_data["qa_unit"]), available=True) + except models.Unit.DoesNotExist: + pass + if self.cleaned_data.get("qa_town", None): + try: + data["town"] = Town.objects.get(pk=int( + self.cleaned_data["qa_town"])) + except Town.DoesNotExist: + pass + if self.cleaned_data.get("qa_parcel", None): + try: + data["parcel"] = Parcel.objects.get(pk=int( + self.cleaned_data["qa_parcel"])) + except Parcel.DoesNotExist: + pass + return self.context_record.duplicate( + self.user, data=data) |