Skip to content

Commit

Permalink
Merge pull request #699 from troygrosfield/master
Browse files Browse the repository at this point in the history
Make an abstract verstion of django's UserSocialAuth's model so it can be extended (fixes #698)
  • Loading branch information
omab committed Aug 14, 2015
2 parents 75fd2a8 + c4faaca commit a2f14d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
12 changes: 12 additions & 0 deletions social/apps/django_app/default/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.db import models


class UserSocialAuthManager(models.Manager):
"""Manager for the UserSocialAuth django model."""

def get_social_auth(self, provider, uid):
try:
return self.select_related('user').get(provider=provider,
uid=uid)
except self.model.DoesNotExist:
return None
19 changes: 14 additions & 5 deletions social/apps/django_app/default/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DjangoCodeMixin, \
BaseDjangoStorage
from social.apps.django_app.default.fields import JSONField
from social.apps.django_app.default.managers import UserSocialAuthManager


USER_MODEL = getattr(settings, setting_name('USER_MODEL'), None) or \
Expand All @@ -26,20 +27,19 @@
settings, setting_name('ASSOCIATION_HANDLE_LENGTH'), 255)


class UserSocialAuth(models.Model, DjangoUserMixin):
"""Social Auth association model"""
class AbstractUserSocialAuth(models.Model, DjangoUserMixin):
"""Abstract Social Auth association model"""
user = models.ForeignKey(USER_MODEL, related_name='social_auth')
provider = models.CharField(max_length=32)
uid = models.CharField(max_length=UID_LENGTH)
extra_data = JSONField()
objects = UserSocialAuthManager()

def __str__(self):
return str(self.user)

class Meta:
"""Meta data"""
unique_together = ('provider', 'uid')
db_table = 'social_auth_usersocialauth'
abstract = True

@classmethod
def get_social_auth(cls, provider, uid):
Expand All @@ -64,6 +64,15 @@ def user_model(cls):
return user_model


class UserSocialAuth(AbstractUserSocialAuth):
"""Social Auth association model"""

class Meta:
"""Meta data"""
unique_together = ('provider', 'uid')
db_table = 'social_auth_usersocialauth'


class Nonce(models.Model, DjangoNonceMixin):
"""One use numbers"""
server_url = models.CharField(max_length=NONCE_SERVER_URL_LENGTH)
Expand Down

0 comments on commit a2f14d1

Please sign in to comment.