-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tagging-articles): tag articles
Users should be able to tag their articles - Modify articles.models.py - Add tag relations serializer - Modify articles.serializers.py - Modify articles.urls.py, correct endpoint /api/tags - Modify articles.views.py [Finishes #162163178]
- Loading branch information
Showing
6 changed files
with
109 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from rest_framework import serializers | ||
|
||
from .models import Tag | ||
|
||
|
||
class TagRelatedField(serializers.RelatedField): | ||
""" | ||
A Special Serializer field for Tags. | ||
If a user tags an article with an non existing tag, that new tag will also be created on article creation | ||
""" | ||
|
||
def get_queryset(self): | ||
return Tag.objects.all() | ||
|
||
def to_internal_value(self, data): | ||
tag, created = Tag.objects.get_or_create(tag=data, slug=data.lower()) | ||
return tag | ||
|
||
def to_representation(self, value): | ||
""" | ||
Return a tag property | ||
""" | ||
return value.tag |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from rest_framework.test import APIClient | ||
from .base_test import BaseTest | ||
from rest_framework import status | ||
|
||
|
||
class TestTagArticle(BaseTest): | ||
def test_create_article_with_tags(self): | ||
reponse = self.client.post( | ||
'/api/articles/', | ||
data={ | ||
"article": { | ||
"title": "How to fight dragons 8", | ||
"description": "Ever wonder jckvlahvhow?", | ||
"body": "You have kenglto believe", | ||
"tagList": ["dragons", "fight"] | ||
} | ||
}, | ||
format='json') | ||
self.assertEqual(reponse.status_code, status.HTTP_201_CREATED) | ||
|
This file contains 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
This file contains 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