203 lines
7.4 KiB
Python
203 lines
7.4 KiB
Python
import datetime
|
|
|
|
from django.shortcuts import get_object_or_404, render
|
|
from django.http import HttpResponseRedirect, JsonResponse, Http404
|
|
from django.views import generic
|
|
from django.urls import reverse
|
|
from django.utils import timezone
|
|
from django.conf import settings
|
|
from rest_framework import status, viewsets
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
from rest_framework.decorators import action
|
|
from .serializers import MigrationSerializer
|
|
|
|
from pprint import pprint
|
|
|
|
|
|
from .models import Migration
|
|
|
|
def checkLimit(limit):
|
|
if limit == "":
|
|
limit = None
|
|
else:
|
|
limit = int(limit)
|
|
return limit
|
|
|
|
def checkValue(value):
|
|
if value:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def checkDayNumber(days):
|
|
if days == "":
|
|
days = 0
|
|
else:
|
|
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]
|
|
|
|
def getFilteredMigrations(limit, **kwargs):
|
|
pass
|
|
|
|
class MigrationList(APIView):
|
|
'''
|
|
Returns a list of migrations with simpleData
|
|
'''
|
|
permission_classes = []
|
|
def get(self, request, format=None):
|
|
return Response({'booked_count': Migration.objects.filter(migration_status="Booked").count(),
|
|
'pendterm_count': Migration.objects.filter(migration_status="Waiting Termination", term_date=timezone.now()).count(),
|
|
'complete_count': Migration.objects.filter(migration_status="Completed").count(),
|
|
'missed_count': Migration.objects.filter(
|
|
booked_date__lte=datetime.datetime.now().date() + datetime.timedelta(-1),
|
|
migration_status="Booked"
|
|
).count(),
|
|
"upcoming_count":Migration.objects.filter(migration_status="Booked", booked_date=datetime.datetime.now().date()).count()})
|
|
|
|
class BookMigration(APIView):
|
|
def post(self, request, format=None):
|
|
booked_date = request.data["booked_date"]
|
|
timeslot = request.data["booked_time"]
|
|
migs_in_slot = Migration.objects.filter(migration_status="Booked", booked_date=booked_date, booked_time=timeslot).count()
|
|
if migs_in_slot >= settings.MIGS_PER_TIMESLOT:
|
|
return Response({"SlotError": f"More than {settings.MIGS_PER_TIMESLOT} in timeslot {request.data['booked_time']}"}, status=status.HTTP_400_BAD_REQUEST)
|
|
serializer = MigrationSerializer(data=request.data)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
class MigrationDetails(APIView):
|
|
'''
|
|
Returns full migration details
|
|
'''
|
|
def getObject(self, pk):
|
|
try:
|
|
migration = Migration.objects.get(id=pk)
|
|
return migration
|
|
except Migration.DoesNotExist:
|
|
raise Http404
|
|
|
|
def get(self, request, pk, format=None):
|
|
migration = self.getObject(pk)
|
|
serializer = MigrationSerializer(migration)
|
|
return Response(serializer.data)
|
|
|
|
def put(self, request, pk, format=None):
|
|
migration = self.getObject(pk)
|
|
serializer = MigrationSerializer(migration, data=request.data)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
return Response(serializer.data)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
class MigrationsBooked(APIView):
|
|
'''
|
|
Returns list of booked migrations
|
|
'''
|
|
def get(self, request, format=None):
|
|
limit = checkLimit(request.GET.get('limit', ''))
|
|
migrations = MigrationSerializer(Migration.objects.filter(
|
|
migration_status="Booked")[:limit], many=True)
|
|
return Response(migrations.data, status=status.HTTP_200_OK)
|
|
|
|
class MigrationsWaitingTerm(APIView):
|
|
'''
|
|
Returns list of booked migrations
|
|
'''
|
|
def get(self, request, format=None):
|
|
days = checkDayNumber(request.GET.get('days', ''))
|
|
limit = checkLimit(request.GET.get('limit', ''))
|
|
migrations = MigrationSerializer(Migration.objects.filter(migration_status="Waiting Termination", term_date=datetime.datetime.now().date() + datetime.timedelta(days))[:limit], many=True)
|
|
return Response(migrations.data, status=status.HTTP_200_OK)
|
|
|
|
class MigrationsCompleted(APIView):
|
|
'''
|
|
Returns list of booked migrations
|
|
'''
|
|
def get(self, request, format=None):
|
|
limit = checkLimit(request.GET.get('limit', ''))
|
|
migrations = MigrationSerializer(Migration.objects.filter(
|
|
migration_status="Completed",)[:limit], many=True)
|
|
return Response(migrations.data, status=status.HTTP_200_OK)
|
|
|
|
class PendingMigrations(APIView):
|
|
'''
|
|
Returns list of 'pending' migrations (migrations that
|
|
are booked for today (maybe before timezone.now))
|
|
'''
|
|
def get(self, request, format=None):
|
|
days = checkDayNumber(request.GET.get('days', ''))
|
|
limit = checkLimit(request.GET.get('limit', ''))
|
|
migrations = Migration.objects.filter(
|
|
migration_status="Booked", booked_date=datetime.datetime.now().date() + datetime.timedelta(days)
|
|
)[:limit]
|
|
serializer = MigrationSerializer(migrations, many=True)
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
|
|
class MissedMigrations(APIView):
|
|
'''
|
|
Returns a list of missed migrations
|
|
'''
|
|
def get(self, request, format=None):
|
|
limit = checkLimit(request.GET.get('limit', ''))
|
|
migrations = Migration.objects.filter(
|
|
booked_date__lte=datetime.datetime.now().date() + datetime.timedelta(-1),
|
|
migration_status="Booked"
|
|
)[:limit]
|
|
serializer = MigrationSerializer(migrations, many=True)
|
|
return Response(serializer.data)
|
|
|
|
class PendingTerm(APIView):
|
|
'''
|
|
Returns a list of accounts pending termination (might add an arg so we can specify how many days)
|
|
'''
|
|
def get(self, request, format=None):
|
|
limit = checkLimit(request.GET.get('limit', ''))
|
|
migrations = Migration.objects.filter(
|
|
migration_status="Waiting Termination")[:limit]
|
|
serializer = MigrationSerializer(migrations, many=True)
|
|
return Response(serializer.data)
|
|
|
|
class MigrationListAll(APIView):
|
|
def get(self, request, format=None):
|
|
limit = checkLimit(request.GET.get('limit', ''))
|
|
migrations = getAllMigrations(limit)
|
|
serializer = MigrationSerializer(migrations, many=True)
|
|
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):
|
|
day = checkDayNumber(request.GET.get('day', ''))
|
|
timeslots = settings.MIG_TIMESLOTS
|
|
daySlotArray = []
|
|
for slot in timeslots:
|
|
daySlotArray.append(buildDictFromTimeslot(slot))
|
|
return Response(daySlotArray)
|
|
|
|
|
|
class getTimeslots(APIView):
|
|
def get(self, request):
|
|
return Response(settings.MIG_TIMESLOTS) |