From 3d94eb3ec2ae41dfd63d636d8f4cfc31153b0043 Mon Sep 17 00:00:00 2001 From: JP White Date: Sat, 15 Aug 2015 23:34:11 -0400 Subject: [PATCH] Adding registration decorator and updating README with Prereqs for Sites Framework --- README.rst | 26 ++++++++++++++++++++++++-- eav/decorators.py | 19 +++++++++++++++++++ eav/tests/models.py | 10 ++++++++++ eav/tests/registry.py | 7 ++++++- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 eav/decorators.py diff --git a/README.rst b/README.rst index c0aac2d..ec5999f 100644 --- a/README.rst +++ b/README.rst @@ -35,12 +35,29 @@ You can install django-eav directly from guthub:: pip install -e git+git://github.com/mvpdev/django-eav.git#egg=django-eav +Prerequisites +------------- + +Django Sites Framework +~~~~~~~~~~~~~~~~~~~~~~ +As of Django 1.7, the `Sites framework `_ is not enabled by default; Django-EAV requires this framework. +To enable the sites framework, follow these steps: + +Add ``django.contrib.sites`` to your INSTALLED_APPS setting. Be sure to add sites to the installed apps list BEFORE eav! + +Define a ``SITE_ID`` setting:: + + SITE_ID = 1 + +Run ``migrate`` + + Usage ----- Edit settings.py ~~~~~~~~~~~~~~~~ -Add ``eav`` to your ``INSTALLED_APPS`` in your project's ``settings.py`` file. +Add ``eav`` to your ``INSTALLED_APPS`` in your project's ``settings.py`` file. Be sure to add eav to the installed apps list AFTER the sites framework! Register your model(s) ~~~~~~~~~~~~~~~~~~~~~~ @@ -51,7 +68,12 @@ model with eav:: >>> eav.register(MyModel) Generally you would do this in your ``models.py`` immediate after your model -declarations. +declarations. Alternatively, you can use the registration decorator provided:: + + from eav.decorators import register_eav + @register_eav() + class MyModel(models.Model): + ... Create some attributes ~~~~~~~~~~~~~~~~~~~~~~ diff --git a/eav/decorators.py b/eav/decorators.py new file mode 100644 index 0000000..ab19e1e --- /dev/null +++ b/eav/decorators.py @@ -0,0 +1,19 @@ +def register_eav(**kwargs): + """ + Registers the given model(s) classes and wrapped Model class with + django-eav: + + @register_eav + class Author(models.Model): + pass + """ + from . import register + from django.db.models import Model + + def _model_eav_wrapper(model_class): + if not issubclass(model_class, Model): + raise ValueError('Wrapped class must subclass Model.') + register(model_class, **kwargs) + return model_class + + return _model_eav_wrapper \ No newline at end of file diff --git a/eav/tests/models.py b/eav/tests/models.py index 0c54584..7a0f270 100644 --- a/eav/tests/models.py +++ b/eav/tests/models.py @@ -1,4 +1,5 @@ from django.db import models +from ..decorators import register_eav class Patient(models.Model): class Meta: @@ -19,3 +20,12 @@ class Encounter(models.Model): def __unicode__(self): return '%s: encounter num %d' % (self.patient, self.num) +@register_eav() +class ExampleModel(models.Model) + class Meta: + app_label = 'eav' + + name = models.CharField(max_length=12) + + def __unicode__(self): + return self.name diff --git a/eav/tests/registry.py b/eav/tests/registry.py index 92bf115..0239f24 100644 --- a/eav/tests/registry.py +++ b/eav/tests/registry.py @@ -5,7 +5,7 @@ from ..registry import Registry, EavConfig from ..managers import EntityManager from ..models import Attribute -from .models import Patient, Encounter +from .models import Patient, Encounter, ExampleModel class RegistryTests(TestCase): @@ -56,6 +56,11 @@ class RegistryTests(TestCase): eav.unregister(Patient) eav.unregister(Encounter) + def test_registering_via_decorator_with_defaults(self): + self.assertTrue(hasattr(ExampleModel, '_eav_config_cls')) + self.assertEqual(ExampleModel._eav_config_cls.manager_attr, 'objects') + self.assertEqual(ExampleModel._eav_config_cls.eav_attr, 'eav') + def test_unregistering(self): old_mgr = Patient.objects eav.register(Patient)