serializers.py 1.05 KB
Newer Older
luxcem's avatar
luxcem committed
1 2
from rest_framework import serializers

3
from taggit.models import Tag
luxcem's avatar
luxcem committed
4 5
from rp.models import Article

6

7 8
class TagListSerializer(serializers.ModelSerializer):
    name = serializers.CharField(max_length=200)
luxcem's avatar
luxcem committed
9

10 11 12
    class Meta:
        model = Tag
        fields = ('name', )
Okhin's avatar
Okhin committed
13

14

luxcem's avatar
luxcem committed
15
class ArticleSerializer(serializers.ModelSerializer):
16 17
    #: List of short tags to describe the article (eg. "Privacy", "Copyright")
    tags = TagListSerializer(help_text="""
18 19 20 21 22
        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)
23

luxcem's avatar
luxcem committed
24 25
    class Meta:
        model = Article
26
        fields = ('id', 'url', 'title', 'tags', 'extracts',
27 28
                  'status', 'score', 'priority',
                  'archive', 'quote', 'speak')
29

Okhin's avatar
Okhin committed
30
    def create(self, validated_data):
31
        article = Article.add_new_url(**validated_data)
32 33
        if article is not None:
            article.save()
Okhin's avatar
Okhin committed
34
        return article