Skip to content

Commit

Permalink
- create a BaseSearch class which is shared by AdviserSearchForm & …
Browse files Browse the repository at this point in the history
…SingleCategorySearchForm - to stop code duplication
  • Loading branch information
MazOneTwoOne committed Dec 12, 2024
1 parent 9cd0380 commit df4b245
Showing 1 changed file with 48 additions and 73 deletions.
121 changes: 48 additions & 73 deletions fala/apps/adviser/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,53 @@ class AdviserRootForm(forms.Form):
)


# Shared method to handle searches
def perform_search(form, postcode, categories, page):
try:
data = laalaa.find(
postcode=postcode,
categories=categories,
page=page,
organisation_types=form.cleaned_data.get("type"),
organisation_name=form.cleaned_data.get("name"),
)
if "error" in data:
form.add_error("postcode", data["error"])
return data
except laalaa.LaaLaaError:
form.add_error("postcode", "%s %s" % (_("Error looking up legal advisers."), _("Please try again later.")))
return {}
class BaseSearch:
def perform_search(self, form, postcode, categories, page):
try:
data = laalaa.find(
postcode=postcode,
categories=categories,
page=page,
organisation_types=form.cleaned_data.get("type"),
organisation_name=form.cleaned_data.get("name"),
)
if "error" in data:
form.add_error("postcode", data["error"])
return data
except laalaa.LaaLaaError:
form.add_error("postcode", "%s %s" % (_("Error looking up legal advisers."), _("Please try again later.")))
return {}

@property
def region(self):
# retrieve the api call variables
country_from_valid_postcode = getattr(self, "_country_from_valid_postcode", None)

# Return `Region.ENGLAND_OR_WALES` from `clean` if set
if not country_from_valid_postcode:
return getattr(self, "_region", None)

# for Guernsey & Jersey the country comes back as 'Channel Islands', we are using `nhs_ha` key, to distinguish between them
country, nhs_ha = country_from_valid_postcode

if country == "Northern Ireland":
return Region.NI
elif country == "Isle of Man":
return Region.IOM
elif country == "Channel Islands" and nhs_ha == "Jersey Health Authority":
return Region.JERSEY
elif country == "Channel Islands" and nhs_ha == "Guernsey Health Authority":
return Region.GUERNSEY
elif country == "Scotland":
return Region.SCOTLAND
elif country == "England" or country == "Wales":
return Region.ENGLAND_OR_WALES
else:
self.add_error("postcode", _("This service is only available for England and Wales"))
return None


class AdviserSearchForm(AdviserRootForm):
class AdviserSearchForm(AdviserRootForm, BaseSearch):
page = forms.IntegerField(required=False, widget=forms.HiddenInput())

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -113,34 +141,6 @@ def clean(self):

return data

@property
def region(self):
# retrieve the api call variables
country_from_valid_postcode = getattr(self, "_country_from_valid_postcode", None)

# Return `Region.ENGLAND_OR_WALES` from `clean` if set
if not country_from_valid_postcode:
return getattr(self, "_region", None)

# for Guernsey & Jersey the country comes back as 'Channel Islands', we are using `nhs_ha` key, to distinguish between them
country, nhs_ha = country_from_valid_postcode

if country == "Northern Ireland":
return Region.NI
elif country == "Isle of Man":
return Region.IOM
elif country == "Channel Islands" and nhs_ha == "Jersey Health Authority":
return Region.JERSEY
elif country == "Channel Islands" and nhs_ha == "Guernsey Health Authority":
return Region.GUERNSEY
elif country == "Scotland":
return Region.SCOTLAND
elif country == "England" or country == "Wales":
return Region.ENGLAND_OR_WALES
else:
self.add_error("postcode", _("This service is only available for England and Wales"))
return None

@property
def current_page(self):
return self.cleaned_data.get("page", 1)
Expand All @@ -149,15 +149,15 @@ def validate_postcode_and_return_country(self, postcode):
return validate_postcode_and_return_country(postcode, form=self)

def search(self):
return perform_search(
return self.perform_search(
self,
self.cleaned_data.get("postcode"),
self.cleaned_data.get("categories"),
self.cleaned_data.get("page", 1),
)


class SingleCategorySearchForm(AdviserRootForm):
class SingleCategorySearchForm(AdviserRootForm, BaseSearch):
page = forms.IntegerField(required=False, widget=forms.HiddenInput())

def __init__(self, categories=None, *args, **kwargs):
Expand Down Expand Up @@ -190,31 +190,6 @@ def clean(self):

return data

@property
def region(self):
country_from_valid_postcode = getattr(self, "_country_from_valid_postcode", None)

if not country_from_valid_postcode:
return getattr(self, "_region", None)

country, nhs_ha = country_from_valid_postcode

if country == "Northern Ireland":
return Region.NI
elif country == "Isle of Man":
return Region.IOM
elif country == "Channel Islands" and nhs_ha == "Jersey Health Authority":
return Region.JERSEY
elif country == "Channel Islands" and nhs_ha == "Guernsey Health Authority":
return Region.GUERNSEY
elif country == "Scotland":
return Region.SCOTLAND
elif country == "England" or country == "Wales":
return Region.ENGLAND_OR_WALES
else:
self.add_error("postcode", _("This service is only available for England and Wales"))
return None

@property
def current_page(self):
return self.cleaned_data.get("page", 1)
Expand All @@ -223,7 +198,7 @@ def validate_postcode_and_return_country(self, postcode):
return validate_postcode_and_return_country(postcode, form=self)

def search(self):
return perform_search(
return self.perform_search(
self,
self.cleaned_data.get("postcode"),
self.categories,
Expand Down

0 comments on commit df4b245

Please sign in to comment.