Commit 6d0cff60 authored by Flavio TARSETTI's avatar Flavio TARSETTI

[beat][web][ui] registration fix URL_PREFIX usage

Part of #558
parent 1abc6723
......@@ -152,7 +152,7 @@ class RegistrationForm(forms.Form):
return self.cleaned_data
def save(self):
def save(self, request):
"""
Create the new ``User`` and ``RegistrationProfile``, and
returns the ``User`` (by calling
......@@ -161,6 +161,7 @@ class RegistrationForm(forms.Form):
"""
new_user = RegistrationProfile.objects.create_inactive_user(
request,
username=self.cleaned_data["username"],
first_name=self.cleaned_data["first_name"],
last_name=self.cleaned_data["last_name"],
......@@ -457,7 +458,7 @@ class RegistrationSupervisorForm(forms.Form):
return self.cleaned_data
def save(self):
def save(self, request):
"""
Create the new ``User`` and ``RegistrationProfile``, and
returns the ``User`` (by calling
......@@ -466,6 +467,7 @@ class RegistrationSupervisorForm(forms.Form):
"""
new_user = RegistrationProfile.objects.create_inactive_user(
request,
username=self.cleaned_data["username"],
first_name=self.cleaned_data["first_name"],
last_name=self.cleaned_data["last_name"],
......
......@@ -35,6 +35,7 @@ from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.db import transaction
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from ...accounts.models import Profile
......@@ -159,7 +160,7 @@ class RegistrationManager(models.Manager):
return False
def create_inactive_user(
self, username, first_name, last_name, password, email, send_email=True
self, request, username, first_name, last_name, password, email, send_email=True
):
"""
Create a new, inactive ``User``, generate a
......@@ -209,14 +210,18 @@ class RegistrationManager(models.Manager):
registration_profile = self.create_profile(new_user)
if send_email:
parsed_url = urlparse(settings.URL_PREFIX)
server_address = "%s://%s" % (parsed_url.scheme, parsed_url.hostname)
registration_activation_url = request.build_absolute_uri(
reverse(
"registration-activation",
kwargs={"activation_key": registration_profile.activation_key},
)
)
contact_url = request.build_absolute_uri(reverse("contact"))
context = {
"activation_key": registration_profile.activation_key,
"expiration_days": settings.ACCOUNT_ACTIVATION_DAYS,
"user": new_user,
"prefix": server_address,
"registration_activation_url": registration_activation_url,
"contact_url": contact_url,
}
mail.send_email(
......
......@@ -4,12 +4,12 @@ Thank you for registering at the Idiap Research Institute's Biometric
Evaluation and Testing (BEAT) platform. Before we can activate your account
you must visit the following URL:
{{ prefix }}{% url 'registration-activation' activation_key %}
{{ registration_activation_url }}
If you don't do this in the next {{ expiration_days }} days, your registration
will be automatically cancelled and your data removed from our servers.
If you are having problems to activate your account, contact a member of our
support staff at {{ prefix }}{% url 'contact' %}.
support staff at {{ contact_url }}.
BEAT Administrators at the Idiap Research Institute
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2020 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from django.contrib.auth.models import User
from django.core import mail
from django.test import Client
from django.test import override_settings
from django.urls import reverse
from ...accounts.tests.core import AccountTestMixin
from ...accounts.tests.test_views import ViewTestCase
from ...utils.tests.helpers import reload_urlconf
# ----------------------------------------------------------
class EmailSendingTestCase(ViewTestCase, AccountTestMixin):
def setUp(self):
super().setUp()
def run_email_check(self, reference_url):
client = Client()
response = client.post(
reverse("registration"),
dict(
username="test",
first_name="test",
last_name="test",
email="test_email@idiap.ch",
password1="123",
password2="123",
supervisor="firstsupervisor",
tos="on",
),
)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 1)
text_lines = mail.outbox[0].body.split("\n")
self.assertTrue(text_lines[6].startswith(reference_url))
def test_email_for_signup(self):
for prefix in ["", "/platform"]:
with self.subTest(url_prefix=prefix):
mail.outbox = []
User.objects.filter(username="test").delete()
with override_settings(URL_PREFIX=prefix):
reload_urlconf()
self.run_email_check("http://testserver" + prefix + "/signup/")
......@@ -174,12 +174,12 @@ def register(
# Check the form
if "supervisor" not in request.POST:
supervisor_form_active = True
form_supervisor = RegistrationSupervisorForm(
form = form_class()
form_supervisor = RegistrationFormTermsOfServiceSupervisor(
data=request.POST, files=request.FILES
)
form = form_class()
if form_supervisor.is_valid():
form_supervisor.save()
form_supervisor.save(request)
# success_url needs to be dynamically generated here; setting a
# a default value using reverse() will cause circular-import
# problems with the default URLConf for this application, which
......@@ -190,7 +190,7 @@ def register(
form = form_class(data=request.POST, files=request.FILES)
form_supervisor = RegistrationSupervisorForm()
if form.is_valid():
form.save()
form.save(request)
# success_url needs to be dynamically generated here; setting a
# a default value using reverse() will cause circular-import
# problems with the default URLConf for this application, which
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment