1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2025 Étienne Loks <etienne.loks at iggdrasil dot net>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# See the file COPYING for details.
from django import forms
from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import re_path, reverse
from django.utils.translation import gettext_lazy as _
from ishtar_common.apps import admin_site
from ishtar_common.admin import (
HistorizedObjectAdmin,
GeneralTypeAdmin,
export_as_csv_action,
serialize_type_action,
ImportActionAdmin,
)
from . import models
class FileAdmin(HistorizedObjectAdmin):
list_display = ["year", "numeric_reference", "file_type", "name"]
if settings.COUNTRY == "fr":
list_display += ["saisine_type", "permit_reference"]
list_filter = ["file_type", "year"]
if settings.COUNTRY == "fr":
list_filter += ["saisine_type"]
search_fields = ("cached_label", "name", "towns__name", "permit_reference")
autocomplete_fields = HistorizedObjectAdmin.autocomplete_fields + [
"in_charge",
"general_contractor",
"corporation_general_contractor",
"responsible_town_planning_service",
"planning_service",
"organization",
"scientist",
"main_town",
"towns",
"related_file",
]
readonly_fields = HistorizedObjectAdmin.readonly_fields + [
"raw_general_contractor",
"raw_town_planning_service",
"cached_label",
"imported_line",
]
exclude = ["documents", "main_image"]
model = models.File
admin_site.register(models.File, FileAdmin)
general_models = [
models.FileType,
models.PermitType,
models.GenericEquipmentServiceType,
models.OperationTypeForRoyalties,
models.AgreementType,
models.DevelopmentType,
models.MonitoringJustificationType
]
if settings.COUNTRY == "fr":
general_models.append(models.SaisineType)
for model in general_models:
admin_site.register(model, GeneralTypeAdmin)
class JobAdmin(GeneralTypeAdmin):
list_filter = ("available", "price_agreement")
LIST_DISPLAY = [
"label",
"price_agreement",
"permanent_contract",
"order",
"ground_daily_cost",
"daily_cost",
"default_daily_need_on_ground",
"default_daily_need",
"child",
"available",
]
admin_site.register(models.Job, JobAdmin)
class EquipmentServiceTypeAdmin(GeneralTypeAdmin):
list_filter = ("available", "generic_equipment_type")
extra_list_display = ["generic_equipment_type", "order"]
admin_site.register(models.EquipmentServiceType, EquipmentServiceTypeAdmin)
class EquipmentServiceCostAdmin(ImportActionAdmin):
search_fields = (
"equipment_service_type__label",
"service_provider",
)
list_filter = ("available", "price_agreement")
list_display = [
"equipment_service_type",
"price_agreement",
"specificity",
"parent",
"unitary_cost",
"unit",
"flat_rate",
"default_quantity_by_day",
"available",
]
actions = [export_as_csv_action(), serialize_type_action]
model = models.EquipmentServiceCost
admin_site.register(models.EquipmentServiceCost, EquipmentServiceCostAdmin)
class CopyPriceForm(forms.Form):
source = forms.ChoiceField(
label=_("Copy from"),
choices=[],
)
destination = forms.ChoiceField(
label=_("To"),
choices=[],
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
choices = [
(idx, label)
for idx, label in models.PriceAgreement.objects.values_list("id", "label")
]
self.fields["source"].choices = choices
self.fields["destination"].choices = choices
def clean(self):
if self.cleaned_data.get("source", None) \
== self.cleaned_data.get("destination", None):
raise forms.ValidationError(
_("Source and destination must be different.")
)
return self.cleaned_data
class CopyPriceAgreementAdmin(GeneralTypeAdmin):
change_list_template = "admin/copy_price_change_list.html"
def get_urls(self):
urls = super(CopyPriceAgreementAdmin, self).get_urls()
my_urls = [
re_path(r"^copy-price-agreement/$", self.copy_price_agreement),
]
return my_urls + urls
def copy_price_agreement(self, request):
form = None
if not request.user.is_superuser and (
not hasattr(request.user, "ishtaruser") or
not request.user.ishtaruser.has_permission(
"archaeological_files.change_priceagreement")):
self.message_user(
request, str(_("Cannot change price agreement."))
)
return HttpResponseRedirect(reverse("admin:login"))
if "apply" in request.POST:
form = CopyPriceForm(request.POST)
if form.is_valid():
created, already_here = 0, 0
source = get_object_or_404(
models.PriceAgreement, pk=form.cleaned_data["source"]
)
destination = get_object_or_404(
models.PriceAgreement, pk=form.cleaned_data["destination"]
)
for model, slug_name, has_child in (
(models.Job, "txt_idx", True),
(models.EquipmentServiceCost, "slug", False)
):
base_query = model.objects.filter(price_agreement_id=source.pk)
if not has_child:
items_queries = [base_query]
else:
items_queries = [
base_query.filter(child__isnull=True),
base_query.filter(child__isnull=False),
]
children = {}
for items in items_queries:
for item in items.all():
slug = getattr(item, slug_name).split("--")[0] + "--" + \
str(destination.pk)
q = model.objects.filter(**{slug_name: slug})
if q.count():
already_here += 1
continue
current_pk = item.pk
new_item = item
new_item.pk = None
setattr(new_item, slug_name, slug)
new_item.price_agreement_id = destination.pk
if has_child and new_item.child:
if new_item.child.pk not in children:
# previous child attached to a bad contract
# cannot correct automatically
pass
else:
new_item.child_id = children[new_item.child.pk]
new_item.save()
created += 1
if has_child and not new_item.child:
children[current_pk] = new_item.pk
if created:
self.message_user(
request, str(_("{} item(s) created.")).format(created)
)
if already_here:
self.message_user(
request, str(_("{} item(s) already in database.")).format(
already_here)
)
c_url = reverse(
"admin:{}_{}_changelist".format(
self.model._meta.app_label, self.model._meta.model_name
)
)
return HttpResponseRedirect(c_url)
if not form:
form = CopyPriceForm()
return render(
request,
"admin/copy_price.html",
{"file_form": form, "current_action": "import_generic"},
)
class PriceAgreementAdmin(CopyPriceAgreementAdmin):
list_filter = ("available",)
extra_list_display = [
"start_date",
"end_date",
]
actions = []
model = models.PriceAgreement
admin_site.register(models.PriceAgreement, PriceAgreementAdmin)
|