diff options
| -rw-r--r-- | archaeological_files/forms.py | 16 | ||||
| -rw-r--r-- | archaeological_files/models.py | 25 | ||||
| -rw-r--r-- | ishtar_common/models.py | 14 | ||||
| -rw-r--r-- | ishtar_common/views.py | 12 | 
4 files changed, 47 insertions, 20 deletions
| diff --git a/archaeological_files/forms.py b/archaeological_files/forms.py index e3b480a57..4364829b2 100644 --- a/archaeological_files/forms.py +++ b/archaeological_files/forms.py @@ -123,10 +123,14 @@ class FileFormSelection(forms.Form):          return cleaned_data  SLICING = (('year',_(u"years")), ("month",_(u"months"))) +DATE_SOURCE = (('creation',_(u"Creation date")), +               ("reception",_(u"Reception date")))  class DashboardForm(forms.Form):      slicing = forms.ChoiceField(label=_("Slicing"), choices=SLICING,                                required=False) +    date_source = forms.ChoiceField(label=_("Date get from"), +                            choices=DATE_SOURCE, required=False)      file_type = forms.ChoiceField(label=_("File type"), choices=[],                                    required=False)      saisine_type = forms.ChoiceField(label=_("Saisine type"), choices=[], @@ -141,18 +145,26 @@ class DashboardForm(forms.Form):          self.fields['saisine_type'].choices = models.SaisineType.get_types()          self.fields['file_type'].choices = models.FileType.get_types() +    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('saisine_type'):              fltr['saisine_type_id'] = self.cleaned_data['saisine_type']          if self.cleaned_data.get('file_type'):              fltr['file_type_id'] = self.cleaned_data['file_type']          if self.cleaned_data.get('after'): -            fltr['creation_date__gte'] = self.cleaned_data['after'] +            fltr[date_source+'_date__gte'] = self.cleaned_data['after']          if self.cleaned_data.get('before'): -            fltr['creation_date__lte'] = self.cleaned_data['before'] +            fltr[date_source+'_date__lte'] = self.cleaned_data['before']          return fltr  class FileFormGeneral(forms.Form): diff --git a/archaeological_files/models.py b/archaeological_files/models.py index a1e8912f4..70570e145 100644 --- a/archaeological_files/models.py +++ b/archaeological_files/models.py @@ -230,32 +230,35 @@ class File(BaseHistorizedItem, OwnPerms, ValueGetter, ShortMenuItem):          return sorted(owns.all(), key=lambda x:x.cached_label)      @classmethod -    def get_periods(cls, slice='year', fltr={}): -        q = cls.objects +    def get_periods(cls, slice='year', fltr={}, date_source='creation'): +        date_var = date_source + '_date' +        q = cls.objects.filter(**{date_var+'__isnull':False})          if fltr:              q = q.filter(**fltr)          if slice == 'year': -            return [res['year'] for res in list(q.values('year' +            return [res[date_var].year for res in list(q.values(date_var                                  ).annotate(Count("id")).order_by())]          elif slice == 'month': -            return [(res['creation_date'].year, res['creation_date'].month) -                               for res in list(q.values('creation_date' +            return [(res[date_var].year, res[date_var].month) +                               for res in list(q.values(date_var                                  ).annotate(Count("id")).order_by())]          return []      @classmethod -    def get_by_year(cls, year, fltr={}): -        q = cls.objects +    def get_by_year(cls, year, fltr={}, date_source='creation'): +        date_var = date_source + '_date' +        q = cls.objects.filter(**{date_var+'__isnull':False})          if fltr:              q = q.filter(**fltr) -        return q.filter(year=year) +        return q.filter(**{date_var+'__year':year})      @classmethod -    def get_by_month(cls, year, month, fltr={}): -        q = cls.objects +    def get_by_month(cls, year, month, fltr={}, date_source='creation'): +        date_var = date_source + '_date' +        q = cls.objects.filter(**{date_var+'__isnull':False})          if fltr:              q = q.filter(**fltr) -        return q.filter(creation_date__year=year, creation_date__month=month) +        return q.filter(**{date_var+'__year':year, date_var+'__month':month})      @classmethod      def get_total_number(cls, fltr={}): diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 0280a8fd7..98f4addb7 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -561,7 +561,8 @@ class UserDashboard:                            .order_by('person__person_types')  class Dashboard: -    def __init__(self, model, slice='year', fltr={}): +    def __init__(self, model, slice='year', date_source=None, fltr={}): +        # don't provide date_source if it is not relevant          self.model = model          self.total_number = model.get_total_number(fltr)          history_model = self.model.history.model @@ -586,7 +587,12 @@ class Dashboard:                  obj.history_date = idx['hd']                  last_lst.append(obj)          # years -        self.periods = model.get_periods(slice=slice, fltr=fltr) +        base_kwargs = {'fltr':fltr} +        if date_source: +            base_kwargs['date_source'] = date_source +        periods_kwargs = base_kwargs.copy() +        periods_kwargs['slice'] = slice +        self.periods = model.get_periods(**periods_kwargs)          self.periods = list(set(self.periods))          self.periods.sort()          if not self.total_number or not self.periods: @@ -594,11 +600,11 @@ class Dashboard:          # numbers          if slice == 'year':              self.values = [('year', _(u"Year"), reversed(self.periods))] -            self.numbers = [model.get_by_year(year, fltr=fltr).count() +            self.numbers = [model.get_by_year(year, **base_kwargs).count()                              for year in self.periods]              self.values += [('number', _(u"Number"), reversed(self.numbers))]          if slice == 'month': -            self.numbers = [model.get_by_month(*period, fltr=fltr).count() +            self.numbers = [model.get_by_month(*period, **base_kwargs).count()                              for period in self.periods]              periods = reversed(self.periods)              self.periods = ["%d-%s-01" % (period[0], ('0'+str(period[1])) diff --git a/ishtar_common/views.py b/ishtar_common/views.py index e51704794..d20b8b2af 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -730,21 +730,27 @@ def dashboard_main_detail(request, item_name):                      'ishtar/dashboards/dashboard_main_detail_users.html',                              dct, context_instance=RequestContext(request))      form = None -    slicing, fltr  = 'year', {} +    slicing, date_source, fltr  = 'year', None, {}      if item_name in DASHBOARD_FORMS:          if request.method == 'POST':              form = DASHBOARD_FORMS[item_name](request.POST)              if form.is_valid():                  slicing = form.cleaned_data['slicing']                  fltr = form.get_filter() +                if hasattr(form, 'get_date_source'): +                    date_source = form.get_date_source()          else:              form = DASHBOARD_FORMS[item_name]()      lbl, dashboard = None, None      if item_name == 'files' and \        'archaeological_files' in settings.INSTALLED_APPS:          from archaeological_files.models import File -        lbl, dashboard = (_(u"Archaeological files"), models.Dashboard(File, -                                                    slice=slicing, fltr=fltr)) +        dashboard_kwargs = {'slice':slicing, 'fltr':fltr,} +        # date_source is only relevant when the form has set one +        if date_source: +            dashboard_kwargs['date_source'] = date_source +        lbl, dashboard = (_(u"Archaeological files"), +                          models.Dashboard(File, **dashboard_kwargs))      if item_name == 'operations':          from archaeological_operations.models import Operation          lbl, dashboard = (_(u"Operations"), models.Dashboard(Operation, | 
