admin(major): add YAMLField for attributes, add codemirror editor
This commit is contained in:
37
passbook/admin/forms/base.py
Normal file
37
passbook/admin/forms/base.py
Normal 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
|
||||
}
|
@ -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,
|
||||
}
|
||||
|
Reference in New Issue
Block a user