Moving to APIViews for more control
This commit is contained in:
parent
f1cc87c037
commit
c42afc4ab5
@ -9,7 +9,7 @@ class Migration(models.Model):
|
||||
"""This is the migration class"""
|
||||
id = models.CharField(max_length=200, unique=True,
|
||||
default=uuid.uuid4, primary_key=True)
|
||||
submit_time = models.DateField('migrtation submitted on')
|
||||
submit_time = models.DateField('migrtation submitted on', default=timezone.now)
|
||||
domain = models.CharField(max_length=200)
|
||||
booked_time = models.DateTimeField('Migration booked for')
|
||||
original_server = models.CharField(max_length=200)
|
||||
@ -30,7 +30,7 @@ class Migration(models.Model):
|
||||
|
||||
def print_missed_migrations(self):
|
||||
now = timezone.now()
|
||||
return now >= self.booked_time
|
||||
return now >= self.booked_time and self.migration_status == 'Booked'
|
||||
print_missed_migrations.admin_order_field = 'booked_time'
|
||||
print_missed_migrations.boolean = True
|
||||
print_missed_migrations.short_description = "Was this migration missed?"
|
||||
|
||||
@ -1,6 +1,13 @@
|
||||
from rest_framework import serializers
|
||||
from .models import Migration
|
||||
|
||||
class MigrationOverView(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Migration
|
||||
fields = ['id',
|
||||
'domain',
|
||||
'booked_time',
|
||||
'migration_status']
|
||||
|
||||
class MigrationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
|
||||
@ -2,9 +2,10 @@ from django.urls import include, path
|
||||
from . import views
|
||||
from rest_framework import routers
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
router.register('migrations', views.MigrationViewSet)
|
||||
# router = routers.DefaultRouter()
|
||||
# router.register('migrations', views.MigrationList)
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls))
|
||||
path('migrations/', views.MigrationList.as_view()),
|
||||
path('migrations/<str:pk>/', views.MigrationDetails.as_view())
|
||||
]
|
||||
|
||||
@ -1,59 +1,86 @@
|
||||
import datetime
|
||||
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.http import HttpResponseRedirect, JsonResponse
|
||||
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
|
||||
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)
|
||||
|
||||
class MigrationViewSet(viewsets.ModelViewSet):
|
||||
''' Class for defining the migration views '''
|
||||
queryset = Migration.objects.all()
|
||||
serializer_class = MigrationSerializer
|
||||
# permission_classes = (AllowAny,)
|
||||
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
|
||||
|
||||
@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)
|
||||
def get(self, request, pk, format=None):
|
||||
migration = self.getObject(pk)
|
||||
print(pk)
|
||||
serializer = MigrationSerializer(migration)
|
||||
return Response(serializer.data)
|
||||
|
||||
@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)
|
||||
# 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 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 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 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)
|
||||
# @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)
|
||||
|
||||
Reference in New Issue
Block a user