-
Notifications
You must be signed in to change notification settings - Fork 832
[bug 1128477] Tags API for questions #2360
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,14 @@ | |
| from rest_framework import serializers, viewsets, permissions, filters, status | ||
| from rest_framework.decorators import action | ||
| from rest_framework.response import Response | ||
| from taggit.models import Tag | ||
|
|
||
| from kitsune.products.api import TopicField | ||
| from kitsune.questions.models import ( | ||
| Question, Answer, QuestionMetaData, AlreadyTakenException, | ||
| InvalidUserException, QuestionVote, AnswerVote) | ||
| from kitsune.sumo.api import DateTimeUTCField, OnlyCreatorEdits, GenericAPIException | ||
| from kitsune.tags.utils import add_existing_tag | ||
| from kitsune.users.api import ProfileFKSerializer | ||
|
|
||
|
|
||
|
|
@@ -46,6 +48,15 @@ def restore_object(self, attrs, instance=None): | |
| return obj | ||
|
|
||
|
|
||
| class QuestionTagSerializer(serializers.ModelSerializer): | ||
| question = serializers.PrimaryKeyRelatedField( | ||
| required=False, write_only=True) | ||
|
|
||
| class Meta: | ||
| model = Tag | ||
| fields = ('name', 'slug') | ||
|
|
||
|
|
||
| class QuestionSerializer(serializers.ModelSerializer): | ||
| # Use slugs for product and topic instead of ids. | ||
| product = serializers.SlugRelatedField(required=True, slug_field='slug') | ||
|
|
@@ -57,6 +68,7 @@ class QuestionSerializer(serializers.ModelSerializer): | |
| is_solved = serializers.Field(source='is_solved') | ||
| is_taken = serializers.Field(source='is_taken') | ||
| metadata = QuestionMetaDataSerializer(source='metadata_set', required=False) | ||
| tags = QuestionTagSerializer(source='tags', read_only=True) | ||
| num_votes = serializers.Field(source='num_votes') | ||
| solution = serializers.PrimaryKeyRelatedField(read_only=True) | ||
| taken_by = ProfileFKSerializer(source='taken_by.get_profile', read_only=True) | ||
|
|
@@ -80,6 +92,7 @@ class Meta: | |
| 'last_answer', | ||
| 'locale', | ||
| 'metadata', | ||
| 'tags', | ||
| 'num_answers', | ||
| 'num_votes_past_week', | ||
| 'num_votes', | ||
|
|
@@ -289,6 +302,40 @@ def take(self, request, pk=None): | |
|
|
||
| return Response(status=204) | ||
|
|
||
| @action(methods=['POST'], permission_classes=[permissions.IsAuthenticated]) | ||
| def add_tag(self, request, pk=None): | ||
| question = self.get_object() | ||
|
|
||
| if 'tag' not in request.DATA: | ||
| return Response({'tag': 'This field is required.'}, | ||
| status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| tag = request.DATA['tag'] | ||
|
|
||
| try: | ||
| canonical_name = add_existing_tag(tag, question.tags) | ||
| except Tag.DoesNotExist: | ||
| if request.user.has_perm('taggit.add_tag'): | ||
| question.tags.add(tag) | ||
| canonical_name = tag | ||
| else: | ||
| raise GenericAPIException(403, 'You are not authorized to create new tags.') | ||
|
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. Nice. |
||
|
|
||
| tag = Tag.objects.get(name=canonical_name) | ||
| return Response(QuestionTagSerializer(instance=tag).data) | ||
|
|
||
| @action(methods=['POST', 'DELETE'], permission_classes=[permissions.IsAuthenticated]) | ||
| def remove_tag(self, request, pk=None): | ||
| question = self.get_object() | ||
|
|
||
| if 'tag' not in request.DATA: | ||
| return Response({'tag': 'This field is required.'}, | ||
| status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| tag = request.DATA['tag'] | ||
| question.tags.remove(tag) | ||
| return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
|
||
|
|
||
| class AnswerSerializer(serializers.ModelSerializer): | ||
| created = DateTimeUTCField(read_only=True) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do this, it will give a 50x error if the user doesn't pass a tag. It is better to do something like in
delete_metadatato check that it is the data, and if not, give a 400 bad request error. 500s are bad because the are completely opaque to users.