Skip to content

Commit

Permalink
[Fix] fix swagger generated doc with service request
Browse files Browse the repository at this point in the history
+ remove get api endpoint for /service/<id>/request
  • Loading branch information
Sispheor committed Dec 20, 2021
1 parent 7c2827d commit 92614c4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 37 deletions.
4 changes: 3 additions & 1 deletion service_catalog/api/operation_api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response

from service_catalog.models import Service, OperationType
from service_catalog.models import Service, OperationType, Operation
from service_catalog.serializers.operation_serializers import OperationSerializer, AdminOperationSerializer


Expand All @@ -29,6 +29,8 @@ def create(self, request, *args, **kwargs):
class OperationDetails(RetrieveUpdateDestroyAPIView):
def get_queryset(self):
service_id = self.kwargs.get('service_id', None)
if service_id is None:
return Service.objects.none()
queryset = Service.objects.get(id=service_id).operations.all()
return queryset

Expand Down
18 changes: 5 additions & 13 deletions service_catalog/api/request_api_views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from guardian.shortcuts import get_objects_for_user
from rest_framework import status
from rest_framework.generics import ListAPIView, CreateAPIView, RetrieveUpdateDestroyAPIView, get_object_or_404, \
ListCreateAPIView
from rest_framework.generics import ListAPIView, CreateAPIView, RetrieveUpdateDestroyAPIView, get_object_or_404
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response

from service_catalog.models import RequestState, OperationType
from service_catalog.models import RequestState, Service
from service_catalog.serializers.request_serializers import RequestSerializer, AdminRequestSerializer, \
OperationRequestSerializer, ServiceRequestSerializer

Expand Down Expand Up @@ -49,14 +48,7 @@ class OperationRequestCreate(CreateAPIView):
serializer_class = OperationRequestSerializer


class ServiceRequestListCreate(ListCreateAPIView):
def get_queryset(self):
return get_objects_for_user(self.request.user, 'service_catalog.view_request').filter(
instance__service_id=self.kwargs.get('pk', None), operation__type=OperationType.CREATE)

def get_serializer_class(self):
if self.request.method in ["POST"]:
return ServiceRequestSerializer
return RequestSerializer

class ServiceRequestCreate(CreateAPIView):
permission_classes = [IsAuthenticated]
serializer_class = ServiceRequestSerializer
queryset = Service.objects.all()
4 changes: 2 additions & 2 deletions service_catalog/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
TowerServerJobTemplateList
from service_catalog.api.admin.tower_server_api_views import TowerServerList, TowerServerDetails
from service_catalog.api.operation_api_views import OperationListCreate, OperationDetails, InstanceOperationList
from service_catalog.api.request_api_views import RequestList, RequestDetails, ServiceRequestListCreate, \
from service_catalog.api.request_api_views import RequestList, RequestDetails, ServiceRequestCreate, \
OperationRequestCreate
from service_catalog.api.service_api_views import ServiceListCreate, ServiceDetails

