Skip to content

Commit

Permalink
Merge pull request #2871 from uktrade/feature/KLS-1907-UKEA-Reg-Form-…
Browse files Browse the repository at this point in the history
…Update

Feature/kls 1907 ukea reg form update
  • Loading branch information
davidu1975 authored Feb 2, 2024
2 parents 28b78cd + a18072b commit 23a6006
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 58 deletions.
12 changes: 11 additions & 1 deletion core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
from ukpostcodeutils import validation

from core.helpers import clam_av_client
from regex import PHONE_NUMBER_REGEX

PHONE_INVALID_MESSAGE = 'Enter a valid UK telephone number'


def is_valid_uk_postcode(value):
if not validation.is_valid_postcode(value.replace(' ', '').upper()):
raise ValidationError(_('Please enter a UK postcode'))
raise ValidationError(_('Enter a valid UK postcode'))


def validate_file_infection(file):
Expand All @@ -22,3 +25,10 @@ def validate_file_infection(file):
raise ValidationError('Rejected: uploaded file did not pass security scan')

file.seek(0)


def is_valid_uk_phone_number(phone_number):
if not PHONE_NUMBER_REGEX.match(phone_number):
raise ValidationError(_(PHONE_INVALID_MESSAGE))
else:
return
46 changes: 16 additions & 30 deletions export_academy/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
DateTimeField,
HiddenInput,
PasswordInput,
ValidationError,
widgets as django_widgets,
)
from django.forms.widgets import ChoiceWidget
Expand All @@ -14,9 +13,8 @@
from wagtail.admin.forms import WagtailAdminModelForm

from contact import constants
from core.validators import is_valid_uk_postcode
from core.validators import is_valid_uk_phone_number, is_valid_uk_postcode
from directory_constants.choices import COUNTRY_CHOICES
from regex import PHONE_NUMBER_REGEX

COUNTRIES = COUNTRY_CHOICES.copy()
COUNTRIES.insert(0, ('', 'Select a country'))
Expand All @@ -39,7 +37,8 @@ def to_python(self, value):
return super().to_python(value)


PHONE_ERROR_MESSAGE = 'Please enter a valid UK phone number'
PHONE_REQUIRED_MESSAGE = 'Enter your telephone number'
PHONE_INVALID_MESSAGE = 'Enter a valid UK telephone number'


class PersonalDetails(forms.Form):
Expand All @@ -48,7 +47,7 @@ class PersonalDetails(forms.Form):
min_length=2,
max_length=50,
error_messages={
'required': _('Enter your name'),
'required': _('Enter your first name'),
},
widget=django_widgets.TextInput(attrs={'class': 'govuk-input great-text-input'}),
)
Expand All @@ -57,20 +56,17 @@ class PersonalDetails(forms.Form):
min_length=2,
max_length=50,
error_messages={
'required': _('Enter your family name'),
'required': _('Enter your last name'),
},
widget=django_widgets.TextInput(attrs={'class': 'govuk-input great-text-input'}),
)
phone_number = forms.CharField(
label='UK telephone number',
min_length=8,
max_length=16,
help_text='This can be a landline or mobile number',
validators=[is_valid_uk_phone_number],
error_messages={
'max_length': PHONE_ERROR_MESSAGE,
'min_length': PHONE_ERROR_MESSAGE,
'invalid': PHONE_ERROR_MESSAGE,
'required': PHONE_ERROR_MESSAGE,
'invalid': PHONE_INVALID_MESSAGE,
'required': PHONE_REQUIRED_MESSAGE,
},
widget=django_widgets.TextInput(attrs={'class': 'govuk-input great-text-input'}),
)
Expand All @@ -83,14 +79,6 @@ class PersonalDetails(forms.Form):
widget=django_widgets.TextInput(attrs={'class': 'govuk-input great-text-input'}),
)

def clean_phone_number(self):
phone_number = self.cleaned_data['phone_number'].replace(' ', '')
if phone_number == '':
return phone_number
if not PHONE_NUMBER_REGEX.match(phone_number):
raise ValidationError(PHONE_ERROR_MESSAGE)
return phone_number

@property
def serialized_data(self):
data = super().serialized_data
Expand All @@ -113,22 +101,21 @@ class ExportExperience(forms.Form):
('I do not have a product for export', 'I do not have a product for export'),
),
widget=forms.RadioSelect(attrs={'id': 'hiring-select'}),
error_messages={'required': _('Please answer this question')},
error_messages={'required': _('Choose one option about your export experience')},
)

