|
1 | | -from django.forms import HiddenInput |
2 | | -from wagtail import VERSION as WAGTAIL_VERSION |
3 | | - |
4 | | - |
5 | | -if WAGTAIL_VERSION >= (6, 0): |
6 | | - from django.forms import Media |
7 | | - from django.utils.safestring import mark_safe |
8 | | - |
9 | | - class ReadonlyUUIDInput(HiddenInput): |
10 | | - """ |
11 | | - This isn't really read-only. It's a hidden input with an an adjacent div |
12 | | - showing the current value; that way we can set the value in JavaScript, but |
13 | | - the user can't easily change it. |
14 | | - """ |
15 | | - |
16 | | - def render(self, name, value, attrs=None, renderer=None): |
17 | | - # no point trying to come up with sensible semantics for when 'id' is missing from attrs, |
18 | | - # so let's make sure it fails early in the process |
19 | | - try: |
20 | | - attrs["id"] |
21 | | - except (KeyError, TypeError) as exc: |
22 | | - raise TypeError( |
23 | | - "ReadonlyUUIDInput cannot be rendered without an 'id' attribute" |
24 | | - ) from exc |
25 | | - |
26 | | - widget_html = self.render_html(name, value, attrs) |
27 | | - |
28 | | - return mark_safe(widget_html) # noqa: S308 |
29 | | - |
30 | | - def render_html(self, name, value, attrs): |
31 | | - """Render the HTML (non-JS) portion of the field markup""" |
32 | | - hidden = super().render(name, value, attrs) |
33 | | - display_value = value[:6] if value is not None else value |
34 | | - shown = f'<div id="{attrs["id"]}_display-value" style="padding-top: 1.2em;">{display_value}</div>' |
35 | | - return shown + hidden |
36 | | - |
37 | | - def build_attrs(self, *args, **kwargs): |
38 | | - attrs = super().build_attrs(*args, **kwargs) |
39 | | - attrs["data-controller"] = "read-only-uuid" |
40 | | - return attrs |
41 | | - |
42 | | - @property |
43 | | - def media(self): |
44 | | - return Media(js=["footnotes/js/read-only-uuid-controller.js"]) |
45 | | - |
46 | | -else: |
47 | | - from wagtail.utils.widgets import WidgetWithScript |
48 | | - |
49 | | - class ReadonlyUUIDInput(WidgetWithScript, HiddenInput): |
50 | | - """ |
51 | | - This isn't really read-only. It's a hidden input with an an adjacent div |
52 | | - showing the current value; that way we can set the value in JavaScript, but |
53 | | - the user can't easily change it. |
54 | | - """ |
55 | | - |
56 | | - def render_html(self, name, value, attrs): |
57 | | - """Render the HTML (non-JS) portion of the field markup""" |
58 | | - hidden = super(WidgetWithScript, self).render(name, value, attrs) |
59 | | - display_value = value[:6] if value is not None else value |
60 | | - shown = f'<div id="{attrs["id"]}_display-value" style="padding-top: 1.2em;">{display_value}</div>' |
61 | | - return shown + hidden |
62 | | - |
63 | | - def render_js_init(self, id_, name, value): |
64 | | - return f'setUUID("{id_}");' |
| 1 | +from django.forms import HiddenInput, Media |
| 2 | +from django.utils.safestring import mark_safe |
| 3 | + |
| 4 | + |
| 5 | +class ReadonlyUUIDInput(HiddenInput): |
| 6 | + """ |
| 7 | + This isn't really read-only. It's a hidden input with an an adjacent div |
| 8 | + showing the current value; that way we can set the value in JavaScript, but |
| 9 | + the user can't easily change it. |
| 10 | + """ |
| 11 | + |
| 12 | + def render(self, name, value, attrs=None, renderer=None): |
| 13 | + # no point trying to come up with sensible semantics for when 'id' is missing from attrs, |
| 14 | + # so let's make sure it fails early in the process |
| 15 | + try: |
| 16 | + attrs["id"] |
| 17 | + except (KeyError, TypeError) as exc: |
| 18 | + raise TypeError( |
| 19 | + "ReadonlyUUIDInput cannot be rendered without an 'id' attribute" |
| 20 | + ) from exc |
| 21 | + |
| 22 | + widget_html = self.render_html(name, value, attrs) |
| 23 | + |
| 24 | + return mark_safe(widget_html) # noqa: S308 |
| 25 | + |
| 26 | + def render_html(self, name, value, attrs): |
| 27 | + """Render the HTML (non-JS) portion of the field markup""" |
| 28 | + hidden = super().render(name, value, attrs) |
| 29 | + display_value = value[:6] if value is not None else value |
| 30 | + shown = f'<div id="{attrs["id"]}_display-value" style="padding-top: 1.2em;">{display_value}</div>' |
| 31 | + return shown + hidden |
| 32 | + |
| 33 | + def build_attrs(self, *args, **kwargs): |
| 34 | + attrs = super().build_attrs(*args, **kwargs) |
| 35 | + attrs["data-controller"] = "read-only-uuid" |
| 36 | + return attrs |
| 37 | + |
| 38 | + @property |
| 39 | + def media(self): |
| 40 | + return Media(js=["footnotes/js/read-only-uuid-controller.js"]) |
0 commit comments