django-orchestra-test/orchestra/plugins/forms.py

41 lines
1.8 KiB
Python
Raw Normal View History

2014-11-12 16:33:40 +00:00
from django import forms
2015-03-04 21:06:16 +00:00
from orchestra.forms.widgets import ReadOnlyWidget
2014-11-12 16:33:40 +00:00
class PluginDataForm(forms.ModelForm):
data = forms.CharField(widget=forms.HiddenInput, required=False)
def __init__(self, *args, **kwargs):
super(PluginDataForm, self).__init__(*args, **kwargs)
2015-03-04 21:06:16 +00:00
if self.plugin_field in self.fields:
value = self.plugin.get_name()
display = '%s <a href=".">change</a>' % unicode(self.plugin.verbose_name)
self.fields[self.plugin_field].widget = ReadOnlyWidget(value, display)
self.fields[self.plugin_field].help_text = getattr(self.plugin, 'help_text', '')
2014-11-12 16:33:40 +00:00
instance = kwargs.get('instance')
if instance:
for field in self.declared_fields:
initial = self.fields[field].initial
self.fields[field].initial = instance.data.get(field, initial)
2015-03-04 21:06:16 +00:00
if self.instance.pk:
for field in self.plugin.get_change_readonly_fileds():
value = getattr(self.instance, field, None) or self.instance.data[field]
2015-03-23 15:36:51 +00:00
display = value
foo_display = getattr(self.instance, 'get_%s_display' % field, None)
if foo_display:
display = foo_display()
2015-03-04 21:06:16 +00:00
self.fields[field].required = False
2015-03-23 15:36:51 +00:00
self.fields[field].widget = ReadOnlyWidget(value, display)
2015-03-04 21:06:16 +00:00
# self.fields[field].help_text = None
2014-11-12 16:33:40 +00:00
def clean(self):
2015-03-04 21:06:16 +00:00
# TODO clean all filed within data???
2014-11-12 16:33:40 +00:00
data = {}
for field in self.declared_fields:
try:
data[field] = self.cleaned_data[field]
except KeyError:
data[field] = self.data[field]
self.cleaned_data['data'] = data