diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-04-20 16:28:21 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-04-20 16:28:21 +0200 |
commit | 0ff92ef98dbfbfc0e2d89dff18f7bb2dc3950b8c (patch) | |
tree | dea08fc84ca5ef9d4bcbaae8e2629e73573eab6d /ishtar_common/models_common.py | |
parent | a589b3ef96c9adf4e408713201ffe7d269e4f78f (diff) | |
download | Ishtar-0ff92ef98dbfbfc0e2d89dff18f7bb2dc3950b8c.tar.bz2 Ishtar-0ff92ef98dbfbfc0e2d89dff18f7bb2dc3950b8c.zip |
precise_town refactoring for django app consistency
Diffstat (limited to 'ishtar_common/models_common.py')
-rw-r--r-- | ishtar_common/models_common.py | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index 8942d57c2..bc4a0549b 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -59,6 +59,7 @@ from simple_history.signals import ( ) from unidecode import unidecode +from ishtar_common.data_importer import post_importer_action, ImporterError from ishtar_common.model_managers import TypeManager from ishtar_common.model_merging import merge_model_objects from ishtar_common.models_imports import Import @@ -3334,7 +3335,7 @@ class Address(BaseHistorizedItem): "address_complement", "postal_code", "town", - "precise_town", + "precise_town_id", "country", "alt_address", "alt_address_complement", @@ -3360,12 +3361,10 @@ class Address(BaseHistorizedItem): _("Postal code"), max_length=10, null=True, blank=True ) town = models.CharField(_("Town (freeform)"), max_length=150, null=True, blank=True) - precise_town = models.ForeignKey( - Town, + precise_town_id = models.PositiveIntegerField( verbose_name=_("Town (precise)"), null=True, blank=True, - on_delete=models.SET_NULL, ) country = models.CharField(_("Country"), max_length=30, null=True, blank=True) alt_address = models.TextField(_("Other address: address"), blank=True, default="") @@ -3409,6 +3408,39 @@ class Address(BaseHistorizedItem): class Meta: abstract = True + @property + def precise_town(self): + if hasattr(self, "_precise_town"): + return self._precise_town + if not self.precise_town_id: + self._precise_town = None + else: + try: + self._precise_town = Town.objects.get(id=self.precise_town_id) + except Town.DoesNotExist: + self._precise_town = None + return self._precise_town + + @property + def precise_town_name(self): + if not self.precise_town: + return "" + return self.precise_town.name + + @post_importer_action + def set_town_by_code(self, context, value): + try: + town = Town.objects.get(numero_insee=value) + except Town.DoesNotExist: + raise ImporterError( + str(_("Town with code: {} does not exists")).format(value) + ) + self.precise_town_id = town.pk + self.skip_history_when_saving = True + self.save() + return self + set_town_by_code.post_save = True + def get_short_html_items(self): items = [] if self.address: |