2014-11-12 16:33:40 +00:00
|
|
|
from django import forms
|
2015-04-02 16:14:55 +00:00
|
|
|
from django.utils.encoding import force_text
|
2014-11-12 16:33:40 +00:00
|
|
|
|
2015-04-27 12:24:17 +00:00
|
|
|
from orchestra.forms.widgets import SpanWidget
|
2015-03-04 21:06:16 +00:00
|
|
|
|
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()
|
2015-04-02 16:14:55 +00:00
|
|
|
display = '%s <a href=".">change</a>' % force_text(self.plugin.verbose_name)
|
2015-04-27 12:24:17 +00:00
|
|
|
self.fields[self.plugin_field].widget = SpanWidget(original=value, display=display)
|
2015-04-16 13:15:21 +00:00
|
|
|
help_text = self.fields[self.plugin_field].help_text
|
|
|
|
self.fields[self.plugin_field].help_text = getattr(self.plugin, 'help_text', help_text)
|
2015-03-27 19:50:54 +00:00
|
|
|
if self.instance:
|
2014-11-12 16:33:40 +00:00
|
|
|
for field in self.declared_fields:
|
|
|
|
initial = self.fields[field].initial
|
2015-03-27 19:50:54 +00:00
|
|
|
self.fields[field].initial = self.instance.data.get(field, initial)
|
2015-03-04 21:06:16 +00:00
|
|
|
if self.instance.pk:
|
2015-04-16 13:15:21 +00:00
|
|
|
# Admin Readonly fields are not availeble in self.fields, so we use Meta
|
|
|
|
plugin = getattr(self.instance, '%s_class' % self.plugin_field)
|
|
|
|
plugin_help_text = getattr(plugin, 'help_text', '')
|
|
|
|
model_help_text = self.instance._meta.get_field_by_name(self.plugin_field)[0].help_text
|
|
|
|
self._meta.help_texts = {
|
|
|
|
self.plugin_field: plugin_help_text or model_help_text
|
|
|
|
}
|
2016-02-11 14:24:09 +00:00
|
|
|
for field in self.plugin.get_change_readonly_fields():
|
2015-10-09 12:54:30 +00:00
|
|
|
value = getattr(self.instance, field, None) or self.instance.data.get(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-04-27 12:24:17 +00:00
|
|
|
self.fields[field].widget = SpanWidget(original=value, display=display)
|
2014-11-12 16:33:40 +00:00
|
|
|
|
|
|
|
def clean(self):
|
2015-04-09 14:32:10 +00:00
|
|
|
super(PluginDataForm, self).clean()
|
2014-11-12 16:33:40 +00:00
|
|
|
data = {}
|
2015-04-02 16:14:55 +00:00
|
|
|
# Update data fields
|
|
|
|
for field in self.declared_fields:
|
2014-11-12 16:33:40 +00:00
|
|
|
try:
|
|
|
|
data[field] = self.cleaned_data[field]
|
|
|
|
except KeyError:
|
2015-04-02 16:14:55 +00:00
|
|
|
data[field] = self.data[field]
|
|
|
|
# Keep old data fields
|
|
|
|
for field, value in self.instance.data.items():
|
|
|
|
if field not in data:
|
|
|
|
try:
|
|
|
|
data[field] = self.cleaned_data[field]
|
|
|
|
except KeyError:
|
|
|
|
data[field] = value
|
2014-11-12 16:33:40 +00:00
|
|
|
self.cleaned_data['data'] = data
|