summaryrefslogtreecommitdiff
path: root/ishtar/furnitures/forms.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar/furnitures/forms.py')
-rw-r--r--ishtar/furnitures/forms.py93
1 files changed, 65 insertions, 28 deletions
diff --git a/ishtar/furnitures/forms.py b/ishtar/furnitures/forms.py
index 02fa838f0..9cd49d142 100644
--- a/ishtar/furnitures/forms.py
+++ b/ishtar/furnitures/forms.py
@@ -341,7 +341,7 @@ class Wizard(NamedUrlSessionFormWizard):
if hasattr(current_form, 'currents'):
initial = {}
for key in current_form.currents:
- model_name = current_form.currents[key].__name__.lower()
+ model_name = current_form.currents[key].__name__.lower()
val = model_name in request.session and \
request.session[model_name]
if val:
@@ -366,7 +366,8 @@ class Wizard(NamedUrlSessionFormWizard):
value = getattr(obj, field)
if hasattr(value, 'pk'):
value = value.pk
- initial[field] = unicode(value)
+ if value != None:
+ initial[field] = unicode(value)
elif hasattr(c_form, 'management_form'):
initial = []
key = current_step.split('-')[0]
@@ -384,7 +385,8 @@ class Wizard(NamedUrlSessionFormWizard):
value = getattr(child_obj, field)
if hasattr(value, 'pk'):
value = value.pk
- vals[field] = unicode(value)
+ if value != None:
+ vals[field] = unicode(value)
if vals:
initial.append(vals)
return initial
@@ -980,20 +982,34 @@ file_administrativeact_wizard = FileAdministrativeActWizard([
class OperationWizard(Wizard):
model = models.Operation
+ def process_post_request(self, request, storage, *args, **kwargs):
+ """
+ Change the town form to a free selection town's form if no archaelogical
+ file is provided
+ """
+ file_key = 'general-' + self.url_name + '-associated_file'
+ if file_key in request.POST.keys():
+ form_key = 'towns-' + self.url_name
+ if request.POST[file_key]:
+ self.form_list[unicode(form_key)] = SelectedTownFormSet
+ else:
+ self.form_list[unicode(form_key)] = SelectedTownGeneralFormSet
+ return super(OperationWizard, self).process_post_request(request,
+ storage, *args, **kwargs)
+
def get_form(self, request, storage, step=None, data=None, files=None):
"""
- Manage towns
+ Manage specifics fields
"""
if data:
data = data.copy()
else:
data = {}
- # manage the dynamic choice of towns
if not step:
step = self.determine_step(request, storage)
form = self.get_form_list(request, storage)[step]
- # put hidden year field
+ # put hidden year field for refs
general_form_key = 'general-' + self.url_name
if not data and step.startswith('refs-') \
and self.session_has_key(request, storage, general_form_key):
@@ -1004,22 +1020,39 @@ class OperationWizard(Wizard):
max_val = models.Operation.objects.filter(year=year).aggregate(
Max('operation_code'))["operation_code__max"]
data[prefix+'-operation_code'] = max_val and (max_val + 1) or 1
+ # manage the dynamic choice of towns
if step.startswith('towns-') and hasattr(form, 'management_form') \
and self.session_has_key(request, storage, general_form_key):
towns = []
- try:
- file_id = int(request.session[storage.prefix]['step_data']\
- [general_form_key][general_form_key+"-associated_file"])
- for town in models.File.objects.get(pk=file_id).towns.all():
- towns.append((town.pk, unicode(town)))
- except (ValueError, ObjectDoesNotExist):
- pass
- data['TOWNS'] = sorted(towns, key=lambda x:x[1])
+ file_id = self.session_get_value(request, storage, general_form_key,
+ "associated_file")
+ if file_id:
+ try:
+ for town in models.File.objects.get(pk=int(file_id)
+ ).towns.all():
+ towns.append((town.pk, unicode(town)))
+ except (ValueError, ObjectDoesNotExist):
+ pass
+ data['TOWNS'] = sorted(towns, key=lambda x:x[1])
data = data or None
form = super(OperationWizard, self).get_form(request, storage, step,
data, files)
return form
+ def get_formated_datas(self, forms):
+ """
+ Show a specific warning if no archaelogical file is provided
+ """
+ datas = super(OperationWizard, self).get_formated_datas(forms)
+ # simple selection town is used if no Archaelogical File is provided
+ has_af = [form for form in forms
+ if isinstance(form, SelectedTownFormSet)]
+ if not has_af:
+ datas = [[_(u"Warning: No Archaelogical File is provided. "
+ u"If you have forget it return to the first step."), []]]\
+ + datas
+ return datas
+
class OperationFormSelection(forms.Form):
form_label = _("Operation")
associated_models = {'pk':models.Operation}
@@ -1044,7 +1077,7 @@ class OperationFormGeneral(forms.Form):
associated_file = forms.IntegerField(label=_("Archaelogical file"),
widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-file'),
associated_model=models.File),
- validators=[models.valid_id(models.File)])
+ validators=[models.valid_id(models.File)], required=False)
operation_type = forms.ChoiceField(label=_("Operation type"),
choices=models.OperationType.get_types())
start_date = forms.DateField(label=_(u"Start date"), required=False,
@@ -1107,6 +1140,10 @@ SelectedTownFormSet = formset_factory(SelectedTownForm, can_delete=True,
formset=TownFormSet)
SelectedTownFormSet.form_label = _("Towns")
+SelectedTownGeneralFormSet = formset_factory(TownForm, can_delete=True,
+ formset=TownFormSet)
+SelectedTownGeneralFormSet.form_label = _("Towns")
+
class RemainForm(forms.Form):
form_label = _("Remain types")
associated_models = {'remain':models.RemainType}
@@ -1136,21 +1173,21 @@ RemainFormSet = formset_factory(RemainForm, can_delete=True,
RemainFormSet.form_label = _("Remain types")
operation_creation_wizard = OperationWizard([
- ('general-operation_creation', OperationFormGeneral),
- ('refs-operation_creation', OperationFormReference),
- ('towns-operation_creation', SelectedTownFormSet),
- ('remains-operation_creation', RemainFormSet),
- ('final-operation_creation', FinalForm)],
- url_name='operation_creation',)
+ ('general-operation_creation', OperationFormGeneral),
+ ('refs-operation_creation', OperationFormReference),
+ ('towns-operation_creation', SelectedTownFormSet),
+ ('remains-operation_creation', RemainFormSet),
+ ('final-operation_creation', FinalForm)],
+ url_name='operation_creation',)
operation_modification_wizard = OperationWizard([
- ('selec-operation_modification', OperationFormSelection),
- ('general-operation_modification', OperationFormGeneral),
- ('refs-operation_modification', OperationFormReference),
- ('towns-operation_modification', SelectedTownFormSet),
- ('remains-operation_modification', RemainFormSet),
- ('final-operation_modification', FinalForm)],
- url_name='operation_modification',)
+ ('selec-operation_modification', OperationFormSelection),
+ ('general-operation_modification', OperationFormGeneral),
+ ('refs-operation_modification', OperationFormReference),
+ ('towns-operation_modification', SelectedTownFormSet),
+ ('remains-operation_modification', RemainFormSet),
+ ('final-operation_modification', FinalForm)],
+ url_name='operation_modification',)
class OperationDateFormSelection(forms.Form):
form_label = _("Closing date")