def register(request): """ Handles user registration request """ template = 'accounts/register-1.html' template_context = {} if request.user.is_authenticated(): # They are already logged on, don't let them register again return redirect('/mypage') if request.POST: template = 'accounts/register-2.html' template_context['post_request'] = True if request.POST.get('signed_request'): # Post request received from first page (through Facebook API) signed_request = request.POST.get('signed_request') data = parse_signed_request(signed_request, settings.FACEBOOK_APP_SECRET) register_info = data['registration'] if 'name' in register_info: name_parts = register_info['name'].split(u' ') template_context['firstname'] = name_parts[0] template_context['lastname'] = name_parts[len(name_parts)-1] else: template_context['firstname'] = register_info['first_name'] template_context['lastname'] = register_info['last_name'] template_context['email'] = register_info['email'] valid = True if not isUniqueEmail(template_context['email']): valid = False template_context['used_email'] = True unique_email = isUniqueEmail(template_context['email']) template_context['fbid'] = -1 if 'user_id' in data: template_context['has_fbid'] = True template_context['redir_uri'] = settings.WEB_ROOT + '/connect' template_context['fbid'] = data['user_id'] if not isUniqueFbid(template_context['fbid']): valid = False template_context['used_fbid'] = True error_msg = "That Facebook account has already been registered \ with this app!" messages.add_message(request, messages.ERROR, error_msg) elif not unique_email: error_msg = 'The email address associated with that Facebook \ account is already being used! If you want to connect it \ to an existing account, please <a href="login">log in</a> \ and go to your dashboard to do so.' messages.add_message(request, messages.ERROR, error_msg, extra_tags='safe') elif not unique_email: error_msg = 'That email address is already being used! Are you \ trying to <a href="login">log in</a>?' messages.add_message(request, messages.ERROR, error_msg, extra_tags='safe') if not valid: template = 'accounts/register-1.html' else: # Post request received from second page form = EmailUserCreationForm(request.POST) if form.is_valid(): # All validation rules pass template_context['extra'] = 'SUCCESS' # Create new user new_user = form.save(request.POST.copy()) # Build activation key username = new_user.username salt = hashlib.sha224(str(random.random())).hexdigest()[:5] activation_key = hashlib.sha1(salt+username).hexdigest() key_expires = datetime.datetime.today() + datetime.timedelta(2) # Create and save user and profile new_profile = new_user.get_profile() new_profile.activation_key = activation_key new_profile.key_expires = key_expires new_profile.save() # Send an email with the confirmation link (disabled for now) email = new_user.email email_subject = 'Your new EventHub account confirmation' email_template = get_template('accounts/email/register.txt') context = Context({ 'email' : email, 'web_root' : settings.WEB_ROOT, 'activation_key' : activation_key }) email_body = email_template.render(context) send_mail(email_subject, email_body, '*****@*****.**', [email]) # Redirect to 'My Page' after successful registration success_msg = "You have successfully registered for an EventHub \ account! Please check your email for your activation link so \ you can start using our site." messages.add_message(request, messages.SUCCESS, success_msg) return redirect('/login') else: # Form did not validate. Assuming email has been taken while user # was still on registration page error_msg = 'The email address "' + request.POST['email'] \ + '" has been taken while you were registering. You \ may already be registered for EventHub.' messages.add_message(request, messages.ERROR, error_msg) return redirect('/register') request_context = RequestContext(request, template_context) return render_to_response(template, request_context)
def connect(request): """ Connects an authenticated user to their Facebook account """ if request.user.is_authenticated(): if request.user.get_profile().fbid != -1: # They already have an account connected, shouldn't be here return redirect('/mypage') if request.GET: # Most likely a reply from Facebook request if 'code' in request.GET: args = { 'client_id': settings.FACEBOOK_APP_ID, 'redirect_uri': settings.WEB_ROOT + '/mypage', 'client_secret': settings.FACEBOOK_APP_SECRET, 'code': request.GET['code'], } url = 'https://graph.facebook.com/oauth/access_token?' + \ urllib.urlencode(args) response = cgi.parse_qs(urllib.urlopen(url).read()) if not response: msg = "Your Facebook session has expired! Please log in to \ Facebook again." messages.add_message(request, messages.ERROR, msg) else: access_token = response['access_token'][0] expires = response['expires'][0] facebook_session = FacebookSession.objects.get_or_create( access_token=access_token, )[0] facebook_session.expires = expires facebook_session.save() profile = facebook_session.query('me') fbid = profile['id'] if (isUniqueFbid(fbid)): user = request.user profile = user.get_profile() profile.fbid = fbid profile.save() success_msg = "You have successfully connected your \ Facebook account!" messages.add_message(request, messages.SUCCESS, success_msg) else: msg = "That Facebook account is already connected to an \ existing EventHub account. If you would like to \ connect a different Facebook account, please log out \ of Facebook and try again." messages.add_message(request, messages.ERROR, msg) elif 'error_reason' in request.GET: msg = "You have refused to connect this app with Facebook." messages.add_message(request, messages.ERROR, msg) return redirect('/mypage', permanent=True) else: return redirect('/login')