summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@peacefrogs.net>2011-01-20 01:31:39 +0100
committerÉtienne Loks <etienne.loks@peacefrogs.net>2011-01-20 01:31:39 +0100
commit97ad819c60462def8e3d8554a0ee419ccc4330fc (patch)
tree3b48e7153b52eb33a9e455ef8e6ea18af3d0b8e0
parent2e09fdb4314308387a8fd6f60e5633677767d3c6 (diff)
downloadIshtar-97ad819c60462def8e3d8554a0ee419ccc4330fc.tar.bz2
Ishtar-97ad819c60462def8e3d8554a0ee419ccc4330fc.zip
Add the possibility to create an operation without an archaelogical file (closes #106)
-rw-r--r--ishtar/furnitures/forms.py93
-rw-r--r--ishtar/furnitures/models.py2
-rw-r--r--ishtar/templates/confirm_wizard.html4
3 files changed, 68 insertions, 31 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")
diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py
index 3a93175b7..f2fa8837a 100644
--- a/ishtar/furnitures/models.py
+++ b/ishtar/furnitures/models.py
@@ -340,7 +340,7 @@ class Operation(BaseHistorizedItem, OwnPerms):
year = models.IntegerField(_(u"Year"))
operation_code = models.IntegerField(_(u"Operation code"))
associated_file = models.ForeignKey(File, related_name='+',
- verbose_name=_(u"File"))
+ verbose_name=_(u"File"), blank=True, null=True)
operation_type = models.ForeignKey(OperationType, related_name='+',
verbose_name=_(u"Operation type"))
remains = models.ManyToManyField("RemainType", verbose_name=_(u'Remains'))
diff --git a/ishtar/templates/confirm_wizard.html b/ishtar/templates/confirm_wizard.html
index a35b6bea1..5a92e0d58 100644
--- a/ishtar/templates/confirm_wizard.html
+++ b/ishtar/templates/confirm_wizard.html
@@ -10,7 +10,7 @@
<li class='current'>&raquo;&nbsp;<a href='#'>{{current_step.form_label}}</a></li>
</ul>
<div class='form'>
- <p>{%if confirm_msg %}{{confirm_msg}}{%else%}{% trans "You have entered the following informations:" %}{%endif%}</p>
+ <p>{%if confirm_msg %}{{confirm_msg|safe}}{%else%}{% trans "You have entered the following informations:" %}{%endif%}</p>
{% for form_label, form_data in datas %}
<table class='confirm'>
<caption>{{form_label}}</caption>
@@ -24,7 +24,7 @@
{{ form.as_table }}
</table>
{%endif%}
- <p>{%if confirm_end_msg %}{{confirm_end_msg}}{%else%}{% trans "Would you like to save them?" %}{%endif%}</p>
+ <p>{%if confirm_end_msg %}{{confirm_end_msg|safe}}{%else%}{% trans "Would you like to save them?" %}{%endif%}</p>
<input type="hidden" name="{{ step_field }}" value="{{ step0 }}" />
{{ previous_fields|safe }}
<input type="submit" value="{% trans "Validate" %}"/>