Skip to content

Commit

Permalink
PC-351: Further work on EPC API integration
Browse files Browse the repository at this point in the history
  • Loading branch information
chidin194 committed Dec 8, 2023
1 parent 42a7821 commit e25f5f5
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 163 deletions.
12 changes: 5 additions & 7 deletions help_to_heat/frontdoor/epc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ def get_access_token():
client_secret = settings.EPC_API_CLIENT_SECRET
token_url = "https://api.epb-staging.digital.communities.gov.uk/auth/oauth/token"

# Define payload for token request (for Client Credentials Grant Type)
payload = {"grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret}

try:
response = requests.post(token_url, data=payload)
response.raise_for_status() # Raise an error for 4XX and 5XX status codes
response.raise_for_status()
access_token = response.json().get("access_token")
return access_token
except requests.exceptions.RequestException as e:
logger.error("Error fetching access token:", e)
logger.exception(f"Error fetching access token: {e}")
return None

def get_address_and_rrn(token, building, postcode):
Expand All @@ -38,12 +37,11 @@ def get_address_and_rrn(token, building, postcode):

try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an error for 4XX and 5XX status codes
response.raise_for_status()
data = response.json()
# Process the response data
return data
except requests.exceptions.RequestException as e:
logger.error("Error making API request:", e)
logger.exception(f"Error making API request: {e}")
return None

def get_epc_details(token, rrn):
Expand All @@ -57,5 +55,5 @@ def get_epc_details(token, rrn):
# Process the response data
return data
except requests.exceptions.RequestException as e:
logger.error("Error making API request:", e)
logger.exception(f"Error making API request: {e}")
return None
17 changes: 8 additions & 9 deletions help_to_heat/frontdoor/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class FindAddressesSchema(marshmallow.Schema):
class GetAddressSchema(marshmallow.Schema):
uprn = marshmallow.fields.String()

class GetEPCSchema(marshmallow.Schema):
rrn = marshmallow.fields.String()

class AddressSchema(marshmallow.Schema):
uprn = marshmallow.fields.String()
Expand All @@ -68,7 +70,7 @@ class FullAddressSchema(marshmallow.Schema):
address = marshmallow.fields.String()


class GetEPCSchema(marshmallow.Schema):
class GetScottishEPCSchema(marshmallow.Schema):
uprn = marshmallow.fields.Integer()
country = marshmallow.fields.String()

Expand Down Expand Up @@ -452,15 +454,10 @@ def parse_dpa_to_address(self, dpa):


class EPC(Entity):
@with_schema(load=GetEPCSchema, dump=EPCSchema)
def get_epc_scotland(self, uprn, country):
@with_schema(load=GetScottishEPCSchema, dump=EPCSchema)
def get_epc_scotland(self, uprn):
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
epc = portal.models.ScottishEpcRating.objects.get(uprn=uprn)
except (portal.models.EpcRating.DoesNotExist, portal.models.ScottishEpcRating.DoesNotExist):
epc = None
if epc:
Expand All @@ -469,12 +466,14 @@ def get_epc_scotland(self, uprn, country):
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)
address_and_epc_details = data["data"]["assessments"]
return address_and_epc_details


def get_epc_details(epc_rrn):
token = EPCApi.get_access_token()
data = EPCApi.get_epc_details(token, epc_rrn)
Expand Down
1 change: 1 addition & 0 deletions help_to_heat/frontdoor/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ class SessionSchema(Schema):
uprn = fields.String()
rrn = fields.String()
epc_details = fields.Dict()
property_main_heat_source = fields.String()
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
40 changes: 32 additions & 8 deletions help_to_heat/frontdoor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"address_line_1": _("Enter Address line 1"),
"postcode": _("Enter a postcode"),
"uprn": _("Select your address"),
"rrn": _("Select your EPC"),
"rrn": _("Select your address"),
"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 @@ -363,10 +363,17 @@ def handle_post(self, request, session_id, page_name, data, is_change_page):
data = interface.api.session.get_answer(session_id, "address")
building_name_or_number = data["building_name_or_number"]
postcode = data["postcode"]
country = interface.api.session.get_answer(session_id, "country")["country"]
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: # noqa: B902
if country == "Scotland":
return redirect("frontdoor:page", session_id=session_id, page_name="address-select")
else:
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 as e: # noqa: B902
logger.exception(f"An error occurred: {e}")
interface.api.session.save_answer(session_id, "epc-select",
{"rrn": "", "epc_details": {}, "uprn": "", "property_main_heat_source": "", "epc_rating": "Not found", "accept_suggested_epc": "Not found", "epc_date": ""})
return redirect("frontdoor:page", session_id=session_id, page_name="address-select")


Expand Down Expand Up @@ -396,9 +403,21 @@ def get_context(self, request, session_id, *args, **kwargs):
def save_data(self, request, session_id, page_name, *args, **kwargs):
rrn = request.POST["rrn"]
epc = interface.EPC.get_epc_details(rrn)
print(epc)
address = self.format_address(epc["data"]["assessment"])
epc_details = epc["data"]["assessment"]
epc_data = {"rrn": rrn, "address": address, "epc_details": epc_details}

if epc_details.get("uprn") is not None:
uprn = epc_details.get("uprn")
else:
uprn = ""

if epc_details.get("mainHeatingDescription") is not None:
property_main_heat_source = epc_details.get("mainHeatingDescription")
else:
property_main_heat_source = ""

epc_data = {"rrn": rrn, "address": address, "epc_details": epc_details, "uprn": uprn, "property_main_heat_source": property_main_heat_source}
data = interface.api.session.save_answer(session_id, page_name, epc_data)
return data

Expand Down Expand Up @@ -444,7 +463,12 @@ def save_data(self, request, session_id, page_name, *args, **kwargs):
data = {**data, "address": address}
data = interface.api.session.save_answer(session_id, page_name, data)
return data


def handle_post(self, request, session_id, page_name, data, is_change_page):
interface.api.session.save_answer(session_id, "epc-select",
{"rrn": "", "epc_details": {}, "uprn": "", "property_main_heat_source": "", "epc_rating": "Not found", "accept_suggested_epc": "Not found", "epc_date": ""})
return super().handle_post(request, session_id, page_name, data, is_change_page)


@register_page("council-tax-band")
class CouncilTaxBandView(PageView):
Expand Down Expand Up @@ -488,7 +512,7 @@ def get_context(self, request, session_id, page_name, data):
epc = {}

context = {
"epc_rating": epc.get("currentEnergyEfficiencyBand"),
"epc_rating": epc.get("currentEnergyEfficiencyBand").upper() if epc.get("currentEnergyEfficiencyBand") else "",
"epc_date": epc.get("lodgementDate"),
"epc_display_options": schemas.epc_display_options_map,
"address": address,
Expand Down Expand Up @@ -522,7 +546,7 @@ def handle_get(self, 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)
epc_rating = data.get("epc_rating")
epc_rating = data.get("epc_rating").upper()
accept_suggested_epc = data.get("accept_suggested_epc")

if not epc_rating:
Expand Down
Loading

0 comments on commit e25f5f5

Please sign in to comment.