-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce VietnamVehiclePlateField for Django
- Loading branch information
Showing
5 changed files
with
75 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__version__ = '0.8.2' | ||
__version__ = '0.8.3' | ||
|
||
from .core import VietnamVehiclePlate, VehicleType # NOQA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .fields import VietnamVehiclePlateField # NOQA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Django model field to return VietnamVehiclePlate object.""" | ||
|
||
from typing import Union, Optional | ||
|
||
from django.db.models import Expression | ||
from django.db.backends.postgresql.base import DatabaseWrapper | ||
from django.core.exceptions import ValidationError | ||
from django.contrib.postgres.fields import CICharField | ||
from django.utils.translation import gettext_lazy as _ | ||
from biensoxe import VietnamVehiclePlate | ||
|
||
|
||
def parse_vehicleplate(number_string: str) -> VietnamVehiclePlate: | ||
"""Validate and parse input string to VietnamVehiclePlate object.""" | ||
try: | ||
return VietnamVehiclePlate.from_string(number_string) | ||
except ValueError: | ||
raise ValidationError(_('Input string does not look like Vietname plate number')) | ||
|
||
|
||
class VietnamVehiclePlateField(CICharField): | ||
"""Field to store Vietnamese vehicle plate. Stored in PostgreSQL as CIText data type, to enable case-insensitive search. | ||
Return data as VietnamVehiclePlate type from biensoxe library. | ||
""" | ||
|
||
description = _('Field to store Vietnamese vehicle plate') | ||
|
||
def from_db_value(self, value: Optional[str], | ||
expression: Expression, connection: DatabaseWrapper): | ||
# Called in all circumstances when the data is loaded from the database, | ||
# including in aggregates and values() calls. | ||
if value is None: | ||
return value | ||
return parse_vehicleplate(value) | ||
|
||
def to_python(self, value: Union[str, VietnamVehiclePlate, None]): | ||
# Called by deserialization and during the clean() method used from forms. | ||
if isinstance(value, VietnamVehiclePlate): | ||
return value | ||
if value is None: | ||
return value | ||
return parse_vehicleplate(value) | ||
|
||
def get_prep_value(self, value: VietnamVehiclePlate): | ||
# Convert Python object back to query value | ||
return value.compact |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "biensoxe" | ||
version = "0.8.2" | ||
version = "0.8.3" | ||
description = "Library to parse and validate Vietnamese vehicle plate" | ||
authors = ["Nguyễn Hồng Quân <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -19,6 +19,7 @@ classifiers = [ | |
'Topic :: Software Development :: Libraries :: Python Modules', | ||
'Development Status :: 4 - Beta', | ||
'Intended Audience :: Developers', | ||
'Framework :: Django :: 2.2', | ||
] | ||
|
||
|
||
|