def add_email(request): context = _create_profile_context(request) context["active_tab"] = "email" if request.method == "POST": form = NewEmailForm(request.POST) if form.is_valid(): cleaned = form.cleaned_data email_string = cleaned["new_email"].lower() # Check if the email already exists if Email.objects.filter(email=cleaned["new_email"]).count() > 0: messages.error( request, _("Eposten %s er allerede registrert.") % email_string ) return redirect("profiles") # Check if it's studmail and if someone else already has it in their profile if re.match(r"[^@]+@stud\.ntnu\.no", email_string): ntnu_username = email_string.split("@")[0] user = User.objects.filter(ntnu_username=ntnu_username) if user.count() == 1: if user[0] != request.user: messages.error( request, _( "En bruker med dette NTNU-brukernavnet eksisterer allerede." ), ) return redirect("profiles") # Create the email email = Email(email=email_string, user=request.user) email.save() # Send the verification mail _send_verification_mail(request, email.email) messages.success( request, _("Eposten ble lagret. Du må sjekke din innboks for å verifisere den."), ) return render(request, "profiles/index.html", context)
def register(request): log = logging.getLogger(__name__) if request.user.is_authenticated: messages.error( request, _('Registrering av ny konto krever at du er logget ut.')) return HttpResponseRedirect('/') else: if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): cleaned = form.cleaned_data # Create user user = User( username=cleaned['username'], first_name=cleaned['first_name'].title(), last_name=cleaned['last_name'].title(), ) # Set remaining fields user.phone_number = cleaned['phone'] user.address = cleaned['address'].title() user.zip_code = cleaned['zip_code'] # Store password properly user.set_password(cleaned['password']) # Users need to be manually activated user.is_active = False user.save() # Set email address email = Email( user=user, email=cleaned['email'].lower(), ) email.primary = True email.save() # Create the registration token token = uuid.uuid4().hex try: rt = RegisterToken(user=user, email=email.email, token=token) rt.save() log.info('Successfully registered token for %s' % request.user) except IntegrityError as ie: log.error('Failed to register token for "%s" due to "%s"' % (request.user, ie)) email_context = {} verify_url = reverse('auth_verify', args=(token, )) email_context['verify_url'] = request.build_absolute_uri( verify_url) message = render_to_string('auth/email/welcome_tpl.txt', email_context) try: send_mail(_('Verifiser din konto'), message, settings.DEFAULT_FROM_EMAIL, [ email.email, ]) except SMTPException: messages.error( request, 'Det oppstod en kritisk feil, epostadressen er ugyldig!' ) return redirect('home') messages.success( request, _('Registreringen var vellykket. Se tilsendt epost for verifiseringsinstrukser.' )) return HttpResponseRedirect('/') else: form = RegisterForm(request.POST, auto_id=True) else: form = RegisterForm() return render(request, 'auth/register.html', { 'form': form, })
def register(request): log = logging.getLogger(__name__) if request.user.is_authenticated: messages.error( request, _("Registrering av ny konto krever at du er logget ut.")) return HttpResponseRedirect("/") else: if request.method == "POST": form = RegisterForm(request.POST) if form.is_valid(): cleaned = form.cleaned_data # Create user user = User( username=cleaned["username"], first_name=cleaned["first_name"].title(), last_name=cleaned["last_name"].title(), ) # Set remaining fields user.phone_number = cleaned["phone"] user.address = cleaned["address"].title() user.zip_code = cleaned["zip_code"] # Store password properly user.set_password(cleaned["password"]) # Users need to be manually activated user.is_active = False user.save() # Set email address email = Email(user=user, email=cleaned["email"].lower()) email.primary = True email.save() # Create the registration token token = uuid.uuid4().hex try: rt = RegisterToken(user=user, email=email.email, token=token) rt.save() log.info("Successfully registered token for %s" % request.user) except IntegrityError as ie: log.error('Failed to register token for "%s" due to "%s"' % (request.user, ie)) email_context = {} verify_url = reverse("auth_verify", args=(token, )) email_context["verify_url"] = request.build_absolute_uri( verify_url) message = render_to_string("auth/email/welcome_tpl.txt", email_context) try: send_mail( _("Verifiser din konto"), message, settings.DEFAULT_FROM_EMAIL, [email.email], ) except SMTPException: messages.error( request, "Det oppstod en kritisk feil, epostadressen er ugyldig!", ) return redirect("home") messages.success( request, _("Registreringen var vellykket. Se tilsendt epost for verifiseringsinstrukser." ), ) return HttpResponseRedirect("/") else: form = RegisterForm(request.POST, auto_id=True) else: form = RegisterForm() return render(request, "auth/register.html", {"form": form})