def CSVReport(request, year, month): response = HttpResponse(mimetype='text/csv') response['Content-Disposition'] = 'attachment; filename="report.csv"' writer = csv.writer(response) writer.writerow(['Username', 'Full name', 'Account type', 'Time used', 'Daypasses used']) users = HiveUser.objects.all() for user in users: credit_month = user.credit_set.filter(datetime__year=year, datetime__month=month, units__lte=0) m_set = credit_month.filter(unit_type='M').aggregate(total=Sum('units')) d_set = credit_month.filter(unit_type='D').aggregate(total=Sum('units')) if m_set['total']: m_used = int(m_set['total'] * -1) else: m_used = 0 if d_set['total']: d_used = int(d_set['total'] * -1) else: d_used = 0 writer.writerow([user, user.get_display_name(), user.account, min_to_hours(m_used), d_used]) return response
def format_credit_minutes(self): return min_to_hours(self.get_credit_minutes())