Gitlab CSE Unil

serializers.py 4.43 KB
Newer Older
Julien Furrer's avatar
Julien Furrer committed
1
2
# coding=utf-8
from __future__ import unicode_literals
Julien Furrer's avatar
Julien Furrer committed
3
from django.contrib.auth import get_user_model
Julien Furrer's avatar
Julien Furrer committed
4
5
6
7
8
9
10
11
from rest_framework import serializers
from adim.models import AnObj, Annotation
import json

ANOBJ_THUMB_CACHE_BASE_KEY = 'anobj_thumb_url'


class UserSerializer(serializers.ModelSerializer):
12
    # full_name = serializers.Field(source='get_full_name')
Julien Furrer's avatar
Julien Furrer committed
13
14

    class Meta:
Julien Furrer's avatar
Julien Furrer committed
15
        model = get_user_model()
16
        fields = ('id', 'username', 'email')
Julien Furrer's avatar
Julien Furrer committed
17
18
19
20
21
22
23
24
25


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
26
27
28
29
30
31
32
33
34
35
# 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 ""
36
37
38


class BaseAnObjSerializer(serializers.ModelSerializer):
Julien Furrer's avatar
Julien Furrer committed
39
    # owner_name = serializers.ReadOnlyField(source='owner.username')
40
    owner_name = serializers.SerializerMethodField()
Julien Furrer's avatar
Julien Furrer committed
41
    annotations = serializers.SerializerMethodField()
42
43
44
45
46
47
48
    # 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')
Julien Furrer's avatar
Julien Furrer committed
49
50
    # image_thumb = serializers.Field(source='thumb_url')
    # image_thumb = serializers.SerializerMethodField('get_image_thumb')
Julien Furrer's avatar
Julien Furrer committed
51
52
53

    class Meta:
        model = AnObj
54
        fields = ('id', 'uuid', 'name', 'owners', 'owner', 'owner_name', 'annotations', 'sharing_mode', 'locked')
Julien Furrer's avatar
Julien Furrer committed
55
        read_only_fields = ('members',)
56
57
58
59
60
61
62

    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

63
64
65
    def get_owner_name(self, anobj):
        return anobj.owners.all()[0].username

66
67
68
69
70
71
72
73
74
75
76
77
    # 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
78

79
80
81
82
83
84
85
86
87
88
    #
    # 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
89
90
91
92
93
94
95
96
97

    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)

98
99

class AnObjSerializer(BaseAnObjSerializer):
Julien Furrer's avatar
Julien Furrer committed
100
    members = UserSerializer(many=True, required=False, read_only=True)
101
102
103

    class Meta:
        model = AnObj
104
        fields = ('id', 'uuid', 'name', 'owners', 'owner', 'owner_name', 'annotations', 'members',
105
                  'sharing_mode', 'sharing_opts', 'locked', 'allow_public_publishing', 'env')
106
107
108


class SharedAnObjSerializer(BaseAnObjSerializer):
109
    sharing_opts = serializers.SerializerMethodField()
110
111
112

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

    def get_sharing_opts(self, anobj):
        if anobj.sharing_mode > 15:
            return anobj.sharing_opts
        else:
            return None
121
122
123
124
125


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