Ejemplo n.º 1
0
    def get_group_queryset(self, period_step, start, end):
        query = self.get_span_queryset(period_step, start, end)
        ann_kwargs = {}
        if period_step == models.STEP_DAY:
            ann_kwargs['_date'] = db_functions.TruncDay(
                self.date_field, output_field=DateField())
            ann_kwargs['_month'] = db_functions.TruncMonth(self.date_field)
            ann_kwargs['_year'] = db_functions.TruncYear(self.date_field)
        if period_step == models.STEP_MONTH:
            ann_kwargs['_date'] = db_functions.TruncMonth(
                self.date_field, output_field=DateField())
            ann_kwargs['_year'] = db_functions.TruncYear(self.date_field)
        if period_step == models.STEP_YEAR:
            ann_kwargs['_date'] = db_functions.TruncYear(
                self.date_field, output_field=DateField())

        return query.annotate(**ann_kwargs).values(*ann_kwargs.keys())
Ejemplo n.º 2
0
 def stats(self, last):
     qs = self.filter(start_time__gte=now() - timedelta(days=last))
     qs = qs.annotate(
         day=dbfunc.TruncDay('start_time'),
         month=dbfunc.TruncMonth('start_time'),
         year=dbfunc.TruncYear('start_time'),
     )
     return OrderedDict(day=self._get_history_stats_by(qs, 'day'),
                        month=self._get_history_stats_by(qs, 'month'),
                        year=self._get_history_stats_by(qs, 'year'))
Ejemplo n.º 3
0
 def trunc_year(self):
     """
     Truncates date or datetime at a whole year.
     """
     return functions.TruncYear(self._name)