-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
53 lines (42 loc) · 1.43 KB
/
utils.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
from django.core.exceptions import ValidationError
from xmppserverui import settings
# Used to save oauth token to session storage
def token_to_int(token):
res = ''
for char in token:
char_code = str(ord(char))
if len(char_code) == 1:
char_code = '00' + char_code
elif len(char_code) == 2:
char_code = '0' + char_code
res += char_code
return int(res)
# Used to give user back
def int_to_token(number):
hash_token = str(number)
if len(hash_token) % 3 == 1:
hash_token = '00' + hash_token
elif len(hash_token) % 3 == 2:
hash_token = '0' + hash_token
token = ''
while len(hash_token) != 0:
char = unichr(int(hash_token[:3]))
token += char
hash_token = hash_token[3:]
return token
def file_size_validator(file_obj):
if file_obj.size > settings.UPLOAD_IMG_MAX_SIZE:
raise ValidationError(
'File "%s" too large. Size should not exceed %s MB.' %
(file_obj.name, settings.UPLOAD_IMG_MAX_SIZE / 1000000),
code='invalid'
)
def file_extension_validator(file_obj):
file_extension = os.path.splitext(file_obj.name)[1]
if file_extension not in [".gif", ".png", ".jpg", ".jpeg"]:
raise ValidationError(
'Invalid file format. Format should be GIF, PNG or JPG.',
code='invalid'
)
return file_extension.split('.')[1]