|
@@ -1,7 +1,13 @@
|
|
|
|
+# coding=utf-8
|
|
|
|
+
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.core.exceptions import ValidationError
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import transaction
|
|
from django.db import transaction
|
|
|
|
|
|
|
|
+import gettext
|
|
|
|
+
|
|
|
|
+_ = gettext.gettext
|
|
|
|
+
|
|
__author__ = 'marcos'
|
|
__author__ = 'marcos'
|
|
|
|
|
|
import collections
|
|
import collections
|
|
@@ -9,7 +15,7 @@ import collections
|
|
from django import forms
|
|
from django import forms
|
|
|
|
|
|
from rapid.widgets import RapidSelector, RapidRelationReadOnly, rapidAlternativesWidget
|
|
from rapid.widgets import RapidSelector, RapidRelationReadOnly, rapidAlternativesWidget
|
|
-from rapid.wrappers import FieldData, ModelData, InstanceData
|
|
|
|
|
|
+from rapid.wrappers import FieldData, ModelData
|
|
|
|
|
|
|
|
|
|
class RapidAlternativesField(forms.Field):
|
|
class RapidAlternativesField(forms.Field):
|
|
@@ -65,6 +71,7 @@ def for_model(request, model, default_relations=()):
|
|
form_model = model
|
|
form_model = model
|
|
form_widgets = dict(widgets)
|
|
form_widgets = dict(widgets)
|
|
|
|
|
|
|
|
+ # noinspection PyTypeChecker
|
|
class CForm(forms.ModelForm):
|
|
class CForm(forms.ModelForm):
|
|
def __init__(self, *args, **kwargs):
|
|
def __init__(self, *args, **kwargs):
|
|
initial = kwargs.get('initial', {})
|
|
initial = kwargs.get('initial', {})
|
|
@@ -76,7 +83,9 @@ def for_model(request, model, default_relations=()):
|
|
for n, f in ModelData(model).rapid_alternative_data():
|
|
for n, f in ModelData(model).rapid_alternative_data():
|
|
ct = ModelData(model).field_by_name(f.ct_field).field
|
|
ct = ModelData(model).field_by_name(f.ct_field).field
|
|
fk = ModelData(model).field_by_name(f.fk_field).field
|
|
fk = ModelData(model).field_by_name(f.fk_field).field
|
|
|
|
+ # noinspection PyTypeChecker
|
|
fl = RapidAlternativesField(n, ct.alternatives, ct.name, self, request, instance)
|
|
fl = RapidAlternativesField(n, ct.alternatives, ct.name, self, request, instance)
|
|
|
|
+ # noinspection PyArgumentList
|
|
type(self.__class__).__setattr__(self.__class__, n, fl)
|
|
type(self.__class__).__setattr__(self.__class__, n, fl)
|
|
nd = collections.OrderedDict()
|
|
nd = collections.OrderedDict()
|
|
for k, v in self.__class__.base_fields.iteritems():
|
|
for k, v in self.__class__.base_fields.iteritems():
|
|
@@ -94,10 +103,7 @@ def for_model(request, model, default_relations=()):
|
|
else:
|
|
else:
|
|
obj = super(CForm, self).save(commit=False)
|
|
obj = super(CForm, self).save(commit=False)
|
|
for n, f in ModelData(model).rapid_alternative_data():
|
|
for n, f in ModelData(model).rapid_alternative_data():
|
|
- ct = ModelData(model).field_by_name(f.ct_field).field
|
|
|
|
- fk = ModelData(model).field_by_name(f.fk_field).field
|
|
|
|
if self.instance:
|
|
if self.instance:
|
|
- fl = RapidAlternativesField(n, ct.alternatives, ct.name, self, request, self.instance)
|
|
|
|
old_t = getattr(self.instance, f.ct_field)
|
|
old_t = getattr(self.instance, f.ct_field)
|
|
new_t = getattr(obj, f.ct_field)
|
|
new_t = getattr(obj, f.ct_field)
|
|
if old_t != new_t:
|
|
if old_t != new_t:
|
|
@@ -111,8 +117,7 @@ def for_model(request, model, default_relations=()):
|
|
self.save_m2m()
|
|
self.save_m2m()
|
|
return obj
|
|
return obj
|
|
|
|
|
|
-
|
|
|
|
- class Meta:
|
|
|
|
|
|
+ class Meta(object):
|
|
model = form_model
|
|
model = form_model
|
|
fields = '__all__'
|
|
fields = '__all__'
|
|
widgets = form_widgets
|
|
widgets = form_widgets
|