Skip to content

Commit

Permalink
Flow type and other enhancements
Browse files Browse the repository at this point in the history
There are two flow types: central government and everything else

This is used in various places as a fork/decision point

It is determined by registrant type - If the value is

"Central government department or agency"

or

"Non-departmental body - also known as an arm's length body"

then the flow type is considered as government flow type

Hence added a method to determine whether it is central government

flow type and it will be set only in the RegistrantType view

as it is derived by Registrant Type and it will be used everywhere

it is needed. This value of flow type is being saved in the

navigation_data in session to distinguish from the other business

data

Also added a utility method

clean_registration_data_field_and_save_in_session

because this is used in many views and it will save us boilerplate

code

Although there are many registrant types, but from one viewpoint,

they fall into either central government or everything else

This distinction is used in various places as a fork/decision point

It is determined by registrant type - If the value is

"Central government department or agency"

or

"Non-departmental body - also known as an arm's length body"

then they are considered as central government

Hence added a method to determine whether it is central government

Also added a utility method add_to_session

because this is used in many views and it will save us
boilerplate code
  • Loading branch information
prakash-gds committed Mar 11, 2024
1 parent 9604367 commit 4537ab5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions request_a_govuk_domain/request/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REGISTRATION_DATA = "registration_data"
23 changes: 23 additions & 0 deletions request_a_govuk_domain/request/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from django.conf import settings

from request_a_govuk_domain.request.constants import REGISTRATION_DATA


def handle_uploaded_file(file):
file_path = os.path.join(settings.MEDIA_ROOT, file.name)
Expand All @@ -28,3 +30,24 @@ def organisations_list() -> list:
for row in reader:
data.append((row[0], (row[0])))
return data


def is_central_government(registrant_type: str) -> bool:
"""
Check if the registrant type is Central Government or Non-departmental body
Note: If above is True then registrant type will be considered as Central Government
"""
return registrant_type in ["central_government", "ndpb"]


def add_to_session(form, request, field_name: str) -> str:
"""
Common utility method to clean the field and save it in the session
Returns the field value
This is to save boilerplate code in the views
"""
registration_data = request.session.get(REGISTRATION_DATA, {})
field_value = form.cleaned_data[field_name]
registration_data[field_name] = field_value
request.session[REGISTRATION_DATA] = registration_data
return field_value
17 changes: 10 additions & 7 deletions request_a_govuk_domain/request/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.views.generic import TemplateView

from .constants import REGISTRATION_DATA
from .forms import (
NameForm,
EmailForm,
Expand All @@ -24,8 +26,10 @@

from django.views.generic.edit import FormView

from .utils import handle_uploaded_file

from .utils import (
handle_uploaded_file,
add_to_session,
)

"""
Some views are example views, please modify remove as needed
Expand Down Expand Up @@ -174,10 +178,9 @@ def get(self, request):
def post(self, request):
form = RegistrantTypeForm(request.POST)
if form.is_valid():
registration_data = request.session.get("registration_data", {})
registration_data["registrant_type"] = form.cleaned_data["registrant_type"]
request.session["registration_data"] = registration_data
if form.cleaned_data["registrant_type"] == "none":
registrant_type = add_to_session(form, request, "registrant_type")

if registrant_type == "none":
return redirect("registrant_type_fail")
else:
return redirect("registrant")
Expand Down Expand Up @@ -241,7 +244,7 @@ def form_valid(self, form):
# registration_data = self.request.session.get("registration_data", {})

# Clear session data after saving
self.request.session.pop("registration_data", None)
self.request.session.pop(REGISTRATION_DATA, None)

return super().form_valid(form)

Expand Down

0 comments on commit 4537ab5

Please sign in to comment.