Commit 37e81ba1 authored by okhin's avatar okhin 🚴

Merge branch...

Merge branch '33-la-liste-des-tags-lors-de-la-creation-d-un-article-n-est-pas-une-liste-mais-une-chaine-de-caracteres' into 'rp2'

Resolve "La liste des tags lors de la création d'un article n'est pas une liste, mais une chaine de caractères"

Closes #33

See merge request !37
parents 7d8b06e1 b2229054
Pipeline #2593 passed with stages
in 2 minutes and 56 seconds
from rest_framework import serializers from rest_framework import serializers
from taggit.managers import TaggableManager from taggit.models import Tag
from rp.models import Article from rp.models import Article
class TagListSerializer(serializers.Field): class TagListSerializer(serializers.ModelSerializer):
class Meta: name = serializers.CharField(max_length=200)
model = TaggableManager(blank=True)
def to_representation(self, obj):
if type(obj) is not list:
return [tag.name for tag in obj.all()]
return obj
def to_internal_value(self, data): class Meta:
return [tag.strip() for tag in data.split(",")] model = Tag
fields = ('name', )
class ArticleSerializer(serializers.ModelSerializer): class ArticleSerializer(serializers.ModelSerializer):
#: List of short tags to describe the article (eg. "Privacy", "Copyright") #: List of short tags to describe the article (eg. "Privacy", "Copyright")
tags = TagListSerializer(help_text=""" tags = TagListSerializer(help_text="""
List of short tags to describe the article (eg."Privacy, Copyright"). List of short tags to describe the article (eg."Privacy", "Copyright").
Must be a list of tags, coma separated (or an empty string). It must be a valid JSON list of items with a field named name.
""", default="")
For instance [{"name": "Privacy"}, {"name": "Copyright"}]
""", many=True, required=False)
class Meta: class Meta:
model = Article model = Article
......
...@@ -195,7 +195,7 @@ class Article(models.Model): ...@@ -195,7 +195,7 @@ class Article(models.Model):
# Let's add the tags # Let's add the tags
if tags: if tags:
article.tags.add(','.join([t for t in tags if len(t) > 0])) article.tags.add(','.join([t['name'] for t in tags if len(t) > 0]))
try: try:
r = requests.get(url, timeout=0.5) r = requests.get(url, timeout=0.5)
article.original_status = r.status_code article.original_status = r.status_code
......
...@@ -158,7 +158,7 @@ class TestArticleApi(TestCase): ...@@ -158,7 +158,7 @@ class TestArticleApi(TestCase):
{'url': a.url, 'title': a.title, {'url': a.url, 'title': a.title,
'tags': '' 'tags': ''
}) })
assert [t.name for t in a.tags.all()] == r.data['tags'] assert [t.name for t in a.tags.all()] == [t['name'] for t in r.data['tags']]
def test_api_recover(self): def test_api_recover(self):
# Can we recover if we're no Jedis # Can we recover if we're no Jedis
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment