mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-05-24 15:13:50 +00:00
Merge changing generic relations
This commit is contained in:
commit
e915a95f06
5 changed files with 21 additions and 193 deletions
|
|
@ -16,7 +16,6 @@ class EavSlugField(models.SlugField):
|
||||||
u"not start with a number, and contain "\
|
u"not start with a number, and contain "\
|
||||||
u"only letters, numbers, or underscores."))
|
u"only letters, numbers, or underscores."))
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_slug_from_name(name):
|
def create_slug_from_name(name):
|
||||||
'''
|
'''
|
||||||
|
|
@ -34,6 +33,7 @@ class EavSlugField(models.SlugField):
|
||||||
class EavDatatypeField(models.SlugField):
|
class EavDatatypeField(models.SlugField):
|
||||||
|
|
||||||
def validate(self, value, instance):
|
def validate(self, value, instance):
|
||||||
|
super(EavDatatypeField, self).validate(value, instance)
|
||||||
from .models import EavAttribute
|
from .models import EavAttribute
|
||||||
if not instance.pk:
|
if not instance.pk:
|
||||||
return
|
return
|
||||||
|
|
|
||||||
24
models.py
24
models.py
|
|
@ -96,7 +96,7 @@ class EavAttribute(models.Model):
|
||||||
doesn't exist.
|
doesn't exist.
|
||||||
'''
|
'''
|
||||||
ct = ContentType.objects.get_for_model(entity)
|
ct = ContentType.objects.get_for_model(entity)
|
||||||
qs = self.eavvalue_set.filter(content_type=ct, object_id=entity.pk)
|
qs = self.eavvalue_set.filter(entity_ct=ct, entity_id=entity.pk)
|
||||||
if qs.count() == 1:
|
if qs.count() == 1:
|
||||||
return qs[0]
|
return qs[0]
|
||||||
raise AttributeError(u"You should have one and only one value"\
|
raise AttributeError(u"You should have one and only one value"\
|
||||||
|
|
@ -123,12 +123,12 @@ class EavAttribute(models.Model):
|
||||||
ct = ContentType.objects.get_for_model(entity)
|
ct = ContentType.objects.get_for_model(entity)
|
||||||
attribute = attribute or self
|
attribute = attribute or self
|
||||||
try:
|
try:
|
||||||
eavvalue = self.eavvalue_set.get(content_type=ct,
|
eavvalue = self.eavvalue_set.get(entity_ct=ct,
|
||||||
object_id=entity.pk,
|
entity_id=entity.pk,
|
||||||
attribute=attribute)
|
attribute=attribute)
|
||||||
except EavValue.DoesNotExist:
|
except EavValue.DoesNotExist:
|
||||||
eavvalue = self.eavvalue_set.model(content_type=ct,
|
eavvalue = self.eavvalue_set.model(entity_ct=ct,
|
||||||
object_id=entity.pk,
|
entity_id=entity.pk,
|
||||||
attribute=attribute)
|
attribute=attribute)
|
||||||
if value != eavvalue.value:
|
if value != eavvalue.value:
|
||||||
eavvalue.value = value
|
eavvalue.value = value
|
||||||
|
|
@ -140,13 +140,13 @@ class EavAttribute(models.Model):
|
||||||
|
|
||||||
class EavValue(models.Model):
|
class EavValue(models.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = ('content_type', 'object_id', 'attribute',
|
unique_together = ('entity_ct', 'entity_id', 'attribute',
|
||||||
'value_text', 'value_float', 'value_date',
|
'value_text', 'value_float', 'value_date',
|
||||||
'value_bool')
|
'value_bool')
|
||||||
|
|
||||||
content_type = models.ForeignKey(ContentType)
|
entity_ct = models.ForeignKey(ContentType)
|
||||||
object_id = models.IntegerField()
|
entity_id = models.IntegerField()
|
||||||
object = generic.GenericForeignKey()
|
entity = generic.GenericForeignKey(ct_field='entity_ct', fk_field='entity_id')
|
||||||
|
|
||||||
value_text = models.TextField(blank=True, null=True)
|
value_text = models.TextField(blank=True, null=True)
|
||||||
value_float = models.FloatField(blank=True, null=True)
|
value_float = models.FloatField(blank=True, null=True)
|
||||||
|
|
@ -177,7 +177,7 @@ class EavValue(models.Model):
|
||||||
value = property(_get_value, _set_value)
|
value = property(_get_value, _set_value)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return u"%s - %s: \"%s\"" % (self.object, self.attribute.name, self.value)
|
return u"%s - %s: \"%s\"" % (self.entity, self.attribute.name, self.value)
|
||||||
|
|
||||||
|
|
||||||
class EavEntity(object):
|
class EavEntity(object):
|
||||||
|
|
@ -213,8 +213,8 @@ class EavEntity(object):
|
||||||
return self._attributes_cache
|
return self._attributes_cache
|
||||||
|
|
||||||
def get_values(self):
|
def get_values(self):
|
||||||
return EavValue.objects.filter(content_type=self.ct,
|
return EavValue.objects.filter(entity_ct=self.ct,
|
||||||
object_id=self.model.pk).select_related()
|
entity_id=self.model.pk).select_related()
|
||||||
|
|
||||||
def get_all_attribute_slugs(self):
|
def get_all_attribute_slugs(self):
|
||||||
if not hasattr(self, '_attributes_cache_dict'):
|
if not hasattr(self, '_attributes_cache_dict'):
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ class EavBasicTests(TestCase):
|
||||||
|
|
||||||
self.entity = Patient.objects.create(name="Doe")
|
self.entity = Patient.objects.create(name="Doe")
|
||||||
|
|
||||||
self.value = EavValue.objects.create(object=self.entity,
|
self.value = EavValue.objects.create(entity=self.entity,
|
||||||
attribute=self.attribute,
|
attribute=self.attribute,
|
||||||
value_text='Denver')
|
value_text='Denver')
|
||||||
|
|
||||||
|
|
@ -47,7 +47,7 @@ class EavBasicTests(TestCase):
|
||||||
|
|
||||||
|
|
||||||
def test_value_creation(self):
|
def test_value_creation(self):
|
||||||
EavValue.objects.create(object=self.entity,
|
EavValue.objects.create(entity=self.entity,
|
||||||
attribute=self.attribute,
|
attribute=self.attribute,
|
||||||
value_float=1.2)
|
value_float=1.2)
|
||||||
|
|
||||||
|
|
@ -63,7 +63,7 @@ class EavBasicTests(TestCase):
|
||||||
_text = EavAttribute.objects.create(datatype=EavAttribute.TYPE_TEXT,
|
_text = EavAttribute.objects.create(datatype=EavAttribute.TYPE_TEXT,
|
||||||
name='Text', slug='text',
|
name='Text', slug='text',
|
||||||
help_text='The text')
|
help_text='The text')
|
||||||
val = EavValue.objects.create(object=self.entity,
|
val = EavValue.objects.create(entity=self.entity,
|
||||||
attribute = _text)
|
attribute = _text)
|
||||||
value = "Test text"
|
value = "Test text"
|
||||||
val.value = value
|
val.value = value
|
||||||
|
|
@ -73,7 +73,7 @@ class EavBasicTests(TestCase):
|
||||||
_float = EavAttribute.objects.create(datatype=EavAttribute.TYPE_FLOAT,
|
_float = EavAttribute.objects.create(datatype=EavAttribute.TYPE_FLOAT,
|
||||||
name='Float', slug='float',
|
name='Float', slug='float',
|
||||||
help_text='The float')
|
help_text='The float')
|
||||||
val = EavValue.objects.create(object=self.entity,
|
val = EavValue.objects.create(entity=self.entity,
|
||||||
attribute = _float)
|
attribute = _float)
|
||||||
value = 1.22
|
value = 1.22
|
||||||
val.value = value
|
val.value = value
|
||||||
|
|
@ -84,7 +84,7 @@ class EavBasicTests(TestCase):
|
||||||
_int = EavAttribute.objects.create(datatype=EavAttribute.TYPE_INT,
|
_int = EavAttribute.objects.create(datatype=EavAttribute.TYPE_INT,
|
||||||
name='Int', slug='int',
|
name='Int', slug='int',
|
||||||
help_text='The int')
|
help_text='The int')
|
||||||
val = EavValue.objects.create(object=self.entity,
|
val = EavValue.objects.create(entity=self.entity,
|
||||||
attribute = _int)
|
attribute = _int)
|
||||||
value = 7
|
value = 7
|
||||||
val.value = value
|
val.value = value
|
||||||
|
|
@ -94,7 +94,7 @@ class EavBasicTests(TestCase):
|
||||||
_date = EavAttribute.objects.create(datatype=EavAttribute.TYPE_DATE,
|
_date = EavAttribute.objects.create(datatype=EavAttribute.TYPE_DATE,
|
||||||
name='Date', slug='date',
|
name='Date', slug='date',
|
||||||
help_text='The date')
|
help_text='The date')
|
||||||
val = EavValue.objects.create(object=self.entity,
|
val = EavValue.objects.create(entity=self.entity,
|
||||||
attribute = _date)
|
attribute = _date)
|
||||||
value = datetime.now()
|
value = datetime.now()
|
||||||
val.value = value
|
val.value = value
|
||||||
|
|
@ -104,7 +104,7 @@ class EavBasicTests(TestCase):
|
||||||
_bool = EavAttribute.objects.create(datatype=EavAttribute.TYPE_BOOLEAN,
|
_bool = EavAttribute.objects.create(datatype=EavAttribute.TYPE_BOOLEAN,
|
||||||
name='Bool', slug='bool',
|
name='Bool', slug='bool',
|
||||||
help_text='The bool')
|
help_text='The bool')
|
||||||
val = EavValue.objects.create(object=self.entity,
|
val = EavValue.objects.create(entity=self.entity,
|
||||||
attribute = _bool)
|
attribute = _bool)
|
||||||
value = False
|
value = False
|
||||||
val.value = value
|
val.value = value
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ class EavSetterAndGetterTests(TestCase):
|
||||||
|
|
||||||
self.patient = Patient.objects.create(name="Doe")
|
self.patient = Patient.objects.create(name="Doe")
|
||||||
|
|
||||||
self.value = EavValue.objects.create(object=self.patient,
|
self.value = EavValue.objects.create(entity=self.patient,
|
||||||
attribute=self.attribute,
|
attribute=self.attribute,
|
||||||
value_text='Denver')
|
value_text='Denver')
|
||||||
|
|
||||||
|
|
|
||||||
172
tests/tests.py
172
tests/tests.py
|
|
@ -1,172 +0,0 @@
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
from django.test import TestCase
|
|
||||||
|
|
||||||
from ..models import *
|
|
||||||
from ..utils import EavRegistry, EavConfig
|
|
||||||
from .models import Patient
|
|
||||||
|
|
||||||
|
|
||||||
class EavBasicTests(TestCase):
|
|
||||||
|
|
||||||
"""
|
|
||||||
Testing basics such as registration, printing and object creation
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
|
|
||||||
EavRegistry.unregister(Patient)
|
|
||||||
EavRegistry.register(Patient)
|
|
||||||
|
|
||||||
self.attribute = EavAttribute.objects\
|
|
||||||
.create(datatype=EavAttribute.TYPE_TEXT,
|
|
||||||
name='City', slug='city')
|
|
||||||
self.entity = Patient.objects.create(name="Doe")
|
|
||||||
|
|
||||||
self.value = EavValue.objects.create(object=self.entity,
|
|
||||||
attribute=self.attribute,
|
|
||||||
value_text='Denver')
|
|
||||||
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
EavRegistry.unregister(Patient)
|
|
||||||
|
|
||||||
|
|
||||||
def test_can_create_attribute(self):
|
|
||||||
EavAttribute.objects.create(datatype=EavAttribute.TYPE_TEXT,
|
|
||||||
name='My text test',
|
|
||||||
help_text='My help text')
|
|
||||||
|
|
||||||
def test_attribute_unicode(self):
|
|
||||||
self.assertEqual(unicode(self.attribute), "City (Text)")
|
|
||||||
|
|
||||||
|
|
||||||
def test_can_eaventity_children_give_you_all_attributes_by_default(self):
|
|
||||||
qs = Patient.eav.get_eav_attributes()
|
|
||||||
self.assertEqual(list(qs), list(EavAttribute.objects.all()))
|
|
||||||
|
|
||||||
|
|
||||||
def test_value_creation(self):
|
|
||||||
EavValue.objects.create(object=self.entity,
|
|
||||||
attribute=self.attribute,
|
|
||||||
value_float=1.2)
|
|
||||||
|
|
||||||
def test_value_unicode(self):
|
|
||||||
self.assertEqual(unicode(self.value), "Doe - City: \"Denver\"")
|
|
||||||
|
|
||||||
|
|
||||||
def test_value_unicode(self):
|
|
||||||
self.assertEqual(unicode(self.value), "Doe - City: \"Denver\"")
|
|
||||||
|
|
||||||
|
|
||||||
def test_value_types(self):
|
|
||||||
_text = EavAttribute.objects.create(datatype=EavAttribute.TYPE_TEXT,
|
|
||||||
name='Text',
|
|
||||||
help_text='The text')
|
|
||||||
val = EavValue.objects.create(object=self.entity,
|
|
||||||
attribute = _text)
|
|
||||||
value = "Test text"
|
|
||||||
val.value = value
|
|
||||||
val.save()
|
|
||||||
self.assertEqual(val.value, value)
|
|
||||||
|
|
||||||
_float = EavAttribute.objects.create(datatype=EavAttribute.TYPE_FLOAT,
|
|
||||||
name='Float',
|
|
||||||
help_text='The float')
|
|
||||||
val = EavValue.objects.create(object=self.entity,
|
|
||||||
attribute = _float)
|
|
||||||
value = 1.22
|
|
||||||
val.value = value
|
|
||||||
val.save()
|
|
||||||
self.assertEqual(val.value, value)
|
|
||||||
|
|
||||||
|
|
||||||
_int = EavAttribute.objects.create(datatype=EavAttribute.TYPE_INT,
|
|
||||||
name='Int',
|
|
||||||
help_text='The int')
|
|
||||||
val = EavValue.objects.create(object=self.entity,
|
|
||||||
attribute = _int)
|
|
||||||
value = 7
|
|
||||||
val.value = value
|
|
||||||
val.save()
|
|
||||||
self.assertEqual(val.value, value)
|
|
||||||
|
|
||||||
_date = EavAttribute.objects.create(datatype=EavAttribute.TYPE_DATE,
|
|
||||||
name='Date',
|
|
||||||
help_text='The date')
|
|
||||||
val = EavValue.objects.create(object=self.entity,
|
|
||||||
attribute = _date)
|
|
||||||
value = datetime.now()
|
|
||||||
val.value = value
|
|
||||||
val.save()
|
|
||||||
self.assertEqual(val.value, value)
|
|
||||||
|
|
||||||
_bool = EavAttribute.objects.create(datatype=EavAttribute.TYPE_BOOLEAN,
|
|
||||||
name='Bool',
|
|
||||||
help_text='The bool')
|
|
||||||
val = EavValue.objects.create(object=self.entity,
|
|
||||||
attribute = _bool)
|
|
||||||
value = False
|
|
||||||
val.value = value
|
|
||||||
val.save()
|
|
||||||
self.assertEqual(val.value, value)
|
|
||||||
|
|
||||||
|
|
||||||
def test_eavregistry_ataches_and_detaches_eav_attribute(self):
|
|
||||||
EavRegistry.unregister(Patient)
|
|
||||||
p = Patient()
|
|
||||||
self.assertFalse(hasattr(p, 'eav'))
|
|
||||||
|
|
||||||
EavRegistry.register(Patient)
|
|
||||||
p2 = Patient()
|
|
||||||
self.assertTrue(p2.eav)
|
|
||||||
|
|
||||||
|
|
||||||
def test_eavregistry_ataches_and_detaches_eav_attribute(self):
|
|
||||||
EavRegistry.unregister(Patient)
|
|
||||||
p = Patient()
|
|
||||||
self.assertFalse(hasattr(p, 'eav'))
|
|
||||||
|
|
||||||
EavRegistry.register(Patient)
|
|
||||||
p2 = Patient()
|
|
||||||
self.assertTrue(p2.eav)
|
|
||||||
|
|
||||||
|
|
||||||
def test_eavregistry_accept_a_settings_class_with_get_queryset(self):
|
|
||||||
EavRegistry.unregister(Patient)
|
|
||||||
|
|
||||||
class PatientEav(EavConfig):
|
|
||||||
|
|
||||||
def get_eav_attributes(self):
|
|
||||||
return EavAttribute.objects.all()
|
|
||||||
|
|
||||||
EavRegistry.register(Patient, PatientEav)
|
|
||||||
|
|
||||||
p = Patient()
|
|
||||||
|
|
||||||
EavRegistry.unregister(Patient)
|
|
||||||
|
|
||||||
|
|
||||||
def test_eavregistry_accept_a_settings_class_with_field_names(self):
|
|
||||||
|
|
||||||
p = Patient()
|
|
||||||
registered_manager = Patient.objects
|
|
||||||
EavRegistry.unregister(Patient)
|
|
||||||
|
|
||||||
class PatientEav(EavConfig):
|
|
||||||
|
|
||||||
proxy_field_name = 'my_eav'
|
|
||||||
manager_field_name ='my_objects'
|
|
||||||
|
|
||||||
EavRegistry.register(Patient, PatientEav)
|
|
||||||
|
|
||||||
p2 = Patient()
|
|
||||||
self.assertEqual(type(p.eav), type(p2.my_eav))
|
|
||||||
self.assertEqual(type(registered_manager), type(Patient.my_objects))
|
|
||||||
|
|
||||||
bak_registered_manager = Patient.objects
|
|
||||||
|
|
||||||
EavRegistry.unregister(Patient)
|
|
||||||
|
|
||||||
self.assertEqual(type(Patient.objects), type(bak_registered_manager))
|
|
||||||
Loading…
Reference in a new issue