diff options
-rw-r--r-- | ishtar/ishtar_base/forms_common.py | 6 | ||||
-rw-r--r-- | ishtar/ishtar_base/forms_operations.py | 62 | ||||
-rw-r--r-- | ishtar/ishtar_base/models.py | 7 |
3 files changed, 73 insertions, 2 deletions
diff --git a/ishtar/ishtar_base/forms_common.py b/ishtar/ishtar_base/forms_common.py index 8621a57b2..c7ca15472 100644 --- a/ishtar/ishtar_base/forms_common.py +++ b/ishtar/ishtar_base/forms_common.py @@ -436,6 +436,12 @@ class SourceForm(forms.Form): title = forms.CharField(label=_(u"Title"), validators=[validators.MaxLengthValidator(200)]) source_type = forms.ChoiceField(label=_(u"Source type"), choices=[]) + associated_url = forms.URLField(required=False, + label=_(u"Numerical ressource (web address)")) + receipt_date = forms.DateField(label=_(u"Receipt date"), required=False, + widget=widgets.JQueryDate) + creation_date = forms.DateField(label=_(u"Creation date"), required=False, + widget=widgets.JQueryDate) def __init__(self, *args, **kwargs): super(SourceForm, self).__init__(*args, **kwargs) 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',) diff --git a/ishtar/ishtar_base/models.py b/ishtar/ishtar_base/models.py index 56c3b551c..d124addf2 100644 --- a/ishtar/ishtar_base/models.py +++ b/ishtar/ishtar_base/models.py @@ -476,6 +476,12 @@ class Source(models.Model): title = models.CharField(_(u"Title"), max_length=200) source_type = models.ForeignKey(SourceType, verbose_name=_(u"Type")) authors = models.ManyToManyField(Author, verbose_name=_(u"Authors")) + associated_url = models.URLField(verify_exists=False, blank=True, null=True, + verbose_name=_(u"Numerical ressource (web address)")) + receipt_date = models.DateField(blank=True, null=True, + verbose_name=_(u"Receipt date")) + creation_date = models.DateField(blank=True, null=True, + verbose_name=_(u"Creation date")) TABLE_COLS = ['title', 'source_type', 'authors',] class Meta: @@ -740,6 +746,7 @@ class OperationSource(Source): verbose_name_plural = _(u"Operation documentations") operation = models.ForeignKey(Operation, verbose_name=_(u"Operation"), related_name="source") + index = models.IntegerField(verbose_name=_(u"Index")) TABLE_COLS = ['operation.year', 'operation.operation_code'] + \ Source.TABLE_COLS |