aboutsummaryrefslogtreecommitdiff
path: root/apps/notes/serializers.py
blob: f811edd19c3849985abdaef329d43b7d1f5df2bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from rest_framework import serializers
from taggit_serializer.serializers import TagListSerializerField, TaggitSerializer

from .models import Note, Notebook, LuxTag

import six


class NewTagListSerializerField(TagListSerializerField):
    def to_internal_value(self, value):
        if isinstance(value, six.string_types):
            value = [v.strip() for v in value.split(',')]

        if not isinstance(value, list):
            self.fail('not_a_list', input_type=type(value).__name__)

        for s in value:
            if not isinstance(s, six.string_types):
                self.fail('not_a_str')

            self.child.run_validation(s)
        return value


class NoteSerializer(TaggitSerializer, serializers.ModelSerializer):
    tags = NewTagListSerializerField(required=False)

    class Meta:
        model = Note
        fields = ('title', 'body_text', 'body_qjson', 'body_html', 'url', 'notebook', 'tags')


class NotebookSerializer(serializers.HyperlinkedModelSerializer):
    json_absolute_url = serializers.URLField(source='get_absolute_url', read_only=True)
    owner = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        model = Notebook
        fields = ('name', 'color_rgb', 'json_absolute_url', 'owner')


class NoteTagSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = LuxTag
        fields = ('name', 'color_hex')