sector = forms.ChoiceField(
label='What is your sector?',
help_text='Select at least one sector that applies to you',
choices=constants.INDUSTRY_CHOICES,
error_messages={'required': _('Please answer this question')},
error_messages={'required': _('Choose a sector')},
widget=django_widgets.Select(attrs={'class': 'govuk-select great-select'}),
)

second_sector = forms.ChoiceField(
label='',
help_text='Select an additional sector (optional)',
choices=constants.INDUSTRY_CHOICES,
error_messages={'required': _('Please answer this question')},
required=False,
widget=django_widgets.Select(attrs={'class': 'govuk-select great-select'}),
)
Expand All @@ -137,7 +124,6 @@ class ExportExperience(forms.Form):
label='',
help_text='Select an additional sector (optional)',
choices=constants.INDUSTRY_CHOICES,
error_messages={'required': _('Please answer this question')},
required=False,
widget=django_widgets.Select(attrs={'class': 'govuk-select great-select'}),
)
Expand All @@ -151,7 +137,7 @@ class ExportExperience(forms.Form):
("I don't know", "I don't know"),
),
widget=forms.RadioSelect,
error_messages={'required': _('Please answer this question')},
error_messages={'required': _('Choose one option about what you export')},
)

@property
Expand Down Expand Up @@ -180,7 +166,7 @@ class BusinessDetails(forms.Form):
business_postcode = forms.CharField(
label='Business unit postcode',
max_length=8,
error_messages={'required': 'Enter your business postcode', 'invalid': 'Please enter a UK postcode'},
error_messages={'required': 'Enter your business postcode', 'invalid': 'Enter a valid UK postcode'},
validators=[is_valid_uk_postcode],
widget=django_widgets.TextInput(attrs={'class': 'govuk-input great-text-input govuk-!-width-one-half'}),
)
Expand All @@ -196,7 +182,7 @@ class BusinessDetails(forms.Form):
("I'd prefer not to say", "I'd prefer not to say"),
),
widget=forms.RadioSelect,
error_messages={'required': _('Please answer this question')},
error_messages={'required': _('Enter a turnover amount')},
)

employee_count = forms.ChoiceField(
Expand All @@ -209,7 +195,7 @@ class BusinessDetails(forms.Form):
('prefer not to say', "I'd prefer not to say"),
),
widget=forms.RadioSelect,
error_messages={'required': _('Please answer this question')},
error_messages={'required': _('Choose number of employees')},
)

@property
Expand All @@ -220,9 +206,9 @@ def serialized_data(self):

class MarketingSources(forms.Form):
marketing_sources = forms.ChoiceField(
label=_('How did you hear about the Export Academy?'),
label='',
choices=constants.MARKETING_SOURCES_CHOICES,
error_messages={'required': _('Please answer this question')},
error_messages={'required': _('Tell us how you heard about the UK Export Academy')},
widget=django_widgets.Select(attrs={'class': 'govuk-select great-select'}),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,17 @@ <h2 class="govuk-heading-m govuk-!-margin-bottom-0">About your business</h2>
<dl class="govuk-summary-list govuk-summary-list--no-border">
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
<h2 class="govuk-heading-m govuk-!-margin-bottom-0">And finally...</h2>
<h2 class="govuk-heading-m govuk-!-margin-bottom-0">How did you hear about the UK Export Academy?</h2>
</dt>
<dd class="govuk-summary-list__value width-quarter">
<a class="govuk-link"
href="{% url 'export_academy:registration-marketing-edit' %}">
Change<span class="govuk-visually-hidden">Finally</span>
Change<span class="govuk-visually-hidden">How did you hear about the UK Export Academy?</span>
</a>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">How you heard of us</dt>
<dt class="govuk-summary-list__key">Tell us how you heard about the UK Export Academy</dt>
<dd class="govuk-summary-list__value width-quarter">
{{ form_data.marketing_sources }}
</dd>
Expand Down
4 changes: 2 additions & 2 deletions export_academy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ def get_context_data(self, **kwargs):
back_url=back_url,
step_text='Step 4 of 4',
landing_page=ExportAcademyHomePage.objects.first(),
title='And finally...',
current_page_breadcrumb='And finally...',
title='How did you hear about the UK Export Academy?',
current_page_breadcrumb='How did you hear about the UK Export Academy?',
)

