示例#1
0
 def test_format_totals_with_param(self):
     entries = [
         {'smurf': Decimal('60.50000'), 'user__first_name': 'Rob', 'user__last_name': 'Lin'},
         {'smurf': Decimal('30.75000'), 'user__first_name': 'Dave', 'user__last_name': 'Roy'},
         {'smurf': Decimal('20.20500'), 'user__first_name': 'Mike', 'user__last_name': 'Jones'},
         ]
     format_totals(entries, 'smurf')
     self.assertEqual(entries[0]['smurf'], "{0:.2f}".format(60.50))
     self.assertEqual(entries[1]['smurf'], "{0:.2f}".format(30.75))
     self.assertEqual(entries[2]['smurf'], "{0:.2f}".format(20.20))
示例#2
0
    def get_context_data(self, **kwargs):
        context = super(ProjectTimesheet, self).get_context_data(**kwargs)
        project = self.object
        year_month_form = YearMonthForm(self.request.GET or None)
        if self.request.GET and year_month_form.is_valid():
            from_date, to_date = year_month_form.save()
        else:
            date = utils.add_timezone(datetime.datetime.today())
            from_date = utils.get_month_start(date).date()
            to_date = from_date + relativedelta(months=1)
        entries_qs = Entry.objects
        entries_qs = entries_qs.timespan(from_date,
                                         span='month').filter(project=project)
        extra_values = ('start_time', 'end_time', 'id', 'project__name')

        month_entries = entries_qs.date_trunc(
            'month', extra_values).order_by('start_time')
        if month_entries:
            format_totals(month_entries, "hours")

        total = entries_qs.aggregate(hours=Sum('hours'))['hours']
        if total:
            total = "{0:.2f}".format(total)
        user_entries = entries_qs.order_by().values('user__first_name',
                                                    'user__last_name')
        user_entries = user_entries.annotate(sum=Sum('hours')).order_by('-sum')
        if user_entries:
            format_totals(user_entries)
##        activity_entries = entries_qs.order_by().values('activity__name')
##        activity_entries = activity_entries.annotate(sum=Sum('hours')).order_by('-sum')
##        if activity_entries:
##            format_totals(activity_entries)

        context.update({
            'project': project,
            'year_month_form': year_month_form,
            'from_date': from_date,
            'to_date': to_date - relativedelta(days=1),
            'entries': month_entries,
            'total': total,
            'user_entries': user_entries,
            #'activity_entries': activity_entries,
        })
        return context
示例#3
0
 def test_format_totals_with_param(self):
     entries = [
         {
             'smurf': Decimal('60.50000'),
             'user__first_name': 'Rob',
             'user__last_name': 'Lin'
         },
         {
             'smurf': Decimal('30.75000'),
             'user__first_name': 'Dave',
             'user__last_name': 'Roy'
         },
         {
             'smurf': Decimal('20.20500'),
             'user__first_name': 'Mike',
             'user__last_name': 'Jones'
         },
     ]
     format_totals(entries, 'smurf')
     self.assertEqual(entries[0]['smurf'], "{0:.2f}".format(60.50))
     self.assertEqual(entries[1]['smurf'], "{0:.2f}".format(30.75))
     self.assertEqual(entries[2]['smurf'], "{0:.2f}".format(20.20))
示例#4
0
    def get_context_data(self, **kwargs):
        context = super(ProjectTimesheet, self).get_context_data(**kwargs)
        project = self.object
        year_month_form = YearMonthForm(self.request.GET or None)
        if self.request.GET and year_month_form.is_valid():
            from_date, to_date = year_month_form.save()
        else:
            date = utils.add_timezone(datetime.datetime.today())
            from_date = utils.get_month_start(date).date()
            to_date = from_date + relativedelta(months=1)
        entries_qs = Entry.objects
        entries_qs = entries_qs.timespan(from_date, span='month').filter(
            project=project
        )
        extra_values = ('start_time', 'end_time', 'comments', 'seconds_paused',
                        'id', 'location__name', 'project__name',
                        'activity__name', 'status')

        month_entries = entries_qs.date_trunc('month', extra_values).order_by('start_time')
        if month_entries:
            format_totals(month_entries, "hours")

        total = entries_qs.aggregate(hours=Sum('hours'))['hours']
        if total:
            total = "{0:.2f}".format(total)
        user_entries = entries_qs.order_by().values('user__first_name', 'user__last_name')
        user_entries = user_entries.annotate(sum=Sum('hours')).order_by('-sum')
        if user_entries:
            format_totals(user_entries)
        activity_entries = entries_qs.order_by().values('activity__name')
        activity_entries = activity_entries.annotate(sum=Sum('hours')).order_by('-sum')
        if activity_entries:
            format_totals(activity_entries)

        context.update({
            'project': project,
            'year_month_form': year_month_form,
            'from_date': from_date,
            'to_date': to_date - relativedelta(days=1),
            'entries': month_entries,
            'total': total,
            'user_entries': user_entries,
            'activity_entries': activity_entries,
        })
        return context