mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-03-16 22:40:26 +00:00
Changed object to entity
This commit is contained in:
parent
05dddabbe2
commit
99cd67c22d
5 changed files with 21 additions and 194 deletions
|
|
@ -16,7 +16,6 @@ class EavSlugField(models.SlugField):
|
|||
u"not start with a number, and contain "\
|
||||
u"only letters, numbers, or underscores."))
|
||||
|
||||
|
||||
@staticmethod
|
||||
def create_slug_from_name(name):
|
||||
'''
|
||||
|
|
@ -34,6 +33,7 @@ class EavSlugField(models.SlugField):
|
|||
class EavDatatypeField(models.SlugField):
|
||||
|
||||
def validate(self, value, instance):
|
||||
super(EavDatatypeField, self).validate(value, instance)
|
||||
from .models import EavAttribute
|
||||
if not instance.pk:
|
||||
return
|
||||
|
|
|
|||
25
models.py
25
models.py
|
|
@ -63,7 +63,6 @@ class EavAttribute(models.Model):
|
|||
choices=DATATYPE_CHOICES)
|
||||
|
||||
created = models.DateTimeField(_(u"created"), default=datetime.now)
|
||||
|
||||
modified = models.DateTimeField(_(u"modified"), auto_now=True)
|
||||
|
||||
labels = models.ManyToManyField(EavAttributeLabel,
|
||||
|
|
@ -96,7 +95,7 @@ class EavAttribute(models.Model):
|
|||
doesn't exist.
|
||||
'''
|
||||
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():
|
||||
return qs[0]
|
||||
|
||||
|
|
@ -107,12 +106,12 @@ class EavAttribute(models.Model):
|
|||
ct = ContentType.objects.get_for_model(entity)
|
||||
attribute = attribute or self
|
||||
try:
|
||||
eavvalue = self.eavvalue_set.get(content_type=ct,
|
||||
object_id=entity.pk,
|
||||
eavvalue = self.eavvalue_set.get(entity_ct=ct,
|
||||
entity_id=entity.pk,
|
||||
attribute=attribute)
|
||||
except EavValue.DoesNotExist:
|
||||
eavvalue = self.eavvalue_set.model(content_type=ct,
|
||||
object_id=entity.pk,
|
||||
eavvalue = self.eavvalue_set.model(entity_ct=ct,
|
||||
entity_id=entity.pk,
|
||||
attribute=attribute)
|
||||
if value != eavvalue.value:
|
||||
eavvalue.value = value
|
||||
|
|
@ -124,13 +123,13 @@ class EavAttribute(models.Model):
|
|||
|
||||
class EavValue(models.Model):
|
||||
class Meta:
|
||||
unique_together = ('content_type', 'object_id', 'attribute',
|
||||
unique_together = ('entity_ct', 'entity_id', 'attribute',
|
||||
'value_text', 'value_float', 'value_date',
|
||||
'value_bool')
|
||||
|
||||
content_type = models.ForeignKey(ContentType)
|
||||
object_id = models.IntegerField()
|
||||
object = generic.GenericForeignKey()
|
||||
entity_ct = models.ForeignKey(ContentType)
|
||||
entity_id = models.IntegerField()
|
||||
entity = generic.GenericForeignKey(ct_field='entity_ct', fk_field='entity_id')
|
||||
|
||||
value_text = models.TextField(blank=True, null=True)
|
||||
value_float = models.FloatField(blank=True, null=True)
|
||||
|
|
@ -161,7 +160,7 @@ class EavValue(models.Model):
|
|||
value = property(_get_value, _set_value)
|
||||
|
||||
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):
|
||||
|
|
@ -197,8 +196,8 @@ class EavEntity(object):
|
|||
return self._attributes_cache
|
||||
|
||||
def get_values(self):
|
||||
return EavValue.objects.filter(content_type=self.ct,
|
||||
object_id=self.model.pk).select_related()
|
||||
return EavValue.objects.filter(entity_ct=self.ct,
|
||||
entity_id=self.model.pk).select_related()
|
||||
|
||||
def get_all_attribute_slugs(self):
|
||||
if not hasattr(self, '_attributes_cache_dict'):
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ class EavBasicTests(TestCase):
|
|||
|
||||
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,
|
||||
value_text='Denver')
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ class EavBasicTests(TestCase):
|
|||
|
||||
|
||||
def test_value_creation(self):
|
||||
EavValue.objects.create(object=self.entity,
|
||||
EavValue.objects.create(entity=self.entity,
|
||||
attribute=self.attribute,
|
||||
value_float=1.2)
|
||||
|
||||
|
|
@ -63,7 +63,7 @@ class EavBasicTests(TestCase):
|
|||
_text = EavAttribute.objects.create(datatype=EavAttribute.TYPE_TEXT,
|
||||
name='Text', slug='text',
|
||||
help_text='The text')
|
||||
val = EavValue.objects.create(object=self.entity,
|
||||
val = EavValue.objects.create(entity=self.entity,
|
||||
attribute = _text)
|
||||
value = "Test text"
|
||||
val.value = value
|
||||
|
|
@ -73,7 +73,7 @@ class EavBasicTests(TestCase):
|
|||
_float = EavAttribute.objects.create(datatype=EavAttribute.TYPE_FLOAT,
|
||||
name='Float', slug='float',
|
||||
help_text='The float')
|
||||
val = EavValue.objects.create(object=self.entity,
|
||||
val = EavValue.objects.create(entity=self.entity,
|
||||
attribute = _float)
|
||||
value = 1.22
|
||||
val.value = value
|
||||
|
|
@ -84,7 +84,7 @@ class EavBasicTests(TestCase):
|
|||
_int = EavAttribute.objects.create(datatype=EavAttribute.TYPE_INT,
|
||||
name='Int', slug='int',
|
||||
help_text='The int')
|
||||
val = EavValue.objects.create(object=self.entity,
|
||||
val = EavValue.objects.create(entity=self.entity,
|
||||
attribute = _int)
|
||||
value = 7
|
||||
val.value = value
|
||||
|
|
@ -94,7 +94,7 @@ class EavBasicTests(TestCase):
|
|||
_date = EavAttribute.objects.create(datatype=EavAttribute.TYPE_DATE,
|
||||
name='Date', slug='date',
|
||||
help_text='The date')
|
||||
val = EavValue.objects.create(object=self.entity,
|
||||
val = EavValue.objects.create(entity=self.entity,
|
||||
attribute = _date)
|
||||
value = datetime.now()
|
||||
val.value = value
|
||||
|
|
@ -104,7 +104,7 @@ class EavBasicTests(TestCase):
|
|||
_bool = EavAttribute.objects.create(datatype=EavAttribute.TYPE_BOOLEAN,
|
||||
name='Bool', slug='bool',
|
||||
help_text='The bool')
|
||||
val = EavValue.objects.create(object=self.entity,
|
||||
val = EavValue.objects.create(entity=self.entity,
|
||||
attribute = _bool)
|
||||
value = False
|
||||
val.value = value
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ class EavSetterAndGetterTests(TestCase):
|
|||
|
||||
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,
|
||||
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