Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Graphql mutation for region #1447

Closed
Tracked by #997
sudan45 opened this issue Apr 2, 2024 · 2 comments
Closed
Tracked by #997

Implement Graphql mutation for region #1447

sudan45 opened this issue Apr 2, 2024 · 2 comments
Assignees

Comments

@sudan45
Copy link
Contributor

sudan45 commented Apr 2, 2024

Problem Statement

The ' create region' features uses a Rest API.

Acceptance Criteria

The 'Region' feature works with a graphql mutation.

Additional Information

serializer

class RegionSerializer(RemoveNullFieldsMixin,
DynamicFieldsMixin, UserResourceSerializer):
"""
Region Model Serializer
"""
admin_levels = SimpleAdminLevelSerializer(
many=True,
source='adminlevel_set',
read_only=True,
)
project = serializers.IntegerField(
write_only=True,
required=False,
)
is_published = serializers.BooleanField(read_only=True)
class Meta:
model = Region
exclude = ('geo_options',)
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('Invalid project')
return project.id
def validate(self, data):
if self.instance and self.instance.is_published:
raise serializers.ValidationError('Published region can\'t be changed. Please contact Admin')
return 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)
return region

views

server/apps/geo/views.py

Lines 40 to 50 in 0c1dc33

class RegionViewSet(viewsets.ModelViewSet):
serializer_class = RegionSerializer
permission_classes = [permissions.IsAuthenticated, ModifyPermission]
filter_backends = (django_filters.rest_framework.DjangoFilterBackend,
filters.SearchFilter, filters.OrderingFilter)
filterset_class = RegionFilterSet
search_fields = ('title', 'code')
def get_queryset(self):
return Region.get_for(self.request.user).defer('geo_options')

Rest Framework urls 
method: POST
url: api/v1/regions/
request payload: {"title":"Nepal Region","code":"NPL","project":"3","public":false,"isPublished":false}
response: {
    "count": int,
    "next": null,
    "previous": null,
    "results":{
        "id": ID,
        "modifiedAt": Datetime,
        "modifiedBy": ID,
        "createdByName": str,
        "modifiedByName": str,
        "versionId": ID,
        "adminLevels": [],
        "isPublished": Boolean,
        "createdAt": Datetime,
        "code": str,
        "title": Str,
        "public": boolean,
        "cacheIndex": int,
        "createdBy": Id
}
}
@sudan45 sudan45 self-assigned this Apr 2, 2024
@sudan45
Copy link
Contributor Author

sudan45 commented Apr 3, 2024

PR: #1449

@thenav56
Copy link
Member

thenav56 commented Apr 10, 2024

Hey @sudan45 Can you check if we have already created ticket for publishing geo regions?

Update: Found it

@sudan45 sudan45 closed this as completed Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants