def handle_post(self, request): """ Logs a user in if it was a request to log a user in and the login attempt was successful. """ # Confirm that the requst is a post, and that this form is # the intended recipient of the posted data. if not request.POST or self.submit_btn_name() not in request.POST: return None form = CustomAuthForm(data=request.POST) if form.is_valid(): # Log in the user and redirect based on the success_url rules. expire_login(request, form.get_user()) response = HttpResponseRedirect(success_url(request)) response.set_cookie('myguid', form.get_user().user_guid, expires=365*24*60*60, domain='.my.jobs') return response return None
def handle_post(self, request): """ Logs a user in if it was a request to log a user in and the login attempt was successful. """ # Confirm that the requst is a post, and that this form is # the intended recipient of the posted data. if not request.POST or self.submit_btn_name() not in request.POST: return None form = CustomAuthForm(data=request.POST) if form.is_valid(): # Log in the user and redirect based on the success_url rules. expire_login(request, form.get_user()) response = HttpResponseRedirect(success_url(request)) response.set_cookie('myguid', form.get_user().user_guid, expires=365 * 24 * 60 * 60, domain='.my.jobs') return response return None
def home(request): """ The home page view receives 2 separate Ajax requests, one for the registration form and another for the initial profile information form. If everything checks out alright and the form saves with no errors, it returns a simple string, 'valid', as an HTTP Response, which the front end recognizes as a signal to continue with the account creation process. If an error occurs, this triggers the jQuery to update the page. The form instances with errors must be passed back to the form template it was originally from. """ registration_form = RegistrationForm(auto_id=False) login_form = CustomAuthForm(auto_id=False) name_form = InitialNameForm(prefix="name") education_form = InitialEducationForm(prefix="edu") phone_form = InitialPhoneForm(prefix="ph") work_form = InitialWorkForm(prefix="work") address_form = InitialAddressForm(prefix="addr") nexturl = request.GET.get('next') title_template = get_title_template(nexturl) if nexturl: nexturl = urllib2.unquote(nexturl) nexturl = urllib2.quote(nexturl.encode('utf8')) last_ms = request.COOKIES.get('lastmicrosite') site_name = '' logo_url = '' show_registration = True if last_ms: try: last_ms = get_domain(last_ms) custom_page = CustomHomepage.objects.get(domain=last_ms) logo_url = custom_page.logo_url show_registration = custom_page.show_signup_form site_name = custom_page.name except CustomHomepage.DoesNotExist: pass data_dict = {'num_modules': len(settings.PROFILE_COMPLETION_MODULES), 'registrationform': registration_form, 'loginform': login_form, 'name_form': name_form, 'phone_form': phone_form, 'address_form': address_form, 'work_form': work_form, 'education_form': education_form, 'nexturl': nexturl, 'logo_url': logo_url, 'show_registration': show_registration, 'site_name': site_name, 'logo_template': title_template, } if request.method == "POST": if request.POST.get('action') == "register": registration_form = RegistrationForm(request.POST, auto_id=False) if registration_form.is_valid(): new_user, created = User.objects.create_user( request=request, send_email=True, **registration_form.cleaned_data) user_cache = authenticate( username=registration_form.cleaned_data['email'], password=registration_form.cleaned_data['password1']) expire_login(request, user_cache) # pass in gravatar url once user is logged in. Image generated # on AJAX success html = render_to_response('includes/account-page-2.html', data_dict, RequestContext(request)) data = {'gravatar_url': new_user.get_gravatar_url(size=100), 'html': html.content} response = HttpResponse(json.dumps(data)) response.set_cookie('myguid', new_user.user_guid, expires=365*24*60*60, domain='.my.jobs') return response else: return HttpResponse(json.dumps( {'errors': registration_form.errors.items()})) elif request.POST.get('action') == "login": login_form = CustomAuthForm(data=request.POST) if login_form.is_valid(): expire_login(request, login_form.get_user()) url = request.POST.get('nexturl') # Boolean for activation login page to show initial forms # again or not has_units = False if len(login_form.get_user().profileunits_set.all()) > 0: has_units = True response_data = { 'validation': 'valid', 'url': url, 'units': has_units, 'gravatar_url': login_form.get_user().get_gravatar_url( size=100)} response = HttpResponse(json.dumps(response_data)) response.set_cookie('myguid', login_form.get_user().user_guid, expires=365*24*60*60, domain='.my.jobs') return response else: return HttpResponse(json.dumps({'errors': login_form.errors.items()})) elif request.POST.get('action') == "save_profile": name_form = InitialNameForm(request.POST, prefix="name", user=request.user) if not name_form.changed_data: name_form = InitialNameForm(prefix="name") education_form = InitialEducationForm(request.POST, prefix="edu", user=request.user) if not education_form.changed_data: education_form = InitialEducationForm(prefix="edu") phone_form = InitialPhoneForm(request.POST, prefix="ph", user=request.user) if not phone_form.changed_data: phone_form = InitialPhoneForm(prefix="ph") work_form = InitialWorkForm(request.POST, prefix="work", user=request.user) if not work_form.changed_data: work_form = InitialWorkForm(prefix="work") address_form = InitialAddressForm(request.POST, prefix="addr", user=request.user) if not address_form.changed_data: address_form = InitialAddressForm(prefix="addr") forms = [name_form, education_form, phone_form, work_form, address_form] valid_forms = [form for form in forms if form.is_valid()] invalid_forms = [] for form in forms: if form.changed_data and not form.is_valid(): invalid_forms.append(form) if not invalid_forms: for form in valid_forms: if form.changed_data: form.save(commit=False) form.user = request.user form.save_m2m() return HttpResponse('valid') else: return render_to_response('includes/initial-profile-form.html', {'name_form': name_form, 'phone_form': phone_form, 'address_form': address_form, 'work_form': work_form, 'education_form': education_form}, context_instance=RequestContext( request)) return render_to_response('index.html', data_dict, RequestContext(request))
def home(request): """ The home page view receives 2 separate Ajax requests, one for the registration form and another for the initial profile information form. If everything checks out alright and the form saves with no errors, it returns a simple string, 'valid', as an HTTP Response, which the front end recognizes as a signal to continue with the account creation process. If an error occurs, this triggers the jQuery to update the page. The form instances with errors must be passed back to the form template it was originally from. """ registration_form = RegistrationForm(auto_id=False) login_form = CustomAuthForm(auto_id=False) name_form = InitialNameForm(prefix="name") education_form = InitialEducationForm(prefix="edu") phone_form = InitialPhoneForm(prefix="ph") work_form = InitialWorkForm(prefix="work") address_form = InitialAddressForm(prefix="addr") nexturl = request.GET.get('next') title_template = get_title_template(nexturl) if nexturl: nexturl = urllib2.unquote(nexturl) nexturl = urllib2.quote(nexturl.encode('utf8')) last_ms = request.COOKIES.get('lastmicrosite') site_name = '' logo_url = '' show_registration = True if last_ms: try: last_ms = get_domain(last_ms) custom_page = CustomHomepage.objects.get(domain=last_ms) logo_url = custom_page.logo_url show_registration = custom_page.show_signup_form site_name = custom_page.name except CustomHomepage.DoesNotExist: pass data_dict = { 'num_modules': len(settings.PROFILE_COMPLETION_MODULES), 'registrationform': registration_form, 'loginform': login_form, 'name_form': name_form, 'phone_form': phone_form, 'address_form': address_form, 'work_form': work_form, 'education_form': education_form, 'nexturl': nexturl, 'logo_url': logo_url, 'show_registration': show_registration, 'site_name': site_name, 'logo_template': title_template, } if request.method == "POST": if request.POST.get('action') == "register": registration_form = RegistrationForm(request.POST, auto_id=False) if registration_form.is_valid(): new_user, created = User.objects.create_user( request=request, send_email=True, **registration_form.cleaned_data) user_cache = authenticate( username=registration_form.cleaned_data['email'], password=registration_form.cleaned_data['password1']) expire_login(request, user_cache) ctx = {} ctx['success'] = True response = HttpResponse(json.dumps(ctx)) response.set_cookie('myguid', new_user.user_guid, expires=365 * 24 * 60 * 60, domain='.my.jobs') response.delete_cookie('loggedout') return response else: return HttpResponse( json.dumps({'errors': registration_form.errors.items()})) elif request.POST.get('action') == "login": login_form = CustomAuthForm(data=request.POST) if login_form.is_valid(): expire_login(request, login_form.get_user()) url = request.POST.get('nexturl') # Boolean for activation login page to show initial forms # again or not has_units = False if len(login_form.get_user().profileunits_set.all()) > 0: has_units = True response_data = { 'validation': 'valid', 'url': url, 'units': has_units, 'gravatar_url': login_form.get_user().get_gravatar_url(size=100) } response = HttpResponse(json.dumps(response_data)) response.set_cookie('myguid', login_form.get_user().user_guid, expires=365 * 24 * 60 * 60, domain='.my.jobs') response.delete_cookie('loggedout') return response else: return HttpResponse( json.dumps({'errors': login_form.errors.items()})) elif request.POST.get('action') == "save_profile": name_form = InitialNameForm(request.POST, prefix="name", user=request.user) if not name_form.changed_data: name_form = InitialNameForm(prefix="name") education_form = InitialEducationForm(request.POST, prefix="edu", user=request.user) if not education_form.changed_data: education_form = InitialEducationForm(prefix="edu") phone_form = InitialPhoneForm(request.POST, prefix="ph", user=request.user) if not phone_form.changed_data: phone_form = InitialPhoneForm(prefix="ph") work_form = InitialWorkForm(request.POST, prefix="work", user=request.user) if not work_form.changed_data: work_form = InitialWorkForm(prefix="work") address_form = InitialAddressForm(request.POST, prefix="addr", user=request.user) if not address_form.changed_data: address_form = InitialAddressForm(prefix="addr") forms = [ name_form, education_form, phone_form, work_form, address_form ] valid_forms = [form for form in forms if form.is_valid()] invalid_forms = [] for form in forms: if form.changed_data and not form.is_valid(): invalid_forms.append(form) if not invalid_forms: for form in valid_forms: if form.changed_data: form.save(commit=False) form.user = request.user form.save_m2m() return HttpResponse('valid') else: return render_to_response( 'includes/initial-profile-form.html', { 'name_form': name_form, 'phone_form': phone_form, 'address_form': address_form, 'work_form': work_form, 'education_form': education_form }, context_instance=RequestContext(request)) return render_to_response('index.html', data_dict, RequestContext(request))