Commit bc2ee4c3 authored by Ristylou Dolar's avatar Ristylou Dolar

Move each viewset to separate file and added new field code for Application model

parent 31e40598
# Generated by Django 2.1.5 on 2019-05-16 16:47
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0002_apiservice_service_token'),
]
operations = [
migrations.AddField(
model_name='application',
name='code',
field=models.CharField(default=1, max_length=300),
preserve_default=False,
),
]
......@@ -6,6 +6,7 @@ class Application(models.Model):
application_no = models.CharField(max_length=250)
name = models.CharField(max_length=200, unique=True)
theme = models.IntegerField()
code = models.CharField(max_length=300)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
deleted_at = models.DateTimeField(null=True, blank=True)
......
......@@ -6,6 +6,7 @@ from django.utils.crypto import get_random_string
class ApplicationSerializer(serializers.ModelSerializer):
class Meta:
model = Application
fields = ('id', 'application_no', 'name', 'theme', 'created_at', 'updated_at', 'deleted_at')
......@@ -53,6 +54,7 @@ class ApplicationSerializer(serializers.ModelSerializer):
return not bool(self._errors)
def create(self, validated_data):
new_application = Application.objects.create(**validated_data)
new_application.application_no = number_generator('APP', new_application.id)
new_application.save()
......
from api.viewsets.applications import ApplicationViewSet
from api.viewsets.services import APIServiceViewSet
from api.viewsets.endpoints import APIEndpointViewSet
from django.urls import path, include
from rest_framework.routers import DefaultRouter, SimpleRouter
from api.views import (
APIServiceViewSet, APIEndpointViewSet, ApplicationViewSet,
APIGatewayList, APIGatewaySlugDetail, APIGatewaySlugModelDetail
)
......
This diff is collapsed.
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.response import Response
from api.models import Application
from api.serializers import ApplicationSerializer
from api.utils import (CustomPagination, BadRequestException,
status_message_response)
class ApplicationViewSet(viewsets.ModelViewSet):
http_method_names = [
'get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'
]
lookup_field = 'pk'
queryset = Application.objects.all()
serializer_class = ApplicationSerializer
pagination_class = CustomPagination
# CREATE Application
def create(self, request, *args, **kwargs):
try:
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
message = status_message_response(
201, 'success',
'New application created', serializer.data
)
return Response(message)
except BadRequestException as e:
message = status_message_response(400, 'failed', str(e), [])
return Response(message, status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
message = status_message_response(
500, 'failed',
'Request was not able to process' + str(e), []
)
return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW LIST of Applications
def list(self, request, *args, **kwargs):
try:
queryset = Application.objects.filter(deleted_at__exact=None)
if not queryset.exists():
message = status_message_response(
200, 'success', 'No records found', []
)
return Response(message)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'List of applications found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(
500, 'failed',
'Request was not able to process' + str(e), [])
return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW application details
def retrieve(self, request, *args, **kwargs):
try:
id = self.kwargs['pk']
queryset = Application.objects.filter(id=id)
serializer = self.get_serializer(queryset, many=True)
if not queryset.exists():
message = status_message_response(404, 'failed',
'No record found', [])
return Response(message, status=status.HTTP_404_NOT_FOUND)
else:
message = status_message_response(
200, 'success',
'Application retrieved', serializer.data
)
return Response(message, status=status.HTTP_200_OK)
except Exception as e:
message = status_message_response(
500, 'failed',
'Request was not able to process' + str(e), []
)
return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# UPDATE Application
def update(self, request, *args, **kwargs):
try:
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data,
partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
instance._prefetched_objects_cache = {}
message = status_message_response(
200, 'success', 'Application updated',
serializer.data
)
return Response(message)
except Exception as e:
message = status_message_response(
500, 'failed',
'Request was not able to process' + str(e), []
)
return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SOFT DELETE / ARCHIVED
def destroy(self, request, *args, **kwargs):
try:
instance = self.get_object()
self.perform_destroy(instance)
message = status_message_response(
200, 'success', 'Application deleted', []
)
return Response(message, status=status.HTTP_200_OK)
except Exception as e:
message = status_message_response(
500, 'failed',
'Request was not able to process' + str(e), []
)
return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# PATCH - RESTORE archived application
def partial_update(self, request, *args, **kwargs):
try:
kwargs['partial'] = True
instance = self.get_object()
instance.deleted_at = None
serializer = self.get_serializer(instance)
message = status_message_response(
200, 'success',
'Archived application restored', serializer.data
)
instance.save()
return Response(message)
except Exception as e:
message = status_message_response(
500, 'failed',
'Request was not able to process' + str(e), []
)
return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# /archived - show list of archived application
@action(methods=["GET"], detail=False)
def archived(self, request, pk=None):
try:
queryset = Application.objects.filter(deleted_at__isnull=False)
if not queryset.exists():
message = status_message_response(
200, 'success',
'No archived applications', []
)
return Response(message)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'Archived applications found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(
500, 'failed',
'Request was not able to process' + str(e), []
)
return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# Show app services
@action(methods=['GET'], detail=True)
def services(self, request, pk):
try:
services = APIService.objects.filter(application=pk)
page = self.paginate_queryset(services)
if page is not None:
serializer = APIServiceSerializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'Application services found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(
500, 'failed',
'Request was not able to process' + str(e), []
)
return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.response import Response
from api.models import APIEndpoint
from api.serializers import APIEndpointSerializer
from api.utils import (APIEndpointFilter, CustomPagination, BadRequestException, status_message_response)
from django_filters.rest_framework import DjangoFilterBackend
class APIEndpointViewSet(viewsets.ModelViewSet):
http_method_names = [
'get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'
]
queryset = APIEndpoint.objects.all().order_by('service')
serializer_class = APIEndpointSerializer
lookup_field = 'pk'
filter_backends = (DjangoFilterBackend, )
filter_class = APIEndpointFilter
pagination_class = CustomPagination
# CREATE Endpoint
def create(self, request, *args, **kwargs):
try:
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
message = status_message_response(201, 'success', 'New endpoint created', serializer.data)
return Response(message)
except BadRequestException as e:
message = status_message_response(400, 'failed', str(e), [])
return Response(message, status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW LIST of endpoints
def list(self, request, *args, **kwargs):
try:
queryset = APIEndpoint.objects.filter(deleted_at__exact=None)
if not queryset.exists():
message = status_message_response(200, 'success', 'No records found', [])
return Response(message)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'List of endpoints found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW endpoint details
def retrieve(self, request, *args, **kwargs):
try:
id = self.kwargs['pk']
queryset = APIEndpoint.objects.filter(id=id)
serializer = self.get_serializer(queryset, many=True)
if not queryset.exists():
message = status_message_response(404, 'failed', 'No record found', [])
return Response(message, status=status.HTTP_404_NOT_FOUND)
else:
message = status_message_response(200, 'success', 'Endpoint retrieved', serializer.data)
return Response(message, status=status.HTTP_200_OK)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# UPDATE endpoint
def update(self, request, *args, **kwargs):
try:
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
instance._prefetched_objects_cache = {}
message = status_message_response(200, 'success', 'Endpoint updated', serializer.data)
return Response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SOFT DELETE / ARCHIVED
def destroy(self, request, *args, **kwargs):
try:
instance = self.get_object()
self.perform_destroy(instance)
message = status_message_response(200, 'success', 'Endpoint deleted', [])
return Response(message, status=status.HTTP_200_OK)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# PATCH - RESTORE archived endpoint
def partial_update(self, request, *args, **kwargs):
try:
kwargs['partial'] = True
instance = self.get_object()
instance.deleted_at = None
serializer = self.get_serializer(instance)
message = status_message_response(200, 'success', 'Archived endpoint restored', serializer.data)
instance.save()
return Response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# /archived - show list of archived endpoints
@action(methods=["GET"], detail=False)
def archived(self, request, pk=None):
try:
queryset = APIEndpoint.objects.filter(deleted_at__isnull=False)
if not queryset.exists():
message = status_message_response(200, 'success', 'No archived endpoints', [])
return Response(message)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'Archived endpoints found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.response import Response
from api.models import APIService
from api.serializers import APIServiceSerializer
from api.utils import (CustomPagination, BadRequestException, status_message_response)
class APIServiceViewSet(viewsets.ModelViewSet):
http_method_names = [
'get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'
]
queryset = APIService.objects.all()
serializer_class = APIServiceSerializer
lookup_field = 'pk'
pagination_class = CustomPagination
# CREATE Service
def create(self, request, *args, **kwargs):
try:
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
message = status_message_response(201, 'success', 'New service created', serializer.data)
return Response(message)
except BadRequestException as e:
message = status_message_response(400, 'failed', str(e), [])
return Response(message, status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW LIST of services
def list(self, request, *args, **kwargs):
try:
queryset = APIService.objects.filter(deleted_at__exact=None)
if not queryset.exists():
message = status_message_response(200, 'success', 'No records found', [])
return Response(message)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'List of services found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW service details
def retrieve(self, request, *args, **kwargs):
try:
id = self.kwargs['pk']
queryset = APIService.objects.filter(id=id)
serializer = self.get_serializer(queryset, many=True)
if not queryset.exists():
message = status_message_response(404, 'failed', 'No record found', [])
return Response(message, status=status.HTTP_404_NOT_FOUND)
else:
message = status_message_response(200, 'success', 'Service retrieved', serializer.data)
return Response(message, status=status.HTTP_200_OK)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# UPDATE service
def update(self, request, *args, **kwargs):
try:
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
instance._prefetched_objects_cache = {}
message = status_message_response(200, 'success', 'Service updated', serializer.data)
return Response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SOFT DELETE / ARCHIVED
def destroy(self, request, *args, **kwargs):
try:
instance = self.get_object()
self.perform_destroy(instance)
message = status_message_response(200, 'success', 'Service deleted', [])
return Response(message, status=status.HTTP_200_OK)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# PATCH - RESTORE archived service
def partial_update(self, request, *args, **kwargs):
try:
kwargs['partial'] = True
instance = self.get_object()
instance.deleted_at = None
serializer = self.get_serializer(instance)
message = status_message_response(200, 'success', 'Archived service restored', serializer.data)
instance.save()
return Response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# /archived - show list of archived services
@action(methods=["GET"], detail=False)
def archived(self, request, pk=None):
try:
queryset = APIService.objects.filter(deleted_at__isnull=False)
if not queryset.exists():
message = status_message_response(200, 'success', 'No archived services', [])
return Response(message)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'Archived services found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# Show service endpoints
@action(methods=['GET'], detail=True)
def endpoints(self, request, pk):
try:
endpoints = APIEndpoint.objects.filter(service=pk)
page = self.paginate_queryset(endpoints)
if page is not None:
serializer = APIEndpointSerializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'Service endpoints found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), [])
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment