Skip to content

Commit

Permalink
When TRAFICOM_MOCK environment variable is set, fetches Traficom details
Browse files Browse the repository at this point in the history
from local database
  • Loading branch information
danjacob-anders committed Jan 12, 2024
1 parent 383f90a commit e8a4bd2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions parking_permits/services/traficom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from parking_permits.exceptions import TraficomFetchVehicleError
from parking_permits.models.driving_class import DrivingClass
from parking_permits.models.driving_licence import DrivingLicence
from parking_permits.models.vehicle import (
EmissionType,
Vehicle,
Expand Down Expand Up @@ -77,6 +78,9 @@ class Traficom:
headers = {"Content-type": "application/xml"}

def fetch_vehicle_details(self, registration_number):
if settings.TRAFICOM_MOCK:
return self._fetch_vehicle_from_db(registration_number)

et = self._fetch_info(registration_number=registration_number)
vehicle_detail = et.find(".//ajoneuvonTiedot")

Expand Down Expand Up @@ -192,6 +196,9 @@ def fetch_vehicle_details(self, registration_number):
return vehicle

def fetch_driving_licence_details(self, hetu):
if settings.TRAFICOM_MOCK:
return self._fetch_driving_licence_details_form_db(hetu)

error_code = None
et = self._fetch_info(hetu=hetu)
driving_licence_et = et.find(".//ajokorttiluokkatieto")
Expand Down Expand Up @@ -225,6 +232,28 @@ def fetch_driving_licence_details(self, hetu):
"issue_date": driving_licence_et.find("ajokortinMyontamisPvm").text,
}

def _fetch_vehicle_from_db(self, registration_number):
try:
return Vehicle.objects.get(registration_number=registration_number)
except Vehicle.DoesNotExist:
raise TraficomFetchVehicleError(
_(
"Could not find vehicle detail with given %(registration_number)s registration number"
)
% {"registration_number": registration_number}
)

def _fetch_driving_licence_details_form_db(self, hetu):
licence = DrivingLicence.objects.filter(
customer__national_id_number=hetu
).first()
if licence is None:
raise TraficomFetchVehicleError(_("The person has no driving licence"))
return {
"issue_date": licence.start_date,
"driving_classes": licence.driving_classes.all(),
}

def _fetch_info(self, registration_number=None, hetu=None):
is_l_type_vehicle = (
len(registration_number) == 6 if registration_number else False
Expand Down
2 changes: 2 additions & 0 deletions project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
PARKKIHUBI_TOKEN=(str, ""),
PARKKIHUBI_OPERATOR_ENDPOINT=(str, ""),
DEBUG_SKIP_PARKKIHUBI_SYNC=(bool, False),
TRAFICOM_MOCK=(bool, False),
TRAFICOM_ENDPOINT=(str, ""),
TRAFICOM_USERNAME=(str, ""),
TRAFICOM_PASSWORD=(str, ""),
Expand Down Expand Up @@ -207,6 +208,7 @@
PARKKIHUBI_OPERATOR_ENDPOINT = env("PARKKIHUBI_OPERATOR_ENDPOINT")

# TRAFICOM
TRAFICOM_MOCK = env("TRAFICOM_MOCK")
TRAFICOM_ENDPOINT = env("TRAFICOM_ENDPOINT")
TRAFICOM_USERNAME = env("TRAFICOM_USERNAME")
TRAFICOM_PASSWORD = env("TRAFICOM_PASSWORD")
Expand Down

0 comments on commit e8a4bd2

Please sign in to comment.