-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathfields.py
More file actions
26 lines (18 loc) · 857 Bytes
/
fields.py
File metadata and controls
26 lines (18 loc) · 857 Bytes
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
from rest_framework_json_api import serializers
from share.util import IDObfuscator
class TypeField(serializers.ReadOnlyField):
"""
Returns the type of a model by getting the model_name from the model's metaclass.
"""
def get_attribute(self, instance):
return instance
def to_representation(self, obj):
return obj._meta.model_name
class ShareIdentityField(serializers.HyperlinkedIdentityField):
def get_object(self, view_name, view_args, view_kwargs):
obfuscated_id = view_kwargs[self.lookup_url_kwarg]
return IDObfuscator.resolve(obfuscated_id)
def get_url(self, obj, view_name, request, format):
obfuscated_id = IDObfuscator.encode(obj)
kwargs = {self.lookup_url_kwarg: obfuscated_id}
return self.reverse(view_name, kwargs=kwargs, request=request, format=format)