summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/forms.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_warehouse/forms.py')
-rw-r--r--archaeological_warehouse/forms.py142
1 files changed, 131 insertions, 11 deletions
diff --git a/archaeological_warehouse/forms.py b/archaeological_warehouse/forms.py
index b34be3ab3..e91d04d61 100644
--- a/archaeological_warehouse/forms.py
+++ b/archaeological_warehouse/forms.py
@@ -19,6 +19,7 @@
from django import forms
from django.conf import settings
+from django.forms.formsets import formset_factory
from django.utils.translation import ugettext_lazy as _
from ishtar_common.models import Person, valid_id
@@ -26,7 +27,7 @@ from archaeological_finds.models import TreatmentType, FindBasket
import models
from ishtar_common import widgets
from ishtar_common.forms import name_validator, reverse_lazy, \
- get_form_selection, TableSelect, ManageOldType
+ get_form_selection, TableSelect, ManageOldType, FinalForm
from archaeological_finds.forms import FindMultipleFormSelection, \
SelectFindBasketForm
@@ -39,7 +40,58 @@ def get_warehouse_field(label=_(u"Warehouse"), required=True):
validators=[valid_id(models.Warehouse)])
+class SelectedDivisionForm(ManageOldType, forms.Form):
+ form_label = _(u"Division")
+ base_model = 'associated_division'
+ associated_models = {'division': models.WarehouseDivision,
+ 'associated_division': models.WarehouseDivisionLink}
+ division = forms.ChoiceField(
+ label=_(u"Division"), choices=(), required=False,
+ validators=[valid_id(models.WarehouseDivision)])
+ order = forms.IntegerField(_(u"Order"), initial=10, required=False)
+
+ def __init__(self, *args, **kwargs):
+ super(SelectedDivisionForm, self).__init__(*args, **kwargs)
+ self.fields['division'].choices = \
+ models.WarehouseDivision.get_types(
+ initial=self.init_data.get('division')
+ )
+
+SelectedDivisionFormset = formset_factory(
+ SelectedDivisionForm, can_delete=True)
+SelectedDivisionFormset.form_label = _(u"Divisions")
+
+
+class WarehouseSelect(TableSelect):
+ name = forms.CharField(label=_(u"Name"))
+ warehouse_type = forms.ChoiceField(label=_(u"Warehouse type"), choices=[])
+ towns = forms.CharField(label=_(u"Town"))
+
+ def __init__(self, *args, **kwargs):
+ super(WarehouseSelect, self).__init__(*args, **kwargs)
+ self.fields['warehouse_type'].choices = \
+ models.WarehouseType.get_types()
+ self.fields['warehouse_type'].help_text = \
+ models.WarehouseType.get_help()
+
+
+class WarehouseFormSelection(forms.Form):
+ form_label = _("Warehouse search")
+ associated_models = {'pk': models.Warehouse}
+ currents = {'pk': models.Warehouse}
+ pk = forms.IntegerField(
+ label="", required=False,
+ widget=widgets.JQueryJqGrid(
+ reverse_lazy('get-warehouse'),
+ WarehouseSelect, models.Warehouse),
+ validators=[valid_id(models.Warehouse)])
+
+
class WarehouseForm(ManageOldType, forms.Form):
+ form_label = _(u"Warehouse")
+ associated_models = {'warehouse_type': models.WarehouseType,
+ 'person_in_charge': Person}
+
name = forms.CharField(label=_(u"Name"), max_length=40,
validators=[name_validator])
warehouse_type = forms.ChoiceField(label=_(u"Warehouse type"),
@@ -48,8 +100,8 @@ class WarehouseForm(ManageOldType, forms.Form):
label=_(u"Person in charge"),
widget=widgets.JQueryAutoComplete(
reverse_lazy('autocomplete-person'),
- associated_model=models.Person),
- validators=[valid_id(models.Person)],
+ associated_model=Person),
+ validators=[valid_id(Person)],
required=False)
comment = forms.CharField(label=_(u"Comment"), widget=forms.Textarea,
required=False)
@@ -63,7 +115,7 @@ class WarehouseForm(ManageOldType, forms.Form):
country = forms.CharField(label=_(u"Country"), max_length=30,
required=False)
phone = forms.CharField(label=_(u"Phone"), max_length=18, required=False)
- mobile_phone = forms.CharField(label=_(u"Town"), max_length=18,
+ mobile_phone = forms.CharField(label=_(u"Mobile phone"), max_length=18,
required=False)
def __init__(self, *args, **kwargs):
@@ -89,12 +141,26 @@ class WarehouseForm(ManageOldType, forms.Form):
return new_item
+class WarehouseDeletionForm(FinalForm):
+ confirm_msg = _(u"Would you like to delete this warehouse?")
+ confirm_end_msg = _(u"Would you like to delete this warehouse?")
+
+
class ContainerForm(ManageOldType, forms.Form):
form_label = _(u"Container")
+ associated_models = {'container_type': models.ContainerType,
+ 'location': models.Warehouse,
+ 'responsible': models.Warehouse}
reference = forms.CharField(label=_(u"Ref."))
container_type = forms.ChoiceField(label=_(u"Container type"), choices=[])
location = forms.IntegerField(
- label=_(u"Warehouse"),
+ label=_(u"Current location (warehouse)"),
+ widget=widgets.JQueryAutoComplete(
+ reverse_lazy('autocomplete-warehouse'),
+ associated_model=models.Warehouse, new=True),
+ validators=[valid_id(models.Warehouse)])
+ responsible = forms.IntegerField(
+ label=_(u"Responsible warehouse"),
widget=widgets.JQueryAutoComplete(
reverse_lazy('autocomplete-warehouse'),
associated_model=models.Warehouse, new=True),
@@ -123,6 +189,31 @@ class ContainerForm(ManageOldType, forms.Form):
return new_item
+class ContainerModifyForm(ContainerForm):
+ pk = forms.IntegerField(required=False, widget=forms.HiddenInput)
+ index = forms.IntegerField(_(u"Index"))
+
+ def __init__(self, *args, **kwargs):
+ super(ContainerModifyForm, self).__init__(*args, **kwargs)
+ self.fields.keyOrder.pop(self.fields.keyOrder.index('index'))
+ self.fields.keyOrder.insert(
+ self.fields.keyOrder.index("location") + 1, 'index')
+
+ def clean(self):
+ # manage unique ID
+ cleaned_data = self.cleaned_data
+ index = cleaned_data.get("index")
+ warehouse = cleaned_data.get("location")
+ q = models.Container.objects.filter(
+ index=index, location__pk=warehouse)
+ if 'pk' in cleaned_data and cleaned_data['pk']:
+ q = q.exclude(pk=int(cleaned_data['pk']))
+ if q.count():
+ raise forms.ValidationError(_(u"This ID already exists for "
+ u"this warehouse."))
+ return cleaned_data
+
+
class ContainerSelect(TableSelect):
location = get_warehouse_field()
container_type = forms.ChoiceField(label=_(u"Container type"), choices=[])
@@ -141,6 +232,12 @@ ContainerFormSelection = get_form_selection(
_(u"You should select a container."), new=True,
new_message=_(u"Add a new container"))
+MainContainerFormSelection = get_form_selection(
+ 'ContainerFormSelection', _(u"Container search"), 'pk',
+ models.Container, ContainerSelect, 'get-container',
+ _(u"You should select a container.")
+)
+
class BasePackagingForm(SelectFindBasketForm):
form_label = _(u"Packaging")
@@ -148,7 +245,6 @@ class BasePackagingForm(SelectFindBasketForm):
'person': Person,
'location': models.Warehouse,
'basket': FindBasket}
- treatment_type = forms.IntegerField(label="", widget=forms.HiddenInput)
person = forms.IntegerField(
label=_(u"Packager"),
widget=widgets.JQueryAutoComplete(
@@ -158,11 +254,35 @@ class BasePackagingForm(SelectFindBasketForm):
start_date = forms.DateField(
label=_(u"Date"), required=False, widget=widgets.JQueryDate)
- def __init__(self, *args, **kwargs):
- super(BasePackagingForm, self).__init__(*args, **kwargs)
- self.fields['treatment_type'].initial = \
- TreatmentType.objects.get(txt_idx='packaging').pk
-
class FindPackagingFormSelection(FindMultipleFormSelection):
form_label = _(u"Packaged finds")
+
+
+class LocalisationForm(forms.Form):
+ form_label = _(u"Localisation")
+
+ def __init__(self, *args, **kwargs):
+ container, warehouse = None, None
+ if 'warehouse' in kwargs:
+ warehouse = kwargs.pop('warehouse')
+ if 'container' in kwargs:
+ container = kwargs.pop('container')
+ super(LocalisationForm, self).__init__(*args, **kwargs)
+ if not warehouse:
+ return
+ for divlink in warehouse.warehousedivisionlink_set.order_by(
+ 'order').all():
+ initial = u"-"
+ if container:
+ q = models.ContainerLocalisation.objects.filter(
+ division=divlink, container=container)
+ if q.count():
+ initial = q.all()[0].reference
+ self.fields['division_{}'.format(divlink.pk)] = forms.CharField(
+ label=str(divlink.division), max_length=200, initial=initial)
+
+
+class ContainerDeletionForm(FinalForm):
+ confirm_msg = _(u"Would you like to delete this container?")
+ confirm_end_msg = _(u"Would you like to delete this container?")