summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/admin.py4
-rw-r--r--ishtar_common/forms.py2
-rw-r--r--ishtar_common/forms_common.py21
-rw-r--r--ishtar_common/migrations/0228_auto_20230418_1622.py20
-rw-r--r--ishtar_common/models.py7
-rw-r--r--ishtar_common/models_common.py40
-rw-r--r--ishtar_common/templates/ishtar/wizard/wizard_person.html2
-rw-r--r--ishtar_common/wizards.py10
8 files changed, 82 insertions, 24 deletions
diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py
index e4c4e90ed..068a697b0 100644
--- a/ishtar_common/admin.py
+++ b/ishtar_common/admin.py
@@ -649,7 +649,7 @@ class OrganizationAdmin(HistorizedObjectAdmin):
"merge_candidate",
)
model = models.Organization
- autocomplete_fields = ["precise_town"]
+ #autocomplete_fields = ["precise_town"]
admin_site.register(models.Organization, OrganizationAdmin)
@@ -671,7 +671,7 @@ class PersonAdmin(HistorizedObjectAdmin):
"merge_exclusion",
"merge_candidate",
)
- autocomplete_fields = ["attached_to", "lock_user", "precise_town"]
+ autocomplete_fields = ["attached_to", "lock_user"] # , "precise_town"]
readonly_fields = HistorizedObjectAdmin.readonly_fields + ["cached_label"]
model = models.Person
inlines = [ProfileInline]
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py
index e29e4ca0f..ad2548aa1 100644
--- a/ishtar_common/forms.py
+++ b/ishtar_common/forms.py
@@ -141,7 +141,7 @@ JSON_VALUE_TYPES_FIELDS = {
}
-class BSForm(object):
+class BSForm:
def _post_init(self):
for k in self.fields:
widget = self.fields[k].widget
diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py
index 76e5bbf1e..cc4cf31a8 100644
--- a/ishtar_common/forms_common.py
+++ b/ishtar_common/forms_common.py
@@ -548,8 +548,11 @@ class OrganizationForm(ManageOldType, NewItemForm):
form_label = _("Organization")
associated_models = {
"organization_type": models.OrganizationType,
- "precise_town": models.Town,
}
+ format_models = {
+ "precise_town_id": models.Town,
+ }
+
name = forms.CharField(label=_("Name"), max_length=300, validators=[name_validator])
organization_type = forms.ChoiceField(label=_("Organization type"), choices=[])
url = forms.URLField(label=_("Web address"), required=False)
@@ -565,7 +568,7 @@ class OrganizationForm(ManageOldType, NewItemForm):
)
postal_code = forms.CharField(label=_("Postal code"), max_length=10, required=False)
town = forms.CharField(label=_("Town (freeform)"), max_length=30, required=False)
- precise_town = get_town_field(required=False)
+ precise_town_id = get_town_field(required=False)
country = forms.CharField(label=_("Country"), max_length=30, required=False)
email = forms.EmailField(label=_("Email"), required=False)
phone = forms.CharField(label=_("Phone"), max_length=32, required=False)
@@ -587,11 +590,11 @@ class OrganizationForm(ManageOldType, NewItemForm):
dct["organization_type"] = models.OrganizationType.objects.get(
pk=dct["organization_type"]
)
- if dct["precise_town"]:
+ if dct["precise_town_id"]:
try:
- dct["precise_town"] = models.Town.objects.get(pk=dct["precise_town"])
+ dct["precise_town_id"] = models.Town.objects.get(pk=dct["precise_town_id"]).pk
except models.Town.DoesNotExist:
- dct.pop("precise_town")
+ dct.pop("precise_town_id")
if not item:
new_item = models.Organization(**dct)
else:
@@ -611,7 +614,7 @@ class OrganizationSelect(CustomForm, TableSelect):
)
name = forms.CharField(label=_("Name"), max_length=300)
organization_type = forms.ChoiceField(label=_("Type"), choices=[])
- precise_town = get_town_field()
+ precise_town_id = get_town_field()
def __init__(self, *args, **kwargs):
super(OrganizationSelect, self).__init__(*args, **kwargs)
@@ -909,7 +912,9 @@ class SimplePersonForm(ManageOldType, NewItemForm):
associated_models = {
"attached_to": models.Organization,
"title": models.TitleType,
- "precise_town": models.Town,
+ }
+ format_models = {
+ "precise_town_id": models.Town,
}
title = forms.ChoiceField(label=_("Title"), choices=[], required=False)
salutation = forms.CharField(label=_("Salutation"), max_length=200, required=False)
@@ -950,7 +955,7 @@ class SimplePersonForm(ManageOldType, NewItemForm):
)
postal_code = forms.CharField(label=_("Postal code"), max_length=10, required=False)
town = forms.CharField(label=_("Town (freeform)"), max_length=30, required=False)
- precise_town = get_town_field(required=False)
+ precise_town_id = get_town_field(required=False)
country = forms.CharField(label=_("Country"), max_length=30, required=False)
alt_address = forms.CharField(
label=_("Other address: address"), widget=forms.Textarea, required=False
diff --git a/ishtar_common/migrations/0228_auto_20230418_1622.py b/ishtar_common/migrations/0228_auto_20230418_1622.py
index 65154f308..a3ff7e472 100644
--- a/ishtar_common/migrations/0228_auto_20230418_1622.py
+++ b/ishtar_common/migrations/0228_auto_20230418_1622.py
@@ -50,4 +50,24 @@ class Migration(migrations.Migration):
name='container_ref_id',
field=models.PositiveIntegerField(blank=True, null=True, verbose_name='Container reference ID'),
),
+ migrations.AlterField(
+ model_name='historicalorganization',
+ name='precise_town_id',
+ field=models.PositiveIntegerField(blank=True, null=True, verbose_name='Town (precise)'),
+ ),
+ migrations.AlterField(
+ model_name='historicalperson',
+ name='precise_town_id',
+ field=models.PositiveIntegerField(blank=True, null=True, verbose_name='Town (precise)'),
+ ),
+ migrations.AlterField(
+ model_name='organization',
+ name='precise_town_id',
+ field=models.PositiveIntegerField(blank=True, null=True, verbose_name='Town (precise)'),
+ ),
+ migrations.AlterField(
+ model_name='person',
+ name='precise_town_id',
+ field=models.PositiveIntegerField(blank=True, null=True, verbose_name='Town (precise)'),
+ ),
]
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index dd968f891..1045adbe5 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -2536,7 +2536,7 @@ organization_type_pks_lazy = lazy(OrganizationType.get_or_create_pks, str)
class Organization(Address, Merge, OwnPerms, BaseGenderedType, ValueGetter, MainItem):
- TABLE_COLS = ("name", "organization_type", "address", "town", "precise_town")
+ TABLE_COLS = ("name", "organization_type", "address", "town") #, "precise_town")
SLUG = "organization"
SHOW_URL = "show-organization"
DELETE_URL = "delete-organization"
@@ -2548,7 +2548,9 @@ class Organization(Address, Merge, OwnPerms, BaseGenderedType, ValueGetter, Main
BASE_SEARCH_VECTORS = [
SearchVectorConfig("name"),
SearchVectorConfig("town"),
- SearchVectorConfig("precise_town__name"),
+ ]
+ PROPERTY_SEARCH_VECTORS = [
+ SearchVectorConfig("precise_town_name"),
]
# alternative names of fields for searches
@@ -3170,7 +3172,6 @@ class Person(Address, Merge, OwnPerms, ValueGetter, MainItem):
]
return actions
-
@classmethod
def get_query_owns(cls, ishtaruser):
return (
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:
diff --git a/ishtar_common/templates/ishtar/wizard/wizard_person.html b/ishtar_common/templates/ishtar/wizard/wizard_person.html
index 5be43cec7..b760c027f 100644
--- a/ishtar_common/templates/ishtar/wizard/wizard_person.html
+++ b/ishtar_common/templates/ishtar/wizard/wizard_person.html
@@ -69,7 +69,7 @@
{% bs_field wizard.form.address_complement %}
{% bs_field wizard.form.postal_code %}
{% bs_field wizard.form.town %}
- {% bs_field wizard.form.precise_town %}
+ {% bs_field wizard.form.precise_town_id %}
{% bs_field wizard.form.country %}
</div>
</div>
diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py
index 8dcb16b70..13ea27720 100644
--- a/ishtar_common/wizards.py
+++ b/ishtar_common/wizards.py
@@ -440,11 +440,11 @@ class Wizard(IshtarWizard):
for form in forms:
is_formset = hasattr(form, "forms")
base_form = is_formset and form.forms[0] or form
- associated_models = (
- hasattr(base_form, "associated_models")
- and base_form.associated_models
- or {}
- )
+ associated_models = {}
+ if hasattr(base_form, "format_models"):
+ associated_models = base_form.format_models
+ if hasattr(base_form, "associated_models"):
+ associated_models.update(base_form.associated_models)
if not hasattr(form, "cleaned_data") and hasattr(form, "forms"):
cleaned_datas = [
frm.cleaned_data for frm in form.forms if frm.is_valid()