summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ishtar/furnitures/forms.py58
-rw-r--r--ishtar/furnitures/models.py27
2 files changed, 67 insertions, 18 deletions
diff --git a/ishtar/furnitures/forms.py b/ishtar/furnitures/forms.py
index b21f801a6..e99d4f077 100644
--- a/ishtar/furnitures/forms.py
+++ b/ishtar/furnitures/forms.py
@@ -47,6 +47,24 @@ from ishtar import settings
reverse_lazy = lazy(reverse, unicode)
+def clean_duplicated(formset, key_names):
+ """Checks for duplicated."""
+ if any(formset.errors):
+ return
+ items = []
+ for i in range(0, formset.total_form_count()):
+ form = formset.forms[i]
+ if not form.is_valid():
+ continue
+ item = [key_name in form.cleaned_data and form.cleaned_data[key_name]
+ for key_name in key_names]
+ if not [v for v in item if v]:
+ continue
+ if item in items:
+ raise forms.ValidationError, \
+ _("There are identical items.")
+ items.append(item)
+
class FinalForm(forms.Form):
final = True
form_label = _("Confirm")
@@ -1360,24 +1378,26 @@ class RemainForm(forms.Form):
class RemainFormSet(FormSet):
def clean(self):
"""Checks that no remain types are duplicated."""
- if any(self.errors):
- return
- remains = []
- for i in range(0, self.total_form_count()):
- form = self.forms[i]
- if not form.is_valid():
- continue
- if 'remain_type' not in form.cleaned_data:
- continue
- remain = form.cleaned_data['remain_type']
- if remain in remains:
- raise forms.ValidationError, \
- _("There are identical remain types.")
- remains.append(remain)
+ return clean_duplicated(self, ['remain_type'])
-RemainFormSet = formset_factory(RemainForm, can_delete=True,
+RemainFormset = formset_factory(RemainForm, can_delete=True,
formset=RemainFormSet)
-RemainFormSet.form_label = _("Remain types")
+RemainFormset.form_label = _("Remain types")
+
+class PeriodForm(forms.Form):
+ form_label = _("Periods")
+ associated_models = {'period':models.Period}
+ period = forms.ChoiceField(label=_("Period"), required=False,
+ choices=models.Period.get_types())
+
+class PeriodFormSet(FormSet):
+ def clean(self):
+ """Checks that no period are duplicated."""
+ return clean_duplicated(self, ['period'])
+
+PeriodFormset = formset_factory(PeriodForm, can_delete=True,
+ formset=PeriodFormSet)
+PeriodFormset.form_label = _("Periods")
operation_creation_wizard = OperationWizard([
('general-operation_creation', OperationFormGeneral),
@@ -1386,7 +1406,8 @@ operation_creation_wizard = OperationWizard([
('preventivediag-operation_creation', OperationFormPreventiveDiag),
('towns-operation_creation', SelectedTownFormSet),
('parcels-operation_creation', SelectedParcelFormSet),
- ('remains-operation_creation', RemainFormSet),
+ ('remains-operation_creation', RemainFormset),
+ ('periods-operation_creation', PeriodFormset),
('final-operation_creation', FinalForm)],
condition_list={
'preventive-operation_creation':is_preventive('general-operation_creation',
@@ -1404,7 +1425,8 @@ operation_modification_wizard = OperationWizard([
('preventivediag-operation_modification', OperationFormPreventiveDiag),
('towns-operation_modification', SelectedTownFormSet),
('parcels-operation_modification', SelectedParcelFormSet),
- ('remains-operation_modification', RemainFormSet),
+ ('remains-operation_modification', RemainFormset),
+ ('periods-operation_modification', PeriodFormset),
('final-operation_modification', FinalForm)],
condition_list={
'preventive-operation_modification':is_preventive(
diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py
index 0ce4ef057..3112d00f6 100644
--- a/ishtar/furnitures/models.py
+++ b/ishtar/furnitures/models.py
@@ -373,6 +373,7 @@ class Operation(BaseHistorizedItem, OwnPerms):
remains = models.ManyToManyField("RemainType", verbose_name=_(u'Remains'))
towns = models.ManyToManyField("Town", verbose_name=_(u"Towns"))
cost = models.IntegerField(_(u"Cost"), blank=True, null=True)
+ periods = models.ManyToManyField('Period', verbose_name=_(u"Periods"))
if settings.COUNTRY == 'fr':
code_patriarche = models.IntegerField(u"Code PATRIARCHE", null=True,
blank=True)
@@ -450,6 +451,32 @@ class Period(GeneralType) :
def __unicode__(self):
return self.label
+ @classmethod
+ def get_types(cls, dct={}):
+ dct['available'] = True
+ yield ('', '--')
+ items = cls.objects.filter(**dct).all()
+ if not items:
+ return
+ items = sorted(items, key=lambda x: x.parent)
+ idx, childs = 0, {None:[]}
+ while idx < len(items) and not items[idx].parent:
+ childs[items[idx]] = []
+ idx += 1
+ while idx < len(items):
+ if items[idx].parent in childs:
+ childs[items[idx].parent].append(items[idx])
+ else:
+ childs[None].append(items[idx])
+ idx += 1
+ for parent in sorted(childs.keys(),
+ key=lambda x:hasattr(x, 'order') and x.order):
+ if parent:
+ yield (parent.id, _(parent.label))
+ prefix = parent and "-- " or ""
+ for child in sorted(childs[parent], key=lambda x:x.order):
+ yield (child.id, _(prefix + child.label))
+
class DatingType(GeneralType):
class Meta:
verbose_name = _(u"Dating type")