From 5df5ea02f1710b2f3b1c4cee22646b7f8d33d8a7 Mon Sep 17 00:00:00 2001 From: Troy Grosfield Date: Wed, 29 Jul 2015 10:42:03 -0600 Subject: [PATCH 1/2] Adding a model manager for the django app. --- social/apps/django_app/default/managers.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 social/apps/django_app/default/managers.py diff --git a/social/apps/django_app/default/managers.py b/social/apps/django_app/default/managers.py new file mode 100644 index 000000000..5e8769d7f --- /dev/null +++ b/social/apps/django_app/default/managers.py @@ -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 From c4faacaf97353a864a0f7a6f514e5b64792291dd Mon Sep 17 00:00:00 2001 From: Troy Grosfield Date: Wed, 29 Jul 2015 10:43:23 -0600 Subject: [PATCH 2/2] Adding abstract UserSocialAuth model as well as a model manager class. --- social/apps/django_app/default/models.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/social/apps/django_app/default/models.py b/social/apps/django_app/default/models.py index d4432b6aa..16cb3d130 100644 --- a/social/apps/django_app/default/models.py +++ b/social/apps/django_app/default/models.py @@ -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 \ @@ -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): @@ -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)