Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show traficom restrictions #430

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion parking_permits/customer_permit.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ def get(self):
vehicle = permit.vehicle
# Update vehicle detail from traficom if it wasn't updated today
if permit.vehicle.updated_from_traficom_on < tz.localdate(tz.now()):
self.customer.fetch_vehicle_detail(vehicle.registration_number)
vehicle = self.customer.fetch_vehicle_detail(
vehicle.registration_number
)

user_of_vehicle = self.customer.is_user_of_vehicle(vehicle)
if not user_of_vehicle:
Expand Down Expand Up @@ -242,6 +244,7 @@ def create(self, address_id, registration):
ParkingPermitEventFactory.make_create_permit_event(
permit, created_by=self.customer.user
)

return permit

def delete(self, permit_id):
Expand Down
23 changes: 23 additions & 0 deletions parking_permits/migrations/0044_add_vehicle_restrictions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.1 on 2023-11-30 11:26

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("parking_permits", "0043_alter_subscription_cancel_reason"),
]

operations = [
migrations.AddField(
model_name="vehicle",
name="restrictions",
field=django.contrib.postgres.fields.ArrayField(
base_field=models.CharField(blank=True, max_length=2),
default=list,
size=None,
verbose_name="Traficom Restrictions",
),
),
]
7 changes: 7 additions & 0 deletions parking_permits/models/vehicle.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.gis.db import models
from django.contrib.postgres.fields import ArrayField
from django.utils import timezone as tz
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -160,6 +161,12 @@ class Vehicle(TimestampedModelMixin):
VehicleUser, verbose_name=_("Vehicle users"), related_name="vehicles"
)

restrictions = ArrayField(
verbose_name=_("Traficom Restrictions"),
base_field=models.CharField(max_length=2, blank=True),
default=list,
)

class Meta:
verbose_name = _("Vehicle")
verbose_name_plural = _("Vehicles")
Expand Down
1 change: 1 addition & 0 deletions parking_permits/schema/parking_permit.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type VehicleNode {
registrationNumber: String
emission: Int
isLowEmission: Boolean
restrictions: [String]
}

type ProductNode {
Expand Down
1 change: 1 addition & 0 deletions parking_permits/schema/parking_permit_admin.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type VehicleNode {
emission: Int
emissionType: String
powerType: VehiclePowerTypeNode
restrictions: [String]
}

type AnnouncementNode {
Expand Down
42 changes: 34 additions & 8 deletions parking_permits/services/traficom.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,26 @@

logger = logging.getLogger("db")


VEHICLE_RESTRICTIONS = (
"03",
"07",
"10",
"11",
"18",
"20",
"22",
"23",
"24",
"25",
"34",
)

# these codes will raise an error and prevent adding a permit
BLOCKING_VEHICLE_RESTRICTIONS = ("18", "19")

CONSUMPTION_TYPE_NEDC = "4"
CONSUMPTION_TYPE_WLTP = "10"
DECOMMISSIONED_VEHICLE_RESTRICTION_TYPE = "18"
VEHICLE_TYPE = 1
LIGHT_WEIGHT_VEHICLE_TYPE = 2
VEHICLE_SEARCH = 841
Expand Down Expand Up @@ -89,17 +106,25 @@ def fetch_vehicle_details(self, registration_number):
}
)

restrictions = et.findall(".//rajoitustiedot/rajoitustieto")
for r in restrictions:
restriction_type = r.find("rajoitusLaji").text
if restriction_type == DECOMMISSIONED_VEHICLE_RESTRICTION_TYPE:
restrictions = []

for restriction in et.findall(".//rajoitustiedot/rajoitustieto"):
try:
restriction_type = restriction.find("rajoitusLaji").text
except AttributeError:
continue

if restriction_type in BLOCKING_VEHICLE_RESTRICTIONS:
raise TraficomFetchVehicleError(
_("Vehicle %(registration_number)s is decommissioned")
% {
"registration_number": registration_number,
}
)

if restriction_type in VEHICLE_RESTRICTIONS:
restrictions.append(restriction_type)

vehicle_identity = et.find(".//tunnus")
motor = et.find(".//moottori")
owners_et = et.findall(".//omistajatHaltijat/omistajaHaltija")
Expand Down Expand Up @@ -152,16 +177,17 @@ def fetch_vehicle_details(self, registration_number):
"last_inspection_date": last_inspection_date.text
if last_inspection_date is not None
else None,
"restrictions": restrictions or [],
}
vehicle_users = []
for user_nin in user_ssns:
user = VehicleUser.objects.get_or_create(national_id_number=user_nin)
vehicle_users.append(user[0])
vehicle = Vehicle.objects.update_or_create(
registration_number=registration_number, defaults=vehicle_details
)
vehicle[0].users.set(vehicle_users)
return vehicle[0]
)[0]
vehicle.users.set(vehicle_users)
return vehicle

def fetch_driving_licence_details(self, hetu):
et = self._fetch_info(hetu=hetu)
Expand Down