summaryrefslogtreecommitdiff
path: root/ishtar_common/forms_common.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/forms_common.py')
-rw-r--r--ishtar_common/forms_common.py90
1 files changed, 86 insertions, 4 deletions
diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py
index 7a6f2fe72..2b52f0919 100644
--- a/ishtar_common/forms_common.py
+++ b/ishtar_common/forms_common.py
@@ -57,7 +57,7 @@ from archaeological_finds.models import Find, FindBasket
from archaeological_warehouse.models import Container
-def get_town_field(label=_(u"Town"), required=True):
+def get_town_field(label=_("Town"), required=True):
help_text = _(
u"<p>Type name, department code of the "
u"town you would like to select. The search is insensitive to case."
@@ -575,20 +575,37 @@ class QAPersonFormMulti(QAForm):
base_models = ['qa_title']
associated_models = {
'qa_title': models.TitleType,
+ 'qa_attached_to': models.Organization,
}
MULTI = True
REPLACE_FIELDS = [
'qa_title',
+ 'qa_attached_to'
]
qa_title = forms.ChoiceField(
label=_(u"Title"), required=False
)
+ qa_attached_to = forms.IntegerField(
+ label=_("Organization"),
+ widget=widgets.JQueryAutoComplete(
+ reverse_lazy('autocomplete-organization'),
+ associated_model=models.Organization),
+ validators=[models.valid_id(models.Organization)],
+ required=False
+ )
TYPES = [
FieldType('qa_title', models.TitleType),
]
+ def _get_qa_attached_to(self, value):
+ try:
+ value = models.Organization.objects.get(pk=value).cached_label
+ except models.Organization.DoesNotExist:
+ return ""
+ return value
+
class PersonMergeFormSelection(ManualMerge, forms.Form):
SEARCH_AND_SELECT = True
@@ -1302,9 +1319,9 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType):
not cleaned_data.get('image', None) and \
not cleaned_data.get('associated_file', None) and \
not cleaned_data.get('associated_url', None):
- raise forms.ValidationError(_(u"You should at least fill one of "
- u"this field: title, url, image or "
- u"file."))
+ raise forms.ValidationError(_("You should at least fill one of "
+ "this field: title, url, image or "
+ "file."))
for rel in models.Document.RELATED_MODELS:
if cleaned_data.get(rel, None):
return cleaned_data
@@ -1457,6 +1474,7 @@ class QADocumentFormMulti(QAForm):
MULTI = True
REPLACE_FIELDS = [
'qa_source_type',
+ 'qa_creation_date',
]
qa_source_type = forms.ChoiceField(
label=_(u"Source type"), required=False
@@ -1464,6 +1482,8 @@ class QADocumentFormMulti(QAForm):
qa_authors = widgets.ModelJQueryAutocompleteField(
model=models.Author, label=_(u"Author"), new=True,
required=False)
+ qa_creation_date = forms.DateField(
+ label=_("Creation date"), widget=DatePicker, required=False)
TYPES = [
FieldType('qa_source_type', models.SourceType),
@@ -1477,6 +1497,68 @@ class QADocumentFormMulti(QAForm):
return value
+class QADocumentDuplicateForm(IshtarForm):
+ qa_title = forms.CharField(label=_("Reference"), max_length=500,
+ required=False)
+ qa_source_type = forms.ChoiceField(label=_("Type"), choices=[],
+ required=False)
+
+ TYPES = [
+ FieldType('qa_source_type', models.SourceType),
+ ]
+
+ def __init__(self, *args, **kwargs):
+ self.user = None
+ if 'user' in kwargs:
+ self.user = kwargs.pop('user')
+ if hasattr(self.user, 'ishtaruser'):
+ self.user = self.user.ishtaruser
+ self.document = kwargs.pop('items')[0]
+ super(QADocumentDuplicateForm, self).__init__(*args, **kwargs)
+
+ self.fields['qa_title'].initial = self.document.title + str(
+ _(" - duplicate"))
+ if self.document.source_type:
+ self.fields['qa_source_type'].initial = self.document.source_type.pk
+
+ for related_key in models.Document.RELATED_MODELS_ALT:
+ related = getattr(self.document, related_key)
+ if not related.count():
+ continue
+ model = models.Document._meta.get_field(related_key).related_model
+ initial = []
+ for item in related.all():
+ initial.append(item.pk)
+ self.fields["qa_" + related_key] = widgets.Select2MultipleField(
+ model=model, remote=True, label=model._meta.verbose_name_plural,
+ required=False, long_widget=True, initial=initial
+ )
+
+ def save(self):
+ data = {"index": None}
+ for k in ["title"]:
+ data[k] = self.cleaned_data.get("qa_" + k, None)
+ if self.cleaned_data.get("qa_source_type", None):
+ try:
+ data["source_type"] = models.SourceType.objects.get(
+ pk=int(self.cleaned_data["qa_source_type"]), available=True)
+ except models.SourceType.DoesNotExist:
+ return
+ new = self.document.duplicate_item(self.user, data=data)
+ for related_key in models.Document.RELATED_MODELS_ALT:
+ getattr(new, related_key).clear()
+ values = self.cleaned_data.get("qa_" + related_key, [])
+ model = models.Document._meta.get_field(related_key).related_model
+ for value in values:
+ getattr(new, related_key).add(model.objects.get(pk=value))
+ new.skip_history_when_saving = True
+ new._cached_label_checked = False
+ new._search_updated = False
+ new._no_move = True
+ new.save() # regen of labels
+ return new
+
+
class QALockForm(forms.Form):
action = forms.ChoiceField(
label=_("Action"), choices=(('lock', _("Lock")),