Fixed: get_eva_attribute are now called as a classmethod

This commit is contained in:
ksamuel 2010-09-07 13:52:16 +00:00
parent 9ce78e1418
commit bdf3e464bd
3 changed files with 50 additions and 1 deletions

View file

@ -102,6 +102,11 @@ class EavAttribute(models.Model):
u"%s" % (self, entity, qs.count()))
def save_value(self, entity, value):
"""
Save any value for an entity, calling the appropriate method
according to the type of the value.
Value should not be an EavValue but a normal value
"""
self._save_single_value(entity, value)
def _save_single_value(self, entity, value=None, attribute=None):
@ -201,7 +206,7 @@ class EavEntity(object):
except AttributeError:
pass
self._attributes_cache = self.get_eav_attributes().select_related()
self._attributes_cache = self.__class__.get_eav_attributes().select_related()
self._attributes_cache_dict = dict((s.slug, s) for s in self._attributes_cache)
return self._attributes_cache

View file

@ -137,6 +137,7 @@ class EavBasicTests(TestCase):
class PatientEav(EavConfig):
@classmethod
def get_eav_attributes(self):
return EavAttribute.objects.all()

View file

@ -56,6 +56,14 @@ class EavSetterAndGetterTests(TestCase):
self.assertEqual(patient.eav.city, 'Paris')
def test_save_value(self):
# TODO: update test_save_value when multiple values are out
# save value
self.attribute.save_value(self.patient, 'Paris')
self.assertEqual(self.patient.eav.city, 'Paris')
def test_you_can_assign_a_value_to_an_unsaved_object(self):
patient = Patient()
@ -139,3 +147,38 @@ class EavSetterAndGetterTests(TestCase):
# remove a label that is not attach does nothing
self.attribute.remove_label('a')
self.attribute.remove_label('x')
def test_can_filter_attribute_availability_for_entity(self):
attribute = EavAttribute.objects\
.create(datatype=EavAttribute.TYPE_TEXT,
name='Country', slug='country')
self.patient.eav.city = 'Paris'
self.patient.save()
self.assertEqual(Patient.objects.get(pk=self.patient.pk).eav.city,
'Paris')
EavRegistry.unregister(Patient)
class PatientEav(EavConfig):
@classmethod
def get_eav_attributes(cls):
return EavAttribute.objects.filter(slug='country')
EavRegistry.register(Patient, PatientEav)
p = Patient.objects.create(name='Patrick')
p.eav.city = 'Paris'
p.eav.country = 'USA'
p.save()
p = Patient.objects.get(pk=self.patient.pk)
self.assertFalse(p.eav.city)
self.assertEqual(p.eav.country, 'USA')
p = Patient()