From c54ee851dd634d71e6e9dbb0690f68156d75fa2c Mon Sep 17 00:00:00 2001 From: Mika Hietanen Date: Thu, 14 Dec 2023 10:32:45 +0200 Subject: [PATCH 1/2] Improve optional Traficom check logic --- parking_permits/admin_resolvers.py | 3 ++- parking_permits/customer_permit.py | 26 ++++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/parking_permits/admin_resolvers.py b/parking_permits/admin_resolvers.py index 65f7d26f..8e037e7c 100644 --- a/parking_permits/admin_resolvers.py +++ b/parking_permits/admin_resolvers.py @@ -305,7 +305,8 @@ def resolve_customers(obj, info, page_input, order_by=None, search_params=None): ) def resolve_vehicle(obj, info, reg_number, national_id_number): customer = Customer.objects.get_or_create(national_id_number=national_id_number)[0] - customer.fetch_driving_licence_detail() + if settings.TRAFICOM_CHECK: + customer.fetch_driving_licence_detail() vehicle = customer.fetch_vehicle_detail(reg_number) if not settings.TRAFICOM_CHECK: return vehicle diff --git a/parking_permits/customer_permit.py b/parking_permits/customer_permit.py index 9c65ecc0..67177c04 100644 --- a/parking_permits/customer_permit.py +++ b/parking_permits/customer_permit.py @@ -206,22 +206,24 @@ def create(self, address_id, registration): primary_vehicle = not primary_permit.primary_vehicle end_time = primary_permit.end_time - self.customer.fetch_driving_licence_detail() + if settings.TRAFICOM_CHECK: + self.customer.fetch_driving_licence_detail() vehicle = self.customer.fetch_vehicle_detail(registration) - is_user_of_vehicle = self.customer.is_user_of_vehicle(vehicle) - if not is_user_of_vehicle: - raise TraficomFetchVehicleError( - _("Owner/holder data of a vehicle could not be verified") - ) + if settings.TRAFICOM_CHECK: + is_user_of_vehicle = self.customer.is_user_of_vehicle(vehicle) + if not is_user_of_vehicle: + raise TraficomFetchVehicleError( + _("Owner/holder data of a vehicle could not be verified") + ) - has_valid_licence = self.customer.has_valid_driving_licence_for_vehicle( - vehicle - ) - if not has_valid_licence: - raise TraficomFetchVehicleError( - _("Customer does not have a valid driving licence") + has_valid_licence = self.customer.has_valid_driving_licence_for_vehicle( + vehicle ) + if not has_valid_licence: + raise TraficomFetchVehicleError( + _("Customer does not have a valid driving licence") + ) start_time = tz.now() if not end_time: From b5cf76b68f077ccba3821150100c8d915ecd7bd0 Mon Sep 17 00:00:00 2001 From: Mika Hietanen Date: Thu, 14 Dec 2023 10:33:40 +0200 Subject: [PATCH 2/2] Make Helsinki address checks optional --- parking_permits/resolvers.py | 6 ++---- parking_permits/services/dvv.py | 8 +++----- parking_permits/utils.py | 7 +++++++ project/settings.py | 2 ++ project/settings_test.py | 1 + 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/parking_permits/resolvers.py b/parking_permits/resolvers.py index 91f000f7..a4b40229 100644 --- a/parking_permits/resolvers.py +++ b/parking_permits/resolvers.py @@ -46,7 +46,7 @@ ) from .services.traficom import Traficom from .talpa.order import TalpaOrderManager -from .utils import ModelDiffer, get_user_from_resolver_args +from .utils import ModelDiffer, get_user_from_resolver_args, is_valid_city logger = logging.getLogger("db") audit_logger = audit.getAuditLoggerAdapter( @@ -81,9 +81,7 @@ def is_valid_address(address): - if not address: - return False - return address.get("city").upper() == "HELSINKI" + return address and is_valid_city(address) @query.field("getPermits") diff --git a/parking_permits/services/dvv.py b/parking_permits/services/dvv.py index 1fe4adde..4982d23b 100644 --- a/parking_permits/services/dvv.py +++ b/parking_permits/services/dvv.py @@ -10,6 +10,7 @@ from parking_permits.exceptions import ObjectNotFound from parking_permits.models import Customer, ParkingZone from parking_permits.services.kami import get_address_details, parse_street_data +from parking_permits.utils import is_valid_city logger = logging.getLogger("db") @@ -120,11 +121,8 @@ def format_address(address_data) -> DvvAddressInfo: def is_valid_address(address): - return ( - address["LahiosoiteS"] != "" - and address["PostitoimipaikkaS"] - and address["PostitoimipaikkaS"].upper() == "HELSINKI" - ) + address_valid = address and address["LahiosoiteS"] and address["PostitoimipaikkaS"] + return address_valid and is_valid_city(address["PostitoimipaikkaS"]) def get_person_info(national_id_number) -> Optional[DvvPersonInfo]: diff --git a/parking_permits/utils.py b/parking_permits/utils.py index 2e77dd66..8e4fbc9f 100644 --- a/parking_permits/utils.py +++ b/parking_permits/utils.py @@ -9,6 +9,7 @@ from ariadne import convert_camel_case_to_snake from dateutil.relativedelta import relativedelta +from django.conf import settings from django.db import models from django.utils import timezone as tz from graphql import GraphQLResolveInfo @@ -342,3 +343,9 @@ def round_up(v): if v else "0.00" ) + + +def is_valid_city(city): + if settings.HELSINKI_ADDRESS_CHECK: + return city and city.casefold() == "helsinki" + return True diff --git a/project/settings.py b/project/settings.py index 22b1bfb1..eaba62f4 100644 --- a/project/settings.py +++ b/project/settings.py @@ -54,6 +54,7 @@ TRAFICOM_PALVELU_TUNNUS=(str, ""), TRAFICOM_VERIFY_SSL=(bool, True), TRAFICOM_CHECK=(bool, True), + HELSINKI_ADDRESS_CHECK=(bool, True), DVV_PERSONAL_INFO_URL=(str, ""), DVV_USERNAME=(str, ""), DVV_PASSWORD=(str, ""), @@ -213,6 +214,7 @@ TRAFICOM_PALVELU_TUNNUS = env("TRAFICOM_PALVELU_TUNNUS") TRAFICOM_VERIFY_SSL = env("TRAFICOM_VERIFY_SSL") TRAFICOM_CHECK = env("TRAFICOM_CHECK") +HELSINKI_ADDRESS_CHECK = env("HELSINKI_ADDRESS_CHECK") # CORS CORS_ALLOWED_ORIGINS = env("CORS_ALLOWED_ORIGINS") diff --git a/project/settings_test.py b/project/settings_test.py index 142982ca..cda00c23 100644 --- a/project/settings_test.py +++ b/project/settings_test.py @@ -10,4 +10,5 @@ # Debug options. Override these in tests if needed. DEBUG_SKIP_PARKKIHUBI_SYNC = True +HELSINKI_ADDRESS_CHECK = True TALPA_WEBHOOK_WAIT_BUFFER_SECONDS = 0