-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathserialize.py
40 lines (30 loc) · 1.06 KB
/
serialize.py
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
# encoding: utf-8
import mimetypes
import re
from django.core.urlresolvers import reverse
def order_name(name):
"""order_name -- Limit a text to 20 chars length, if necessary strips the
middle of the text and substitute it for an ellipsis.
name -- text to be limited.
"""
name = re.sub(r'^.*/', '', name)
if len(name) <= 20:
return name
return name[:10] + "..." + name[-7:]
def serialize(instance, file_attr='file'):
"""serialize -- Serialize a Picture instance into a dict.
instance -- Picture instance
file_attr -- attribute name that contains the FileField or ImageField
"""
obj = getattr(instance, file_attr)
return {
'url': obj.url,
'name': order_name(obj.name),
#'type': mimetypes.guess_type(obj.path)[0] or 'image/png',
'type': mimetypes.guess_type(obj.path)[0] or 'audio/wav',
'thumbnailUrl': obj.url,
'size': obj.size,
'deleteUrl': reverse('upload-delete', args=[instance.pk]),
'deleteId' : instance.pk,
'deleteType': 'DELETE',
}