def test_create(self): # We shouldn't emit the signal when we initially create a user with test_utils.mock_handler(signals.user_profile_changed) as handler: u = User() u.first_name = 'ben' u.save() assert_false(handler.called)
def authenticate(self, access_token): '''authenticates the token by requesting user information from twitter ''' twitter = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, access_token) try: userinfo = twitter.GetUserInfo() except: # If we cannot get the user information, user cannot be authenticated raise screen_name = userinfo.screen_name img_url = userinfo.profile_image_url try: user_profile = TwitterUserProfile.objects.get(screen_name = screen_name) if user_profile.user.is_active: return user_profile.user else: return except TwitterUserProfile.DoesNotExist: #Create new user same_name_count = User.objects.filter(username__startswith = screen_name).count() if same_name_count: username = '******' % (screen_name, same_name_count + 1) else: username = screen_name username = '******'+username name_count = AuthUser.objects.filter(username__startswith = username).count() if name_count: username = '******'%(username, name_count + 1) user = User(username = username) temp_password = User.objects.make_random_password(length=12) user.set_password(temp_password) name_data = userinfo.name.split() try: first_name, last_name = name_data[0], ' '.join(name_data[1:]) except: first_name, last_name = screen_name, '' user.first_name, user.last_name = first_name, last_name if img_url: img = ContentFile(urlopen(img_url).read()) name = img_url.split('/')[-1] user.picture.save(name, img, False) #user.email = '%s@twitteruser.%s.com'%(userinfo.screen_name, settings.SITE_NAME) user.save() userprofile = TwitterUserProfile(user = user, screen_name = screen_name) userprofile.access_token = access_token.key userprofile.url = userinfo.url userprofile.location = userinfo.location userprofile.description = userinfo.description userprofile.profile_image_url = userinfo.profile_image_url userprofile.save() AuthMeta(user=user, provider='Twitter').save() return user
def authenticate(self, access_token): '''authenticates the token by requesting user information from twitter ''' twitter = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, access_token) try: userinfo = twitter.GetUserInfo() except: # If we cannot get the user information, user cannot be authenticated raise screen_name = userinfo.screen_name img_url = userinfo.profile_image_url try: user_profile = TwitterUserProfile.objects.get(screen_name = screen_name) if user_profile.user.is_active: return user_profile.user else: return except TwitterUserProfile.DoesNotExist: #Create new user same_name_count = User.objects.filter(username__startswith = screen_name).count() if same_name_count: username = '******' % (screen_name, same_name_count + 1) else: username = screen_name username = '******'+username name_count = AuthUser.objects.filter(username__startswith = username).count() if name_count: username = '******'%(username, name_count + 1) user = User(username = username) temp_password = User.objects.make_random_password(length=12) user.set_password(temp_password) name_data = userinfo.name.split() try: first_name, last_name = name_data[0], ' '.join(name_data[1:]) except: first_name, last_name = screen_name, '' user.first_name, user.last_name = first_name, last_name if img_url: img = ContentFile(urlopen(img_url).read()) name = img_url.split('/')[-1] user.picture.save(name, img, False) #user.email = '%s@twitteruser.%s.com'%(userinfo.screen_name, settings.SITE_NAME) user.save() userprofile = TwitterUserProfile(user = user, screen_name = screen_name) userprofile.access_token = access_token.key userprofile.url = userinfo.url userprofile.location = userinfo.location userprofile.description = userinfo.description userprofile.profile_image_url = userinfo.profile_image_url userprofile.save() AuthMeta(user=user, provider='Twitter').save() return user