Expand All @@ -25,7 +25,7 @@
path('service/<int:pk>/', ServiceDetails.as_view(), name='api_service_details'),
path('service/<int:service_id>/operation/', OperationListCreate.as_view(), name='api_operation_list_create'),
path('service/<int:service_id>/operation/<int:pk>/', OperationDetails.as_view(), name='api_operation_details'),
path('service/<int:pk>/request/', ServiceRequestListCreate.as_view(), name='api_service_request_list_create'),
path('service/<int:pk>/request/', ServiceRequestCreate.as_view(), name='api_service_request_create'),
path('tower/', TowerServerList.as_view(), name='api_tower_server_list_create'),
path('tower/<int:pk>/', TowerServerDetails.as_view(), name='api_tower_server_details'),
path('tower/<int:tower_server_id>/job_template/', TowerServerJobTemplateList.as_view(), name='api_tower_server_job_template_list'),
Expand Down
5 changes: 2 additions & 3 deletions service_catalog/serializers/dynamic_survey_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _get_fields_from_survey(self):
elif survey_filed["type"] == "integer":
fields[survey_filed['variable']] = IntegerField(
label=survey_filed['question_name'],
initial=int(survey_filed['default']),
initial=0 if not survey_filed['default'] else int(survey_filed['default']),
required=survey_filed['required'],
help_text=survey_filed['question_description'],
min_value=survey_filed['min'],
Expand All @@ -73,11 +73,10 @@ def _get_fields_from_survey(self):
elif survey_filed["type"] == "float":
fields[survey_filed['variable']] = FloatField(
label=survey_filed['question_name'],
initial=float(survey_filed['default']),
initial=0 if not survey_filed['default'] else float(survey_filed['default']),
required=survey_filed['required'],
help_text=survey_filed['question_description'],
min_value=survey_filed['min'],
max_value=survey_filed['max'],
)
return fields

33 changes: 17 additions & 16 deletions service_catalog/serializers/request_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ def __init__(self, *args, **kwargs):
self.service_id = self.view.kwargs.get('pk', None)
self.request = context.get('request', None)
super(ServiceRequestSerializer, self).__init__(*args, **kwargs)
self.service = get_object_or_404(Service.objects.filter(enabled=True), id=self.service_id)
# get the create operation of this service
self.create_operation = Operation.objects.get(service=self.service, type=OperationType.CREATE)
# get all field that are not disabled by the admin
purged_survey = FormUtils.get_available_fields(job_template_survey=self.create_operation.job_template.survey,
operation_survey=self.create_operation.enabled_survey_fields)
self.fields['fill_in_survey'] = DynamicSurveySerializer(fill_in_survey=purged_survey)
if self.service_id is not None:
self.service = get_object_or_404(Service.objects.filter(enabled=True), id=self.service_id)
# get the create operation of this service
self.create_operation = Operation.objects.get(service=self.service, type=OperationType.CREATE)
# get all field that are not disabled by the admin
purged_survey = FormUtils.get_available_fields(job_template_survey=self.create_operation.job_template.survey,
operation_survey=self.create_operation.enabled_survey_fields)
self.fields['fill_in_survey'] = DynamicSurveySerializer(fill_in_survey=purged_survey)

def validate_billing_group(self, value):
if not self.service.billing_group_is_selectable:
Expand Down Expand Up @@ -78,15 +79,15 @@ def __init__(self, *args, **kwargs):
operation_id = self.view.kwargs.get('operation_id', None)
instance_id = self.view.kwargs.get('instance_id', None)
super(OperationRequestSerializer, self).__init__(*args, **kwargs)

self.target_operation = get_object_or_404(Operation.objects.exclude(type=OperationType.CREATE), id=operation_id)
self.target_instance = get_object_or_404(
get_objects_for_user(self.request.user, 'service_catalog.view_instance'), id=instance_id)

# get all field that are not disabled by the admin
purged_survey = FormUtils.get_available_fields(job_template_survey=self.target_operation.job_template.survey,
operation_survey=self.target_operation.enabled_survey_fields)
self.fields['fill_in_survey'] = DynamicSurveySerializer(fill_in_survey=purged_survey)
if operation_id is not None and instance_id is not None:
self.target_operation = get_object_or_404(Operation.objects.exclude(type=OperationType.CREATE), id=operation_id)
self.target_instance = get_object_or_404(
get_objects_for_user(self.request.user, 'service_catalog.view_instance'), id=instance_id)

# get all field that are not disabled by the admin
purged_survey = FormUtils.get_available_fields(job_template_survey=self.target_operation.job_template.survey,
operation_survey=self.target_operation.enabled_survey_fields)
self.fields['fill_in_survey'] = DynamicSurveySerializer(fill_in_survey=purged_survey)

def save(self, **kwargs):
new_request = Request.objects.create(instance=self.target_instance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setUp(self):
self.kwargs = {
"pk": self.service_test.id,
}
self.url = reverse('api_service_request_list_create', kwargs=self.kwargs)
self.url = reverse('api_service_request_create', kwargs=self.kwargs)
self.data = {
'instance_name': 'instance test',
'billing_group': None,
Expand Down Expand Up @@ -49,7 +49,7 @@ def test_cannot_create_when_logout(self):

def test_cannot_create_on_non_existing_service(self):
self.kwargs['pk'] = 9999999
self.url = reverse('api_service_request_list_create', kwargs=self.kwargs)
self.url = reverse('api_service_request_create', kwargs=self.kwargs)
self._check_create(status.HTTP_404_NOT_FOUND)

def test_cannot_create_with_non_own_billing_group(self):
Expand Down

0 comments on commit 92614c4

Please sign in to comment.