Another try. This should resolve the validation errors by having bad values for the forms just go to different default values.

This commit is contained in:
Tehom 2016-11-21 19:13:12 -05:00 committed by Griatch
parent ab6b7680a1
commit 1e22b89447
2 changed files with 4 additions and 10 deletions

View file

@ -148,7 +148,7 @@ class AttributeForm(forms.ModelForm):
the creation, change, or deletion of an Attribute for us, as well as updating the handler's cache so that all the creation, change, or deletion of an Attribute for us, as well as updating the handler's cache so that all
changes are instantly updated in-game. changes are instantly updated in-game.
""" """
attr_key = forms.CharField(label='Attribute Name') attr_key = forms.CharField(label='Attribute Name', required=False, initial="Enter Attribute Name Here")
attr_category = forms.CharField(label="Category", help_text="type of attribute, for sorting", required=False) attr_category = forms.CharField(label="Category", help_text="type of attribute, for sorting", required=False)
attr_value = PickledFormField(label="Value", help_text="Value to pickle/save", required=False) attr_value = PickledFormField(label="Value", help_text="Value to pickle/save", required=False)
attr_type = forms.CharField(label="Type", help_text="nick for nickname, else leave blank", required=False) attr_type = forms.CharField(label="Type", help_text="nick for nickname, else leave blank", required=False)
@ -201,7 +201,7 @@ class AttributeForm(forms.ModelForm):
""" """
# we are spoofing an Attribute for the Handler that will be called # we are spoofing an Attribute for the Handler that will be called
instance = self.instance instance = self.instance
instance.attr_key = self.cleaned_data['attr_key'] instance.attr_key = self.cleaned_data['attr_key'] or "no_name_entered_for_attribute"
instance.attr_category = self.cleaned_data['attr_category'] or None instance.attr_category = self.cleaned_data['attr_category'] or None
instance.attr_value = self.cleaned_data['attr_value'] or None instance.attr_value = self.cleaned_data['attr_value'] or None
# convert the serialized string value into an object, if necessary, for AttributeHandler # convert the serialized string value into an object, if necessary, for AttributeHandler
@ -216,12 +216,6 @@ class AttributeFormSet(forms.BaseInlineFormSet):
""" """
Attribute version of TagFormSet, as above. Attribute version of TagFormSet, as above.
""" """
def clean(self):
if any(self.errors):
from django.core.exceptions import FieldError
raise FieldError("Sorry, there was an error in saving that form. You may have forgotten to add a name "
"for the Attribute, or you may have provided an invalid literal for an Attribute's value.")
super(AttributeFormSet, self).clean()
def save(self, commit=True): def save(self, commit=True):
def get_handler(finished_object): def get_handler(finished_object):

View file

@ -128,7 +128,7 @@ class PickledWidget(Textarea):
final_attrs = self.build_attrs(attrs, name=name) final_attrs = self.build_attrs(attrs, name=name)
return format_html('<textarea{0}>\r\n{1}</textarea>', return format_html('<textarea{0}>\r\n{1}</textarea>',
flatatt(final_attrs), flatatt(final_attrs),
force_text(value)) value)
class PickledFormField(CharField): class PickledFormField(CharField):
@ -154,7 +154,7 @@ class PickledFormField(CharField):
return literal_eval(value) return literal_eval(value)
except (ValueError, SyntaxError): except (ValueError, SyntaxError):
try: try:
value = "u'%s'" % force_text(value) value = repr(value)
return literal_eval(value) return literal_eval(value)
except (ValueError, SyntaxError): except (ValueError, SyntaxError):
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])