diff options
Diffstat (limited to 'ishtar/ishtar_base/forms_operations.py')
-rw-r--r-- | ishtar/ishtar_base/forms_operations.py | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/ishtar/ishtar_base/forms_operations.py b/ishtar/ishtar_base/forms_operations.py index 5ed882530..e2b0aadb1 100644 --- a/ishtar/ishtar_base/forms_operations.py +++ b/ishtar/ishtar_base/forms_operations.py @@ -517,6 +517,64 @@ operation_deletion_wizard = OperationDeletionWizard([ class OperationSourceWizard(SourceWizard): model = models.OperationSource + def get_form_initial(self, request, storage, step): + initial = super(OperationSourceWizard, self).get_form_initial(request, + storage, step) + # put default index and operation_id field in the main source form + general_form_key = 'selec-' + self.url_name + print "hop" + if step.startswith('source-') \ + and self.session_has_key(request, storage, general_form_key): + gen_storage = request.session[storage.prefix]['step_data']\ + [general_form_key] + if general_form_key+"-operation" in gen_storage: + operation_id = int(gen_storage[general_form_key+"-operation"]) + elif general_form_key+"-pk" in gen_storage: + pk = int(gen_storage[general_form_key+"-pk"]) + print pk + try: + source = models.OperationSource.objects.get(pk=pk) + operation_id = source.operation.pk + except ObjectDoesNotExist: + pass + if operation_id: + initial['hidden_operation_id'] = operation_id + if 'index' not in initial: + max_val = models.OperationSource.objects.filter( + operation__pk=operation_id).aggregate( + Max('index'))["index__max"] + initial['index'] = max_val and (max_val + 1) or 1 + return initial + +class OperationSourceForm(SourceForm): + pk = forms.IntegerField(required=False, widget=forms.HiddenInput) + index = forms.IntegerField(label=_(u"Index")) + hidden_operation_id = forms.IntegerField(label="", widget=forms.HiddenInput) + + def __init__(self, *args, **kwargs): + super(OperationSourceForm, self).__init__(*args, **kwargs) + keyOrder = self.fields.keyOrder + keyOrder.pop(keyOrder.index('index')) + keyOrder.insert(keyOrder.index('source_type') + 1, 'index') + + def clean(self): + # manage unique operation ID + cleaned_data = self.cleaned_data + operation_id = cleaned_data.get("hidden_operation_id") + index = cleaned_data.get("index") + srcs = models.OperationSource.objects.filter(index=index, + operation__pk=operation_id) + if 'pk' in cleaned_data and cleaned_data['pk']: + srcs = srcs.exclude(pk=cleaned_data['pk']) + if srcs.count(): + max_val = models.OperationSource.objects.filter( + operation__pk=operation_id + ).aggregate(Max('index'))["index__max"] + operation = models.Operation.objects.get(pk=operation_id) + raise forms.ValidationError(_(u"Index already exist for " +"operation: %(operation)s - use a value bigger than %(last_val)d") % { + "operation":unicode(operation), 'last_val':max_val}) + return cleaned_data SourceOperationFormSelection = get_form_selection( 'SourceOperationFormSelection', _(u"Operation search"), 'operation', @@ -525,7 +583,7 @@ SourceOperationFormSelection = get_form_selection( operation_source_creation_wizard = OperationSourceWizard([ ('selec-operation_source_creation', SourceOperationFormSelection), - ('source-operation_source_creation', SourceForm), + ('source-operation_source_creation',OperationSourceForm), ('authors-operation_source_creation', AuthorFormset), ('final-operation_source_creation', FinalForm)], url_name='operation_source_creation',) @@ -551,7 +609,7 @@ OperationSourceFormSelection = get_form_selection( operation_source_modification_wizard = OperationSourceWizard([ ('selec-operation_source_modification', OperationSourceFormSelection), - ('source-operation_source_modification', SourceForm), + ('source-operation_source_modification', OperationSourceForm), ('authors-operation_source_modification', AuthorFormset), ('final-operation_source_modification', FinalForm)], url_name='operation_source_modification',) |