Commit 76dfa862 authored by Ristylou Dolar's avatar Ristylou Dolar

Application theme not required. Set default. Removed extra spaces and lines.

parent 0708dab4
......@@ -5,7 +5,7 @@ from django.utils import timezone
class Application(models.Model):
application_no = models.CharField(max_length=250)
name = models.CharField(max_length=200, unique=True)
theme = models.IntegerField(null=True, blank=True, default=1)
theme = models.IntegerField(blank=True, default=1)
code = models.CharField(max_length=300)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
......
......@@ -46,7 +46,7 @@ class ApplicationSerializer(serializers.ModelSerializer):
else:
self._errors = {}
#if validation failed
# if validation failed
if self._errors and raise_exception:
error_message = {}
message = str(self.errors)
......@@ -66,7 +66,9 @@ class ApplicationSerializer(serializers.ModelSerializer):
def create(self, validated_data):
new_application = Application.objects.create(**validated_data)
new_application.application_no = number_generator('APP', new_application.id)
new_application.application_no = number_generator(
'APP', new_application.id
)
new_application.save()
return new_application
......@@ -74,8 +76,15 @@ class ApplicationSerializer(serializers.ModelSerializer):
class APIServiceSerializer(serializers.ModelSerializer):
class Meta:
model = APIService
fields = ('id', 'api_service_no', 'name', 'service_token', 'base_url', 'service_url', 'application', 'created_at', 'updated_at', 'deleted_at')
read_only_fields = ('id', 'api_service_no', 'service_token', 'created_at', 'updated_at', 'deleted_at')
fields = (
'id', 'api_service_no', 'name', 'service_token', 'base_url',
'service_url', 'application', 'created_at',
'updated_at', 'deleted_at'
)
read_only_fields = (
'id', 'api_service_no', 'service_token', 'created_at',
'updated_at', 'deleted_at'
)
def is_valid(self, raise_exception=False):
......@@ -100,7 +109,7 @@ class APIServiceSerializer(serializers.ModelSerializer):
else:
self._errors = {}
#if validation failed
# if validation failed
if self._errors and raise_exception:
error_message = {}
message = str(self.errors)
......@@ -131,10 +140,13 @@ class APIEndpointSerializer(serializers.ModelSerializer):
class Meta:
model = APIEndpoint
fields = (
'id', 'api_endpoint_no', 'service', 'service_name', 'name', 'description',
'http_method', 'endpoint_url', 'is_need_auth', 'is_active', 'created_at', 'updated_at', 'deleted_at'
'id', 'api_endpoint_no', 'service', 'service_name', 'name',
'description', 'http_method', 'endpoint_url', 'is_need_auth',
'is_active', 'created_at', 'updated_at', 'deleted_at'
)
read_only_fields = (
'id', 'api_endpoint_no', 'created_at', 'updated_at', 'deleted_at'
)
read_only_fields = ('id', 'api_endpoint_no', 'created_at', 'updated_at', 'deleted_at')
def is_valid(self, raise_exception=False):
......@@ -159,7 +171,7 @@ class APIEndpointSerializer(serializers.ModelSerializer):
else:
self._errors = {}
#if validation failed
# if validation failed
if self._errors and raise_exception:
error_message = {}
message = str(self.errors)
......
......@@ -2,7 +2,9 @@ from django.urls import path, include
from rest_framework.routers import DefaultRouter
from api.viewsets.services import APIServiceViewSet
from api.viewsets.endpoints import APIEndpointViewSet
from api.viewsets.applications import ApplicationViewSet, MainApplicationViewSet
from api.viewsets.applications import (
ApplicationViewSet, MainApplicationViewSet
)
from api.views import (
APIGatewayList, APIGatewaySlugDetail, APIGatewaySlugModelDetail
)
......
......@@ -119,9 +119,11 @@ class Helper:
return self._request_method(request, final_endpoint,
self._headers)
else:
return self._response_data({'message': 'Invalid'},
status.HTTP_500_INTERNAL_SERVER_ERROR,
self._headers)
return self._response_data(
{'message': 'Invalid'},
status.HTTP_500_INTERNAL_SERVER_ERROR,
self._headers
)
except APIEndpoint.DoesNotExist:
raise Http404
except Exception as e:
......
......@@ -161,7 +161,6 @@ class ApplicationViewSet(viewsets.ModelViewSet):
return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# PATCH - RESTORE archived application
def partial_update(self, request, *args, **kwargs):
try:
......@@ -184,7 +183,6 @@ class ApplicationViewSet(viewsets.ModelViewSet):
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):
......
......@@ -28,7 +28,10 @@ class APIEndpointViewSet(viewsets.ModelViewSet):
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
message = status_message_response(201, 'success', 'New endpoint created', serializer.data)
message = status_message_response(
201, 'success',
'New endpoint created', serializer.data
)
return Response(message)
except BadRequestException as e:
......@@ -36,8 +39,12 @@ class APIEndpointViewSet(viewsets.ModelViewSet):
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)
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):
......@@ -45,7 +52,9 @@ class APIEndpointViewSet(viewsets.ModelViewSet):
queryset = APIEndpoint.objects.filter(deleted_at__exact=None)
if not queryset.exists():
message = status_message_response(200, 'success', 'No records found', [])
message = status_message_response(
200, 'success', 'No records found', []
)
return Response(message)
page = self.paginate_queryset(queryset)
......@@ -60,8 +69,12 @@ class APIEndpointViewSet(viewsets.ModelViewSet):
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)
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):
......@@ -71,47 +84,69 @@ class APIEndpointViewSet(viewsets.ModelViewSet):
serializer = self.get_serializer(queryset, many=True)
if not queryset.exists():
message = status_message_response(404, 'failed', 'No record found', [])
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)
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)
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 = 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)
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)
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', [])
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)
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):
......@@ -120,14 +155,20 @@ class APIEndpointViewSet(viewsets.ModelViewSet):
instance = self.get_object()
instance.deleted_at = None
serializer = self.get_serializer(instance)
message = status_message_response(200, 'success', 'Archived endpoint restored', serializer.data)
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)
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)
......@@ -136,7 +177,9 @@ class APIEndpointViewSet(viewsets.ModelViewSet):
queryset = APIEndpoint.objects.filter(deleted_at__isnull=False)
if not queryset.exists():
message = status_message_response(200, 'success', 'No archived endpoints', [])
message = status_message_response(
200, 'success', 'No archived endpoints', []
)
return Response(message)
page = self.paginate_queryset(queryset)
......@@ -151,5 +194,9 @@ class APIEndpointViewSet(viewsets.ModelViewSet):
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)
message = status_message_response(
500, 'failed',
'Request was not able to process' + str(e), []
)
return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
......@@ -3,7 +3,9 @@ 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)
from api.utils import (
CustomPagination, BadRequestException, status_message_response
)
class APIServiceViewSet(viewsets.ModelViewSet):
......@@ -22,7 +24,10 @@ class APIServiceViewSet(viewsets.ModelViewSet):
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
message = status_message_response(201, 'success', 'New service created', serializer.data)
message = status_message_response(
201, 'success',
'New service created', serializer.data
)
return Response(message)
except BadRequestException as e:
......@@ -30,8 +35,12 @@ class APIServiceViewSet(viewsets.ModelViewSet):
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)
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):
......@@ -39,7 +48,9 @@ class APIServiceViewSet(viewsets.ModelViewSet):
queryset = APIService.objects.filter(deleted_at__exact=None)
if not queryset.exists():
message = status_message_response(200, 'success', 'No records found', [])
message = status_message_response(
200, 'success', 'No records found', []
)
return Response(message)
page = self.paginate_queryset(queryset)
......@@ -54,8 +65,12 @@ class APIServiceViewSet(viewsets.ModelViewSet):
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)
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):
......@@ -65,33 +80,51 @@ class APIServiceViewSet(viewsets.ModelViewSet):
serializer = self.get_serializer(queryset, many=True)
if not queryset.exists():
message = status_message_response(404, 'failed', 'No record found', [])
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)
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)
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 = 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)
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)
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):
......@@ -99,13 +132,18 @@ class APIServiceViewSet(viewsets.ModelViewSet):
instance = self.get_object()
self.perform_destroy(instance)
message = status_message_response(200, 'success', 'Service deleted', [])
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)
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):
......@@ -114,14 +152,20 @@ class APIServiceViewSet(viewsets.ModelViewSet):
instance = self.get_object()
instance.deleted_at = None
serializer = self.get_serializer(instance)
message = status_message_response(200, 'success', 'Archived service restored', serializer.data)
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)
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)
......@@ -130,7 +174,9 @@ class APIServiceViewSet(viewsets.ModelViewSet):
queryset = APIService.objects.filter(deleted_at__isnull=False)
if not queryset.exists():
message = status_message_response(200, 'success', 'No archived services', [])
message = status_message_response(
200, 'success', 'No archived services', []
)
return Response(message)
page = self.paginate_queryset(queryset)
......@@ -145,8 +191,12 @@ class APIServiceViewSet(viewsets.ModelViewSet):
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)
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)
......@@ -166,5 +216,9 @@ class APIServiceViewSet(viewsets.ModelViewSet):
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)
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