]> git.parisson.com Git - django-social-auth.git/commitdiff
Load social_auth models based on a configuration setting
authorSteven Cummings <estebistec@gmail.com>
Fri, 15 Jun 2012 03:22:43 +0000 (22:22 -0500)
committerSteven Cummings <estebistec@gmail.com>
Sat, 16 Jun 2012 01:32:23 +0000 (20:32 -0500)
* This provides the ability to supply alternate storage modules
* Moved models into django_models module, and gave each model an app_label
* Defined a configuration setting, SOCIAL_AUTH_MODELS, that defaults to the django models
* Still need to define the protocol for implementing new storage modules

social_auth/django_models.py [new file with mode: 0644]
social_auth/models.py

diff --git a/social_auth/django_models.py b/social_auth/django_models.py
new file mode 100644 (file)
index 0000000..2a1cfa3
--- /dev/null
@@ -0,0 +1,94 @@
+"""Social auth models"""
+from datetime import timedelta
+
+from django.db import models
+
+from social_auth.fields import JSONField
+from social_auth.utils import setting
+
+
+# If User class is overridden, it *must* provide the following fields
+# and methods work with django-social-auth:
+#
+#   username   = CharField()
+#   last_login = DateTimeField()
+#   is_active  = BooleanField()
+#   def is_authenticated():
+#       ...
+
+if setting('SOCIAL_AUTH_USER_MODEL'):
+    User = models.get_model(*setting('SOCIAL_AUTH_USER_MODEL').rsplit('.', 1))
+else:
+    from django.contrib.auth.models import User
+
+
+class UserSocialAuth(models.Model):
+    """Social Auth association model"""
+    user = models.ForeignKey(User, related_name='social_auth')
+    provider = models.CharField(max_length=32)
+    uid = models.CharField(max_length=255)
+    extra_data = JSONField(default='{}')
+
+    class Meta:
+        """Meta data"""
+        unique_together = ('provider', 'uid')
+        app_label = 'social_auth'
+
+    def __unicode__(self):
+        """Return associated user unicode representation"""
+        return u'%s - %s' % (unicode(self.user), self.provider.title())
+
+    @property
+    def tokens(self):
+        """Return access_token stored in extra_data or None"""
+        # Make import here to avoid recursive imports :-/
+        from social_auth.backends import get_backends
+        backend = get_backends().get(self.provider)
+        if backend:
+            return backend.AUTH_BACKEND.tokens(self)
+        else:
+            return {}
+
+    def expiration_delta(self):
+        """Return saved session expiration seconds if any. Is returned in
+        the form of a timedelta data type. None is returned if there's no
+        value stored or it's malformed.
+        """
+        if self.extra_data:
+            name = setting('SOCIAL_AUTH_EXPIRATION', 'expires')
+            try:
+                return timedelta(seconds=int(self.extra_data.get(name)))
+            except (ValueError, TypeError):
+                pass
+        return None
+
+
+class Nonce(models.Model):
+    """One use numbers"""
+    server_url = models.CharField(max_length=255)
+    timestamp = models.IntegerField()
+    salt = models.CharField(max_length=40)
+
+    class Meta:
+        app_label = 'social_auth'
+
+    def __unicode__(self):
+        """Unicode representation"""
+        return self.server_url
+
+
+class Association(models.Model):
+    """OpenId account association"""
+    server_url = models.CharField(max_length=255)
+    handle = models.CharField(max_length=255)
+    secret = models.CharField(max_length=255)  # Stored base64 encoded
+    issued = models.IntegerField()
+    lifetime = models.IntegerField()
+    assoc_type = models.CharField(max_length=64)
+
+    class Meta:
+        app_label = 'social_auth'
+
+    def __unicode__(self):
+        """Unicode representation"""
+        return '%s %s' % (self.handle, self.issued)
index 7b7934e31e18ef073f46f3fbc8cbaf9f9f359fab..75a66cf0539897f9bf154e19ff8c46aa6a5bf0a8 100644 (file)
@@ -1,87 +1,15 @@
 """Social auth models"""
-from datetime import timedelta
+# TODO define protocol for implementing modules...
 
-from django.db import models
 
-from social_auth.fields import JSONField
-from social_auth.utils import setting
+from django.conf import settings
+from django.utils import importlib
 
 
-# If User class is overridden, it *must* provide the following fields
-# and methods work with django-social-auth:
-#
-#   username   = CharField()
-#   last_login = DateTimeField()
-#   is_active  = BooleanField()
-#   def is_authenticated():
-#       ...
+models_module_name = getattr(settings, 'SOCIAL_AUTH_MODELS',
+        'social_auth.django_models')
+models_module = importlib.import_module(models_module_name)
 
-if setting('SOCIAL_AUTH_USER_MODEL'):
-    User = models.get_model(*setting('SOCIAL_AUTH_USER_MODEL').rsplit('.', 1))
-else:
-    from django.contrib.auth.models import User
-
-
-class UserSocialAuth(models.Model):
-    """Social Auth association model"""
-    user = models.ForeignKey(User, related_name='social_auth')
-    provider = models.CharField(max_length=32)
-    uid = models.CharField(max_length=255)
-    extra_data = JSONField(default='{}')
-
-    class Meta:
-        """Meta data"""
-        unique_together = ('provider', 'uid')
-
-    def __unicode__(self):
-        """Return associated user unicode representation"""
-        return u'%s - %s' % (unicode(self.user), self.provider.title())
-
-    @property
-    def tokens(self):
-        """Return access_token stored in extra_data or None"""
-        # Make import here to avoid recursive imports :-/
-        from social_auth.backends import get_backends
-        backend = get_backends().get(self.provider)
-        if backend:
-            return backend.AUTH_BACKEND.tokens(self)
-        else:
-            return {}
-
-    def expiration_delta(self):
-        """Return saved session expiration seconds if any. Is returned in
-        the form of a timedelta data type. None is returned if there's no
-        value stored or it's malformed.
-        """
-        if self.extra_data:
-            name = setting('SOCIAL_AUTH_EXPIRATION', 'expires')
-            try:
-                return timedelta(seconds=int(self.extra_data.get(name)))
-            except (ValueError, TypeError):
-                pass
-        return None
-
-
-class Nonce(models.Model):
-    """One use numbers"""
-    server_url = models.CharField(max_length=255)
-    timestamp = models.IntegerField()
-    salt = models.CharField(max_length=40)
-
-    def __unicode__(self):
-        """Unicode representation"""
-        return self.server_url
-
-
-class Association(models.Model):
-    """OpenId account association"""
-    server_url = models.CharField(max_length=255)
-    handle = models.CharField(max_length=255)
-    secret = models.CharField(max_length=255)  # Stored base64 encoded
-    issued = models.IntegerField()
-    lifetime = models.IntegerField()
-    assoc_type = models.CharField(max_length=64)
-
-    def __unicode__(self):
-        """Unicode representation"""
-        return '%s %s' % (self.handle, self.issued)
+this_module = globals()
+for key in dir(models_module):
+    this_module[key] = getattr(models_module, key)