summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--archaeological_finds/forms_treatments.py18
-rw-r--r--archaeological_finds/wizards.py7
-rw-r--r--ishtar_common/admin.py3
-rw-r--r--ishtar_common/migrations/0259_ishtarsiteprofile_default_location_for_treatment.py20
-rw-r--r--ishtar_common/models.py14
5 files changed, 55 insertions, 7 deletions
diff --git a/archaeological_finds/forms_treatments.py b/archaeological_finds/forms_treatments.py
index be1921568..d5e353a53 100644
--- a/archaeological_finds/forms_treatments.py
+++ b/archaeological_finds/forms_treatments.py
@@ -214,9 +214,22 @@ class BaseTreatmentForm(CustomForm, ManageOldType):
def __init__(self, *args, **kwargs):
user = kwargs.pop('user')
- super(BaseTreatmentForm, self).__init__(*args, **kwargs)
+ current_profile = get_current_profile()
+ has_default_location = kwargs.get("data", None) or (
+ "initial" in kwargs and
+ kwargs["initial"].get("location", None))
+ super().__init__(*args, **kwargs)
+
+ if not has_default_location and \
+ current_profile.default_location_for_treatment_id:
+ self.fields["location"].initial = \
+ current_profile.default_location_for_treatment_id
+ if current_profile.default_location_for_treatment.organization:
+ self.fields["organization"].initial = \
+ current_profile.default_location_for_treatment.organization_id
+
# set current currency
- currency = get_current_profile().currency
+ currency = current_profile.currency
for key in ('estimated_cost', 'quoted_cost', 'realized_cost',
'insurance_cost'):
self.fields[key].label = self.fields[key].label.format(
@@ -261,7 +274,6 @@ class BaseTreatmentForm(CustomForm, ManageOldType):
except models.TreatmentType.DoesNotExist:
raise forms.ValidationError(_("Unknow treatment type"))
-
change_current_location = [
str(tp) for tp in treatment_types
if tp.change_current_location
diff --git a/archaeological_finds/wizards.py b/archaeological_finds/wizards.py
index c70721eac..f1d4a2dc4 100644
--- a/archaeological_finds/wizards.py
+++ b/archaeological_finds/wizards.py
@@ -177,13 +177,16 @@ class TreatmentBase(Wizard):
finds = self.get_current_finds()
if not finds:
return initial
- locations = [find.container.location.pk for find in finds if find.container]
+ locations = [find.container.location for find in finds if find.container]
# no location or multiple locations
if not locations or len(set(locations)) != 1:
return initial
if not initial:
initial = {}
- initial["location"] = locations[0]
+ default_location = locations[0]
+ initial["location"] = default_location.id
+ if default_location.organization:
+ initial["organization"] = default_location.organization_id
return initial
def get_extra_model(self, dct, m2m, form_list):
diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py
index 909421188..6fda81283 100644
--- a/ishtar_common/admin.py
+++ b/ishtar_common/admin.py
@@ -565,7 +565,7 @@ class IshtarSiteProfileAdmin(admin.ModelAdmin):
)
model = models.IshtarSiteProfile
form = AdminIshtarSiteProfileForm
- autocomplete_fields = ["no_context_button"]
+ autocomplete_fields = ["no_context_button", "default_location_for_treatment"]
fieldsets = (
(None, {
"fields": (
@@ -631,6 +631,7 @@ class IshtarSiteProfileAdmin(admin.ModelAdmin):
"relation_graph",
"parcel_mandatory",
"no_context_button",
+ "default_location_for_treatment",
),
}),
(_("Index"), {
diff --git a/ishtar_common/migrations/0259_ishtarsiteprofile_default_location_for_treatment.py b/ishtar_common/migrations/0259_ishtarsiteprofile_default_location_for_treatment.py
new file mode 100644
index 000000000..7baf9eada
--- /dev/null
+++ b/ishtar_common/migrations/0259_ishtarsiteprofile_default_location_for_treatment.py
@@ -0,0 +1,20 @@
+# Generated by Django 2.2.24 on 2024-11-28 15:44
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('archaeological_warehouse', '0123_warehouse_container_ishtar_users'),
+ ('ishtar_common', '0258_rename_perm_query'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='ishtarsiteprofile',
+ name='default_location_for_treatment',
+ field=models.ForeignKey(blank=True, help_text='If provided treatment forms have by default a location provided.', null=True, on_delete=django.db.models.deletion.SET_NULL, to='archaeological_warehouse.Warehouse', verbose_name='Default location for treatments'),
+ ),
+ ]
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 2f103ab06..dc3c5d29a 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -1166,7 +1166,19 @@ class IshtarSiteProfile(models.Model, Cached):
no_context_button = models.ForeignKey(
"archaeological_context_records.ContextRecord",
verbose_name=_("Context record for no context button"),
- help_text=_('If provided a button is displayed on find add page to create a "No context" find'),
+ help_text=_('If provided, a button is displayed on find add page to '
+ 'create a "No context" find'),
+ on_delete=models.SET_NULL,
+ null=True,
+ blank=True
+ )
+ default_location_for_treatment = models.ForeignKey(
+ "archaeological_warehouse.Warehouse",
+ verbose_name=_("Default location for treatments"),
+ help_text=_("If provided, treatment forms have by default this "
+ "location set. Furthermore if this location has "
+ "an organization attached, this organization is set "
+ "by default."),
on_delete=models.SET_NULL,
null=True,
blank=True