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

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

6

7 8 9 10 11 12 13 14
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
luxcem's avatar
luxcem committed
15

Okhin's avatar
Okhin committed
16 17 18
    def to_internal_value(self, data):
        return [tag.strip() for tag in data.split(",")]

19

luxcem's avatar
luxcem committed
20
class ArticleSerializer(serializers.ModelSerializer):
21 22 23 24 25 26 27 28 29 30 31 32
    #: 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").
        Can also be a list of tags or an empty list.
        """)

    und_score_up = serializers.IntegerField(
            required=False,
            help_text="This is used to increase the vote count by this value")
    und_score_down = serializers.IntegerField(
            required=False,
            help_text="This is used to decrease the vote count by this value")
33

luxcem's avatar
luxcem committed
34 35
    class Meta:
        model = Article
36
        fields = ('id', 'url', 'title', 'tags', 'extracts', 'status',
37
                  'und_score_up', 'und_score_down')
38 39 40 41

    def create(self, **validated_data):
        article = Article.add_new_url(**validated_data)
        article.save()