Skip to content

Commit a2f21c4

Browse files
committed
added python 3 and uuid entity id support
1 parent 41e99fa commit a2f21c4

File tree

10 files changed

+95
-13
lines changed

10 files changed

+95
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66
_build
77
build
88
django_eav.egg-info/*
9+
*.DS_Store
10+
env/
11+
.idea/

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ django-eav
55
Introduction
66
------------
77

8+
89
django-eav provides an Entity-Attribute-Value storage model for django apps.
910

1011
For a decent explanation of what an Entity-Attribute-Value storage model is,

docs/index.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ For example::
139139

140140
eav.register(MyModel, MyEavConfigClass)
141141

142+
To override ``entity_id`` to use ``entity_uuid`` for entity relationship with
143+
model add ``EAV_ENTITY_ID_TYPE='uuid'`` to use ``entity_uuid`` (defaults to
144+
int for ``entity_id``)
142145

143146
Using Attributes
144147
================
@@ -151,7 +154,7 @@ First, let's create some attributes::
151154

152155
>>> Attribute.objects.create(name='Weight', datatype=Attribute.TYPE_FLOAT)
153156
>>> Attribute.objects.create(name='Height', datatype=Attribute.TYPE_INT)
154-
>>> Attribute.objects.create(name='Is pregant?', datatype=Attribute.TYPE_BOOLEAN)
157+
>>> Attribute.objects.create(name='Is pregnant?', datatype=Attribute.TYPE_BOOLEAN)
155158

156159
Now let's create a patient, and set some of these attributes::
157160

eav/admin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from .models import Attribute, Value, EnumValue, EnumGroup
2929

30+
3031
class BaseEntityAdmin(ModelAdmin):
3132

3233
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
@@ -93,6 +94,7 @@ def get_fieldsets(self, request, obj=None):
9394

9495
return [(None, {'fields': list(form.fields.keys())})]
9596

97+
9698
class AttributeAdmin(ModelAdmin):
9799
list_display = ('name', 'content_type', 'slug', 'datatype', 'description', 'site')
98100
list_filter = ['site']
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.10.3 on 2016-11-04 09:22
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
import eav.fields
8+
9+
10+
class Migration(migrations.Migration):
11+
12+
dependencies = [
13+
('eav', '0002_auto_20161014_0157'),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name='Encounter',
19+
fields=[
20+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21+
('num', models.PositiveSmallIntegerField()),
22+
],
23+
),
24+
migrations.CreateModel(
25+
name='Patient',
26+
fields=[
27+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
28+
('name', models.CharField(max_length=12)),
29+
],
30+
),
31+
migrations.AlterField(
32+
model_name='attribute',
33+
name='datatype',
34+
field=eav.fields.EavDatatypeField(choices=[('text', 'Text'), ('float', 'Float'), ('int', 'Integer'), ('date', 'Date'), ('bool', 'True / False'), ('object', 'Django Object'), ('enum', 'Multiple Choice')], max_length=6, verbose_name='data type'),
35+
),
36+
migrations.AddField(
37+
model_name='encounter',
38+
name='patient',
39+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='eav.Patient'),
40+
),
41+
migrations.AddField(
42+
model_name='value',
43+
name='entity_uuid',
44+
field=models.UUIDField(blank=True, null=True),
45+
),
46+
migrations.AlterField(
47+
model_name='value',
48+
name='entity_id',
49+
field=models.IntegerField(blank=True, null=True),
50+
),
51+
]

eav/models.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
from .validators import *
5151
from .fields import EavSlugField, EavDatatypeField
52+
from .utils.utilities import Utils
5253

5354

5455
class EnumValue(models.Model):
@@ -86,7 +87,7 @@ class EnumValue(models.Model):
8687
@python_2_unicode_compatible
8788
def __str__(self):
8889
return self.value
89-
90+
9091

9192
class EnumGroup(models.Model):
9293
'''
@@ -304,16 +305,15 @@ def save_value(self, entity, value):
304305
Attribute and *entity*, it will delete that :class:`Value` object.
305306
'''
306307
ct = ContentType.objects.get_for_model(entity)
308+
entity_dict = dict(entity_ct=ct,
309+
attribute=self)
310+
entity_dict[Value.entity_id_type] = entity.pk
307311
try:
308-
value_obj = self.value_set.get(entity_ct=ct,
309-
entity_id=entity.pk,
310-
attribute=self)
312+
value_obj = self.value_set.get(**entity_dict)
311313
except Value.DoesNotExist:
312314
if value == None or value == '':
313315
return
314-
value_obj = Value.objects.create(entity_ct=ct,
315-
entity_id=entity.pk,
316-
attribute=self)
316+
value_obj = Value.objects.create(**entity_dict)
317317
if value == None or value == '':
318318
value_obj.delete()
319319
return
@@ -347,10 +347,13 @@ class Value(models.Model):
347347
<Value: crazy_dev_user - Favorite Drink: "red bull">
348348
'''
349349

350+
entity_id_type = Utils().get_eav_entity_id_type()
351+
350352
entity_ct = models.ForeignKey(ContentType, related_name='value_entities')
351-
entity_id = models.IntegerField()
353+
entity_id = models.IntegerField(blank=True, null=True)
354+
entity_uuid = models.UUIDField(blank=True, null=True)
352355
entity = generic.GenericForeignKey(ct_field='entity_ct',
353-
fk_field='entity_id')
356+
fk_field=entity_id_type)
354357

355358
value_text = models.TextField(blank=True, null=True)
356359
value_float = models.FloatField(blank=True, null=True)
@@ -521,8 +524,9 @@ def get_values(self):
521524
'''
522525
Get all set :class:`Value` objects for self.model
523526
'''
524-
return Value.objects.filter(entity_ct=self.ct,
525-
entity_id=self.model.pk).select_related()
527+
entiity_filter = dict(entity_ct=self.ct)
528+
entiity_filter[Value.entity_id_type] = self.model.pk
529+
return Value.objects.filter(**entiity_filter).select_related()
526530

527531
def get_all_attribute_slugs(self):
528532
'''

eav/registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def _attach_generic_relation(self):
165165
gr_name = self.config_cls.generic_relation_attr.lower()
166166
generic_relation = \
167167
generic.GenericRelation(Value,
168-
object_id_field='entity_id',
168+
object_id_field=Value.entity_id_type,
169169
content_type_field='entity_ct',
170170
related_query_name=rel_name)
171171
generic_relation.contribute_to_class(self.model_cls, gr_name)

eav/utils/__init__.py

Whitespace-only changes.

eav/utils/utilities.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.conf import settings
2+
3+
4+
class Utils(object):
5+
ENTITY_ID_TYPES = {
6+
'uuid': 'entity_uuid',
7+
'int': 'entity_id'
8+
}
9+
10+
def get_eav_entity_id_type(self):
11+
key = getattr(settings, 'EAV_ENTITY_ID_TYPE', 'int')
12+
try:
13+
return self.ENTITY_ID_TYPES[key]
14+
except KeyError:
15+
print('%s not supported, kindly try uuid or int, defaulting to int' % key)
16+
return self.ENTITY_ID_TYPES['int']

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Django==1.10.3
2+
eav-django==1.4.7

0 commit comments

Comments
 (0)