101 lines
4.0 KiB
Python
101 lines
4.0 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 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, MigrationOverView
|
|
|
|
|
|
from .models import Migration
|
|
|
|
class MigrationList(APIView):
|
|
'''
|
|
Returns a list of migrations with simpleData
|
|
'''
|
|
def get(self, request, format=None):
|
|
migrations = Migration.objects.all()
|
|
serializer = MigrationOverView(migrations, many=True)
|
|
return Response(serializer.data)
|
|
|
|
def post(self, request, format=None):
|
|
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 Migrtation.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 MigrationViewSet(viewsets.ModelViewSet):
|
|
# ''' Class for defining the migration views '''
|
|
# queryset = Migration.objects.all()
|
|
# serializer_class = MigrationOverView
|
|
# # permission_classes = (AllowAny,)
|
|
|
|
# @action(detail=False, methods=['GET'])
|
|
# def upcoming(self, request):
|
|
# ''' Returns a list of the migrations due today '''
|
|
# queryset = MigrationSerializer(
|
|
# Migration.objects.filter(booked_time=timezone.now(),
|
|
# migration_status="Booked"), many=True)
|
|
# return Response(queryset.data, status=status.HTTP_200_OK)
|
|
|
|
# @action(detail=False, methods=['GET'])
|
|
# def missed(self, request):
|
|
# ''' Returns a list of the missed migrations (Still have the status
|
|
# booked and date is greater then today) '''
|
|
# queryset = MigrationSerializer(
|
|
# Migration.objects.filter(booked_time__gte=timezone.now() + datetime.timedelta(1),
|
|
# migration_status="Booked"), many=True)
|
|
# return Response(queryset.data, status=status.HTTP_200_OK)
|
|
|
|
# @action(detail=False, methods=['GET'])
|
|
# def booked(self, request):
|
|
# ''' Returns a list of the booked migrations '''
|
|
# queryset = MigrationSerializer(Migration.objects.filter(
|
|
# migration_status="Booked",), many=True)
|
|
# return Response(queryset.data, status=status.HTTP_200_OK)
|
|
|
|
# @action(detail=False, methods=['GET'])
|
|
# def completed(self, request):
|
|
# ''' Returns a list of the completed migrations '''
|
|
# queryset = MigrationSerializer(Migration.objects.filter(
|
|
# migration_status="Completed",), many=True)
|
|
# return Response(queryset.data, status=status.HTTP_200_OK)
|
|
|
|
# @action(detail=False, methods=['GET'])
|
|
# def awaitterm(self, request):
|
|
# ''' Returns migrations awaiting termination '''
|
|
# queryset = MigrationSerializer(Migration.objects.filter(
|
|
# migration_status="Waiting Termination", term_date__lte=timezone.now()), many=True)
|
|
# return Response(queryset.data, status=status.HTTP_200_OK)
|