Handle cases where monetary field has no currency
This commit is contained in:
parent
bcd08a7eda
commit
dba800b5ed
@ -1,4 +1,4 @@
|
||||
# Generated by Django 5.1.1 on 2024-09-29 00:39
|
||||
# Generated by Django 5.1.1 on 2024-09-29 05:16
|
||||
|
||||
import django.db.models.functions.text
|
||||
from django.db import migrations
|
||||
@ -16,7 +16,17 @@ class Migration(migrations.Migration):
|
||||
name="value_monetary_amount",
|
||||
field=models.GeneratedField(
|
||||
db_persist=True,
|
||||
expression=django.db.models.functions.text.Substr("value_monetary", 4),
|
||||
expression=models.Case(
|
||||
models.When(
|
||||
then=django.db.models.functions.text.Substr(
|
||||
"value_monetary",
|
||||
1,
|
||||
),
|
||||
value_monetary__regex="^\\d+",
|
||||
),
|
||||
default=django.db.models.functions.text.Substr("value_monetary", 4),
|
||||
output_field=models.DecimalField(decimal_places=2, max_digits=125),
|
||||
),
|
||||
output_field=models.DecimalField(decimal_places=2, max_digits=125),
|
||||
),
|
||||
),
|
||||
|
@ -22,6 +22,7 @@ from multiselectfield import MultiSelectField
|
||||
if settings.AUDIT_LOG_ENABLED:
|
||||
from auditlog.registry import auditlog
|
||||
|
||||
from django.db.models import Case
|
||||
from django.db.models.functions import Substr
|
||||
from django_softdelete.models import SoftDeleteModel
|
||||
|
||||
@ -924,7 +925,16 @@ class CustomFieldInstance(models.Model):
|
||||
value_monetary = models.CharField(null=True, max_length=128)
|
||||
|
||||
value_monetary_amount = models.GeneratedField(
|
||||
expression=Substr("value_monetary", 4),
|
||||
expression=Case(
|
||||
# If the value starts with a number and no currency symbol, use the whole string
|
||||
models.When(
|
||||
value_monetary__regex=r"^\d+",
|
||||
then=Substr("value_monetary", 1),
|
||||
),
|
||||
# If the value starts with a 3-char currency symbol, use the rest of the string
|
||||
default=Substr("value_monetary", 4),
|
||||
output_field=models.DecimalField(decimal_places=2, max_digits=125),
|
||||
),
|
||||
output_field=models.DecimalField(decimal_places=2, max_digits=125),
|
||||
db_persist=True,
|
||||
)
|
||||
|
@ -104,6 +104,7 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase):
|
||||
self._create_document(monetary_field="USD100.00")
|
||||
self._create_document(monetary_field="USD1.00")
|
||||
self._create_document(monetary_field="EUR50.00")
|
||||
self._create_document(monetary_field="101.00")
|
||||
|
||||
# CustomField.FieldDataType.DOCUMENTLINK
|
||||
self._create_document(documentlink_field=None)
|
||||
@ -398,7 +399,10 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase):
|
||||
["monetary_field", "gt", "99"],
|
||||
lambda document: "monetary_field" in document
|
||||
and document["monetary_field"] is not None
|
||||
and document["monetary_field"] == "USD100.00",
|
||||
and (
|
||||
document["monetary_field"] == "USD100.00" # With currency symbol
|
||||
or document["monetary_field"] == "101.00" # No currency symbol
|
||||
),
|
||||
)
|
||||
|
||||
# ==========================================================#
|
||||
|
Loading…
x
Reference in New Issue
Block a user