Skip to content

Commit

Permalink
Convert REST API for Analysis creation to GraphQL mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravsapkota committed Jul 24, 2024
1 parent a9d342d commit bbcda71
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
30 changes: 27 additions & 3 deletions apps/analysis/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,6 +19,7 @@
AnalysisReport,
AnalysisReportUpload,
AnalysisReportSnapshot,
Analysis,
)
from .schema import (
get_analysis_pillar_qs,
Expand All @@ -31,6 +34,7 @@
AnalysisReportType,
AnalysisReportUploadType,
AnalysisReportSnapshotType,
AnalysisType,
)
from .serializers import (
AnalysisPillarGqlSerializer,
Expand All @@ -42,6 +46,7 @@
AnalysisReportSerializer,
AnalysisReportSnapshotSerializer,
AnalysisReportUploadSerializer,
AnalysisGqlSerializer,
)


Expand Down Expand Up @@ -84,7 +89,7 @@
)


# Analysi Report
# Analysis Report
AnalysisReportInputType = generate_input_type_for_serializer(
'AnalysisReportInputType',
serializer_class=AnalysisReportSerializer,
Expand All @@ -105,6 +110,11 @@
serializer_class=AnalysisReportUploadSerializer,
)

AnalysisInputType = generate_input_type_for_serializer(
'AnalysisInputType',
serializer_class=AnalysisGqlSerializer,
)


class RequiredPermissionMixin():
permissions = [
Expand Down Expand Up @@ -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()
Expand All @@ -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()
5 changes: 3 additions & 2 deletions apps/analysis/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -420,10 +420,10 @@ class Meta:
'id',
'title',
'team_lead',
'project',
'start_date',
'end_date',
'cloned_from',
'analysis_pillar',
)

def validate_project(self, project):
Expand All @@ -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:
Expand Down

0 comments on commit bbcda71

Please sign in to comment.