def get_success_url(self):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ celery[redis]==5.2.7
# via
# -r requirements.in
# django-celery-beat
certifi==2023.11.17
certifi==2024.2.2
# via
# elastic-apm
# elasticsearch
Expand Down
4 changes: 2 additions & 2 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ celery[redis]==5.2.7
# via
# -r requirements.in
# django-celery-beat
certifi==2023.11.17
certifi==2024.2.2
# via
# elastic-apm
# elasticsearch
Expand Down Expand Up @@ -580,7 +580,7 @@ pytest-cov==4.1.0
# pytest-codecov
pytest-django==4.8.0
# via -r requirements_test.in
pytest-sugar==0.9.7
pytest-sugar==1.0.0
# via -r requirements_test.in
pytest-xdist==3.5.0
# via -r requirements_test.in
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/core/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from django.forms import ValidationError
from django.test import override_settings

from core.validators import is_valid_uk_postcode, validate_file_infection
from core.validators import (
is_valid_uk_phone_number,
is_valid_uk_postcode,
validate_file_infection,
)
from tests.helpers import create_response


Expand Down Expand Up @@ -55,3 +59,21 @@ def test_validate_file_infection_negative_scan(mock_clam_av_client_scan):
pytest.fail('Should not raise a validator error.')

assert file.seek.call_count == 1


@pytest.mark.parametrize(
'phone_number, raise_expected',
(
('07508236677', False),
('90201', True),
('phone_number', True),
),
)
def test_is_valid_uk_phone_number(phone_number, raise_expected):
try:
is_valid_uk_phone_number(phone_number)
if raise_expected:
assert False, f'Excepted {phone_number} to fail validation. It did not'
except ValidationError:
if not raise_expected:
assert False, f'Excepted {phone_number} to pass validation. It did not'
18 changes: 9 additions & 9 deletions tests/unit/export_academy/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
},
),
{
'first_name': 'Enter your name',
'phone_number': 'Please enter a valid UK phone number',
'last_name': 'Enter your family name',
'first_name': 'Enter your first name',
'phone_number': 'Enter your telephone number',
'last_name': 'Enter your last name',
'job_title': 'Enter your job title',
},
),
Expand All @@ -59,9 +59,9 @@
},
),
{
'export_experience': 'Please answer this question',
'sector': 'Please answer this question',
'export_product': 'Please answer this question',
'export_experience': 'Choose one option about your export experience',
'sector': 'Choose a sector',
'export_product': 'Choose one option about what you export',
},
),
(
Expand All @@ -87,8 +87,8 @@
'business_name': 'Enter your business name',
'business_address_line_1': 'Enter the first line of your business address',
'business_postcode': 'Enter your business postcode',
'annual_turnover': 'Please answer this question',
'employee_count': 'Please answer this question',
'annual_turnover': 'Enter a turnover amount',
'employee_count': 'Choose number of employees',
},
),
(
Expand All @@ -103,7 +103,7 @@
},
),
{
'marketing_sources': 'Please answer this question',
'marketing_sources': 'Tell us how you heard about the UK Export Academy',
},
),
(
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/export_academy/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ def test_booking_success_view(
},
reverse('export_academy:registration-experience'),
{
'first_name': 'Enter your name',
'last_name': 'Enter your family name',
'first_name': 'Enter your first name',
'last_name': 'Enter your last name',
'job_title': 'Enter your job title',
'phone_number': 'Please enter a valid UK phone number',
'phone_number': 'Enter your telephone number',
},
),
(
Expand All @@ -285,9 +285,9 @@ def test_booking_success_view(
},
reverse('export_academy:registration-business'),
{
'export_experience': 'Please answer this question',
'sector': 'Please answer this question',
'export_product': 'Please answer this question',
'export_experience': 'Choose one option about your export experience',
'sector': 'Choose a sector',
'export_product': 'Choose one option about what you export',
},
),
(
Expand All @@ -304,8 +304,8 @@ def test_booking_success_view(
'business_name': 'Enter your business name',
'business_address_line_1': 'Enter the first line of your business address',
'business_postcode': 'Enter your business postcode',
'annual_turnover': 'Please answer this question',
'employee_count': 'Please answer this question',
'annual_turnover': 'Enter a turnover amount',
'employee_count': 'Choose number of employees',
},
),
(
Expand All @@ -315,7 +315,7 @@ def test_booking_success_view(
},
reverse('export_academy:registration-confirm'),
{
'marketing_sources': 'Please answer this question',
'marketing_sources': 'Tell us how you heard about the UK Export Academy',
},
),
),
Expand Down

0 comments on commit 23a6006

Please sign in to comment.