Skip to content

Commit

Permalink
WIP: PC-351 - EPC API integration
Browse files Browse the repository at this point in the history
  • Loading branch information
chidin194 committed Dec 7, 2023
1 parent 5a93836 commit 20cb962
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 115 deletions.
2 changes: 0 additions & 2 deletions help_to_heat/frontdoor/epc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def get_address_and_rrn(token, building, postcode):
response.raise_for_status() # Raise an error for 4XX and 5XX status codes
data = response.json()
# Process the response data
print(data)
return data
except requests.exceptions.RequestException as e:
print('Error making API request:', e)
Expand All @@ -99,7 +98,6 @@ def get_epc_details(token, rrn):
response.raise_for_status() # Raise an error for 4XX and 5XX status codes
data = response.json()
# Process the response data
print(data)
return data
except requests.exceptions.RequestException as e:
print('Error making API request:', e)
Expand Down
78 changes: 20 additions & 58 deletions help_to_heat/frontdoor/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class FindAddressesSchema(marshmallow.Schema):


class GetAddressSchema(marshmallow.Schema):
rrn = marshmallow.fields.String()
uprn = marshmallow.fields.String()


class AddressSchema(marshmallow.Schema):
Expand Down Expand Up @@ -255,23 +255,16 @@ def get_address(self, uprn):

try:
api_results = api.uprn(int(uprn), dataset="LPI")["features"]
api_results_dpa = api.uprn(int(uprn))["features"]
address = api_results[0]["properties"]["ADDRESS"]

if "BUILDING_NUMBER" in api_results_dpa[0]["properties"]:
building = api_results_dpa[0]["properties"]["BUILDING_NUMBER"]
else:
building = api_results_dpa[0]["properties"]["BUILDING_NAME"]

postcode = api_results_dpa[0]["properties"]["POSTCODE"]
result = {"uprn": uprn, "address": address, "building": building, "postcode": postcode}
result = {"uprn": uprn, "address": address}
return result
except requests.exceptions.HTTPError or requests.exceptions.RequestException as e:
if e.response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
raise ThrottledApiException
else:
raise e


