Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Jun 7, 2024
1 parent 5a9ff43 commit 3a9cd9c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 56 deletions.
35 changes: 25 additions & 10 deletions apps/geo/mutations.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import graphene

from geo.models import AdminLevel, Region
from geo.schema import AdminLevelType, RegionDetailType
from geo.serializers import AdminLevelGqlSerializer, RegionGqSerializer
from geo.models import Region, AdminLevel
from geo.schema import RegionType, RegionDetailType, AdminLevelType
from geo.serializers import RegionGqSerializer, AdminLevelGqlSerializer

from utils.graphene.error_types import CustomErrorType
from utils.graphene.mutation import GrapheneMutation, generate_input_type_for_serializer

RegionInputType = generate_input_type_for_serializer(
Expand Down Expand Up @@ -56,18 +57,32 @@ def check_permissions(cls, info, **_):
return True # global permission is always True


class PublishRegion(GrapheneMutation):
class PublishRegion(graphene.Mutation):
class Arguments:
data = RegionPublishInputType(required=True)
id = graphene.ID(required=True)
model = Region
serializer_class = PublishRegionGqSerializer
errors = graphene.List(graphene.NonNull(CustomErrorType))
ok = graphene.Boolean()
result = graphene.Field(RegionType)

@classmethod
def check_permissions(cls, *args, **_):
if PP.Permission.UPDATE_PROJECT:
return True
@staticmethod
def mutate(root, info, id):
try:
instance = Region.objects.get(
created_by=info.context.user,
id=id
)
except Region.DoesNotExist:
return PublishRegion(errors=[
dict(
field='nonFieldErrors',
messages="Authorized User can only published the region"
)
], ok=False)
instance.is_published = True
instance.save(update_fields=['is_published'])
# instance.save(update_fields=('is_published'))
return PublishRegion(result=instance, errors=None, ok=True)


class Mutation():
Expand Down
1 change: 1 addition & 0 deletions apps/geo/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,6 @@ def validate(self, data):
return data

def update(self, instance, validated_data):
instance.is_published = True
data = super().update(instance, validated_data)
return data
41 changes: 41 additions & 0 deletions apps/geo/tests/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from utils.graphene.tests import GraphQLTestCase

from geo.models import Region
from geo.factories import RegionFactory


class CreateTestMutation(GraphQLTestCase):
Expand Down Expand Up @@ -64,3 +65,43 @@ def _query_check(minput, **kwargs):
content = _query_check(minput)
self.assertNotIn(non_project_member_user, project.members.all())
self.assertEqual(content['data']['createRegion']['errors'][0]['messages'], "Permission Denied")

def test_publish_region(self):
self.publish_region_query = '''
mutation MyMutation($id:ID!){
publishRegion(id: $id) {
ok
errors
}
}
'''
user = UserFactory.create()
other_user = UserFactory.create()
project = ProjectFactory.create()
region = RegionFactory.create(created_by=user)
project.add_member(user)

def _query_check(**kwargs):
return self.query_check(
self.publish_region_query,
variables={'id': region.id},
# minput=minput,
**kwargs
)
_query_check(assert_for_error=True)

# login with user

self.force_login(user)
content = _query_check()
region.refresh_from_db()
self.assertEqual(content['data']['publishRegion']['errors'], None)
self.assertEqual(region.is_published, True)

# login with other user
self.force_login(other_user)
content = _query_check()
self.assertEqual(
content['data']['publishRegion']['errors'][0]['messages'],
'Authorized User can only published the region'
)
47 changes: 1 addition & 46 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3614,22 +3614,12 @@ type CreateProjectJoin {
result: ProjectJoinRequestType
}

<<<<<<< HEAD
type CreateRegion {
errors: [GenericScalar!]
ok: Boolean
result: RegionDetailType
}

||||||| parent of fd4de667e (implemented region in graphql)
=======
type CreateRegion {
errors: [GenericScalar!]
ok: Boolean
result: RegionType
}

>>>>>>> fd4de667e (implemented region in graphql)
type CreateUnifiedConnector {
errors: [GenericScalar!]
ok: Boolean
Expand Down Expand Up @@ -5007,20 +4997,11 @@ type MissingPredictionReviewType {
}

type Mutation {
<<<<<<< HEAD
createRegion(data: RegionInputType!): CreateRegion
<<<<<<< HEAD
createAdminLevel(data: AdminLevelInputType!): CreateAdminLevel
updateAdminLevel(data: AdminLevelInputType!, id: ID!): UpdateAdminLevel
publishRegion(id: ID!): PublishRegion
organizationCreate(data: OrganizationInputType!): OrganizationCreate
||||||| parent of fd4de667e (implemented region in graphql)
=======
createRegion(data: RegionInputType!): CreateRegion
>>>>>>> fd4de667e (implemented region in graphql)
||||||| parent of b652dd00b (publish the geoarea in the project)
=======
publishRegion(data: RegionPublishInputType!, id: ID!): PublishRegion
>>>>>>> b652dd00b (publish the geoarea in the project)
createAssessmentRegSummaryIssue(data: AssessmentRegistrySummaryIssueCreateInputType!): AssessmentRegistryCreateIssue
fileUpload(data: FileUploadInputType!): UploadFile
genericExportCreate(data: GenericExportCreateInputType!): CreateUserGenericExport
Expand Down Expand Up @@ -5897,34 +5878,13 @@ type RegionDetailType {
adminLevels: [AdminLevelType!]
}

<<<<<<< HEAD
input RegionInputType {
title: String!
code: String!
project: ID!
clientId: String
}

||||||| parent of fd4de667e (implemented region in graphql)
=======
input RegionInputType {
clientId: String
project: Int!
code: String!
title: String!
public: Boolean
isPublished: Boolean
regionalGroups: GenericScalar
keyFigures: GenericScalar
populationData: GenericScalar
mediaSources: GenericScalar
cacheIndex: Int
centroid: String
geoOptions: GenericScalar
createdBy: ID
}

>>>>>>> fd4de667e (implemented region in graphql)
type RegionListType {
results: [RegionType!]
totalCount: Int
Expand Down Expand Up @@ -5954,11 +5914,6 @@ input RegionProjectFilterData {
isTest: Boolean
}

input RegionPublishInputType {
isPublished: Boolean
project: Int!
}

type RegionType {
id: ID!
title: String!
Expand Down

0 comments on commit 3a9cd9c

Please sign in to comment.