-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.py
81 lines (63 loc) · 2.22 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
72
73
74
75
76
77
78
79
80
81
from django.utils.safestring import mark_safe
from social_core.exceptions import AuthForbidden
from django.utils.html import format_html
from wagtail.admin.edit_handlers import EditHandler
def social_for_authed_only(backend, *args, **kwargs):
request = kwargs.get('request')
if not request or not request.user.is_authenticated:
raise AuthForbidden(backend)
def result_pks(response, cast=None):
"""
returns ids from wagtail admin search result
:param cast: cast pks to a type, default int
:param response: webtest response
:return: ids list
"""
cast = cast or int
result_rows = response.lxml.xpath('.//tr[@data-object-pk]/@data-object-pk')
return [
cast(r) for r in result_rows
]
def required_inputs(response):
inputs = response.lxml.xpath('.//*[@required]/@id')
return [
i.replace('id_', '') for i in inputs
]
def disabled_in_admin(func):
"""
returns empty dict instead of actual context processor in admin
"""
def _inner(request):
if request.path.startswith('/admin/'):
return {}
return func(request)
return _inner
def get_messages(r):
messages = [
(m.get('class'), m.xpath('./text()')[1].strip()) for m in r.lxml.xpath(".//div[@class='messages']//li")
]
return messages
class ReadOnlyPanel(EditHandler):
def __init__(self, attr, *args, **kwargs):
self.attr = attr
super().__init__(*args, **kwargs)
def clone(self):
return self.__class__(
attr=self.attr,
heading=self.heading,
classname=self.classname,
help_text=self.help_text,
)
def render(self):
value = getattr(self.instance, self.attr)
if callable(value):
value = value()
return format_html('<div>{}</div>', mark_safe(value))
def render_as_object(self):
return format_html(
'<fieldset><legend>{}</legend>'
'<ul class="fields"><li><div class="field">{}</div></li></ul>'
'</fieldset>',
self.heading, self.render())
def render_as_field(self):
return format_html(f'<label>{self.heading}:</label> <div class="field_content">{self.render()}</div>')