From 79c97b7e5a3371e4746c5f03498ddaabca770a6f Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Wed, 28 May 2025 22:07:06 +0300 Subject: [PATCH] Features: 1) Add `address_line` field to `Address` model for enhanced customer address representation; 2) Extend serializers with `address_line_1` and `address_line_2` fields; 3) Update address parsing logic to include house number in street and support `address_line` storage; Fixes: None; Extra: Refactor address manager to construct `address_line` from parsed data; --- core/managers.py | 3 ++- core/migrations/0023_address_address_line.py | 18 ++++++++++++++++++ core/models.py | 6 ++++++ core/serializers/__init__.py | 10 ++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 core/migrations/0023_address_address_line.py diff --git a/core/managers.py b/core/managers.py index 96bd2af3..8b502c53 100644 --- a/core/managers.py +++ b/core/managers.py @@ -31,7 +31,7 @@ class AddressManager(models.Manager): # Parse address components addr = data.get("address", {}) - street = addr.get("road") or addr.get("pedestrian") or "" + street = f"{addr.get('road', '') or addr.get('pedestrian', '')}, {addr.get('house_number', '')}" district = addr.get("city_district") or addr.get("suburb") or "" city = addr.get("city") or addr.get("town") or addr.get("village") or "" region = addr.get("state") or addr.get("region") or "" @@ -49,6 +49,7 @@ class AddressManager(models.Manager): # Create the model instance, storing both the input string and full API response return super().create( raw_data=raw_data, + address_line=f"{kwargs.get('address_line_1')}, {kwargs.get('address_line_2')}", street=street, district=district, city=city, diff --git a/core/migrations/0023_address_address_line.py b/core/migrations/0023_address_address_line.py new file mode 100644 index 00000000..55654c18 --- /dev/null +++ b/core/migrations/0023_address_address_line.py @@ -0,0 +1,18 @@ +# Generated by Django 5.2 on 2025-05-28 19:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ('core', '0022_category_slug'), + ] + + operations = [ + migrations.AddField( + model_name='address', + name='address_line', + field=models.TextField(blank=True, help_text='address line for the customer', null=True, + verbose_name='address line'), + ), + ] diff --git a/core/models.py b/core/models.py index 3b1b64e9..e36436d8 100644 --- a/core/models.py +++ b/core/models.py @@ -1259,6 +1259,12 @@ class Documentary(NiceModel): class Address(NiceModel): is_publicly_visible = False + address_line = TextField( # noqa: DJ001 + blank=True, + null=True, + help_text=_("address line for the customer"), + verbose_name=_("address line"), + ) street = CharField(_("street"), max_length=255, null=True) # noqa: DJ001 district = CharField(_("district"), max_length=255, null=True) # noqa: DJ001 city = CharField(_("city"), max_length=100, null=True) # noqa: DJ001 diff --git a/core/serializers/__init__.py b/core/serializers/__init__.py index 95e372bf..7e38070b 100644 --- a/core/serializers/__init__.py +++ b/core/serializers/__init__.py @@ -158,6 +158,16 @@ class AddressCreateSerializer(ModelSerializer): # noqa: F405 write_only=True, max_length=512, ) + address_line_1 = CharField( + write_only=True, + max_length=128, + required=False + ) + address_line_2 = CharField( + write_only=True, + max_length=128, + required=False + ) class Meta: model = Address