diff --git a/eav/forms.py b/eav/forms.py index c1c7c1f..82a997d 100644 --- a/eav/forms.py +++ b/eav/forms.py @@ -96,7 +96,7 @@ def _build_dynamic_fields(self): self.fields[attribute.slug] = MappedField(**defaults) # fill initial data (if attribute was already defined) - if value and not datatype == attribute.TYPE_ENUM: #enum done above + if value and datatype != attribute.TYPE_ENUM: #enum done above self.initial[attribute.slug] = value def save(self, commit=True): diff --git a/eav/models.py b/eav/models.py index e098f36..2afc59a 100644 --- a/eav/models.py +++ b/eav/models.py @@ -237,11 +237,13 @@ def validate_value(self, value): ''' for validator in self.get_validators(): validator(value) - if self.datatype == self.TYPE_ENUM: - if value not in self.enum_group.enums.all(): - raise ValidationError(_(u"%(enum)s is not a valid choice " - u"for %(attr)s") % \ - {'enum': value, 'attr': self}) + if ( + self.datatype == self.TYPE_ENUM + and value not in self.enum_group.enums.all() + ): + raise ValidationError(_(u"%(enum)s is not a valid choice " + u"for %(attr)s") % \ + {'enum': value, 'attr': self}) def save(self, *args, **kwargs): ''' @@ -274,7 +276,7 @@ def get_choices(self): Returns a query set of :class:`EnumValue` objects for this attribute. Returns None if the datatype of this attribute is not *TYPE_ENUM*. ''' - if not self.datatype == Attribute.TYPE_ENUM: + if self.datatype != Attribute.TYPE_ENUM: return None return self.enum_group.enums.all() @@ -297,12 +299,12 @@ def save_value(self, entity, value): entity_id=entity.pk, attribute=self) except Value.DoesNotExist: - if value == None or value == '': + if value in [None, '']: return value_obj = Value.objects.create(entity_ct=ct, entity_id=entity.pk, attribute=self) - if value == None or value == '': + if value is None or value == '': value_obj.delete() return @@ -371,13 +373,15 @@ def clean(self): Raises ``ValidationError`` if this value's attribute is *TYPE_ENUM* and value_enum is not a valid choice for this value's attribute. ''' - if self.attribute.datatype == Attribute.TYPE_ENUM and \ - self.value_enum: - if self.value_enum not in self.attribute.enum_group.enums.all(): - raise ValidationError(_(u"%(choice)s is not a valid " \ - u"choice for %s(attribute)") % \ - {'choice': self.value_enum, - 'attribute': self.attribute}) + if ( + self.attribute.datatype == Attribute.TYPE_ENUM + and self.value_enum + and self.value_enum not in self.attribute.enum_group.enums.all() + ): + raise ValidationError(_(u"%(choice)s is not a valid " \ + u"choice for %s(attribute)") % \ + {'choice': self.value_enum, + 'attribute': self.attribute}) def _get_value(self): ''' @@ -496,7 +500,7 @@ def validate_attributes(self): 'err': e}) def get_values_dict(self): - values_dict = dict() + values_dict = {} for value in self.get_values(): values_dict[value.attribute.slug] = value.value diff --git a/eav/validators.py b/eav/validators.py index f5d6c5a..b62688b 100644 --- a/eav/validators.py +++ b/eav/validators.py @@ -45,7 +45,7 @@ def validate_text(value): ''' Raises ``ValidationError`` unless *value* type is ``str`` or ``unicode`` ''' - if not (type(value) == unicode or type(value) == str): + if not type(value) in [unicode, str]: raise ValidationError(_(u"Must be str or unicode")) @@ -82,7 +82,7 @@ def validate_bool(value): ''' Raises ``ValidationError`` unless *value* type is ``bool`` ''' - if not type(value) == bool: + if type(value) != bool: raise ValidationError(_(u"Must be a boolean"))