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"""
|
"""This is the migration class"""
|
||||||
id = models.CharField(max_length=200, unique=True,
|
id = models.CharField(max_length=200, unique=True,
|
||||||
default=uuid.uuid4, primary_key=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)
|
domain = models.CharField(max_length=200)
|
||||||
booked_time = models.DateTimeField('Migration booked for')
|
booked_time = models.DateTimeField('Migration booked for')
|
||||||
original_server = models.CharField(max_length=200)
|
original_server = models.CharField(max_length=200)
|
||||||
@ -30,7 +30,7 @@ class Migration(models.Model):
|
|||||||
|
|
||||||
def print_missed_migrations(self):
|
def print_missed_migrations(self):
|
||||||
now = timezone.now()
|
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.admin_order_field = 'booked_time'
|
||||||
print_missed_migrations.boolean = True
|
print_missed_migrations.boolean = True
|
||||||
print_missed_migrations.short_description = "Was this migration missed?"
|
print_missed_migrations.short_description = "Was this migration missed?"
|
||||||
|
|||||||
@ -1,6 +1,13 @@
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from .models import Migration
|
from .models import Migration
|
||||||
|
|
||||||
|
class MigrationOverView(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Migration
|
||||||
|
fields = ['id',
|
||||||
|
'domain',
|
||||||
|
'booked_time',
|
||||||
|
'migration_status']
|
||||||
|
|
||||||
class MigrationSerializer(serializers.ModelSerializer):
|
class MigrationSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
@ -2,9 +2,10 @@ from django.urls import include, path
|
|||||||
from . import views
|
from . import views
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
|
|
||||||
router = routers.DefaultRouter()
|
# router = routers.DefaultRouter()
|
||||||
router.register('migrations', views.MigrationViewSet)
|
# router.register('migrations', views.MigrationList)
|
||||||
|
|
||||||
urlpatterns = [
|
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
|
import datetime
|
||||||
|
|
||||||
from django.shortcuts import get_object_or_404, render
|
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.views import generic
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from rest_framework import status, viewsets
|
from rest_framework import status, viewsets
|
||||||
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from .serializers import MigrationSerializer
|
from .serializers import MigrationSerializer, MigrationOverView
|
||||||
|
|
||||||
|
|
||||||
from .models import Migration
|
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 MigrationDetails(APIView):
|
||||||
''' Class for defining the migration views '''
|
'''
|
||||||
queryset = Migration.objects.all()
|
Returns full migration details
|
||||||
serializer_class = MigrationSerializer
|
'''
|
||||||
# permission_classes = (AllowAny,)
|
def getObject(self, pk):
|
||||||
|
try:
|
||||||
|
migration = Migration.objects.get(id=pk)
|
||||||
|
return migration
|
||||||
|
except Migrtation.DoesNotExist:
|
||||||
|
raise Http404
|
||||||
|
|
||||||
@action(detail=False, methods=['GET'])
|
def get(self, request, pk, format=None):
|
||||||
def upcoming(self, request):
|
migration = self.getObject(pk)
|
||||||
''' Returns a list of the migrations due today '''
|
print(pk)
|
||||||
queryset = MigrationSerializer(
|
serializer = MigrationSerializer(migration)
|
||||||
Migration.objects.filter(booked_time=timezone.now(),
|
return Response(serializer.data)
|
||||||
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'])
|
# class MigrationViewSet(viewsets.ModelViewSet):
|
||||||
def booked(self, request):
|
# ''' Class for defining the migration views '''
|
||||||
''' Returns a list of the booked migrations '''
|
# queryset = Migration.objects.all()
|
||||||
queryset = MigrationSerializer(Migration.objects.filter(
|
# serializer_class = MigrationOverView
|
||||||
migration_status="Booked",), many=True)
|
# # permission_classes = (AllowAny,)
|
||||||
return Response(queryset.data, status=status.HTTP_200_OK)
|
|
||||||
|
|
||||||
@action(detail=False, methods=['GET'])
|
# @action(detail=False, methods=['GET'])
|
||||||
def completed(self, request):
|
# def upcoming(self, request):
|
||||||
''' Returns a list of the completed migrations '''
|
# ''' Returns a list of the migrations due today '''
|
||||||
queryset = MigrationSerializer(Migration.objects.filter(
|
# queryset = MigrationSerializer(
|
||||||
migration_status="Completed",), many=True)
|
# Migration.objects.filter(booked_time=timezone.now(),
|
||||||
return Response(queryset.data, status=status.HTTP_200_OK)
|
# migration_status="Booked"), many=True)
|
||||||
|
# return Response(queryset.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@action(detail=False, methods=['GET'])
|
# @action(detail=False, methods=['GET'])
|
||||||
def awaitterm(self, request):
|
# def missed(self, request):
|
||||||
''' Returns migrations awaiting termination '''
|
# ''' Returns a list of the missed migrations (Still have the status
|
||||||
queryset = MigrationSerializer(Migration.objects.filter(
|
# booked and date is greater then today) '''
|
||||||
migration_status="Waiting Termination", term_date__lte=timezone.now()), many=True)
|
# queryset = MigrationSerializer(
|
||||||
return Response(queryset.data, status=status.HTTP_200_OK)
|
# 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