-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
72 lines (59 loc) · 2.48 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from django.contrib.auth.models import User, Group
def prepare_query_params(params):
kwargs = {}
for param in params:
if param == 'type':
kwargs['issue_type'] = params[param][0]
elif param == 'status':
kwargs['status'] = params[param][0]
elif param == 'tag':
kwargs['tags__id'] = params[param][0]
elif param == 'created_by':
kwargs['created_by__pk'] = params[param][0]
return kwargs
def smart_list(value, delimiter=",", func=None):
"""Convert a value to a list, if possible.
Args:
value: the value to be parsed. Ideally a string of comma separated
values - e.g. "1,2,3,4", but could be a list, a tuple, ...
Kwargs:
delimiter: string, the delimiter used to split the value argument,
if it's a string / unicode. Defaults to ','.
func: a function applied to each individual element in the list
once the value arg is split. e.g. lambda x: int(x) would return
a list of integers. Defaults to None - in which case you just
get the list.
Returns: a list if one can be parsed out of the input value. If the
value input is an empty string or None, returns an empty
list. If the split or func parsing fails, raises a ValueError.
This is mainly used for ensuring the CSV model fields are properly
formatted. Use this function in the save() model method and post_init()
model signal to ensure that you always get a list back from the field.
"""
if value in ["", u"", "[]", u"[]", u"[ ]", None]:
return []
if isinstance(value, list):
l = value
elif isinstance(value, tuple):
l = list(value)
elif isinstance(value, basestring) or isinstance(value, unicode):
# TODO: regex this.
value = value.lstrip('[').rstrip(']').strip(' ')
if len(value) == 0:
return []
else:
l = value.split(delimiter)
elif isinstance(value, int):
l = [value]
elif isinstance(value, long):
l = [int(value)]
else:
raise ValueError(u"Unparseable smart_list value: %s" % value)
try:
func = func or (lambda x: x)
return [func(e) for e in l]
except Exception as ex:
raise ValueError(u"Unable to parse value '%s': %s" % (value, ex))
def get_moderators():
moderators = map(str, User.objects.filter(groups__name="moderator").values_list("email", flat=True))
return moderators