Add API for downloading Usage CSV report

This commit is contained in:
Tanner Collin 2022-04-25 01:15:05 +00:00
parent b4a98859e0
commit ca064a7617
2 changed files with 42 additions and 0 deletions

View File

@ -18,8 +18,11 @@ from rest_auth.views import PasswordChangeView, PasswordResetView, PasswordReset
from rest_auth.registration.views import RegisterView
from fuzzywuzzy import fuzz, process
from collections import OrderedDict
from dateutil import relativedelta
import icalendar
import datetime, time
import io
import csv
import requests
@ -937,6 +940,44 @@ class VettingViewSet(Base, List):
return queryset.order_by('-current_start_date')
class UsageViewSet(Base):
permission_classes = [AllowMetadata | IsAdmin]
# TODO: add filtering by device
@action(detail=False, methods=['get'])
def csv(self, request):
usages = models.Usage.objects.order_by('id')
month = self.request.query_params.get('month', None)
if month:
try:
dt = datetime.datetime.strptime(month, '%Y-%m')
dt = utils.TIMEZONE_CALGARY.localize(dt)
except ValueError:
raise exceptions.ValidationError(dict(month='Should be YYYY-MM.'))
usages = usages.filter(
started_at__gte=dt,
started_at__lt=dt + relativedelta.relativedelta(months=1),
)
response = HttpResponse(
content_type='text/csv',
)
response['Content-Disposition'] = 'attachment; filename="usage-{}.csv"'.format(month or 'all')
fieldnames = ['id', 'user__username', 'device', 'started_at', 'finished_at', 'num_seconds']
writer = csv.DictWriter(response, fieldnames=fieldnames)
writer.writeheader()
for u in usages.values(*fieldnames):
u['started_at'] = u['started_at'].astimezone(utils.TIMEZONE_CALGARY)
u['finished_at'] = u['finished_at'].astimezone(utils.TIMEZONE_CALGARY)
writer.writerow(u)
return response
class RegistrationView(RegisterView):
serializer_class = serializers.MyRegisterSerializer

View File

@ -13,6 +13,7 @@ router.register(r'door', views.DoorViewSet, basename='door')
router.register(r'lockout', views.LockoutViewSet, basename='lockout')
router.register(r'cards', views.CardViewSet, basename='card')
router.register(r'stats', views.StatsViewSet, basename='stats')
router.register(r'usage', views.UsageViewSet, basename='usage')
router.register(r'search', views.SearchViewSet, basename='search')
router.register(r'members', views.MemberViewSet, basename='members')
router.register(r'courses', views.CourseViewSet, basename='course')