Compare commits
7 Commits
125fe62da0
...
070a647fdd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
070a647fdd | ||
|
|
ad5dfa5af2 | ||
|
|
eacb6ce3be | ||
|
|
426b7ac11f | ||
|
|
1bc2e87d31 | ||
|
|
c42afc4ab5 | ||
|
|
f1cc87c037 |
@ -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,14 @@
|
|||||||
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',
|
||||||
|
'term_date']
|
||||||
|
|
||||||
class MigrationSerializer(serializers.ModelSerializer):
|
class MigrationSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
@ -2,9 +2,16 @@ 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/booked/', views.MigrationsBooked.as_view()),
|
||||||
|
path('migrations/waitingterm/', views.MigrationsWaitingTerm.as_view()),
|
||||||
|
path('migrations/completed/', views.MigrationsCompleted.as_view()),
|
||||||
|
path('migrations/pending/', views.PendingMigrations.as_view()),
|
||||||
|
path('migrations/missed/', views.MissedMigrations.as_view()),
|
||||||
|
path('migrations/pendingterm/', views.PendingTerm.as_view()),
|
||||||
|
path('migrations/<str:pk>/', views.MigrationDetails.as_view()),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,59 +1,161 @@
|
|||||||
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):
|
def post(self, request, format=None):
|
||||||
''' Class for defining the migration views '''
|
serializer = MigrationSerializer(data=request.data)
|
||||||
queryset = Migration.objects.all()
|
if serializer.is_valid():
|
||||||
serializer_class = MigrationSerializer
|
serializer.save()
|
||||||
# permission_classes = (AllowAny,)
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
@action(detail=False, methods=['GET'])
|
class MigrationDetails(APIView):
|
||||||
def upcoming(self, request):
|
'''
|
||||||
''' Returns a list of the migrations due today '''
|
Returns full migration details
|
||||||
queryset = MigrationSerializer(
|
'''
|
||||||
Migration.objects.filter(booked_time=timezone.now(),
|
def getObject(self, pk):
|
||||||
migration_status="Booked"), many=True)
|
try:
|
||||||
return Response(queryset.data, status=status.HTTP_200_OK)
|
migration = Migration.objects.get(id=pk)
|
||||||
|
return migration
|
||||||
|
except Migration.DoesNotExist:
|
||||||
|
raise Http404
|
||||||
|
|
||||||
@action(detail=False, methods=['GET'])
|
def get(self, request, pk, format=None):
|
||||||
def missed(self, request):
|
migration = self.getObject(pk)
|
||||||
''' Returns a list of the missed migrations (Still have the status
|
serializer = MigrationSerializer(migration)
|
||||||
booked and date is greater then today) '''
|
return Response(serializer.data)
|
||||||
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 put(self, request, pk, format=None):
|
||||||
def booked(self, request):
|
migration = self.getObject(pk)
|
||||||
''' Returns a list of the booked migrations '''
|
serializer = MigrationSerializer(migration, data=request.data)
|
||||||
queryset = MigrationSerializer(Migration.objects.filter(
|
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):
|
||||||
|
migrations = MigrationOverView(Migration.objects.filter(
|
||||||
migration_status="Booked",), many=True)
|
migration_status="Booked",), many=True)
|
||||||
return Response(queryset.data, status=status.HTTP_200_OK)
|
return Response(migrations.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@action(detail=False, methods=['GET'])
|
class MigrationsWaitingTerm(APIView):
|
||||||
def completed(self, request):
|
'''
|
||||||
''' Returns a list of the completed migrations '''
|
Returns list of booked migrations
|
||||||
queryset = MigrationSerializer(Migration.objects.filter(
|
'''
|
||||||
|
def get(self, request, format=None):
|
||||||
|
migrations = MigrationOverView(Migration.objects.filter(
|
||||||
|
migration_status="Waiting Termination",), 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):
|
||||||
|
migrations = MigrationOverView(Migration.objects.filter(
|
||||||
migration_status="Completed",), many=True)
|
migration_status="Completed",), many=True)
|
||||||
return Response(queryset.data, status=status.HTTP_200_OK)
|
return Response(migrations.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@action(detail=False, methods=['GET'])
|
class PendingMigrations(APIView):
|
||||||
def awaitterm(self, request):
|
'''
|
||||||
''' Returns migrations awaiting termination '''
|
Returns list of 'pending' migrations (migrations that are booked for today (maybe before timezone.now))
|
||||||
queryset = MigrationSerializer(Migration.objects.filter(
|
'''
|
||||||
migration_status="Waiting Termination", term_date__lte=timezone.now()), many=True)
|
def get(self, request, format=None):
|
||||||
return Response(queryset.data, status=status.HTTP_200_OK)
|
migrations = Migration.objects.filter(
|
||||||
|
migration_status="Booked", booked_time__lte=timezone.now() + datetime.timedelta(1),
|
||||||
|
booked_time__gte=(timezone.now() + datetime.timedelta(-1))
|
||||||
|
)
|
||||||
|
serializer = MigrationOverView(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):
|
||||||
|
migrations = Migration.objects.filter(
|
||||||
|
booked_time__lte=timezone.now() + datetime.timedelta(-1),
|
||||||
|
migration_status="Booked"
|
||||||
|
)
|
||||||
|
serializer = MigrationOverView(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):
|
||||||
|
migrations = Migration.objects.filter(
|
||||||
|
migration_status="Waiting Termination", term_date__lte=timezone.now() + datetime.timedelta(1)
|
||||||
|
)
|
||||||
|
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 = 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)
|
||||||
|
|||||||
@ -25,7 +25,7 @@ SECRET_KEY = 'cq3daur*kk2+*-)@s%wq1c+pc7xi-c1ig@-%wq)m7pn3+zxbre'
|
|||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
|
|
||||||
ALLOWED_HOSTS = ['10.6.9.42']
|
ALLOWED_HOSTS = ['10.6.9.42', 'benjamyn.love', 'localhost']
|
||||||
|
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
@ -80,9 +80,9 @@ WSGI_APPLICATION = 'migratorapi.wsgi.application'
|
|||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.mysql',
|
'ENGINE': 'django.db.backends.mysql',
|
||||||
'NAME': 'migration',
|
'NAME': 'devmigs',
|
||||||
'USER': 'migration',
|
'USER': 'devmig',
|
||||||
'PASSWORD': 'migration123',
|
'PASSWORD': 'XXgDh1i6w8rqp4BG',
|
||||||
'HOST': '127.0.0.1',
|
'HOST': '127.0.0.1',
|
||||||
'PORT': '3306',
|
'PORT': '3306',
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user