Gitlab CSE Unil

serializers.py 4.41 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
Julien Furrer's avatar
Julien Furrer committed
4
from django.contrib.auth import get_user_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
from adim.models import AnObj, Annotation
8
# from sorl.thumbnail import get_thumbnail
Julien Furrer's avatar
Julien Furrer committed
9
10
11
12
13
14
import json
from django.core.cache import cache


ANOBJ_THUMB_CACHE_BASE_KEY = 'anobj_thumb_url'

Julien Furrer's avatar
Julien Furrer committed
15
#from rest_framework.pagination import PaginationSerializer
16

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:
Julien Furrer's avatar
Julien Furrer committed
22
        model = get_user_model()
23
        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",)


Julien Furrer's avatar
Julien Furrer committed
33
34
35
36
37
38
39
40
41
42
# 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 ""
43
44
45


class BaseAnObjSerializer(serializers.ModelSerializer):
46
47
    #owner_name = serializers.ReadOnlyField(source='owner.username')
    owner_name = serializers.SerializerMethodField()
Julien Furrer's avatar
Julien Furrer committed
48
    annotations = serializers.SerializerMethodField()
49
50
51
52
53
54
55
    # 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')
56
57
    #image_thumb = serializers.Field(source='thumb_url')
    #image_thumb = serializers.SerializerMethodField('get_image_thumb')
Julien Furrer's avatar
Julien Furrer committed
58
59
60

    class Meta:
        model = AnObj
61
        fields = ('id', 'uuid', 'name', 'owners', 'owner', 'owner_name', 'annotations', 'sharing_mode', 'locked')
Julien Furrer's avatar
Julien Furrer committed
62
        read_only_fields = ('members',)
63
64
65
66
67
68
69

    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

70
71
72
    def get_owner_name(self, anobj):
        return anobj.owners.all()[0].username

73
74
75
76
77
78
79
80
81
82
83
84
    # 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
85

86
87
88
89
90
91
92
93
94
95
    #
    # 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
96
97
98
99
100
101
102
103
104

    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)

105
106

class AnObjSerializer(BaseAnObjSerializer):
Julien Furrer's avatar
Julien Furrer committed
107
    members = UserSerializer(many=True, required=False, read_only=True)
108
109
110

    class Meta:
        model = AnObj
111
        fields = ('id', 'uuid', 'name', 'owners', 'owner', 'owner_name', 'annotations', 'members',
112
                  'sharing_mode', 'sharing_opts', 'locked', 'allow_public_publishing')
113
114
115
116
117
118


class SharedAnObjSerializer(BaseAnObjSerializer):

    class Meta:
        model = AnObj
119
        fields = ('id', 'uuid', 'name', 'owners', 'owner', 'owner_name', 'annotations', 'locked',
120
                  'sharing_mode', 'allow_public_publishing')
121
122
123
124
125


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