2020-09-30 21:55:23 +00:00
|
|
|
"""Additional fields"""
|
2019-10-12 12:23:03 +00:00
|
|
|
import yaml
|
|
|
|
from django import forms
|
2020-09-30 21:55:23 +00:00
|
|
|
from django.utils.datastructures import MultiValueDict
|
2019-10-12 12:23:03 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
|
2020-09-30 21:55:23 +00:00
|
|
|
class ArrayFieldSelectMultiple(forms.SelectMultiple):
|
|
|
|
"""This is a Form Widget for use with a Postgres ArrayField. It implements
|
|
|
|
a multi-select interface that can be given a set of `choices`.
|
|
|
|
You can provide a `delimiter` keyword argument to specify the delimeter used.
|
|
|
|
|
|
|
|
https://gist.github.com/stephane/00e73c0002de52b1c601"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# Accept a `delimiter` argument, and grab it (defaulting to a comma)
|
|
|
|
self.delimiter = kwargs.pop("delimiter", ",")
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def value_from_datadict(self, data, files, name):
|
|
|
|
if isinstance(data, MultiValueDict):
|
|
|
|
# Normally, we'd want a list here, which is what we get from the
|
|
|
|
# SelectMultiple superclass, but the SimpleArrayField expects to
|
|
|
|
# get a delimited string, so we're doing a little extra work.
|
|
|
|
return self.delimiter.join(data.getlist(name))
|
|
|
|
|
|
|
|
return data.get(name)
|
|
|
|
|
|
|
|
def get_context(self, name, value, attrs):
|
|
|
|
return super().get_context(name, value.split(self.delimiter), attrs)
|
|
|
|
|
|
|
|
|
2020-05-20 11:00:45 +00:00
|
|
|
class CodeMirrorWidget(forms.Textarea):
|
|
|
|
"""Custom Textarea-based Widget that triggers a CodeMirror editor"""
|
|
|
|
|
|
|
|
# CodeMirror mode to enable
|
|
|
|
mode: str
|
|
|
|
|
|
|
|
def __init__(self, *args, mode="yaml", **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.mode = mode
|
|
|
|
|
|
|
|
def render(self, *args, **kwargs):
|
2020-09-16 19:54:35 +00:00
|
|
|
attrs = kwargs.setdefault("attrs", {})
|
|
|
|
attrs.setdefault("class", "")
|
2020-05-20 11:00:45 +00:00
|
|
|
attrs["class"] += " codemirror"
|
|
|
|
attrs["data-cm-mode"] = self.mode
|
|
|
|
return super().render(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2019-10-12 12:23:03 +00:00
|
|
|
class InvalidYAMLInput(str):
|
|
|
|
"""Invalid YAML String type"""
|
|
|
|
|
|
|
|
|
|
|
|
class YAMLString(str):
|
|
|
|
"""YAML String type"""
|
|
|
|
|
|
|
|
|
2020-08-15 19:04:22 +00:00
|
|
|
class YAMLField(forms.JSONField):
|
2019-10-12 12:23:03 +00:00
|
|
|
"""Django's JSON Field converted to YAML"""
|
|
|
|
|
|
|
|
default_error_messages = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"invalid": _("'%(value)s' value must be valid YAML."),
|
2019-10-12 12:23:03 +00:00
|
|
|
}
|
|
|
|
widget = forms.Textarea
|
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
if self.disabled:
|
|
|
|
return value
|
|
|
|
if value in self.empty_values:
|
|
|
|
return None
|
|
|
|
if isinstance(value, (list, dict, int, float, YAMLString)):
|
|
|
|
return value
|
|
|
|
try:
|
|
|
|
converted = yaml.safe_load(value)
|
|
|
|
except yaml.YAMLError:
|
|
|
|
raise forms.ValidationError(
|
2020-09-30 17:34:22 +00:00
|
|
|
self.error_messages["invalid"],
|
|
|
|
code="invalid",
|
|
|
|
params={"value": value},
|
2019-10-12 12:23:03 +00:00
|
|
|
)
|
|
|
|
if isinstance(converted, str):
|
|
|
|
return YAMLString(converted)
|
2020-09-18 22:00:55 +00:00
|
|
|
if converted is None:
|
|
|
|
return {}
|
2019-10-12 12:23:03 +00:00
|
|
|
return converted
|
|
|
|
|
|
|
|
def bound_data(self, data, initial):
|
|
|
|
if self.disabled:
|
|
|
|
return initial
|
|
|
|
try:
|
|
|
|
return yaml.safe_load(data)
|
|
|
|
except yaml.YAMLError:
|
|
|
|
return InvalidYAMLInput(data)
|
|
|
|
|
|
|
|
def prepare_value(self, value):
|
|
|
|
if isinstance(value, InvalidYAMLInput):
|
|
|
|
return value
|
2020-02-23 14:27:11 +00:00
|
|
|
return yaml.dump(value, explicit_start=True, default_flow_style=False)
|
2019-10-12 12:23:03 +00:00
|
|
|
|
|
|
|
def has_changed(self, initial, data):
|
|
|
|
if super().has_changed(initial, data):
|
|
|
|
return True
|
|
|
|
# For purposes of seeing whether something has changed, True isn't the
|
|
|
|
# same as 1 and the order of keys doesn't matter.
|
|
|
|
data = self.to_python(data)
|
|
|
|
return yaml.dump(initial, sort_keys=True) != yaml.dump(data, sort_keys=True)
|