Skip to content

Commit

Permalink
various fixes to the myself journey (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenriordan authored Sep 3, 2024
1 parent 4e5e065 commit f73973b
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 68 deletions.
2 changes: 1 addition & 1 deletion django_app/apply_for_a_licence/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.14 on 2024-08-30 08:16
# Generated by Django 4.2.14 on 2024-09-02 10:14

import core.document_storage
from django.conf import settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,71 +148,73 @@ <h3 class="govuk-heading-s"> Your Details</h3>
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link"
href="{% url 'add_yourself_address' %}">Change<span
href="{% url 'add_yourself_address' location=add_yourself_address individual_uuid=add_yourself_id %}&redirect_to_url=check_your_answers">Change<span
class="govuk-visually-hidden">your address</span></a>
</dd>
</div>
</dl>
{% for id, individual in individuals.items %}
<h3 class="govuk-heading-s"> Individual {{ forloop.counter }}</h3>
<dl class="govuk-summary-list">
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key govuk-!-font-weight-regular">
Name
</dt>
<dd class="govuk-summary-list__value">
{{ individual.name_data.cleaned_data.first_name }} {{ individual.name_data.cleaned_data.last_name }}
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link"
href="{% url 'add_an_individual' %}?individual_uuid={{ id }}&redirect_to_url=check_your_answers">Change<span
class="govuk-visually-hidden">name of individual {{ forloop.counter }}</span></a>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key govuk-!-font-weight-regular">
Connection to the UK
</dt>
<dd class="govuk-summary-list__value">
{{ individual.name_data.cleaned_data.nationality }}
{% if form_data.do_you_want_to_add_another_individual == "Yes" or form_data.who_do_you_want_the_licence_to_cover == "individual" or form_data.who_do_you_want_the_licence_to_cover == "business" %}
{% for id, individual in individuals.items %}
<h3 class="govuk-heading-s"> Individual {{ forloop.counter }}</h3>
<dl class="govuk-summary-list">
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key govuk-!-font-weight-regular">
Name
</dt>
<dd class="govuk-summary-list__value">
{{ individual.name_data.cleaned_data.first_name }} {{ individual.name_data.cleaned_data.last_name }}
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link"
href="{% url 'add_an_individual' %}?individual_uuid={{ id }}&redirect_to_url=check_your_answers">Change<span
class="govuk-visually-hidden">name of individual {{ forloop.counter }}</span></a>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key govuk-!-font-weight-regular">
Connection to the UK
</dt>
<dd class="govuk-summary-list__value">
{{ individual.name_data.cleaned_data.nationality }}

</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link"
href="{% url 'add_an_individual' %}?individual_uuid={{ id }}"
&redirect_to_url=check_your_answers>Change<span
class="govuk-visually-hidden">connection to the UK of individual {{ forloop.counter }}</span></a>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key govuk-!-font-weight-regular">
Location of individual
</dt>
<dd class="govuk-summary-list__value">
{{ individual.name_data.cleaned_data.nationality_and_location | readable_location }}
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link"
href="{% url 'add_an_individual' %}?individual_uuid={{ id }}&redirect_to_url=check_your_answers">Change<span
class="govuk-visually-hidden">location of individual {{ forloop.counter }}</span></a>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key govuk-!-font-weight-regular">
Address
</dt>
<dd class="govuk-summary-list__value">
{% include "apply_for_a_licence/partials/truncated_text.html" with text=individual.address_data.cleaned_data.readable_address|linebreaksbr %}
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link"
href="{% url 'what_is_individuals_address' location=individual.address_data.cleaned_data.url_location individual_uuid=id %}?redirect_to_url=check_your_answers">
Change
<span class="govuk-visually-hidden">address of individual {{ forloop.counter }}</span></a>
</dd>
</div>
</dl>
{% endfor %}
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link"
href="{% url 'add_an_individual' %}?individual_uuid={{ id }}"
&redirect_to_url=check_your_answers>Change<span
class="govuk-visually-hidden">connection to the UK of individual {{ forloop.counter }}</span></a>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key govuk-!-font-weight-regular">
Location of individual
</dt>
<dd class="govuk-summary-list__value">
{{ individual.name_data.cleaned_data.nationality_and_location | readable_location }}
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link"
href="{% url 'add_an_individual' %}?individual_uuid={{ id }}&redirect_to_url=check_your_answers">Change<span
class="govuk-visually-hidden">location of individual {{ forloop.counter }}</span></a>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key govuk-!-font-weight-regular">
Address
</dt>
<dd class="govuk-summary-list__value">
{% include "apply_for_a_licence/partials/truncated_text.html" with text=individual.address_data.cleaned_data.readable_address|linebreaksbr %}
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link"
href="{% url 'what_is_individuals_address' location=individual.address_data.cleaned_data.url_location individual_uuid=id %}?redirect_to_url=check_your_answers">
Change
<span class="govuk-visually-hidden">address of individual {{ forloop.counter }}</span></a>
</dd>
</div>
</dl>
{% endfor %}
{% endif %}
<dl class="govuk-summary-list">
<a class="govuk-link"
href="{% url 'add_an_individual' %}?redirect_to_url=check_your_answers&new=yes">
Expand Down
6 changes: 5 additions & 1 deletion django_app/apply_for_a_licence/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@

views_yourself_urls = [
path("add_yourself", views_yourself.AddYourselfView.as_view(), name="add_yourself"),
path("add_yourself_address", views_yourself.AddYourselfAddressView.as_view(), name="add_yourself_address"),
path(
"add_yourself_address/<str:location>/<str:individual_uuid>",
views_yourself.AddYourselfAddressView.as_view(),
name="add_yourself_address",
),
path(
"yourself_and_individual_added",
views_yourself.YourselfAndIndividualAddedView.as_view(),
Expand Down
9 changes: 9 additions & 0 deletions django_app/apply_for_a_licence/views/views_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def get_context_data(self, **kwargs: object) -> dict[str, Any]:
if session_files := get_all_session_files(TemporaryDocumentStorage(), self.request.session):
context["session_files"] = session_files

if add_yourself_address_id := self.request.session.get("add_yourself_id", None):
context["add_yourself_id"] = add_yourself_address_id
context["add_yourself_address"] = (
"in_the_uk" if self.request.session["add_yourself_address"]["country"] == "GB" else "outside_the_uk"
)

if businesses := self.request.session.get("businesses", None):
context["businesses"] = businesses
if individuals := self.request.session.get("individuals", None):
Expand Down Expand Up @@ -66,6 +72,9 @@ def form_valid(self, form: DeclarationForm) -> HttpResponse:
is_individual = True
if cleaned_data["start"]["who_do_you_want_the_licence_to_cover"] == "individual":
business_employing_individual = True
# TODO: temporary fix
if cleaned_data["start"]["who_do_you_want_the_licence_to_cover"] == "myself":
cleaned_data["add_yourself_address"] = self.request.session["add_yourself_address"]

if (
cleaned_data["is_the_business_registered_with_companies_house"].get("business_registered_on_companies_house", "")
Expand Down
71 changes: 70 additions & 1 deletion django_app/apply_for_a_licence/views/views_yourself.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging
import urllib.parse
import uuid

from apply_for_a_licence.choices import NationalityAndLocation
from apply_for_a_licence.forms import forms_individual as individual_forms
from apply_for_a_licence.forms import forms_yourself as forms
from apply_for_a_licence.utils import get_form
Expand All @@ -13,7 +16,45 @@

class AddYourselfView(BaseFormView):
form_class = forms.AddYourselfForm
success_url = reverse_lazy("add_yourself_address")

def form_valid(self, form: forms.AddYourselfForm) -> HttpResponse:
your_details = {
"cleaned_data": form.cleaned_data,
"dirty_data": form.data,
}
self.request.session["name_data"] = your_details

current_individuals = self.request.session.get("individuals", {})
# get the individual_uuid if it exists, otherwise create it
if individual_uuid := self.request.GET.get("individual_uuid", str(uuid.uuid4())):
# used to display the individual_uuid data in individual_added.html
if individual_uuid not in current_individuals:
current_individuals[individual_uuid] = {}

current_individuals[individual_uuid]["name_data"] = your_details
self.individual_uuid = individual_uuid

self.request.session["individuals"] = current_individuals

self.is_uk_individual = form.cleaned_data["nationality_and_location"] in [
NationalityAndLocation.uk_national_uk_location.value,
NationalityAndLocation.dual_national_uk_location.value,
NationalityAndLocation.non_uk_national_uk_location.value,
]

return super().form_valid(form)

def get_success_url(self):
success_url = reverse(
"add_yourself_address",
kwargs={
"location": "in_the_uk" if self.is_uk_individual else "outside_the_uk",
"individual_uuid": self.individual_uuid,
},
)
if get_parameters := urllib.parse.urlencode(self.request.GET):
success_url += "?" + get_parameters
return success_url


class AddYourselfAddressView(BaseFormView):
Expand All @@ -31,12 +72,40 @@ def get_form_class(self) -> [forms.AddYourselfUKAddressForm | forms.AddYourselfN
form_class = forms.AddYourselfUKAddressForm
return form_class

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()

# restore the form data from the individual_uuid, if it exists
if self.request.method == "GET":
if individual_uuid := self.request.GET.get("individual_uuid", None):
if individuals_dict := self.request.session.get("individuals", {}).get(individual_uuid, None):
kwargs["data"] = individuals_dict["name_data"]["dirty_data"]

return kwargs

def form_valid(self, form: forms.AddYourselfUKAddressForm | forms.AddYourselfNonUKAddressForm) -> HttpResponse:
your_address = {
"cleaned_data": form.cleaned_data,
"dirty_data": form.data,
}
self.request.session["your_address"] = your_address

current_individuals = self.request.session.get("individuals", {})
# get the individual_uuid if it exists, otherwise create it
if individual_uuid := self.kwargs.get("individual_uuid", str(uuid.uuid4())):
# used to display the individual_uuid data in individual_added.html
if individual_uuid not in current_individuals:
current_individuals[individual_uuid] = {}

self.request.session["add_yourself_id"] = individual_uuid
self.request.session["add_yourself_address"] = your_address
current_individuals[individual_uuid]["address_data"] = your_address
self.individual_uuid = individual_uuid

# is it a UK address?
self.is_uk_individual = form.cleaned_data["url_location"] == "in_the_uk"
self.request.session["individuals"] = current_individuals

return super().form_valid(form)


Expand Down
1 change: 0 additions & 1 deletion django_app/utils/save_to_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def save_licence(self) -> Licence:

if self.data["start"]["who_do_you_want_the_licence_to_cover"] == "myself":
licence.applicant_full_name = f"{self.data['add_yourself']['first_name']} {self.data['add_yourself']['last_name']}"
licence.applicant_business = self.data["business_employing_individual"]["name"]
else:
licence.applicant_role = self.data["your_details"]["applicant_role"]
licence.applicant_business = self.data["your_details"]["applicant_business"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from apply_for_a_licence.choices import NationalityAndLocation
from django.test import RequestFactory
from django.urls import reverse

Expand Down Expand Up @@ -34,17 +35,44 @@ class TestAddYourselfView:
def test_successful_post(self, al_client):
response = al_client.post(
reverse("add_yourself"),
data={"first_name": "John", "last_name": "Doe", "nationality_and_location": "uk_national_uk_location"},
data={
"first_name": "John",
"last_name": "Doe",
"nationality_and_location": NationalityAndLocation.uk_national_uk_location.value,
},
follow=True,
)

assert (
reverse(
"add_yourself_address",
kwargs={
"location": "in_the_uk",
"individual_uuid": response.resolver_match.kwargs["individual_uuid"],
},
)
in response.redirect_chain[0][0]
)
assert response.url == reverse("add_yourself_address")


class TestAddYourselfAddressView:
def test_successful_non_uk_address_post(self, al_client):
response = al_client.post(
reverse("add_yourself_address"),
data={"country": "DE", "town_or_city": "Berlin", "address_line_1": "Checkpoint Charlie"},
reverse(
"add_yourself_address",
kwargs={
"location": "outside_the_uk",
"individual_uuid": "individual1",
},
),
data={
"country": "DE",
"town_or_city": "Berlin",
"address_line_1": "Checkpoint Charlie",
"nationality_and_location": NationalityAndLocation.non_uk_national_uk_location.value,
},
)

assert response.url == reverse("yourself_and_individual_added")


Expand Down

0 comments on commit f73973b

Please sign in to comment.