Clean up code a bit; work on code-style

This commit is contained in:
Iwo Herka 2018-07-13 09:02:43 +00:00
parent e9e6712cb5
commit 0677e7f7b5
4 changed files with 10 additions and 10 deletions

View file

@ -11,7 +11,6 @@ Along with the :class:`Entity` helper class.
'''
from copy import copy
from django.conf import settings
from django.contrib.contenttypes import fields as generic
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError

View file

@ -94,7 +94,6 @@ def rewrite_q_expr(model_cls, expr):
if isinstance(expr, Q):
config_cls = getattr(model_cls, '_eav_config_cls', None)
assert config_cls
gr_name = config_cls.generic_relation_attr
# Recurively check child nodes.
@ -111,11 +110,13 @@ def rewrite_q_expr(model_cls, expr):
other = [c for c in expr.children if not c in rewritable]
for child in rewritable:
assert child.children and len(child.children) == 1
if not (child.children and len(child.children) == 1):
raise AssertionError('Child must have exactly one descendant')
# Child to be merged is always a terminal Q node,
# i.e. it's an AND expression with attribute-value tuple child.
attrval = child.children[0]
assert isinstance(attrval, tuple)
if not isinstance(attrval, tuple):
raise AssertionError('Attribute-value must be a tuple')
fname = '{}__in'.format(gr_name)
@ -261,7 +262,7 @@ class EavQuerySet(QuerySet):
Pass *args* and *kwargs* through ``eav_filter``, then pass to
the ``models.Manager`` filter method.
'''
return super(self.__class__, self).filter(*args, **kwargs)
return super().filter(*args, **kwargs)
@eav_filter
def exclude(self, *args, **kwargs):
@ -269,7 +270,7 @@ class EavQuerySet(QuerySet):
Pass *args* and *kwargs* through ``eav_filter``, then pass to
the ``models.Manager`` exclude method.
'''
return super(self.__class__, self).exclude(*args, **kwargs)
return super().exclude(*args, **kwargs)
@eav_filter
def get(self, *args, **kwargs):
@ -277,4 +278,4 @@ class EavQuerySet(QuerySet):
Pass *args* and *kwargs* through ``eav_filter``, then pass to
the ``models.Manager`` get method.
'''
return super(self.__class__, self).get(*args, **kwargs)
return super().get(*args, **kwargs)

View file

@ -48,7 +48,7 @@ class DataValidation(TestCase):
self.assertEqual(Patient.objects.count(), 0)
self.assertEqual(Value.objects.count(), 0)
p = Patient.objects.create(name='Joe', eav__weight=2, eav__age=5)
Patient.objects.create(name='Joe', eav__weight=2, eav__age=5)
self.assertEqual(Patient.objects.count(), 1)
self.assertEqual(Value.objects.count(), 2)

View file

@ -38,10 +38,10 @@ class Queries(TestCase):
Patient.objects.get_or_create(name='Bob', eav__age=5)
self.assertEqual(Patient.objects.count(), 1)
self.assertEqual(Value.objects.count(), 1)
p = Patient.objects.get_or_create(name='Bob', eav__age=5)
Patient.objects.get_or_create(name='Bob', eav__age=5)
self.assertEqual(Patient.objects.count(), 1)
self.assertEqual(Value.objects.count(), 1)
p = Patient.objects.get_or_create(name='Bob', eav__age=6)
Patient.objects.get_or_create(name='Bob', eav__age=6)
self.assertEqual(Patient.objects.count(), 2)
self.assertEqual(Value.objects.count(), 2)