Skip to content

Commit

Permalink
Optimize testcases and error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Aug 14, 2024
1 parent 54f6302 commit ee5a0cc
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 144 deletions.
68 changes: 38 additions & 30 deletions apps/geo/mutations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import graphene
from django.utils.translation import gettext

from geo.models import Region, AdminLevel
from geo.schema import RegionType, RegionDetailType, AdminLevelType
Expand Down Expand Up @@ -29,7 +28,7 @@ class Arguments:
@classmethod
def check_permissions(cls, info, **_):
return True # global permission is always true
# NOTE: Region permission is checked using serializers
# NOTE: Project permission is checked using serializers


class CreateAdminLevel(GrapheneMutation):
Expand Down Expand Up @@ -83,19 +82,24 @@ class Arguments:
def mutate(root, info, admin_level_id):
admin_level_qs = AdminLevel.objects.filter(
id=admin_level_id,
region__is_published=False
)
if not admin_level_qs:
return DeleteAdminLevel(errors=[
dict(
field='nonFieldErrors',
messages=gettext(
'You should be Region owner to delete admin level or region is published'
),
)
], ok=False)
admin_level_qs.delete()
return DeleteAdminLevel(result=admin_level_qs, errors=None, ok=True)
).first()

error_data = []
if admin_level_qs is None:
error_data.append('AdminLevel does\'t exist')
elif admin_level_qs.region.created_by != info.context.user:
error_data.append('Region Owner can delete admin level ')
elif admin_level_qs.region.is_published:
error_data.append('Published region can\'t be changed. Please contact')
else:
admin_level_qs.delete()
return DeleteAdminLevel(errors=None, ok=True)
return DeleteAdminLevel(errors=[
dict(
field='nonFieldErrors',
messages=error_data
)
], ok=False)


class PublishRegion(graphene.Mutation):
Expand All @@ -108,28 +112,32 @@ class Arguments:

@staticmethod
def mutate(root, info, id):
try:
instance = Region.objects.get(
created_by=info.context.user,
id=id

instance = Region.objects.filter(
id=id
).first()

error_data = []
if instance is None:
error_data.append('Region does\'t exist')
elif instance.created_by != info.context.user:
error_data.append('Authorized User can only published the region')
else:
instance.is_published = True
instance.save(update_fields=['is_published'])
return PublishRegion(result=instance, errors=None, ok=True)
return PublishRegion(errors=[
dict(
field='nonFieldErrors',
messages=error_data
)
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'])
return PublishRegion(result=instance, errors=None, ok=True)
], ok=False)


class Mutation():
create_region = CreateRegion.Field()
update_region = UpdateRegion.Field()
create_admin_level = CreateAdminLevel.Field()
publish_region = PublishRegion.Field()
create_admin_level = CreateAdminLevel.Field()
update_admin_level = UpdateAdminLevel.Field()
delete_admin_level = DeleteAdminLevel.Field()
2 changes: 1 addition & 1 deletion apps/geo/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_geo_area_queryset_for_project_geo_area_type(queryset=None, defer_fields=
return _queryset


class AdminLevelType(DjangoObjectType):
class AdminLevelType(DjangoObjectType, ClientIdMixin):
class Meta:
model = AdminLevel
only_fields = (
Expand Down
31 changes: 6 additions & 25 deletions apps/geo/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from drf_dynamic_fields import DynamicFieldsMixin

from deep.serializers import (
ProjectPropertySerializerMixin,
RemoveNullFieldsMixin,
TempClientIdMixin,
URLCachedFileField,
Expand Down Expand Up @@ -161,15 +160,13 @@ class RegionGqSerializer(UserResourceSerializer, TempClientIdMixin):
help_text="Project is only used while creating region"
)

client_id = serializers.CharField(required=False)

class Meta:
model = Region
fields = ['title', 'code', 'project', 'client_id']

def validate_project(self, project):
if not project.can_modify(self.context['request'].user):
raise serializers.ValidationError('Permission Denied')
raise serializers.ValidationError('You Don\'t have permission in project')
return project

def validate(self, data):
Expand All @@ -178,7 +175,7 @@ def validate(self, data):
return data

def create(self, validated_data):
project = validated_data.pop('project', None)
project = validated_data.pop('project')
region = super().create(validated_data)
project.regions.add(region)
return region
Expand All @@ -202,13 +199,14 @@ class Meta:
'parent_name_prop',
'geo_shape_file',
'geo_shape_file',
'bounds_file',
]

def validate(self, data):
region = data.get('region', (self.instance and self.instance.region))
if not region.can_modify(self.context['request'].user):
raise serializers.ValidationError('You don\'t have the access to the region or region is published')
if region.created_by != self.context['request'].user:
raise serializers.ValidationError('You don\'t have the access to the region')
if region.is_published:
raise serializers.ValidationError('Published region can\'t be changed. Please contact Admin')
return data

def create(self, validated_data):
Expand All @@ -230,20 +228,3 @@ def update(self, instance, validated_data):
transaction.on_commit(lambda: load_geo_areas.delay(region.id))

return admin_level


class PublishRegionGqSerializer(ProjectPropertySerializerMixin, UserResourceSerializer):

class Meta:
model = Region
fields = ['is_published']

def validate(self, data):
if not self.instance.can_publish(self.context['request'].user):
raise serializers.ValidationError("Authorized User can only published the region")
return data

def update(self, instance, validated_data):
instance.is_published = True
data = super().update(instance, validated_data)
return data
Loading

0 comments on commit ee5a0cc

Please sign in to comment.