Skip to content

Commit eaeaa6f

Browse files
author
João Paulo Dubas
committed
Minor improvements in order_name and serialize methods.
1 parent b612edf commit eaeaa6f

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

fileupload/serialize.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@
55

66

77
def order_name(name):
8-
"""order_name -- Limit the name to 20 chars length, and convert to a
9-
ellipsed string.
8+
"""order_name -- Limit a text to 20 chars length, if necessary strips the
9+
middle of the text and substitute it for an ellipsis.
1010
1111
name -- text to be limited.
1212
1313
"""
1414
name = re.sub(r'^.*/', '', name)
15-
if len(name)>20:
16-
return name[:10] + "..." + name[-7:]
17-
else:
15+
if len(name) <= 20:
1816
return name
17+
return name[:10] + "..." + name[-7:]
1918

2019

21-
def serialize(instance):
22-
"""serialize -- Serialize a Picture instance into a `json` object.
20+
def serialize(instance, file_attr='file'):
21+
"""serialize -- Serialize a Picture instance into a dict.
2322
2423
instance -- Picture instance
24+
file_attr -- attribute name that contains the FileField or ImageField
25+
2526
"""
27+
obj = getattr(instance, file_attr)
2628
return {
27-
'url': instance.file.url,
28-
'name': order_name(instance.file.name),
29-
'type': mimetypes.guess_type(instance.file.path)[0] or 'image/png',
30-
'thumbnailUrl': instance.file.url,
31-
'size': instance.file.size,
29+
'url': obj.url,
30+
'name': order_name(obj.name),
31+
'type': mimetypes.guess_type(obj.path)[0] or 'image/png',
32+
'thumbnailUrl': obj.url,
33+
'size': obj.size,
3234
'deleteUrl': reverse('upload-delete', args=[instance.pk]),
3335
'deleteType': 'DELETE',
3436
}

0 commit comments

Comments
 (0)