admin(major): add YAMLField for attributes, add codemirror editor

This commit is contained in:
Langhammer, Jens
2019-10-12 14:23:03 +02:00
parent 50172e58d8
commit 1fe420fd80
419 changed files with 75081 additions and 1 deletions

View File

@ -0,0 +1,37 @@
"""p2 form helpers"""
from django import forms
from passbook.admin.fields import YAMLField
class TagModelForm(forms.ModelForm):
"""Base form for models that have attributes"""
def __init__(self, *args, **kwargs):
# Check if we have an instance, load tags otherwise use an empty dict
instance = kwargs.get('instance', None)
tags = instance.tags if instance else {}
# Make sure all predefined tags exist in tags, and set default if they don't
predefined_tags = self._meta.model().get_predefined_tags() # pylint: disable=no-member
for key, value in predefined_tags.items():
if key not in tags:
tags[key] = value
# Format JSON
kwargs['initial']['tags'] = tags
super().__init__(*args, **kwargs)
def clean_tags(self):
"""Make sure all required tags are set"""
if hasattr(self.instance, 'get_required_keys') and hasattr(self.instance, 'tags'):
for key in self.instance.get_required_keys():
if key not in self.cleaned_data.get('tags'):
raise forms.ValidationError("Tag %s missing." % key)
return self.cleaned_data.get('tags')
# pylint: disable=too-few-public-methods
class TagModelFormMeta:
"""Base Meta class that uses the YAMLField"""
field_classes = {
'tags': YAMLField
}

View File

@ -2,6 +2,7 @@
from django import forms
from passbook.admin.fields import YAMLField
from passbook.core.models import User
@ -13,5 +14,8 @@ class UserForm(forms.ModelForm):
model = User
fields = ['username', 'name', 'email', 'is_staff', 'is_active', 'attributes']
widgets = {
'name': forms.TextInput
'name': forms.TextInput,
}
field_classes = {
'attributes': YAMLField,
}