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