spejstore-new/storage/widgets.py

66 lines
2.2 KiB
Python
Raw Permalink Normal View History

2018-04-27 20:53:38 +00:00
from pkg_resources import parse_version
from django_select2.forms import HeavySelect2Widget
2018-04-27 20:53:38 +00:00
from django import get_version
from django.urls import reverse
from django.conf import settings
from django import forms
2018-04-27 20:53:38 +00:00
from django.utils.safestring import mark_safe
from django.template import Context
from django.template.loader import get_template
from django.contrib.admin.widgets import AdminTextareaWidget
from django_admin_hstore_widget.forms import HStoreFormWidget
from django.contrib.postgres.forms import forms
from django.templatetags.static import static
2018-04-27 20:53:38 +00:00
class PropsSelectWidget(HStoreFormWidget):
@property
def media(self):
internal_js = [
"vendor/jquery/jquery.js",
"django_admin_hstore_widget/underscore-min.js",
"django_admin_hstore_widget/django_admin_hstore_widget.js",
]
js = [static("admin/js/%s" % path) for path in internal_js]
2018-04-27 20:53:38 +00:00
return forms.Media(js=js)
2018-04-27 20:53:38 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def render(self, name, value, attrs=None, renderer=None):
2018-04-27 20:53:38 +00:00
if attrs is None:
attrs = {}
# it's called "original" because it will be replaced by a copy
2023-07-11 13:34:35 +00:00
attrs["class"] = "hstore-original-textarea"
w = HeavySelect2Widget(
data_view="prop-complete", attrs={"data-tags": "true", "class": "hs-key"}
)
2018-04-27 20:53:38 +00:00
# get default HTML from AdminTextareaWidget
html = AdminTextareaWidget.render(self, name, value, attrs, renderer)
2018-04-27 20:53:38 +00:00
# prepare template context
2018-09-26 20:20:16 +00:00
template_context = {
2023-07-11 13:34:35 +00:00
"field_name": name,
"STATIC_URL": settings.STATIC_URL,
"use_svg": parse_version(get_version())
>= parse_version("1.9"), # use svg icons if django >= 1.9
"ajax_url": reverse("prop-complete"),
"w": w.build_attrs(base_attrs=w.attrs),
2018-09-26 20:20:16 +00:00
}
2018-04-27 20:53:38 +00:00
# get template object
template = get_template("hstore_default_widget.html")
2018-04-27 20:53:38 +00:00
# render additional html
additional_html = template.render(template_context)
# append additional HTML and mark as safe
html = html + additional_html
html = mark_safe(html)
return html