diff options
Diffstat (limited to 'archaeological_operations/forms.py')
| -rw-r--r-- | archaeological_operations/forms.py | 106 | 
1 files changed, 94 insertions, 12 deletions
| diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index d31d5c542..bb743372d 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -35,7 +35,11 @@ from django.utils.safestring import mark_safe  from ishtar_common.models import valid_id, PersonType, Person, Town, \                               DocumentTemplate, Organization, OrganizationType -from archaeological_files.models import File + +FILES_AVAILABLE = 'archaeological_files' in settings.INSTALLED_APPS + +if FILES_AVAILABLE: +    from archaeological_files.models import File  import models  from widgets import ParcelWidget, SelectParcelWidget @@ -292,14 +296,90 @@ class OperationCodeInput(forms.TextInput):                                 'url':reverse_lazy('get_available_operation_code')}          return mark_safe(rendered + js) -class OperationFormFileChoice(forms.Form): -    form_label = _(u"Associated file") -    associated_models = {'associated_file':File,} -    currents = {'associated_file':File} -    associated_file = forms.IntegerField(label=_(u"Archaelogical file"), -         widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-file'), -                                           associated_model=File), -         validators=[valid_id(File)], required=False) +if FILES_AVAILABLE: +    class OperationFormFileChoice(forms.Form): +        form_label = _(u"Associated file") +        associated_models = {'associated_file':File,} +        currents = {'associated_file':File} +        associated_file = forms.IntegerField(label=_(u"Archaelogical file"), +             widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-file'), +                                               associated_model=File), +             validators=[valid_id(File)], required=False) + +SLICING = (("month",_(u"months")), ('year',_(u"years")),) + +DATE_SOURCE = (('creation',_(u"Creation date")), +               ("start",_(u"Start of field work"))) + +PREVENTIVE_RESARCH = (('all', _('All')), +                      ('preventive', _(u"Preventive")), +                      ('research', _(u"Research")),) + +class DashboardForm(forms.Form): +    slicing = forms.ChoiceField(label=_("Slicing"), choices=SLICING, +                              required=False) +    department_detail = forms.BooleanField(label=_("Department detail"), +                              required=False) +    date_source = forms.ChoiceField(label=_("Date get from"), +                            choices=DATE_SOURCE, required=False) +    preventive_research = forms.ChoiceField(label=_("Preventive/Research"), +                                    choices=PREVENTIVE_RESARCH, required=False) +    operation_type = forms.ChoiceField(label=_("Operation type"), choices=[], +                                       required=False) +    operator = forms.ChoiceField(label=_("Operator"), choices=[], +                                 required=False) +    after = forms.DateField(label=_(u"Date after"), +                            widget=widgets.JQueryDate, required=False) +    before = forms.DateField(label=_(u"Date before"), +                            widget=widgets.JQueryDate, required=False) +    with_report = forms.BooleanField(label=_("With reports"), required=False) +    with_finds = forms.BooleanField(label=_("With finds"), required=False) + +    def __init__(self, *args, **kwargs): +        if 'prefix' not in kwargs: +            kwargs['prefix'] = 'operations' +        super(DashboardForm, self).__init__(*args, **kwargs) +        self.fields['operation_type'].choices = models.OperationType.get_types() +        self.fields['operator'].choices = [('', '--')] +        self.fields['operator'].choices += [(orga.pk, orga.name) +            for orga in Organization.objects.filter(operator__isnull=False)\ +                                            .order_by('name').distinct().all()] + +    def get_show_detail(self): +        return hasattr(self, 'cleaned_data') and \ +           self.cleaned_data.get('department_detail') + +    def get_date_source(self): +        date_source = 'creation' +        if hasattr(self, 'cleaned_data') and \ +           self.cleaned_data.get('date_source'): +            date_source = self.cleaned_data['date_source'] +        return date_source + +    def get_filter(self): +        if not hasattr(self, 'cleaned_data') or not self.cleaned_data: +            return {} +        date_source = self.get_date_source() +        fltr = {} +        if self.cleaned_data.get('preventive_research'): +            preventive_research = self.cleaned_data['preventive_research'] +            if preventive_research == 'preventive': +                fltr['file_type__preventive'] = True +            elif preventive_research == 'preventive': +                fltr['file_type__preventive'] = False +        if self.cleaned_data.get('operation_type'): +            fltr['operation_type_id'] = self.cleaned_data['operation_type'] +        if self.cleaned_data.get('operator'): +            fltr['operator_id'] = self.cleaned_data['operator'] +        if self.cleaned_data.get('after'): +            fltr[date_source+'_date__gte'] = self.cleaned_data['after'] +        if self.cleaned_data.get('before'): +            fltr[date_source+'_date__lte'] = self.cleaned_data['before'] +        if self.cleaned_data.get('with_report'): +            fltr['report_delivery_date__isnull'] = False +        if self.cleaned_data.get('with_finds'): +            fltr['context_record__base_finds__isnull'] = False +        return fltr  class OperationFormGeneral(forms.Form):      form_label = _(u"General") @@ -419,9 +499,10 @@ class OperationFormGeneral(forms.Form):          return self.cleaned_data  class OperationFormModifGeneral(OperationFormGeneral): -    currents = {'associated_file':File}      operation_code = forms.IntegerField(label=_(u"Operation code")) -    associated_file = forms.IntegerField(label=_(u"Archaelogical file"), +    if FILES_AVAILABLE: +        currents = {'associated_file':File} +        associated_file = forms.IntegerField(label=_(u"Archaelogical file"),           widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-file'),                                             associated_model=File),           validators=[valid_id(File)], required=False) @@ -435,7 +516,8 @@ class OperationFormModifGeneral(OperationFormGeneral):  OperationFormModifGeneral.associated_models = \                                  OperationFormGeneral.associated_models.copy() -OperationFormModifGeneral.associated_models['associated_file'] = File +if FILES_AVAILABLE: +    OperationFormModifGeneral.associated_models['associated_file'] = File  class OperationFormPreventive(forms.Form): | 
