Skip to content

Commit ac6dcc8

Browse files
authored
Merge pull request #33 from jpwhite3/master
Adding pre-reqs and registration decorator. Resolves issues #23 and #39
2 parents e046c54 + 36bfc2e commit ac6dcc8

4 files changed

Lines changed: 65 additions & 3 deletions

File tree

README.rst

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,29 @@ You can install django-eav directly from guthub::
3535

3636
pip install -e git+git://github.com/mvpdev/django-eav.git#egg=django-eav
3737

38+
Prerequisites
39+
-------------
40+
41+
Django Sites Framework
42+
~~~~~~~~~~~~~~~~~~~~~~
43+
As of Django 1.7, the `Sites framework <https://docs.djangoproject.com/en/1.8/ref/contrib/sites/#enabling-the-sites-framework>`_ is not enabled by default; Django-EAV requires this framework.
44+
To enable the sites framework, follow these steps:
45+
46+
Add ``django.contrib.sites`` to your INSTALLED_APPS setting. Be sure to add sites to the installed apps list BEFORE eav!
47+
48+
Define a ``SITE_ID`` setting::
49+
50+
SITE_ID = 1
51+
52+
Run ``migrate``
53+
54+
3855
Usage
3956
-----
4057

4158
Edit settings.py
4259
~~~~~~~~~~~~~~~~
43-
Add ``eav`` to your ``INSTALLED_APPS`` in your project's ``settings.py`` file.
60+
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!
4461

4562
Register your model(s)
4663
~~~~~~~~~~~~~~~~~~~~~~
@@ -51,7 +68,12 @@ model with eav::
5168
>>> eav.register(MyModel)
5269

5370
Generally you would do this in your ``models.py`` immediate after your model
54-
declarations.
71+
declarations. Alternatively, you can use the registration decorator provided::
72+
73+
from eav.decorators import register_eav
74+
@register_eav()
75+
class MyModel(models.Model):
76+
...
5577

5678
Create some attributes
5779
~~~~~~~~~~~~~~~~~~~~~~

eav/decorators.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def register_eav(**kwargs):
2+
"""
3+
Registers the given model(s) classes and wrapped Model class with
4+
django-eav:
5+
6+
@register_eav
7+
class Author(models.Model):
8+
pass
9+
"""
10+
from . import register
11+
from django.db.models import Model
12+
13+
def _model_eav_wrapper(model_class):
14+
if not issubclass(model_class, Model):
15+
raise ValueError('Wrapped class must subclass Model.')
16+
register(model_class, **kwargs)
17+
return model_class
18+
19+
return _model_eav_wrapper

eav/tests/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.db import models
2+
from ..decorators import register_eav
23

34
class Patient(models.Model):
45
class Meta:
@@ -19,3 +20,12 @@ class Meta:
1920
def __unicode__(self):
2021
return '%s: encounter num %d' % (self.patient, self.num)
2122

23+
@register_eav()
24+
class ExampleModel(models.Model):
25+
class Meta:
26+
app_label = 'eav'
27+
28+
name = models.CharField(max_length=12)
29+
30+
def __unicode__(self):
31+
return self.name

eav/tests/registry.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ..managers import EntityManager
66
from ..models import Attribute
77

8-
from .models import Patient, Encounter
8+
from .models import Patient, Encounter, ExampleModel
99

1010

1111
class RegistryTests(TestCase):
@@ -56,6 +56,11 @@ def test_registering_overriding_defaults(self):
5656
eav.unregister(Patient)
5757
eav.unregister(Encounter)
5858

59+
def test_registering_via_decorator_with_defaults(self):
60+
self.assertTrue(hasattr(ExampleModel, '_eav_config_cls'))
61+
self.assertEqual(ExampleModel._eav_config_cls.manager_attr, 'objects')
62+
self.assertEqual(ExampleModel._eav_config_cls.eav_attr, 'eav')
63+
5964
def test_unregistering(self):
6065
old_mgr = Patient.objects
6166
eav.register(Patient)
@@ -65,6 +70,12 @@ def test_unregistering(self):
6570
self.assertEqual(Patient.objects, old_mgr)
6671
self.assertFalse(hasattr(Patient, '_eav_config_cls'))
6772

73+
def test_unregistering_via_decorator(self):
74+
self.assertTrue(ExampleModel.objects.__class__.__name__ == 'EntityManager')
75+
eav.unregister(ExampleModel)
76+
e = ExampleModel()
77+
self.assertFalse(ExampleModel.objects.__class__.__name__ == 'EntityManager')
78+
6879
def test_unregistering_unregistered_model_proceeds_silently(self):
6980
eav.unregister(Patient)
7081

0 commit comments

Comments
 (0)