def is_current_residential(self, lpi):
return (
lpi["POSTAL_ADDRESS_CODE"] != "N" # is a postal address
Expand Down Expand Up @@ -460,22 +453,23 @@ def parse_dpa_to_address(self, dpa):


class EPC(Entity):
# @with_schema(load=GetEPCSchema, dump=EPCSchema)
# def get_epc(self, uprn, country):
# try:
# if country == "England" or country == "Wales":
# epc = portal.models.EpcRating.objects.get(uprn=uprn)
# elif country == "Scotland":
# epc = portal.models.ScottishEpcRating.objects.get(uprn=uprn)
# else:
# epc = None
# except (portal.models.EpcRating.DoesNotExist, portal.models.ScottishEpcRating.DoesNotExist):
# epc = None
# if epc:
# data = {"uprn": epc.uprn, "rating": epc.rating, "date": epc.date}
# else:
# data = {}
# return data
@with_schema(load=GetEPCSchema, dump=EPCSchema)
def get_epc_scotland(self, uprn, country):
try:
# if country == "England" or country == "Wales":
# epc = portal.models.EpcRating.objects.get(uprn=uprn)
if country == "Scotland":
epc = portal.models.ScottishEpcRating.objects.get(uprn=uprn)
else:
epc = None
except (portal.models.EpcRating.DoesNotExist, portal.models.ScottishEpcRating.DoesNotExist):
epc = None
if epc:
data = {"uprn": epc.uprn, "rating": epc.rating, "date": epc.date}
else:
data = {}
return data

def get_address_and_epc_rrn(building_name_or_number, postcode):
token = EPCApi.get_access_token()
data = EPCApi.get_address_and_rrn(token, building_name_or_number, postcode)
Expand All @@ -488,8 +482,6 @@ def get_epc_details(epc_rrn):
return data




class Feedback(Entity):
@with_schema(load=FeedbackSchema, dump=SuccessSchema)
def save_feedback(self, session_id, page_name, data):
Expand All @@ -498,33 +490,3 @@ def save_feedback(self, session_id, page_name, data):


api = Interface(session=Session(), address=Address(), epc=EPC(), feedback=Feedback())






[
{
"epcRrn": "1111-1111-1111-1111-1111",
"address": {
"addressLine1": "11 Acacia Avenue",
"addressLine2": "Upper Wellgood",
"addressLine3": "",
"addressLine4": "",
"town": "Fulchester",
"postcode": "FL23 4JA"
}
},
{
"epcRrn": "2222-2222-2222-2222-2222",
"address": {
"addressLine1": "22 Acacia Avenue",
"addressLine2": "Upper Wellgood",
"addressLine3": "",
"addressLine4": "",
"town": "Fulchester",
"postcode": "FL23 4JA"
}
}
]
5 changes: 5 additions & 0 deletions help_to_heat/frontdoor/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
extra_pages = (
"applications-closed",
"address-select",
"epc-select",
"address-manual",
"loft-access",
"loft-insulation",
Expand All @@ -58,6 +59,7 @@

page_prev_next_map = {
"address-select": {"prev": "address", "next": "council-tax-band"},
"epc-select": {"prev": "address", "next": "council-tax-band"},
"address-manual": {"prev": "address", "next": "council-tax-band"},
"loft": {"prev": "wall-insulation", "next": "loft-access"},
"loft-access": {"prev": "loft", "next": "loft-insulation"},
Expand All @@ -76,6 +78,7 @@

page_prev_next_map_park_home = {
"address-select": {"prev": "address", "next": "benefits"},
"epc-select": {"prev": "address", "next": "benefits"},
"address-manual": {"prev": "address", "next": "benefits"},
"northern-ireland": {"prev": "country", "next": None},
"bulb-warning-page": {"prev": "supplier", "next": "own-property"},
Expand Down Expand Up @@ -703,6 +706,8 @@ class SessionSchema(Schema):
validate=validate.Regexp(postcode_regex_collection, error=_("Please enter a valid UK postcode"))
)
uprn = fields.String()
rrn = fields.String()
epc_details = fields.Dict()
address = fields.String(validate=validate.Length(max=512))
council_tax_band = fields.String(validate=validate.OneOf(welsh_council_tax_band_options))
accept_suggested_epc = fields.String(
Expand Down
156 changes: 104 additions & 52 deletions help_to_heat/frontdoor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"park-home": ("park_home",),
"park-home-main-residence": ("park_home_main_residence",),
"address": ("building_name_or_number", "postcode"),
"address-select": ("rrn",),
"epc-select": ("rrn",),
"address-select": ("uprn",),
"address-manual": ("address_line_1", "town_or_city", "postcode"),
"council-tax-band": ("council_tax_band",),
"epc": ("accept_suggested_epc",),
Expand All @@ -53,7 +54,8 @@
"building_name_or_number": _("Enter building name or number"),
"address_line_1": _("Enter Address line 1"),
"postcode": _("Enter a postcode"),
"rrn": _("Select your address"),
"uprn": _("Select your address"),
"rrn": _("Select your EPC"),
"town_or_city": _("Enter your Town or city"),
"council_tax_band": _("Enter the Council Tax Band of the property"),
"accept_suggested_epc": _("Select if your EPC rating is correct or not, or that you don’t know"),
Expand Down Expand Up @@ -358,35 +360,31 @@ def save_data(self, request, session_id, page_name, *args, **kwargs):
@register_page("address")
class AddressView(PageView):
def handle_post(self, request, session_id, page_name, data, is_change_page):
return redirect("frontdoor:page", session_id=session_id, page_name="address-select")


@register_page("address-select")
class AddressSelectView(PageView):
data = interface.api.session.get_answer(session_id, "address")
building_name_or_number = data["building_name_or_number"]
postcode = data["postcode"]
try:
interface.EPC.get_address_and_epc_rrn(building_name_or_number, postcode)
return redirect("frontdoor:page", session_id=session_id, page_name="epc-select")
except Exception:

return redirect("frontdoor:page", session_id=session_id, page_name="address-select")


@register_page("epc-select")
class EpcSelectView(PageView):
def format_address(self, a):
return f"""{a['address']['addressLine1'] + ',' if a['address']['addressLine1'] else ''}
{a['address']['addressLine2'] + ',' if a['address']['addressLine2'] else ''}
{a['address']['addressLine3'] + ',' if a['address']['addressLine3'] else ''}
{a['address']['addressLine4'] + ',' if a['address']['addressLine4'] else ''}
{a['address']['town']}, {a['address']['postcode']}"""

def get_context(self, request, session_id, *args, **kwargs):
data = interface.api.session.get_answer(session_id, "address")
building_name_or_number = data["building_name_or_number"]
postcode = data["postcode"]
# addresses = interface.api.address.find_addresses(building_name_or_number, postcode)
# uprn_options = tuple(
# {
# "value": a["uprn"],
# "label": f"""{a['address_line_1'] + ',' if a['address_line_1'] else ''}
# {a['address_line_2'] + ',' if a['address_line_2'] else ''}
# {a['town']}, {a['postcode']}""",
# }
# for a in addresses
# )
# return {"uprn_options": uprn_options}
address_and_rrn_details = interface.EPC.get_address_and_epc_rrn(building_name_or_number, postcode)

rrn_options = tuple(
{
"value": a["epcRrn"],
Expand All @@ -395,21 +393,42 @@ def get_context(self, request, session_id, *args, **kwargs):
for a in address_and_rrn_details
)
return {"rrn_options": rrn_options}


def save_data(self, request, session_id, page_name, *args, **kwargs):
rrn = request.POST["rrn"]
print(rrn)
# uprn = request.POST["uprn"]
# address_info = uprn.split("#")
# data = interface.api.address.get_address(uprn)
# print(data)
# building = data["building"]
# postcode = data["postcode"]
# address_line_1 = data["address"]
epc_data = interface.EPC.get_epc_details(rrn)
return epc_data
# data = interface.api.session.save_answer(session_id, page_name, data)
# return data
epc = interface.EPC.get_epc_details(rrn)
address = self.format_address(epc["data"]["assessment"])
epc_details = epc["data"]["assessment"]
epc_data = {"rrn": rrn, "address": address, "epc_details": epc_details}
data = interface.api.session.save_answer(session_id, page_name, epc_data)
return data


@register_page("address-select")
class AddressSelectView(PageView):
def get_context(self, request, session_id, *args, **kwargs):
data = interface.api.session.get_answer(session_id, "address")
building_name_or_number = data["building_name_or_number"]
postcode = data["postcode"]
addresses = interface.api.address.find_addresses(building_name_or_number, postcode)
uprn_options = tuple(
{
"value": a["uprn"],
"label": f"""{a['address_line_1'] + ',' if a['address_line_1'] else ''}
{a['address_line_2'] + ',' if a['address_line_2'] else ''}
{a['town']}, {a['postcode']}""",
}
for a in addresses
)
return {"uprn_options": uprn_options}


def save_data(self, request, session_id, page_name, *args, **kwargs):
uprn = request.POST["uprn"]
data = interface.api.address.get_address(uprn)
data = interface.api.session.save_answer(session_id, page_name, data)
return data


@register_page("address-manual")
Expand Down Expand Up @@ -444,34 +463,67 @@ def get_context(self, request, session_id, *args, **kwargs):
@register_page("epc")
class EpcView(PageView):
def get_context(self, request, session_id, page_name, data):

session_data = interface.api.session.get_session(session_id)
uprn = session_data.get("uprn")
address = session_data.get("address")
country = session_data.get("country")
if uprn:
epc = interface.api.epc.get_epc(uprn, country)

if country == "Scotland":
uprn = session_data.get("uprn")
address = session_data.get("address")
country = session_data.get("country")
if uprn:
epc = interface.api.epc.get_epc_scotland(uprn, country)
else:
epc = {}
context = {
"epc_rating": epc.get("rating"),
"epc_date": epc.get("date"),
"epc_display_options": schemas.epc_display_options_map,
"address": address,
}
return context
else:
epc = {}
context = {
"epc_rating": epc.get("rating"),
"epc_date": epc.get("date"),
"epc_display_options": schemas.epc_display_options_map,
"address": address,
}
return context
rrn = session_data.get("rrn")
address = session_data.get("address")
context = {}
if rrn:
epc = session_data.get("epc_details")
else:
epc = {}

context = {
"epc_rating": epc.get("currentEnergyEfficiencyBand"),
"epc_date": epc.get("lodgementDate"),
"epc_display_options": schemas.epc_display_options_map,
"address": address,
}
return context


def handle_get(self, response, request, session_id, page_name, context):
session_data = interface.api.session.get_session(session_id)
uprn = session_data.get("uprn")
country = session_data.get("country")
uprn = session_data.get("uprn")
if uprn:
epc = interface.api.epc.get_epc(uprn, country)

if country == "Scotland":
uprn = session_data.get("uprn")
country = session_data.get("country")
uprn = session_data.get("uprn")
if uprn:
epc = interface.api.epc.get_epc_scotland(uprn, country)
else:
epc = {}
if not epc:
return redirect("frontdoor:page", session_id=session_id, page_name="benefits")
return super().handle_get(response, request, session_id, page_name, context)
else:
epc = {}
if not epc:
return redirect("frontdoor:page", session_id=session_id, page_name="benefits")
return super().handle_get(response, request, session_id, page_name, context)
rrn = session_data.get("rrn")
if rrn:
epc = session_data.get("epc_details")
else:
epc = {}
if not epc:
return redirect("frontdoor:page", session_id=session_id, page_name="benefits")
return super().handle_get(response, request, session_id, page_name, context)

def handle_post(self, request, session_id, page_name, data, is_change_page):
prev_page_name, next_page_name = get_prev_next_page_name(page_name)
Expand Down
4 changes: 2 additions & 2 deletions help_to_heat/templates/frontdoor/address-select.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ <h1 class="govuk-fieldset__heading">
</h1>
</legend>

{% if rrn_options %}
{{macros.radios('rrn', rrn_options, data, errors)}}
{% if uprn_options %}
{{macros.radios('uprn', uprn_options, data, errors)}}
{% else %}
<p class="govuk-body">{{_("No addresses found")}}</p>
{% endif %}
Expand Down
Loading

0 comments on commit 20cb962

Please sign in to comment.