Gitlab CSE Unil

serializers.py 4.25 KB
Newer Older
Julien Furrer's avatar
Julien Furrer committed
1
2
3
# coding=utf-8
from __future__ import unicode_literals
from django.conf import settings
4
from django.db.models import get_model
Julien Furrer's avatar
Julien Furrer committed
5
from rest_framework import serializers
6
from rest_framework.fields import Field
Julien Furrer's avatar
Julien Furrer committed
7
8
9
10
11
12
13
14
from adim.models import AnObj, Annotation
from sorl.thumbnail import get_thumbnail
import json
from django.core.cache import cache


ANOBJ_THUMB_CACHE_BASE_KEY = 'anobj_thumb_url'

15
16
from rest_framework.pagination import PaginationSerializer

Julien Furrer's avatar
Julien Furrer committed
17
18

class UserSerializer(serializers.ModelSerializer):
19
    # full_name = serializers.Field(source='get_full_name')
Julien Furrer's avatar
Julien Furrer committed
20
21

    class Meta:
22
23
        model = get_model(*settings.AUTH_USER_MODEL.split('.'))
        fields = ('id', 'username', 'email')
Julien Furrer's avatar
Julien Furrer committed
24
25
26
27
28
29
30
31
32


class AnnotationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Annotation
        fields = ("id", "owner", "annotable", "item", "comment", "order")
        read_only_fields = ("id",)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class OwnerOnlyField(serializers.WritableField):
    """
    A field that display it's value to the owner only
    """

    def field_to_native(self, obj, field_name):
        if self.context['request'].user == obj.owner:
            return super(OwnerOnlyField, self).field_to_native(obj, field_name)
        else:
            return ""


class BaseAnObjSerializer(serializers.ModelSerializer):
    owner_name = serializers.Field(source='owner.username')
    annotations = serializers.SerializerMethodField('get_annotations')
    # members = serializers.SerializerMethodField('get_members')
    # members = UserSerializer(many=True, read_only=True)
    # sharing_opts = OwnerOnlyField(source='sharing_opts', required=False)

    # attrs = serializers.SerializerMethodField('attrs_as_dict')

    # image_url = serializers.Field(source='image.url')
55
56
    #image_thumb = serializers.Field(source='thumb_url')
    #image_thumb = serializers.SerializerMethodField('get_image_thumb')
Julien Furrer's avatar
Julien Furrer committed
57
58
59

    class Meta:
        model = AnObj
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        fields = ('id', 'uuid', 'name', 'owner', 'owner_name', 'ao_type', 'annotations', 'sharing_mode', 'locked')

    def get_annotations(self, anobj):
        request = self.context.get('request')
        annot_serializer = AnnotationSerializer(instance=anobj.annotations.filter(owner=request.user), many=True,
                                                context=self.context)
        return annot_serializer.data

    # def get_sharing_opts(self, anobj):
    #     request = self.context.get('request')
    #     if request is not None and request.user == anobj.owner:
    #         return anobj.sharing_opts
    #     return ""
    #
    # def get_members(self, anobj):
    #     request = self.context.get('request')
    #     if request is not None and request.user == anobj.owner:
    #         users_serializer = UserSerializer(instance=anobj.members.all(), many=True, context=self.context)
    #         return users_serializer.data
    #     return []
Julien Furrer's avatar
Julien Furrer committed
80

81
82
83
84
85
86
87
88
89
90
    #
    # def get_image_thumb(self, obj):
    #     cache_key = '_'.join((ANOBJ_THUMB_CACHE_BASE_KEY, str(obj.id)))
    #     im_url = cache.get(cache_key)
    #     if im_url is None:
    #         im = get_thumbnail(obj.image.name, '150x150', quality=80, crop="center")
    #         im_url = im.url
    #         cache.set(cache_key, im_url, None)
    #     return im_url
    #
Julien Furrer's avatar
Julien Furrer committed
91
92
93
94
95
96
97
98
99

    def attrs_as_dict(self, obj):
        return None
        # obj_dict = dict((a.schema.name, getattr(obj, a.schema.name)) for a in obj.attrs.all())
        # return json.loads(json.dumps(obj_dict, default=unicode))

    def attrs_as_json(self, obj):
        return json.dumps(self.attrs_as_dict(obj), default=unicode)

100
101
102
103
104
105

class AnObjSerializer(BaseAnObjSerializer):
    members = UserSerializer(many=True, required=False, allow_add_remove=True)  # read_only=True)

    class Meta:
        model = AnObj
106
        fields = ('id', 'uuid', 'name', 'owner', 'owner_name', 'annotations', 'members',
107
                  'sharing_mode', 'sharing_opts', 'locked', 'allow_public_publishing')
108
109
110
111
112
113


class SharedAnObjSerializer(BaseAnObjSerializer):

    class Meta:
        model = AnObj
114
        fields = ('id', 'uuid', 'name', 'owner', 'owner_name', 'annotations', 'locked',
115
                  'sharing_mode', 'allow_public_publishing')
116
117
118
119
120


class AnObjListSerializer(BaseAnObjSerializer):
    class Meta:
        model = AnObj
121
        fields = ('id', 'uuid', 'name', 'owner', 'locked', 'sharing_mode')