Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Sourcery refactored master branch #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eav/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseDynamicEntityForm._build_dynamic_fields refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

self.initial[attribute.slug] = value

def save(self, commit=True):
Expand Down
36 changes: 20 additions & 16 deletions eav/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Comment on lines -240 to +246
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Attribute.validate_value refactored with the following changes:

  • Merge nested if conditions (merge-nested-ifs)


def save(self, *args, **kwargs):
'''
Expand Down Expand Up @@ -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:
Comment on lines -277 to +279
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Attribute.get_choices refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

return None
return self.enum_group.enums.all()

Expand 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 == '':
Comment on lines -300 to +307
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Attribute.save_value refactored with the following changes:

  • Use x is None rather than x == None (none-compare)
  • Replace multiple comparisons of same variable with in operator (merge-comparisons)

value_obj.delete()
return

Expand Down Expand Up @@ -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})
Comment on lines -374 to +384
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Value.clean refactored with the following changes:

  • Merge nested if conditions (merge-nested-ifs)


def _get_value(self):
'''
Expand Down Expand Up @@ -496,7 +500,7 @@ def validate_attributes(self):
'err': e})

def get_values_dict(self):
values_dict = dict()
values_dict = {}
Comment on lines -499 to +503
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Entity.get_values_dict refactored with the following changes:

  • Replace dict() with {} (dict-literal)

for value in self.get_values():
values_dict[value.attribute.slug] = value.value

Expand Down
4 changes: 2 additions & 2 deletions eav/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_text refactored with the following changes:

  • Replace multiple comparisons of same variable with in operator (merge-comparisons)

raise ValidationError(_(u"Must be str or unicode"))


Expand Down Expand Up @@ -82,7 +82,7 @@ def validate_bool(value):
'''
Raises ``ValidationError`` unless *value* type is ``bool``
'''
if not type(value) == bool:
if type(value) != bool:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_bool refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

raise ValidationError(_(u"Must be a boolean"))


Expand Down