forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
280 lines (224 loc) · 9.15 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import socket
import string
import functools
import warnings
from cachetools import cached, TTLCache
from collections import OrderedDict
from urllib.parse import parse_qs, parse_qsl, urlencode, urlsplit, urlunparse, urlunsplit, urlparse
from PIL import Image
from typing import Any, Dict, Iterable, List
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 is_ajax(request):
"""
Detect AJAX requests.
Request object method is_ajax() was removed in Django 4.0, this can be used instead.
"""
return request.headers.get('x-requested-with') == 'XMLHttpRequest'
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
# Any is used here because Python's type hints can't ensure that the input
# iterable's 1st and 2nd item match the keys and values of the output dict,
# respectively.
def pairs_to_dict(pairs: Iterable[Iterable[Any]]) -> Dict[Any, List[Any]]:
"""
Transforms the provided key-value-pairs into a dict. Each key may appear
multiple times, which is why the output dict's values are lists. This can
be used to turn the result of `query_dict_to_list_of_tuples` back into a
dict (not a QueryDict).
Example: `[["field_1", "1"], ["field_2", "a"], ["field_2", "b"]]` is
transformed into `{"field_1": ["1"], "field_2": ["a", "b"]}`.
"""
data: Dict[str, List[str]] = {}
for key, value in pairs:
if key in data:
data[key].append(value)
else:
data[key] = [value]
return data
def url_with_query_in_data(url: str, data: dict = {}): # pylint: disable=dangerous-default-value
"""
Take an url with (or without) query parameters and a dictionary of data.
Return url without query parameters and a dictionary with merged values from the query and the data.
"""
scheme, netloc, path, query = urlsplit(url)[:4]
query = dict(parse_qsl(query))
query.update(data)
return urlunsplit((scheme, netloc, path, None, None)), query
def update_url_params(url, params):
delimiter = "&" if "?" in url else "?"
return url + delimiter + urlencode(params)
def remove_query_param_from_url(url, param):
"""
Take an url with (or without) query parameters. Return url without the selected query parameter.
"""
url = urlsplit(url)
query = parse_qs(url.query, keep_blank_values=True)
query.pop(param, None)
return urlunsplit(url._replace(query=urlencode(query, True)))
def build_aplus_url(url: str, user_url: bool = False) -> str:
"""
Enforce that the given URL is a full absolute URL that always uses
the network location from the configured BASE_URL. In some installations, particularly
local docker environments, separate SERVICE_BASE_URL is used to distinguish the
docker internal network addresses from user-facing address (typically localhost).
Optional argument 'user_url' tells which one the caller is interested in.
Takes URL as a parameter, returns (possibly) modified URL.
"""
baseurl = settings.BASE_URL
if not user_url and hasattr(settings, 'SERVICE_BASE_URL'):
baseurl = settings.SERVICE_BASE_URL
parsed = urlparse(url)
baseparsed = urlparse(baseurl)
parsed = parsed._replace(scheme=baseparsed.scheme, netloc=baseparsed.netloc)
return urlunparse(parsed)
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.LANCZOS)
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)): # pylint: disable=consider-using-enumerate
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() or settings.LANGUAGE_CODE).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')
def show_debug_toolbar(request):
"""Return True if the Django Debug Toolbar should be shown on a given page."""
return settings.ENABLE_DJANGO_DEBUG_TOOLBAR and request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS
def format_points(points: int, is_revealed: bool, is_container: bool) -> str:
"""
Formats a number of points to be displayed in the UI. The formatting
depends on two parameters:
`is_revealed`: False if the points are for an exercise whose feedback is
hidden, or if the points are for a module/category that contains at least
one exercise whose feedback is hidden. Otherwise true.
`is_container`: False if the points are for an exercise or a submission.
Otherwise true.
"""
if is_revealed:
return str(points)
if is_container:
return f'{points}+'
return '?'
class Enum:
"""
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 values(self):
return (x for x in self._strings)
def __contains__(self, value):
return value in self._strings
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)