forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
185 lines (147 loc) · 5.47 KB
/
helpers.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import socket
import string
import functools
import warnings
from cachetools import cached, TTLCache
from collections import OrderedDict
from urllib.parse import urlsplit, urlencode
from PIL import Image
from django.conf import settings
from django.utils.crypto import get_random_string as django_get_random_string
from django.utils.deprecation import RemovedInNextVersionWarning
from django.utils.translation import get_language
def deprecated(message):
'''
This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted
when the function is used.
'''
def wrapper(func):
@functools.wraps(func)
def new_func(*args, **kwargs):
warnings.warn(message, category=RemovedInNextVersionWarning, stacklevel=2)
return func(*args, **kwargs)
return new_func
return wrapper
def extract_form_errors(form):
"""
Extracts Django form errors to a list of error messages.
"""
errors = []
for field in form.errors:
for err in form.errors[field]:
errors.append("%s: %s" % (field, err))
return errors
def get_random_string(length=32, choices=None):
"""
This function creates a random string with a given length.
The strings consist of upper and lower case letters and numbers.
@param length: the length of the randomized string, defaults to 32
@return: a random string containing lower and upper case letters and digits
"""
# Use all letters and numbers in the identifier
if not choices:
choices = string.ascii_letters + string.digits
return django_get_random_string(length=length, allowed_chars=choices)
def query_dict_to_list_of_tuples(query_dict):
"""
This helper function creates a list of tuples with the values
from a QueryDict object. In a QueryDict the same key can have
several values, which is not possible with a typical dict nor a JSON
object. The resulting list will be similar to [(key1, value1), (key2, value2)].
@param query_dict: a QueryDict object
@return: a list of tuples with the same keys and values as in the given QueryDict
"""
list_of_tuples = []
for key in query_dict:
for val in query_dict.getlist(key):
list_of_tuples.append((key, val))
return list_of_tuples
def update_url_params(url, params):
delimiter = "&" if "?" in url else "?"
return url + delimiter + urlencode(params)
def has_same_domain(url1, url2):
uri1 = urlsplit(url1)
uri2 = urlsplit(url2)
return uri1.netloc == uri2.netloc
FILENAME_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-0123456789"
def safe_file_name(name):
safename = "".join(c for c in name if c in FILENAME_CHARS)
if safename[0] == "-":
return "_" + safename[1:80]
return safename[:80]
def resize_image(path, max_size):
image = Image.open(path)
image.thumbnail(max_size, Image.ANTIALIAS)
image.save(path)
def roman_numeral(number):
numbers = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
letters = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"];
roman = ""
for i in range(len(numbers)):
while number >= numbers[i]:
roman += letters[i]
number -= numbers[i]
return roman
def settings_text(key):
def get(name):
if hasattr(settings, name):
return getattr(settings, name)
return None
return get('{}_{}'.format(key, get_language().upper())) or get(key)
@cached(TTLCache(100, ttl=30))
def get_url_ip_address_list(url):
"""
This function takes a full URL as a parameter and returns the IP addresses
of the host as a string.
It will cache results for 30 seconds, so repeated calls return fast
"""
hostname = urlsplit(url).hostname
assert hostname, "Invalid url: no hostname found"
ips = (a[4][0] for a in socket.getaddrinfo(hostname, None, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP))
return tuple(set(ips))
def get_remote_addr(request):
real_ip = request.META.get('HTTP_X_REAL_IP')
if real_ip:
return real_ip
forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if forwarded_for:
return forwarded_for.split(',', 1)[0].strip()
return request.META.get('REMOTE_ADDR')
class Enum(object):
"""
Represents constant enumeration.
Usage:
OPTS = Enum(
('FOO', 1, 'help string for foo'),
('BAR', 2, 'help string for bar'),
)
if OPTS.FOO == test_var:
return OPTS[test_var]
ChoicesField(choices=OPTS.choices)
"""
def __init__(self, *choices):
if len(choices) == 1 and isinstance(choices[0], list):
choices = choices[0]
self._strings = OrderedDict()
self._keys = []
for name, value, string in choices:
assert value not in self._strings, "Multiple choices have same value"
self._strings[value] = string
self._keys.append(name)
setattr(self, name, value)
@property
def choices(self):
return tuple(sorted(self._strings.items()))
def keys(self):
return (x for x in self._keys)
def __getitem__(self, key):
return self._strings[key]
def __str__(self):
s = ["<%s([" % (self.__class__.__name__,)]
for key in self.keys():
val = getattr(self, key)
txt = self[val]
s.append(" (%s, %s, %s)," % (key, val, txt))
s.append("])>")
return '\n'.join(s)