def unsubscribe(request): from accounts.forms import EmailUnsubscribeForm import accounts.emaillib as emaillib if request.POST: form = EmailUnsubscribeForm(request.POST) if form.is_valid(): # if user is auth'd, update prferences if request.user.is_authenticated(): try: pref = Pref(user=request.user,name="notification") except: pref = Pref(user=user,name="notification") # save new value pref.value = 0 pref.save() # if anonymous, send email to admins to remove else: notification = emaillib.NotificationEmail("Add following user to blocklist: " + form.cleaned_data['email']) notification.trigger() # log flash message messages.success(request, 'You have successfully unsubscribed.') return HttpResponseRedirect('/unsubscribe') else: email = request.POST['email'] else: # empty form form = EmailUnsubscribeForm(request.POST) # check if email specified in URL if 'email_address' in request.GET: email = request.GET['email_address'] elif request.user.is_authenticated(): email = request.user.email else: email = None return render_to_response('accounts/unsubscribe.html',{'form':form,'email':email},context_instance=RequestContext(request))
def finish_login(request): # TODO: redirect if not not authenticated through LinkedIn already # if request.POST: if request.method == "POST": # form submitted form = FinishAuthForm(request.POST) if form.is_valid(): from accounts.models import Pref import accounts.emaillib as emaillib # grab cleaned values from form username = form.cleaned_data['username'] email = form.cleaned_data['email'] # location = form.cleaned_data['location'] # headline = form.cleaned_data['headline'] password = form.cleaned_data['password'] # fetch LI data linkedin_user_info = request.session['linkedin_user_info'] access_token = request.session['access_token'] # check to see if dormant user already exists try: print '@ accounts.finish -- checking for dormant user' user = User.objects.get(profile__status="dormant",account__uniq_id=linkedin_user_info['id']) existing = True user.profile.status = "active" user.profile.save() user.set_password(password) user.username = username user.is_active = True user.save() print "@ accounts.finish -- user already exists" except: print '@ accounts.finish -- new user' # create user user = User.objects.create_user(username,email,password) user.save() existing = False user.profile.status = "active" user.profile.save() print "@ accounts.finish -- created new user" # send welcome email welcome = emaillib.WelcomeEmail(user) welcome.send_email() # add email prefs if form.cleaned_data['notification']: pref = Pref(user=user,name="notification",value=1) pref.save() else: pref = Pref(user=user,name="notification",value=0) pref.save() # make sure using right backend request.session['_auth_user_backend'] = 'django.contrib.auth.backends.ModelBackend' # log user in user = authenticate(username=username,password=password) # make sure authentication worked if user is not None: print 'in to auth login' auth_login(request,user) else: # try logging in now with LinkedIn request.session['_auth_user_backend'] = 'prosperime.accounts.backends.LinkedinBackend' try: user = authenticate(acct_id=linkedin_user_info['id']) if user is not None: auth_login(request,user) except: # somehow authentication failed, redirect with error message messages.error(request, 'Something went wrong. Please try again.') return render_to_response('accounts/finish_login.html',{'form':form},context_instance=RequestContext(request)) # update user profile # user.profile.full_name = request.session['linkedin_user_info']['firstName'] + " " + request.session['linkedin_user_info']['lastName'] user.profile.first_name = linkedin_user_info['firstName'] user.profile.last_name = linkedin_user_info['lastName'] # user.profile.location = location # check to see if user provided a headline # if headline: # user.profile.headline = headline # else: user.profile.headline = linkedin_user_info['headline'] user.profile.save() # add pofile picture # if 'pictureUrl' in linkedin_user_info: # # _add_profile_pic(user,linkedin_user_info['pictureUrl']) # li_parser = LIProfile() # li_parser.add_profile_pic(user,linkedin_user_info['pictureUrl']) if 'pictureUrls' in linkedin_user_info: li_parser = LIProfile() li_parser.add_profile_pic(user,linkedin_user_info['pictureUrls']['values'][0]) if existing: # get existing LI account acct = Account.objects.get(owner=user,service="linkedin") else: # create LinkedIn account acct = Account() acct.owner = user acct.access_token = access_token['oauth_token'] acct.token_secret = access_token['oauth_token_secret'] acct.service = 'linkedin' acct.expires_on = datetime.now() + timedelta(seconds=int(access_token['oauth_authorization_expires_in'])) acct.uniq_id = linkedin_user_info['id'] acct.status = "active" acct.save() # finish processing LI profile profile_task = process_li_profile.delay(user.id,acct.id) # start processing connections connections_task = process_li_connections.delay(user.id,acct.id) # save task ids to session request.session['tasks'] = { 'profile': profile_task.id, 'connections': connections_task.id } #return HttpResponseRedirect('/account/success') if 'next' not in request.session: return HttpResponseRedirect('/majors/') else: return HttpResponseRedirect(request.session['next']) else: form = FinishAuthForm() return render_to_response('accounts/finish_login.html',{'form':form},context_instance=RequestContext(request))