diff --git a/apps/rp/api/serializers.py b/apps/rp/api/serializers.py index 501ac4584e62bb519fca4934edb60b969c3b8851..ddf4f95924e1278e16c7066513eb3dcd460d769d 100644 --- a/apps/rp/api/serializers.py +++ b/apps/rp/api/serializers.py @@ -1,28 +1,25 @@ from rest_framework import serializers -from taggit.managers import TaggableManager +from taggit.models import Tag from rp.models import Article -class TagListSerializer(serializers.Field): - class Meta: - 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 +class TagListSerializer(serializers.ModelSerializer): + name = serializers.CharField(max_length=200) - def to_internal_value(self, data): - return [tag.strip() for tag in data.split(",")] + class Meta: + model = Tag + fields = ('name', ) class ArticleSerializer(serializers.ModelSerializer): #: List of short tags to describe the article (eg. "Privacy", "Copyright") tags = TagListSerializer(help_text=""" - List of short tags to describe the article (eg."Privacy, Copyright"). - Must be a list of tags, coma separated (or an empty string). - """, default="") + List of short tags to describe the article (eg."Privacy", "Copyright"). + It must be a valid JSON list of items with a field named name. + + For instance [{"name": "Privacy"}, {"name": "Copyright"}] + """, many=True, required=False) class Meta: model = Article diff --git a/apps/rp/models.py b/apps/rp/models.py index bd864af2947f9d593d79ee5c0bb17ea968f53311..efdad3d8973536b68e2d444881ea2f3fca6452e7 100644 --- a/apps/rp/models.py +++ b/apps/rp/models.py @@ -195,7 +195,7 @@ class Article(models.Model): # Let's add the 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: r = requests.get(url, timeout=0.5) article.original_status = r.status_code diff --git a/apps/rp/tests/test_article.py b/apps/rp/tests/test_article.py index 5868b3c639554ea4039faf39847498569172f2f5..8d2d5ca7fdb3183bfaf567600186bc3947be1684 100644 --- a/apps/rp/tests/test_article.py +++ b/apps/rp/tests/test_article.py @@ -158,7 +158,7 @@ class TestArticleApi(TestCase): {'url': a.url, 'title': a.title, '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): # Can we recover if we're no Jedis