def register(request): if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): data = form.cleaned_data salt = sha_constructor(str(random.random())).hexdigest()[:5] activation_code = sha_constructor(salt+data['username']).hexdigest() user_temp = form.save(commit=False) user_temp.activation_code = activation_code user_temp.post_code = request.POST['zipcode'] user_temp.county = request.POST['county'] user_temp.district = request.POST['district'] user_temp.save() html_content = render_to_string('email/activate.html', { 'host': request.get_host(), 'STATIC_URL': settings.STATIC_URL, 'activation_code': activation_code, 'name': user_temp.first_name, 'username': user_temp.username, 'password': user_temp.password }) subject = _('EMPOR Account Activation') message = EmailMessage(subject, html_content, settings.DEFAULT_FROM_EMAIL, [user_temp.email]) message.content_subtype = "html" message.send() complete_url = "%s?username=%s&email=%s" % \ (reverse('member-register-complete'), user_temp.username, user_temp.email) return redirect(complete_url) else: form = RegisterForm() return render(request, 'member/register.html', {'form': form})
def facebook_connect_new(request): profile = request.session.get('facebook_profile', None) if not profile: return redirect('/') if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): user_temp = form.save(commit=False) user_temp.post_code = request.POST['zipcode'] user_temp.county = request.POST['county'] user_temp.district = request.POST['district'] user_temp.save() # create user user = User.objects.create_user(user_temp.username, profile['email'], user_temp.password) user.first_name = user_temp.first_name user.last_name = user_temp.last_name user.save() # user profile user_profile = UserProfile( user = user, gender = user_temp.gender, phone = user_temp.phone, birthday = user_temp.birthday, county = user_temp.county, district = user_temp.district, country = user_temp.country, post_code = user_temp.post_code, address = user_temp.address, ) user_profile.save() # facebook profile fbprofile = FacebookProfile( uid = profile['id'], user = user, name = profile.get('name', ''), gender = profile.get('gender', 'male'), locale = profile['locale'], url = profile['link'], timezone = profile.get('timezone', ''), verified = profile.get('verified', False), created_at = datetime.now() ) fbprofile.save() # login user user = auth.authenticate(username=user.username, password=user_temp.password) auth.login(request, user) user_temp.delete() request.session['fb_connect_type'] = 'new' return redirect('member-facebook-connect-done') else: username = profile.get('username', '') birthday = profile.get('birthday', None) if birthday: birthday = birthday.split('/') birthday = birthday[2] + '/' + birthday[0] + '/' + birthday[1] gender = profile.get('gender', 0) first_name = profile.get('first_name', '') last_name = profile.get('last_name', '') email = profile.get('email', '') form = RegisterForm(initial={ 'username': username, 'birthday': birthday, 'gender': gender, 'first_name': first_name, 'last_name': last_name, 'email': email, }) return render(request, 'member/facebook/new.html', {'profile': profile, 'form': form})