From a34731ed0057ab357996c3ef0897faf36ad228a3 Mon Sep 17 00:00:00 2001 From: Luke Faraone Date: Fri, 1 Nov 2013 15:22:12 -0400 Subject: [PATCH] Factor out get_user in zproject/backends.py (imported from commit d60b5440722ed596ffbcb81086b2f62d535288dd) --- zproject/backends.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/zproject/backends.py b/zproject/backends.py index 8f62e86c3a..9234d291dc 100644 --- a/zproject/backends.py +++ b/zproject/backends.py @@ -3,7 +3,15 @@ from zerver.models import UserProfile, get_user_profile_by_id, \ from openid.consumer.consumer import SUCCESS -class EmailAuthBackend(object): +class ZulipAuthMixin(object): + def get_user(self, user_profile_id): + """ Get a UserProfile object from the user_profile_id. """ + try: + return get_user_profile_by_id(user_profile_id) + except UserProfile.DoesNotExist: + return None + +class EmailAuthBackend(ZulipAuthMixin): """ Email Authentication Backend @@ -26,16 +34,9 @@ class EmailAuthBackend(object): except UserProfile.DoesNotExist: return None - def get_user(self, user_profile_id): - """ Get a UserProfile object from the user_profile_id. """ - try: - return get_user_profile_by_id(user_profile_id) - except UserProfile.DoesNotExist: - return None - # Adapted from http://djangosnippets.org/snippets/2183/ by user Hangya (September 1, 2010) -class GoogleBackend(object): +class GoogleBackend(ZulipAuthMixin): def authenticate(self, openid_response): if openid_response is None: return None @@ -52,9 +53,3 @@ class GoogleBackend(object): return user_profile - def get_user(self, user_profile_id): - """ Get a UserProfile object from the user_profile_id. """ - try: - return get_user_profile_by_id(user_profile_id) - except UserProfile.DoesNotExist: - return None