From: Steven Cummings Date: Fri, 15 Jun 2012 03:22:43 +0000 (-0500) Subject: Load social_auth models based on a configuration setting X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=ca9252e9a925bb9dc51d8d5c83cbdaffec810249;p=django-social-auth.git Load social_auth models based on a configuration setting * 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 --- diff --git a/social_auth/django_models.py b/social_auth/django_models.py new file mode 100644 index 0000000..2a1cfa3 --- /dev/null +++ b/social_auth/django_models.py @@ -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) diff --git a/social_auth/models.py b/social_auth/models.py index 7b7934e..75a66cf 100644 --- a/social_auth/models.py +++ b/social_auth/models.py @@ -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)