Repository navigation
feat: CRUD API for Taxonomy Tags [FC-0036] #96
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
Changes from 9 commits
f763c0e
6f5c5a1
079f1d7
7750ccd
f09f75d
f9e28da
bdaffeb
b2e5323
dd35747
7126e46
b0e4b26
d86eb91
4b58a47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| """ | ||
| API Serializers for taxonomies | ||
| """ | ||
|
|
||
| from rest_framework import serializers | ||
| from rest_framework.reverse import reverse | ||
|
|
||
|
|
@@ -110,6 +109,7 @@ class Meta: | |
| "value", | ||
| "taxonomy_id", | ||
| "parent_id", | ||
| "external_id", | ||
| "sub_tags_link", | ||
| "children_count", | ||
| ) | ||
|
|
@@ -192,3 +192,67 @@ def get_children_count(self, obj): | |
| Returns the number of child tags of the given tag. | ||
| """ | ||
| return len(obj.sub_tags) | ||
|
|
||
|
|
||
| class TaxonomyTagCreateBodySerializer(serializers.Serializer): # pylint: disable=abstract-method | ||
| """ | ||
| Serializer of the body for the Taxonomy Tags CREATE view | ||
|
yusuf-musleh marked this conversation as resolved.
Resolved
|
||
| """ | ||
|
|
||
| tag = serializers.CharField(required=True) | ||
| parent_tag_value = serializers.CharField( | ||
| source='parent.value', required=False | ||
|
yusuf-musleh marked this conversation as resolved.
Resolved
|
||
| ) | ||
| external_id = serializers.CharField(required=False) | ||
|
|
||
| def validate_parent_tag_value(self, value): | ||
| """ | ||
| Check that the provided parent Tag exists based on the value | ||
| """ | ||
| valid = Tag.objects.filter(value__iexact=value).exists() | ||
| if not valid: | ||
| raise serializers.ValidationError("Invalid `parent_tag_value` provided") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I would put this validation in the Removing the validation method(s) from these three serializers would also make the serializer code much more compact.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After trying it out, it's definitely more concise and having the validations all in one place is cleaner, thanks @pomegranited and @bradenmacdonald for the suggestions! I updated it. b0e4b26 |
||
|
|
||
| return value | ||
|
|
||
|
|
||
| class TaxonomyTagUpdateBodySerializer(serializers.Serializer): # pylint: disable=abstract-method | ||
| """ | ||
| Serializer of the body for the Taxonomy Tags UPDATE view | ||
| """ | ||
|
|
||
| tag = serializers.CharField(source="value", required=True) | ||
| updated_tag_value = serializers.CharField(required=True) | ||
|
|
||
| def validate_tag(self, value): | ||
| """ | ||
| Check that the provided Tag exists based on the value | ||
| """ | ||
|
|
||
| valid = Tag.objects.filter(value__iexact=value).exists() | ||
| if not valid: | ||
| raise serializers.ValidationError("Invalid `tag` provided") | ||
|
|
||
| return value | ||
|
|
||
|
|
||
| class TaxonomyTagDeleteBodySerializer(serializers.Serializer): # pylint: disable=abstract-method | ||
| """ | ||
| Serializer of the body for the Taxonomy Tags DELETE view | ||
| """ | ||
|
|
||
| tags = serializers.ListField( | ||
| child=serializers.CharField(), required=True | ||
| ) | ||
| with_subtags = serializers.BooleanField(required=False) | ||
|
|
||
| def validate_tags(self, value): | ||
| """ | ||
| Check that the provided Tags exists based on the values | ||
| """ | ||
|
|
||
| valid = Tag.objects.filter(value__in=value).count() == len(value) | ||
| if not valid: | ||
| raise serializers.ValidationError("One or more tag in `tags` is invalid") | ||
|
|
||
| return value | ||
Uh oh!
There was an error while loading. Please reload this page.