From cb1f9dfb888b06b41ad4b57b975edb21f9f55310 Mon Sep 17 00:00:00 2001 From: pepper Date: Thu, 21 Jan 2021 01:25:06 -0500 Subject: [PATCH] Changed output format for bookedslots --- migratorapi/api/views.py | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/migratorapi/api/views.py b/migratorapi/api/views.py index c701e2c..b079905 100644 --- a/migratorapi/api/views.py +++ b/migratorapi/api/views.py @@ -37,6 +37,19 @@ def checkDayNumber(days): days = int(days) return days +def getMigCount(day, slot): + return Migration.objects.filter(migration_status="Booked", booked_date=datetime.datetime.now().date() + datetime.timedelta(day), booked_time=slot).count() + +def buildDictFromTimeslot(slot): + slotDict = {} + slotDict["timeslot"] = slot + slotDict["yesterday"] = getMigCount(-1, slot) + slotDict["today"] = getMigCount(0, slot) + slotDict["tomorrow"] = getMigCount(1, slot) + slotDict["2days"] = getMigCount(2, slot) + slotDict["3days"] = getMigCount(3, slot) + return slotDict + def getAllMigrations(limit): return Migration.objects.all()[:limit] @@ -171,31 +184,18 @@ class MigrationListAll(APIView): return Response(serializer.data) class MigrationTimeslotDetails(APIView): + ''' + Returns the number of items booked in a slot + + Takes a noOfDays argument to specify how many days worth of data to grab + ''' def get(self, request): - #TODO: FIX THIS GARBAGE I HATE IT - # 5 days starting day before - Return Array of arrays day = checkDayNumber(request.GET.get('day', '')) - days = checkDayNumber(request.GET.get('noOfDays', '')) - # timeslots = ['00:00-03:00', '03:00-06:00', '06:00-09:00', '08:00-12:00', '12:00-18:00', '18:00-00:00'] timeslots = settings.MIG_TIMESLOTS - if checkValue(request.GET.get('values', '')): - slotArray = [] - if days: - daySlotArray = [] - for daynum in range(days): - for slot in timeslots: - slotArray.append(Migration.objects.filter(migration_status="Booked", booked_date=datetime.datetime.now().date() + datetime.timedelta(daynum -1), booked_time=slot).count()) - daySlotArray.append(slotArray) - slotArray = [] - return Response(daySlotArray) - for slot in timeslots: - slotArray.append(Migration.objects.filter(migration_status="Booked", booked_date=datetime.datetime.now().date() + datetime.timedelta(day), booked_time=slot).count()) - return Response(slotArray) - else: - slotDict = {} - for slot in timeslots: - slotDict[slot] = Migration.objects.filter(migration_status="Booked", booked_date=datetime.datetime.now().date() + datetime.timedelta(day), booked_time=slot).count() - return Response(slotDict) + daySlotArray = [] + for slot in timeslots: + daySlotArray.append(buildDictFromTimeslot(slot)) + return Response(daySlotArray) class getTimeslots(APIView):