summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorValentin Deniaud <vdeniaud@entrouvert.com>2019-03-29 15:30:39 (GMT)
committerValentin Deniaud <vdeniaud@entrouvert.com>2019-03-29 15:30:39 (GMT)
commitb95ea4d6ce3e586d5d56aae3e5351cba6d79fd67 (patch)
treeaa2d56da30665edbd348f00812c89cb8583bb7e7
parent90c7daaa8ab0c6de7b1b5a6f9d737a93cf3dff06 (diff)
downloaddjango-mellon-auth_levels.zip
django-mellon-auth_levels.tar.gz
django-mellon-auth_levels.tar.bz2
wip add auth_levelsauth_levels
-rw-r--r--mellon/models.py44
1 files changed, 43 insertions, 1 deletions
diff --git a/mellon/models.py b/mellon/models.py
index 20aba33..b89e7f4 100644
--- a/mellon/models.py
+++ b/mellon/models.py
@@ -1,5 +1,8 @@
+import django
from django.contrib.auth.models import Group
from django.db import models
+from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor
+from django.db.transaction import atomic
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
@@ -23,8 +26,47 @@ class UserSAMLIdentifier(models.Model):
unique_together = (('issuer', 'name_id'),)
+class AutoSingleRelatedObjectDescriptor(ReverseOneToOneDescriptor):
+ """From https://github.com/skorokithakis/django-annoying
+ Temporary, we will write a migration after testing"""
+
+ @atomic
+ def __get__(self, instance, instance_type=None):
+ model = getattr(self.related, 'related_model', self.related.model)
+
+ try:
+ return (
+ super(AutoSingleRelatedObjectDescriptor, self)
+ .__get__(instance, instance_type)
+ )
+ except model.DoesNotExist:
+ # Using get_or_create instead() of save() or create() as it better handles race conditions
+ obj, _ = model.objects.get_or_create(**{self.related.field.name: instance})
+
+ # Update Django's cache, otherwise first 2 calls to obj.relobj
+ # will return 2 different in-memory objects
+ if django.VERSION >= (2, 0):
+ self.related.set_cached_value(instance, obj)
+ self.related.field.set_cached_value(obj, instance)
+ else:
+ setattr(instance, self.cache_name, obj)
+ setattr(obj, self.related.field.get_cache_name(), instance)
+ return obj
+
+
+class AutoOneToOneField(models.OneToOneField):
+ """From https://github.com/skorokithakis/django-annoying"""
+
+ def contribute_to_related_class(self, cls, related):
+ setattr(
+ cls,
+ related.get_accessor_name(),
+ AutoSingleRelatedObjectDescriptor(related)
+ )
+
+
class AuthenticationLevel(models.Model):
- group = models.OneToOneField(Group, related_name='auth_level')
+ group = AutoOneToOneField(Group, related_name='auth_level')
# ou plutot laisser blanc pour qu'il y ait une erreur si on
# a pas recu les niveaux ?
value = models.PositiveSmallIntegerField(default=1)