def twitter_followers_on_drawquest(request, twitter_access_token, twitter_access_token_secret): """ Returns one field, `users`, a list of `User` dicts. """ twitter_user = TwitterUser.get_or_create_from_access_token( twitter_access_token, twitter_access_token_secret) twitter_friends = twitter_user.followers_on_drawquest( twitter_access_token, twitter_access_token_secret) twitter_friends = list(twitter_friends.select_related('user')) twitter_friend_uids = dict( (friend.user_id, friend.twitter_uid) for friend in twitter_friends) users = CachedCall.multicall([ User.details_by_id(twitter_friend.user_id) for twitter_friend in twitter_friends if twitter_friend.user_id is not None ]) for user in users: user.twitter_uid = twitter_friend_uids[user.id] if request.user.is_authenticated() and request.user.id != user.id: user.viewer_is_following = request.user.is_following(user.id) return { 'users': users, }
def login_with_twitter(request, twitter_access_token, twitter_access_token_secret): def denied(): raise PermissionDenied("No DrawQuest user exists for this Twitter account.") try: twitter_user = TwitterUser.get_from_access_token(twitter_access_token, twitter_access_token_secret) except TwitterUser.DoesNotExist: denied() if twitter_user.user is None: denied() try: return _login_user(request, twitter_user.user) except InvalidTwitterAccessToken: raise PermissionDenied(_("Invalid Twitter access token, most likely because of revoked access."))
def twitter_followers_on_drawquest(request, twitter_access_token, twitter_access_token_secret): """ Returns one field, `users`, a list of `User` dicts. """ twitter_user = TwitterUser.get_or_create_from_access_token(twitter_access_token, twitter_access_token_secret) twitter_friends = twitter_user.followers_on_drawquest(twitter_access_token, twitter_access_token_secret) twitter_friends = list(twitter_friends.select_related('user')) twitter_friend_uids = dict((friend.user_id, friend.twitter_uid) for friend in twitter_friends) users = CachedCall.multicall([User.details_by_id(twitter_friend.user_id) for twitter_friend in twitter_friends if twitter_friend.user_id is not None]) for user in users: user.twitter_uid = twitter_friend_uids[user.id] if request.user.is_authenticated() and request.user.id != user.id: user.viewer_is_following = request.user.is_following(user.id) return { 'users': users, }
def login_with_twitter(request, twitter_access_token, twitter_access_token_secret): def denied(): raise PermissionDenied( "No DrawQuest user exists for this Twitter account.") try: twitter_user = TwitterUser.get_from_access_token( twitter_access_token, twitter_access_token_secret) except TwitterUser.DoesNotExist: denied() if twitter_user.user is None: denied() try: return _login_user(request, twitter_user.user) except InvalidTwitterAccessToken: raise PermissionDenied( _("Invalid Twitter access token, most likely because of revoked access." ))
def signup(request, username, password, email, facebook_access_token=None, twitter_access_token=None, twitter_access_token_secret=None): if check_rate_limit(request, username): raise ServiceError( _("Too many signup attempts. Wait a minute and try again.")) if not _validate_charset(username): raise ValidationError({ 'username': [ _("Usernames can only contain the letters a-z or A-Z, digits, and underscores." ) ], }) if not _validate_charset(email): raise ValidationError({ 'email': [ _("Sorry, your email address contains invalid characters. Please remove them and try again." ) ], }) try: return _login(request, password, username=username, email=email) except ValidationError: pass errors = defaultdict(list) fb_user = None twitter_user = None if facebook_access_token: fb_user = FacebookUser.get_or_create_from_access_token( facebook_access_token) elif twitter_access_token and twitter_access_token_secret: twitter_user = TwitterUser.get_or_create_from_access_token( twitter_access_token, twitter_access_token_secret) def username_taken(): # Ugly hack. for error in errors['username']: if 'taken' in error: return errors['username'].append(_("Sorry! That username is already taken.")) if not password: errors['password'].append(_("Please enter a password.")) if not User.validate_password(password): errors['password'].append( _(u"Sorry, your password is too short. Please use %(count)d or more characters." % {'count': User.MINIMUM_PASSWORD_LENGTH})) if not email: errors['email'].append(_("Please enter your email address.")) elif not User.validate_email(email): errors['email'].append(_("Please enter a valid email address.")) username_error = User.validate_username(username) if username_error: errors['username'].append(username_error) if not User.email_is_unused(email): errors['email'].append( _("Sorry! That email address is already being used for an account. Try signing in instead, or use another email address." )) if errors: if fb_user: fb_user.delete() raise ValidationError(errors) try: user = User.objects.create_user(username, email, password) except IntegrityError: username_taken() raise ValidationError(errors) ui = UserInfo.objects.create(user=user) ui.update_hashes() if request.app_version is not None: user.kv.signup_app_version.set(request.app_version) if fb_user: fb_user.user = user fb_user.save() fb_user.notify_friends_of_signup(facebook_access_token) fb_user.respond_to_apprequest_invites(facebook_access_token) user.migrate_facebook_avatar(request, facebook_access_token) elif twitter_user: twitter_user.user = user twitter_user.save() twitter_user.notify_followers_of_signup(twitter_access_token, twitter_access_token_secret) twitter_user.auto_follow_from_invite(twitter_access_token, twitter_access_token_secret) @bgwork.defer def migrate_twitter_avatar(): user.migrate_twitter_avatar(request, twitter_access_token, twitter_access_token_secret) user = auth.authenticate(username=username, password=password) # auth.login starts a new session and copies the session data from the old one to the new one auth.login(request, user) return { 'user': PrivateUserDetails.from_id(user.id).to_client(), 'user_bio': user.userinfo.bio_text, 'user_subscribed_to_starred': is_subscribed(user, 'starred'), 'comment_count': 0, 'quest_count': Quest.all_objects.filter(author=user).count(), 'sessionid': request.session.session_key, 'migrated_from_canvas_account': False, 'login': False, 'heavy_state_sync': heavy_state_sync(request.user, app_version=request.app_version, app_version_tuple=request.app_version_tuple), }
def signup(request, username, password, email, facebook_access_token=None, twitter_access_token=None, twitter_access_token_secret=None): if check_rate_limit(request, username): raise ServiceError(_("Too many signup attempts. Wait a minute and try again.")) if not _validate_charset(username): raise ValidationError({ 'username': [_("Usernames can only contain the letters a-z or A-Z, digits, and underscores.")], }) if not _validate_charset(email): raise ValidationError({ 'email': [_("Sorry, your email address contains invalid characters. Please remove them and try again.")], }) try: return _login(request, password, username=username, email=email) except ValidationError: pass errors = defaultdict(list) fb_user = None twitter_user = None if facebook_access_token: fb_user = FacebookUser.get_or_create_from_access_token(facebook_access_token) elif twitter_access_token and twitter_access_token_secret: twitter_user = TwitterUser.get_or_create_from_access_token(twitter_access_token, twitter_access_token_secret) def username_taken(): # Ugly hack. for error in errors['username']: if 'taken' in error: return errors['username'].append(_("Sorry! That username is already taken.")) if not password: errors['password'].append(_("Please enter a password.")) if not User.validate_password(password): errors['password'].append(_(u"Sorry, your password is too short. Please use %(count)d or more characters." % {'count': User.MINIMUM_PASSWORD_LENGTH})) if not email: errors['email'].append(_("Please enter your email address.")) elif not User.validate_email(email): errors['email'].append(_("Please enter a valid email address.")) username_error = User.validate_username(username) if username_error: errors['username'].append(username_error) if not User.email_is_unused(email): errors['email'].append(_("Sorry! That email address is already being used for an account. Try signing in instead, or use another email address.")) if errors: if fb_user: fb_user.delete() raise ValidationError(errors) try: user = User.objects.create_user(username, email, password) except IntegrityError: username_taken() raise ValidationError(errors) ui = UserInfo.objects.create(user=user) ui.update_hashes() if request.app_version is not None: user.kv.signup_app_version.set(request.app_version) if fb_user: fb_user.user = user fb_user.save() fb_user.notify_friends_of_signup(facebook_access_token) fb_user.respond_to_apprequest_invites(facebook_access_token) user.migrate_facebook_avatar(request, facebook_access_token) elif twitter_user: twitter_user.user = user twitter_user.save() twitter_user.notify_followers_of_signup(twitter_access_token, twitter_access_token_secret) twitter_user.auto_follow_from_invite(twitter_access_token, twitter_access_token_secret) @bgwork.defer def migrate_twitter_avatar(): user.migrate_twitter_avatar(request, twitter_access_token, twitter_access_token_secret) user = auth.authenticate(username=username, password=password) # auth.login starts a new session and copies the session data from the old one to the new one auth.login(request, user) return { 'user': PrivateUserDetails.from_id(user.id).to_client(), 'user_bio': user.userinfo.bio_text, 'user_subscribed_to_starred': is_subscribed(user, 'starred'), 'comment_count': 0, 'quest_count': Quest.all_objects.filter(author=user).count(), 'sessionid': request.session.session_key, 'migrated_from_canvas_account': False, 'login': False, 'heavy_state_sync': heavy_state_sync(request.user, app_version=request.app_version, app_version_tuple=request.app_version_tuple), }