summaryrefslogtreecommitdiff
path: root/archaeological_operations/forms.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_operations/forms.py')
-rw-r--r--archaeological_operations/forms.py135
1 files changed, 130 insertions, 5 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py
index 18534fe9d..d665fc779 100644
--- a/archaeological_operations/forms.py
+++ b/archaeological_operations/forms.py
@@ -41,7 +41,7 @@ from ishtar_common import widgets
from ishtar_common.forms import FinalForm, FormSet, get_now, \
reverse_lazy, TableSelect, get_data_from_formset, QAForm, CustomFormSearch,\
ManageOldType, IshtarForm, CustomForm, FieldType, FormHeader, \
- HistorySelect, LockForm, MultiSearchForm
+ DocumentItemSelect, LockForm, MultiSearchForm
from ishtar_common.forms_common import TownFormSet, get_town_field, TownForm
from ishtar_common.models import valid_id, valid_ids, Person, Town, \
DocumentTemplate, Organization, get_current_profile, \
@@ -472,7 +472,7 @@ RecordRelationsFormSet.form_admin_name = _(u"Operation - 080 - Relations")
RecordRelationsFormSet.form_slug = "operation-080-relations"
-class OperationSelect(HistorySelect):
+class OperationSelect(DocumentItemSelect):
_model = models.Operation
form_admin_name = _(u"Operation - 001 - Search")
form_slug = "operation-001-search"
@@ -604,6 +604,7 @@ class OperationFormSelection(LockForm, CustomFormSearch):
SEARCH_AND_SELECT = True
form_label = _(u"Operation search")
associated_models = {'pk': models.Operation}
+ extra_form_modals = ["person", "organization"]
currents = {'pk': models.Operation}
pk = forms.IntegerField(
label="", required=False,
@@ -617,6 +618,7 @@ class OperationFormSelection(LockForm, CustomFormSearch):
class OperationFormMultiSelection(LockForm, MultiSearchForm):
form_label = _(u"Operation search")
associated_models = {'pks': models.Operation}
+ extra_form_modals = ["person", "organization"]
pk_key = 'pks'
pk = forms.CharField(
label="", required=False,
@@ -1365,7 +1367,7 @@ class OperationDeletionForm(FinalForm):
#########
-class SiteSelect(HistorySelect):
+class SiteSelect(DocumentItemSelect):
_model = models.ArchaeologicalSite
form_admin_name = _(u"Archaeological site - 001 - Search")
form_slug = "archaeological_site-001-search"
@@ -1848,22 +1850,32 @@ class AdministrativeActRegisterFormSelection(IshtarForm):
class QAOperationFormMulti(QAForm):
- form_admin_name = _(u"Operation - Quick action - Modify")
+ form_admin_name = _("Operation - Quick action - Modify")
form_slug = "operation-quickaction-modify"
base_models = ['qa_operation_type']
associated_models = {
'qa_operation_type': models.OperationType,
- 'qa_towns': Town
+ 'qa_towns': Town,
+ 'qa_operator': Organization,
}
MULTI = True
REPLACE_FIELDS = [
'qa_operation_type',
+ 'qa_operator',
]
qa_operation_type = forms.ChoiceField(
label=_(u"Operation type"), required=False
)
qa_towns = get_town_field(required=False)
+ qa_operator = forms.IntegerField(
+ label=_("Operator"),
+ widget=widgets.JQueryAutoComplete(
+ reverse_lazy('autocomplete-organization',
+ args=[organization_type_pk_lazy('operator')]),
+ limit={'organization_type': organization_type_pk_lazy('operator')},
+ associated_model=Organization, new=True),
+ validators=[valid_id(Organization)], required=False)
TYPES = [
FieldType('qa_operation_type', models.OperationType),
@@ -1876,3 +1888,116 @@ class QAOperationFormMulti(QAForm):
return ""
return value
+ def _get_qa_operator(self, value):
+ try:
+ value = Organization.objects.get(pk=value).cached_label
+ except Organization.DoesNotExist:
+ return ""
+ return value
+
+
+class QAOperationDuplicateForm(IshtarForm):
+ qa_code_patriarche = forms.CharField(
+ max_length=500, widget=OAWidget, label=_("Code PATRIARCHE"),
+ required=False)
+ qa_year = forms.IntegerField(label=_("Year"), required=False,
+ validators=[validators.MinValueValidator(1000),
+ validators.MaxValueValidator(2100)])
+ qa_common_name = forms.CharField(label=_("Generic name"), required=False,
+ max_length=500, widget=forms.Textarea)
+ qa_operation_type = forms.ChoiceField(label=_("Operation type"), choices=[])
+
+ TYPES = [
+ FieldType('qa_operation_type', models.OperationType),
+ ]
+
+ 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.operation = kwargs.pop('items')[0]
+ super(QAOperationDuplicateForm, self).__init__(*args, **kwargs)
+
+ self.fields['qa_year'].initial = self.operation.year
+ self.fields['qa_common_name'].initial = self.operation.common_name
+
+ self.fields["qa_operation_type"].initial = \
+ self.operation.operation_type.pk
+
+ def clean_qa_code_patriarche(self):
+ code = self.cleaned_data['qa_code_patriarche']
+ if models.Operation.objects \
+ .filter(code_patriarche=code).count():
+ raise forms.ValidationError(_("This code already exists."))
+ return code
+
+ def save(self):
+ data = {"operation_code": None}
+ for k in ("code_patriarche", "common_name", "year"):
+ data[k] = self.cleaned_data.get("qa_" + k, None)
+ try:
+ data["operation_type"] = models.OperationType.objects.get(
+ pk=self.cleaned_data["qa_operation_type"], available=True
+ )
+ except models.OperationType.DoesNotExist:
+ return
+ operation = self.operation.duplicate(self.user, data=data)
+ # clear associated sites
+ operation.archaeological_sites.clear()
+ operation.skip_history_when_saving = True
+ operation._cached_label_checked = False
+ operation._search_updated = False
+ operation._no_move = True
+ operation.save() # regen of labels
+ return operation
+
+
+class QAArchaeologicalSiteDuplicateForm(IshtarForm):
+ qa_reference = forms.CharField(label=_("Reference"), max_length=200)
+ qa_name = forms.CharField(label=_("Name"), max_length=200, required=False)
+
+ 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.site = kwargs.pop('items')[0]
+ super(QAArchaeologicalSiteDuplicateForm, self).__init__(*args, **kwargs)
+
+ self.fields['qa_reference'].initial = (
+ self.site.reference or "") + str(_(" - duplicate"))
+ self.fields['qa_name'].initial = self.site.name
+
+ def clean_qa_reference(self):
+ reference = self.cleaned_data['qa_reference']
+ if models.ArchaeologicalSite.objects \
+ .filter(reference=reference).count():
+ raise forms.ValidationError(_("This reference already exists."))
+ return reference
+
+ def save(self):
+ data = {}
+ for k in ("name", "reference"):
+ data[k] = self.cleaned_data.get("qa_" + k, None)
+ return self.site.duplicate(self.user, data=data)
+
+
+class QAArchaeologicalSiteFormMulti(QAForm):
+ form_admin_name = _("Archaeological files - Quick action - Modify")
+ form_slug = "archaeological_site-quickaction-modify"
+ associated_models = {
+ 'qa_towns': Town,
+ }
+
+ MULTI = True
+ qa_towns = get_town_field(required=False)
+
+ def _get_qa_towns(self, value):
+ try:
+ value = Town.objects.get(pk=value).cached_label
+ except Town.DoesNotExist:
+ return ""
+ return value