def register_view(request, template_name='registration.html'): """ Form that send registration email to user """ context = {} if request.method == "POST": form = RegisterForm(request.POST) if form.is_valid(): registration = form.save() context['registration'] = registration context['email_to'] = registration.email text_content = render_string(request, 'email/registration.txt', context) send_mail('Your Registration is done', text_content, DEFAULT_EMAIL_FROM, [registration.email, ]) return HttpResponseRedirect(reverse('registration_sent')) else: # form is invalid context['register_form'] = form else: # no post, user just arrived context['register_form'] = RegisterForm() return render_response(request, template_name, context)
def register_confirm(request, key, template_name='create_account.html'): """ Account creation from email link. if the key is wrong the user is redirected to the login view. """ registration = Registration.objects.filter(key=key) if not registration: return HttpResponseRedirect(reverse('login_view')) if request.user.is_authenticated(): logout(request) registration = registration[0] # check that user does not already exist with this email if User.objects.filter(email__iexact=registration.email): Registration.objects.filter(email__iexact=registration.email).delete() return HttpResponseRedirect(reverse('login_view')) context = {'registration': registration} if request.method == "POST": form = SetPasswordFormWithAgreementCopyTimeRecord(request.POST) if form.is_valid(): time_records = TimeRecord.objects.get_time_records(request) password = form.cleaned_data['password2'] user = User.objects.create_user(sha_constructor(str(registration.email)).hexdigest()[:30], registration.email, password) profile = Profile.objects.create(user=user) user = authenticate(username=user.username, password=password) if form.cleaned_data['backup_time_records']: for time_record in time_records: time_record.user = user time_record.save() login(request, user) context['email_to'] = registration.email context['password'] = password text_content = render_string(request, 'email/welcome.txt', context) send_mail('Welcome', text_content, DEFAULT_EMAIL_FROM, [user.email, ]) Registration.objects.filter(email__iexact=registration.email).delete() request.session['first_login'] = True return HttpResponseRedirect(reverse('thank_you_for_registering')) else: context['password_form'] = form else: context['password_form'] = SetPasswordFormWithAgreementCopyTimeRecord() return render_response(request, template_name, context)
def register_confirm(request, key): """ Account creation from email link. """ registration = Registration.objects.filter(key=key) if not registration: return HttpResponseRedirect(reverse("login_view")) if request.user.is_authenticated(): logout(request) registration = registration[0] # check that user does not already exist with this email if User.objects.filter(email__iexact=registration.email): Registration.objects.filter(email__iexact=registration.email).delete() return HttpResponseRedirect(reverse("login_view")) context = {"registration": registration} if request.method == "POST": form = SetPasswordFormWithAgreementCopyTimeRecord(request.POST) if form.is_valid(): time_records = TimeRecord.objects.get_time_records(request) password = form.cleaned_data["password2"] user = User.objects.create_user( sha_constructor(str(registration.email)).hexdigest()[:30], registration.email, password ) profile = Profile.objects.create(user=user) user = authenticate(username=user.username, password=password) if form.cleaned_data["backup_time_records"]: for time_record in time_records: time_record.user = user time_record.save() login(request, user) context["email_to"] = registration.email context["password"] = password text_content = render_string(request, "email/welcome.txt", context) send_mail("Welcome to kaaloo", text_content, settings.DEFAULT_EMAIL_FROM, [user.email]) Registration.objects.filter(email__iexact=registration.email).delete() return HttpResponseRedirect(reverse("thank_you_for_registering")) else: context["password_form"] = form else: context["password_form"] = SetPasswordFormWithAgreementCopyTimeRecord() return render_response(request, "profile/create_account.html", context)
def invite_confirm_view(request, key): """ Create an account for the user. """ """ Account creation from email link. """ invites = InviteRequest.objects.filter(key=key) if not invites: return HttpResponseRedirect(reverse('login_view')) invite = invites[0] # check that user does not already exist with this email if User.objects.filter(email__iexact=invite.to_email): return HttpResponseRedirect(reverse('login_view')) context = {} if request.method == "POST": form = SetPasswordForm(request.POST) if form.is_valid(): password = form.cleaned_data['password2'] user = User.objects.create_user(sha_constructor(str(invite.to_email)).hexdigest()[:30], invite.to_email, password) profile = Profile.objects.create(user=user) user = authenticate(username=user.username, password=password) login(request, user) context['email_to'] = user.email context['password'] = password text_content = render_string(request, 'email/welcome.txt', context) send_mail('Welcome to kaaloo', text_content, settings.DEFAULT_EMAIL_FROM, [user.email, ]) # transform all invites in friend request invites = InviteRequest.objects.filter(to_email__iexact = user.email) for invite in invites: ContactRequest.objects.create(from_user=invite.from_user, to_user=user) invites.delete() return HttpResponseRedirect(reverse('contact_requests_view')) else: context['password_form'] = form else: context['password_form'] = SetPasswordForm() return render_response(request, 'profile/create_account.html', context)
def register_view(request): """ Formular which send registration email to user """ context = {} if request.method == "POST": form = RegisterForm(request.POST) if form.is_valid(): registration = form.save() context["registration"] = registration context["email_to"] = registration.email text_content = render_string(request, "email/registration.txt", context) send_mail("Registration to kaaloo", text_content, settings.DEFAULT_EMAIL_FROM, [registration.email]) return HttpResponseRedirect(reverse("registration_sent")) else: # form is invalid context["register_form"] = form else: # no post, user just arrived context["register_form"] = RegisterForm() context["login_form"] = LoginForm() return render_response(request, "profile/login.html", context)
def add_contact_view(request): context = {'current':'contacts'} if request.method == 'POST': form = AddContactForm(data=request.POST) if form.is_valid(): mail_context = {'from_user': request.user } email = form.cleaned_data['email'] email_owners = User.objects.filter(email__iexact=email) if email_owners: email_owner = email_owners[0] if email_owner == request.user: context['message'] = 'The email address you entered is yours. It\'s may seam rude but you cannot invite yourself to be a contact.' return render_response(request, 'contact/request_already_sent.html', context) contact_requests = ContactRequest.objects.filter(from_user=request.user, to_user=email_owner) # make sure to not double an existing request: if contact_requests: context['message'] = 'You already create a request to this person. We recommand you to contact directly this person. ' return render_response(request, 'contact/request_already_sent.html', context) if email_owner in request.user.get_profile().contacts.all(): context['message'] = 'You are already friend with this person' return render_response(request, 'contact/request_already_sent.html', context) contact_request = ContactRequest(from_user=request.user, to_user=email_owner) mail_context['email_to'] = email mail_context['to_user'] = email_owner mail_context['contact_request'] = contact_request text_content = render_string(request, 'email/contact_request.txt', mail_context) send_mail('Contact Request', text_content, settings.DEFAULT_EMAIL_FROM, [email_owner.email, ]) contact_request.save() else: invite_requests = InviteRequest.objects.filter(from_user=request.user, to_email__iexact=email) # make sure to not double an existing request: if invite_requests: context['message'] = 'You already create a request to this person. We recommand you to contact directly this person. ' return render_response(request, 'contact/request_already_sent.html', context) invite_request = InviteRequest(from_user=request.user, to_email=email) mail_context['email_to'] = email mail_context['invite_request'] = invite_request mail_context['other_requests'] = InviteRequest.objects.filter(to_email__iexact=email) text_content = render_string(request, 'email/invite.txt', mail_context) send_mail('Someone invited you to join Kaaloo', text_content, settings.DEFAULT_EMAIL_FROM, [email, ]) invite_request.save() return HttpResponseRedirect(reverse('invite_sent')) else: context['contact_form'] = form else: context['contact_form'] = AddContactForm() return render_response(request, 'contact/add_contact.html', context)