def fb_connect(request): """Merge FB session with current profile""" next = request.REQUEST.get("next", "/") if request.user_profile.fb_userid: # User has already connected an FB account add_message(request, u"You've already connected a Facebook account.") return HttpResponseRedirect(next) api_key, secret = settings.FB_API_KEY, settings.FB_SECRET_KEY fb_uid, fb_session_key, fb_session_secret = get_fb_session(request) if not fb_uid: return _logout_redirect(request, next) try: profile = UserProfile.objects.active().filter(fb_userid=fb_uid)[:1].get() add_message(request, u"You've already connected this Facebook account with another RiotVine account.") return HttpResponseRedirect(next) except UserProfile.DoesNotExist: # save fb id profile = request.user_profile profile.fb_userid = fb_uid profile.is_sso = False profile.fb_suggested = 0 profile.save() request.user_profile = profile request.session['fb_uid'] = fb_uid # if this user was invited by any existing users, create a friendship inv = FBInvitedUser.objects.select_related('inviter_profile').filter(fb_userid=fb_uid) for u in inv: inviter = u.inviter_profile Friendship.objects.make_friends(inviter, request.user_profile, source='fb') u.delete() # calculate new suggested friends and add their count to session try: user_profile = profile all_fb_friends = _get_connected_friends(api_key, secret, fb_session_key, fb_uid) fb_users = UserProfile.objects.active().exclude(fb_userid=u'') app_friends = set(list(fb_users.filter(friends2__user_profile1__pk=user_profile.pk).values_list('fb_userid', flat=True))) pending_friends = set(list(fb_users.filter(pending_friends_inviter__invitee_profile__pk=user_profile.pk).values_list('fb_userid', flat=True))) invited_friends = set(list(fb_users.filter(pending_friends_invitee__inviter_profile__pk=user_profile.pk).values_list('fb_userid', flat=True))) recommended_friends = all_fb_friends.difference(app_friends).difference(pending_friends).difference(invited_friends) db_reco, created = FBSuggestedFriends.objects.get_or_create(user_profile=user_profile) db_recommended_friends = db_reco.friendset new_friends = recommended_friends.difference(db_recommended_friends) if new_friends: if settings.FB_AUTO_FRIEND: num_auto_friended = auto_friend(profile, new_friends) _log.debug("Auto-friended %s friends of %s", num_auto_friended, profile) else: request.session['num_fb_new_friends'] = len(new_friends) except Exception, e: _log.exception(e)
def fb_login(request): next = request.REQUEST.get("next", "/") if request.user.is_authenticated(): return HttpResponseRedirect(next) # if user has an FB Connect session, create account and # log the user in. api_key, secret = settings.FB_API_KEY, settings.FB_SECRET_KEY fb_uid, fb_session_key, fb_session_secret = get_fb_session(request) if not fb_uid: return _logout_redirect(request, next) # Create account if necessary new_signup = True try: profile = UserProfile.objects.active().filter(fb_userid=fb_uid)[:1].get() new_signup = False # account exists; log the user in user = profile.user if user.email.startswith("sso+fb_"): # get FB proxied email and save it under this user's record uinfo = get_user_info(api_key, secret, fb_session_key, fb_uid) if uinfo: name, email = uinfo['name'], uinfo['email'] if email: user.email = email user.save() profile.is_sso = False profile.send_reminders = True profile.send_favorites = True profile.save() except UserProfile.DoesNotExist: # create new acount new_signup = True email = request.session.get("email", None) if not email: add_message(request, u"Create a new account below in two easy steps before signing in with Facebook.") return HttpResponseRedirect(reverse("signup") + "?next=" + next) uinfo = get_user_info(api_key, secret, fb_session_key, fb_uid) # screen_name, name, email = None, None, None screen_name, name = None, None if uinfo: # name, email = uinfo['name'], uinfo['email'] name = uinfo['name'] if name: # screen name is firstname followed by last initial; all lowercased screen_name = name.lower().strip() x = screen_name.split(' ') if len(x) > 1: firstname, lastname = x[0], x[1] if lastname: screen_name = firstname + lastname[0] else: screen_name = firstname screen_name = slugify(screen_name)[:25] unames = list(User.objects.filter(username__istartswith=screen_name).values_list("username", flat=True).order_by("username")) unames = set([x.lower() for x in unames]) if screen_name in unames: # generate a unique screen_name by suffixing it with a number for n in range(2, 1000): s = u"%s%s" % (screen_name, n) if s not in unames: screen_name = s break if not screen_name: # Use FB User ID as the screen name screen_name = fb_uid if User.objects.filter(username=screen_name).count(): # screen name not available. Generate one with a random suffix screen_name = u''.join([screen_name.strip()[:25], u'-', uuid4().hex[::6]]) # create user and profile user = User.objects.create( username=screen_name, first_name='', last_name='', email=email or u'*****@*****.**' % uuid4().hex[::5], ) profile = user.get_profile() profile.fb_userid = fb_uid profile.is_sso = False profile.send_reminders = bool(email) profile.send_favorites = bool(email) profile.fb_suggested = 0 profile.save() # annotate the user object with the path of the backend so that # login() works without a password: backend = get_backends()[0] user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__) login(request, user) if new_signup: user.message_set.create(message=_(u'Thank you for signing-up with Facebook!')) _log.debug("FB user %s logged in" % profile.username) request.user_profile = profile request.session['fb_uid'] = fb_uid request.session['fb_session_key'] = fb_session_key # if this user was invited by any existing users, create a friendship inv = FBInvitedUser.objects.select_related('inviter_profile').filter(fb_userid=fb_uid) for u in inv: inviter = u.inviter_profile Friendship.objects.make_friends(inviter, request.user_profile, source='fb') u.delete() # calculate new suggested friends and add their count to session try: user_profile = profile all_fb_friends = _get_connected_friends(api_key, secret, fb_session_key, fb_uid) fb_users = UserProfile.objects.active().exclude(fb_userid=u'') app_friends = set(list(fb_users.filter(friends2__user_profile1__pk=user_profile.pk).values_list('fb_userid', flat=True))) pending_friends = set(list(fb_users.filter(pending_friends_inviter__invitee_profile__pk=user_profile.pk).values_list('fb_userid', flat=True))) invited_friends = set(list(fb_users.filter(pending_friends_invitee__inviter_profile__pk=user_profile.pk).values_list('fb_userid', flat=True))) recommended_friends = all_fb_friends.difference(app_friends).difference(pending_friends).difference(invited_friends) db_reco, created = FBSuggestedFriends.objects.get_or_create(user_profile=user_profile) db_recommended_friends = db_reco.friendset new_friends = recommended_friends.difference(db_recommended_friends) if new_friends: if new_signup and settings.FB_AUTO_FRIEND: num_auto_friended = auto_friend(profile, new_friends) _log.debug("Auto-friended %s friends of %s", num_auto_friended, profile) else: request.session['num_fb_new_friends'] = len(new_friends) except Exception, e: _log.exception(e)
def fb_connect(request): """Merge FB session with current profile""" next = request.REQUEST.get("next", "/") if request.user_profile.fb_userid: # User has already connected an FB account add_message(request, u"You've already connected a Facebook account.") return HttpResponseRedirect(next) api_key, secret = settings.FB_API_KEY, settings.FB_SECRET_KEY fb_uid, fb_session_key, fb_session_secret = get_fb_session(request) if not fb_uid: return _logout_redirect(request, next) try: profile = UserProfile.objects.active().filter( fb_userid=fb_uid)[:1].get() add_message( request, u"You've already connected this Facebook account with another RiotVine account." ) return HttpResponseRedirect(next) except UserProfile.DoesNotExist: # save fb id profile = request.user_profile profile.fb_userid = fb_uid profile.is_sso = False profile.fb_suggested = 0 profile.save() request.user_profile = profile request.session['fb_uid'] = fb_uid # if this user was invited by any existing users, create a friendship inv = FBInvitedUser.objects.select_related('inviter_profile').filter( fb_userid=fb_uid) for u in inv: inviter = u.inviter_profile Friendship.objects.make_friends(inviter, request.user_profile, source='fb') u.delete() # calculate new suggested friends and add their count to session try: user_profile = profile all_fb_friends = _get_connected_friends(api_key, secret, fb_session_key, fb_uid) fb_users = UserProfile.objects.active().exclude(fb_userid=u'') app_friends = set( list( fb_users.filter( friends2__user_profile1__pk=user_profile.pk).values_list( 'fb_userid', flat=True))) pending_friends = set( list( fb_users.filter( pending_friends_inviter__invitee_profile__pk=user_profile. pk).values_list('fb_userid', flat=True))) invited_friends = set( list( fb_users.filter( pending_friends_invitee__inviter_profile__pk=user_profile. pk).values_list('fb_userid', flat=True))) recommended_friends = all_fb_friends.difference( app_friends).difference(pending_friends).difference( invited_friends) db_reco, created = FBSuggestedFriends.objects.get_or_create( user_profile=user_profile) db_recommended_friends = db_reco.friendset new_friends = recommended_friends.difference(db_recommended_friends) if new_friends: if settings.FB_AUTO_FRIEND: num_auto_friended = auto_friend(profile, new_friends) _log.debug("Auto-friended %s friends of %s", num_auto_friended, profile) else: request.session['num_fb_new_friends'] = len(new_friends) except Exception, e: _log.exception(e)
def fb_login(request): next = request.REQUEST.get("next", "/") if request.user.is_authenticated(): return HttpResponseRedirect(next) # if user has an FB Connect session, create account and # log the user in. api_key, secret = settings.FB_API_KEY, settings.FB_SECRET_KEY fb_uid, fb_session_key, fb_session_secret = get_fb_session(request) if not fb_uid: return _logout_redirect(request, next) # Create account if necessary new_signup = True try: profile = UserProfile.objects.active().filter( fb_userid=fb_uid)[:1].get() new_signup = False # account exists; log the user in user = profile.user if user.email.startswith("sso+fb_"): # get FB proxied email and save it under this user's record uinfo = get_user_info(api_key, secret, fb_session_key, fb_uid) if uinfo: name, email = uinfo['name'], uinfo['email'] if email: user.email = email user.save() profile.is_sso = False profile.send_reminders = True profile.send_favorites = True profile.save() except UserProfile.DoesNotExist: # create new acount new_signup = True email = request.session.get("email", None) if not email: add_message( request, u"Create a new account below in two easy steps before signing in with Facebook." ) return HttpResponseRedirect(reverse("signup") + "?next=" + next) uinfo = get_user_info(api_key, secret, fb_session_key, fb_uid) # screen_name, name, email = None, None, None screen_name, name = None, None if uinfo: # name, email = uinfo['name'], uinfo['email'] name = uinfo['name'] if name: # screen name is firstname followed by last initial; all lowercased screen_name = name.lower().strip() x = screen_name.split(' ') if len(x) > 1: firstname, lastname = x[0], x[1] if lastname: screen_name = firstname + lastname[0] else: screen_name = firstname screen_name = slugify(screen_name)[:25] unames = list( User.objects.filter( username__istartswith=screen_name).values_list( "username", flat=True).order_by("username")) unames = set([x.lower() for x in unames]) if screen_name in unames: # generate a unique screen_name by suffixing it with a number for n in range(2, 1000): s = u"%s%s" % (screen_name, n) if s not in unames: screen_name = s break if not screen_name: # Use FB User ID as the screen name screen_name = fb_uid if User.objects.filter(username=screen_name).count(): # screen name not available. Generate one with a random suffix screen_name = u''.join( [screen_name.strip()[:25], u'-', uuid4().hex[::6]]) # create user and profile user = User.objects.create( username=screen_name, first_name='', last_name='', email=email or u'*****@*****.**' % uuid4().hex[::5], ) profile = user.get_profile() profile.fb_userid = fb_uid profile.is_sso = False profile.send_reminders = bool(email) profile.send_favorites = bool(email) profile.fb_suggested = 0 profile.save() # annotate the user object with the path of the backend so that # login() works without a password: backend = get_backends()[0] user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__) login(request, user) if new_signup: user.message_set.create( message=_(u'Thank you for signing-up with Facebook!')) _log.debug("FB user %s logged in" % profile.username) request.user_profile = profile request.session['fb_uid'] = fb_uid request.session['fb_session_key'] = fb_session_key # if this user was invited by any existing users, create a friendship inv = FBInvitedUser.objects.select_related('inviter_profile').filter( fb_userid=fb_uid) for u in inv: inviter = u.inviter_profile Friendship.objects.make_friends(inviter, request.user_profile, source='fb') u.delete() # calculate new suggested friends and add their count to session try: user_profile = profile all_fb_friends = _get_connected_friends(api_key, secret, fb_session_key, fb_uid) fb_users = UserProfile.objects.active().exclude(fb_userid=u'') app_friends = set( list( fb_users.filter( friends2__user_profile1__pk=user_profile.pk).values_list( 'fb_userid', flat=True))) pending_friends = set( list( fb_users.filter( pending_friends_inviter__invitee_profile__pk=user_profile. pk).values_list('fb_userid', flat=True))) invited_friends = set( list( fb_users.filter( pending_friends_invitee__inviter_profile__pk=user_profile. pk).values_list('fb_userid', flat=True))) recommended_friends = all_fb_friends.difference( app_friends).difference(pending_friends).difference( invited_friends) db_reco, created = FBSuggestedFriends.objects.get_or_create( user_profile=user_profile) db_recommended_friends = db_reco.friendset new_friends = recommended_friends.difference(db_recommended_friends) if new_friends: if new_signup and settings.FB_AUTO_FRIEND: num_auto_friended = auto_friend(profile, new_friends) _log.debug("Auto-friended %s friends of %s", num_auto_friended, profile) else: request.session['num_fb_new_friends'] = len(new_friends) except Exception, e: _log.exception(e)