From bbcda711e46c3566487290e0a61b7b96ed413a2f Mon Sep 17 00:00:00 2001 From: sauravsapkota Date: Wed, 24 Jul 2024 14:36:39 +0545 Subject: [PATCH] Convert REST API for Analysis creation to GraphQL mutation --- apps/analysis/mutation.py | 30 +++++++++++++++++++++++++++--- apps/analysis/serializers.py | 5 +++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/apps/analysis/mutation.py b/apps/analysis/mutation.py index de5bd03c19..a64b9edc14 100644 --- a/apps/analysis/mutation.py +++ b/apps/analysis/mutation.py @@ -5,8 +5,10 @@ PsGrapheneMutation, PsDeleteMutation, ) -from deep.permissions import ProjectPermissions as PP - +from deep.permissions import ( + ProjectPermissions as PP, + IsProjectMember, +) from .models import ( AnalysisPillar, DiscardedEntry, @@ -17,6 +19,7 @@ AnalysisReport, AnalysisReportUpload, AnalysisReportSnapshot, + Analysis, ) from .schema import ( get_analysis_pillar_qs, @@ -31,6 +34,7 @@ AnalysisReportType, AnalysisReportUploadType, AnalysisReportSnapshotType, + AnalysisType, ) from .serializers import ( AnalysisPillarGqlSerializer, @@ -42,6 +46,7 @@ AnalysisReportSerializer, AnalysisReportSnapshotSerializer, AnalysisReportUploadSerializer, + AnalysisGqlSerializer, ) @@ -84,7 +89,7 @@ ) -# Analysi Report +# Analysis Report AnalysisReportInputType = generate_input_type_for_serializer( 'AnalysisReportInputType', serializer_class=AnalysisReportSerializer, @@ -105,6 +110,11 @@ serializer_class=AnalysisReportUploadSerializer, ) +AnalysisInputType = generate_input_type_for_serializer( + 'AnalysisInputType', + serializer_class=AnalysisGqlSerializer, +) + class RequiredPermissionMixin(): permissions = [ @@ -269,6 +279,15 @@ class Arguments: result = graphene.Field(AnalysisReportUploadType) +class CreateAnalysis(PsGrapheneMutation): + class Arguments: + data = AnalysisInputType(required=True) + model = Analysis + serializer_class = AnalysisGqlSerializer + result = graphene.Field(AnalysisType) + permissions = [IsProjectMember] + + class Mutation(): # Analysis Pillar analysis_pillar_update = UpdateAnalysisPillar.Field() @@ -289,3 +308,8 @@ class Mutation(): # -- Uploads analysis_report_upload_create = CreateAnalysisReportUpload.Field() analysis_report_upload_delete = DeleteAnalysisReportUpload.Field() + # Analysis + analysis_create = CreateAnalysis.Field() + # analysis_update = UpdateAnalysis.Field() + # analysis_delete = DeleteAnalysis.Field() + # analysis_bulk = BulkAnalysis.Field() diff --git a/apps/analysis/serializers.py b/apps/analysis/serializers.py index fbc4a41654..de39bb4ec4 100644 --- a/apps/analysis/serializers.py +++ b/apps/analysis/serializers.py @@ -409,7 +409,7 @@ def validate(self, data): return data -class AnalysisGqlSerializer(UserResourceSerializer): +class AnalysisGqlSerializer(UserResourceSerializer, ProjectPropertySerializerMixin): id = IntegerIDField(required=False) analysis_pillar = AnalysisPillarGqlSerializer(many=True, source='analysispillar_set', required=False) start_date = serializers.DateField(required=False, allow_null=True) @@ -420,10 +420,10 @@ class Meta: 'id', 'title', 'team_lead', - 'project', 'start_date', 'end_date', 'cloned_from', + 'analysis_pillar', ) def validate_project(self, project): @@ -432,6 +432,7 @@ def validate_project(self, project): return project def validate(self, data): + data['project'] = self.project start_date = data.get('start_date') end_date = data.get('end_date') if start_date and start_date > end_date: