diff --git a/apps/geo/serializers.py b/apps/geo/serializers.py index 8104928a02..a3425547a3 100644 --- a/apps/geo/serializers.py +++ b/apps/geo/serializers.py @@ -6,7 +6,6 @@ ProjectPropertySerializerMixin, RemoveNullFieldsMixin, URLCachedFileField, - IntegerIDField, ) from rest_framework import serializers from user_resource.serializers import UserResourceSerializer @@ -156,23 +155,17 @@ class Meta: class RegionGqSerializer(ProjectPropertySerializerMixin, UserResourceSerializer): - project = IntegerIDField() + project = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all()) + client_id = serializers.CharField(required=False) class Meta: model = Region - exclude = ('geo_options', 'is_published') + fields = ['title', 'code', 'project', 'client_id'] def validate_project(self, project): - try: - project = Project.objects.get(id=project) - except Project.DoesNotExist: - raise serializers.ValidationError( - 'Project matching query does not exist' - ) - if not project.can_modify(self.context['request'].user): raise serializers.ValidationError('Permission Denied') - return project.id + return project def validate(self, data): if self.instance and self.instance.is_published: @@ -182,9 +175,5 @@ def validate(self, data): def create(self, validated_data): project = validated_data.pop('project', None) region = super().create(validated_data) - - if project: - project = Project.objects.get(id=project) - project.regions.add(region) - + project.regions.add(region) return region diff --git a/apps/geo/tests/test_mutations.py b/apps/geo/tests/test_mutations.py index 0361954d96..4bc8a27b4a 100644 --- a/apps/geo/tests/test_mutations.py +++ b/apps/geo/tests/test_mutations.py @@ -22,7 +22,6 @@ def test_create_region_in_project(self): user = UserFactory.create() user2 = UserFactory.create() project = ProjectFactory.create( - title="dummy project 1", created_by=user ) project.add_member(user) @@ -34,11 +33,11 @@ def _query_check(minput, **kwargs): **kwargs ) - minput = dict( - project=project.id, - code="NPL", - title="Test", - ) + minput = { + 'project': project.id, + 'code': 'NPL', + 'title': 'Test' + } # without login _query_check(minput, assert_for_error=True) diff --git a/schema.graphql b/schema.graphql index 3ba73f15a4..5120887fde 100644 --- a/schema.graphql +++ b/schema.graphql @@ -5760,18 +5760,10 @@ type RegionDetailType { } input RegionInputType { - clientId: String - project: ID! - code: String! title: String! - public: Boolean - regionalGroups: GenericScalar - keyFigures: GenericScalar - populationData: GenericScalar - mediaSources: GenericScalar - cacheIndex: Int - centroid: String - createdBy: ID + code: String! + project: ID! + clientId: String } type RegionListType {