Commit fea7c857 authored by John Red Medrano's avatar John Red Medrano

added group and modules key at application endpoint

parent bc2ee4c3
import requests
from rest_framework import serializers from rest_framework import serializers
from .models import APIService, APIEndpoint, Application from .models import APIService, APIEndpoint, Application
from api.utils import BadRequestException, number_generator from api.utils import BadRequestException, number_generator
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
from django.conf import settings
ACCOUNT_GROUP = settings.ACCOUNT_GROUP
class ApplicationSerializer(serializers.ModelSerializer): class ApplicationSerializer(serializers.ModelSerializer):
groups = serializers.ListField(read_only=True)
modules = serializers.ListField(read_only=True)
def to_representation(self, data):
ids = data.id
req = requests.get(f'{ACCOUNT_GROUP}/{ids}/')
groups = req.json()['groups']
modules = req.json()['modules']
setattr(data, 'groups', groups)
setattr(data, 'modules', modules)
return super().to_representation(data)
class Meta: class Meta:
model = Application model = Application
fields = ('id', 'application_no', 'name', 'theme', 'created_at', 'updated_at', 'deleted_at') fields = ('id', 'application_no', 'name', 'theme', 'groups', 'modules')
read_only_fields = ('id', 'application_no', 'created_at', 'updated_at', 'deleted_at') read_only_fields = (
'id', 'application_no', 'created_at', 'updated_at', 'deleted_at'
)
def is_valid(self, raise_exception=False): def is_valid(self, raise_exception=False):
...@@ -40,21 +58,21 @@ class ApplicationSerializer(serializers.ModelSerializer): ...@@ -40,21 +58,21 @@ class ApplicationSerializer(serializers.ModelSerializer):
if self._errors and raise_exception: if self._errors and raise_exception:
error_message = {} error_message = {}
message = str(self.errors) message = str(self.errors)
for k, v in self.errors.items(): for k, v in self.errors.items():
message = str(v) message = str(v)
start = message.find('string=') + 8 start = message.find('string=') + 8
end = message.find(', code=') - 1 end = message.find(', code=') - 1
message = message[start:end] message = message[start:end]
error_message[str(k)] = message error_message[str(k)] = message
raise BadRequestException(error_message) raise BadRequestException(error_message)
return not bool(self._errors) return not bool(self._errors)
def create(self, validated_data): def create(self, validated_data):
new_application = Application.objects.create(**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() new_application.save()
...@@ -68,7 +86,7 @@ class APIServiceSerializer(serializers.ModelSerializer): ...@@ -68,7 +86,7 @@ class APIServiceSerializer(serializers.ModelSerializer):
read_only_fields = ('id', 'api_service_no', 'service_token', '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): def is_valid(self, raise_exception=False):
assert not hasattr(self, 'restore_object'), ( assert not hasattr(self, 'restore_object'), (
'Serializer `%s.%s` has old-style version 2 `.restore_object()` ' 'Serializer `%s.%s` has old-style version 2 `.restore_object()` '
'that is no longer compatible with REST framework 3. ' 'that is no longer compatible with REST framework 3. '
...@@ -94,19 +112,19 @@ class APIServiceSerializer(serializers.ModelSerializer): ...@@ -94,19 +112,19 @@ class APIServiceSerializer(serializers.ModelSerializer):
if self._errors and raise_exception: if self._errors and raise_exception:
error_message = {} error_message = {}
message = str(self.errors) message = str(self.errors)
for k, v in self.errors.items(): for k, v in self.errors.items():
message = str(v) message = str(v)
start = message.find('string=') + 8 start = message.find('string=') + 8
end = message.find(', code=') - 1 end = message.find(', code=') - 1
message = message[start:end] message = message[start:end]
error_message[str(k)] = message error_message[str(k)] = message
raise BadRequestException(error_message) raise BadRequestException(error_message)
return not bool(self._errors) return not bool(self._errors)
def create(self, validated_data): def create(self, validated_data):
new_service = APIService.objects.create(**validated_data) new_service = APIService.objects.create(**validated_data)
new_service.api_service_no = number_generator('SVC', new_service.id) new_service.api_service_no = number_generator('SVC', new_service.id)
...@@ -127,7 +145,7 @@ class APIEndpointSerializer(serializers.ModelSerializer): ...@@ -127,7 +145,7 @@ class APIEndpointSerializer(serializers.ModelSerializer):
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): def is_valid(self, raise_exception=False):
assert not hasattr(self, 'restore_object'), ( assert not hasattr(self, 'restore_object'), (
'Serializer `%s.%s` has old-style version 2 `.restore_object()` ' 'Serializer `%s.%s` has old-style version 2 `.restore_object()` '
'that is no longer compatible with REST framework 3. ' 'that is no longer compatible with REST framework 3. '
...@@ -153,19 +171,19 @@ class APIEndpointSerializer(serializers.ModelSerializer): ...@@ -153,19 +171,19 @@ class APIEndpointSerializer(serializers.ModelSerializer):
if self._errors and raise_exception: if self._errors and raise_exception:
error_message = {} error_message = {}
message = str(self.errors) message = str(self.errors)
for k, v in self.errors.items(): for k, v in self.errors.items():
message = str(v) message = str(v)
start = message.find('string=') + 8 start = message.find('string=') + 8
end = message.find(', code=') - 1 end = message.find(', code=') - 1
message = message[start:end] message = message[start:end]
error_message[str(k)] = message error_message[str(k)] = message
raise BadRequestException(error_message) raise BadRequestException(error_message)
return not bool(self._errors) return not bool(self._errors)
def create(self, validated_data): def create(self, validated_data):
new_endpoint = APIEndpoint.objects.create(**validated_data) new_endpoint = APIEndpoint.objects.create(**validated_data)
new_endpoint.api_endpoint_no = number_generator('ENP', new_endpoint.id) new_endpoint.api_endpoint_no = number_generator('ENP', new_endpoint.id)
......
...@@ -8,9 +8,9 @@ from api.views import ( ...@@ -8,9 +8,9 @@ from api.views import (
) )
router = DefaultRouter() router = DefaultRouter()
router.register(r'api-applications', ApplicationViewSet) router.register(r'applications', ApplicationViewSet)
router.register(r'api-services', APIServiceViewSet) router.register(r'services', APIServiceViewSet)
router.register(r'api-endpoint', APIEndpointViewSet) router.register(r'endpoint', APIEndpointViewSet)
urlpatterns = [ urlpatterns = [
path('', include(router.urls)), path('', include(router.urls)),
......
import requests
from django.db.models import F
from django.db.models import OuterRef, Subquery
from django.conf import settings
from rest_framework import viewsets, status from rest_framework import viewsets, status
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.response import Response from rest_framework.response import Response
...@@ -7,6 +11,9 @@ from api.utils import (CustomPagination, BadRequestException, ...@@ -7,6 +11,9 @@ from api.utils import (CustomPagination, BadRequestException,
status_message_response) status_message_response)
ACCOUNT_GROUP = settings.ACCOUNT_GROUP
class ApplicationViewSet(viewsets.ModelViewSet): class ApplicationViewSet(viewsets.ModelViewSet):
http_method_names = [ http_method_names = [
'get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace' 'get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'
...@@ -29,7 +36,7 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -29,7 +36,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
'New application created', serializer.data 'New application created', serializer.data
) )
return Response(message) return Response(message)
except BadRequestException as e: except BadRequestException as e:
message = status_message_response(400, 'failed', str(e), []) message = status_message_response(400, 'failed', str(e), [])
return Response(message, status=status.HTTP_400_BAD_REQUEST) return Response(message, status=status.HTTP_400_BAD_REQUEST)
...@@ -46,11 +53,10 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -46,11 +53,10 @@ class ApplicationViewSet(viewsets.ModelViewSet):
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
try: try:
queryset = Application.objects.filter(deleted_at__exact=None) queryset = Application.objects.filter(deleted_at__exact=None)
if not queryset.exists(): if not queryset.exists():
message = status_message_response( message = status_message_response(
200, 'success', 'No records found', [] 200, 'success', 'No records found', []
) )
return Response(message) return Response(message)
page = self.paginate_queryset(queryset) page = self.paginate_queryset(queryset)
...@@ -66,7 +72,7 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -66,7 +72,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
except Exception as e: except Exception as e:
message = status_message_response( message = status_message_response(
500, 'failed', 500, 'failed',
'Request was not able to process' + str(e), []) 'Request was not able to process' + str(e), [])
return Response(message, return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR) status=status.HTTP_500_INTERNAL_SERVER_ERROR)
...@@ -88,10 +94,10 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -88,10 +94,10 @@ class ApplicationViewSet(viewsets.ModelViewSet):
'Application retrieved', serializer.data 'Application retrieved', serializer.data
) )
return Response(message, status=status.HTTP_200_OK) return Response(message, status=status.HTTP_200_OK)
except Exception as e: except Exception as e:
message = status_message_response( message = status_message_response(
500, 'failed', 500, 'failed',
'Request was not able to process' + str(e), [] 'Request was not able to process' + str(e), []
) )
return Response(message, return Response(message,
...@@ -114,7 +120,7 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -114,7 +120,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
serializer.data serializer.data
) )
return Response(message) return Response(message)
except Exception as e: except Exception as e:
message = status_message_response( message = status_message_response(
500, 'failed', 500, 'failed',
...@@ -140,7 +146,7 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -140,7 +146,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
'Request was not able to process' + str(e), [] 'Request was not able to process' + str(e), []
) )
return Response(message, return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR) status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# PATCH - RESTORE archived application # PATCH - RESTORE archived application
...@@ -151,7 +157,7 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -151,7 +157,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
instance.deleted_at = None instance.deleted_at = None
serializer = self.get_serializer(instance) serializer = self.get_serializer(instance)
message = status_message_response( message = status_message_response(
200, 'success', 200, 'success',
'Archived application restored', serializer.data 'Archived application restored', serializer.data
) )
instance.save() instance.save()
...@@ -162,9 +168,9 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -162,9 +168,9 @@ class ApplicationViewSet(viewsets.ModelViewSet):
500, 'failed', 500, 'failed',
'Request was not able to process' + str(e), [] 'Request was not able to process' + str(e), []
) )
return Response(message, return Response(message,
status=status.HTTP_500_INTERNAL_SERVER_ERROR) status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# /archived - show list of archived application # /archived - show list of archived application
@action(methods=["GET"], detail=False) @action(methods=["GET"], detail=False)
......
...@@ -33,3 +33,4 @@ AUTHENTICATOR_IP = config['SERVICE']['AUTHENTICATOR_IP'] ...@@ -33,3 +33,4 @@ AUTHENTICATOR_IP = config['SERVICE']['AUTHENTICATOR_IP']
AUTHENTICATOR_PATH = '/api/v1/authenticator' AUTHENTICATOR_PATH = '/api/v1/authenticator'
VALIDATE_TOKEN_URL = f'http://{AUTHENTICATOR_IP}{AUTHENTICATOR_PATH}/validate-token/' VALIDATE_TOKEN_URL = f'http://{AUTHENTICATOR_IP}{AUTHENTICATOR_PATH}/validate-token/'
ACCOUNT_GROUP = f'http://{AUTHENTICATOR_IP}{AUTHENTICATOR_PATH}/account-dependent'
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