-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
308 lines (266 loc) · 9.86 KB
/
admin.py
File metadata and controls
308 lines (266 loc) · 9.86 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import json
from django import forms
from django.contrib import admin, messages
from django.contrib.admin import helpers
from django.db import models
from django.shortcuts import redirect, render
from django.urls import path, reverse
from django.utils.html import format_html
from iaso.admin import IasoJSONEditorWidget
from iaso.models import Account
from plugins.snt_malaria.api.account_settings.serializers import AccountSettings
from plugins.snt_malaria.management.commands.load_impact_org_unit_mappings import (
HELP_TEXT as IMPACT_MAPPING_HELP_TEXT,
MappingLoadError,
load_impact_org_unit_mappings,
)
from .models import (
Budget,
BudgetAssumptions,
BudgetSettings,
ImpactOrgUnitMapping,
ImpactProviderConfig,
Intervention,
InterventionAssignment,
InterventionCategory,
InterventionCostBreakdownLine,
Scenario,
ScenarioRule,
)
from .models.scenario import ScenarioRuleInterventionProperties
@admin.register(InterventionCategory)
class InterventionCategoryAdmin(admin.ModelAdmin):
list_display = (
"name",
"short_name",
"account",
"created_by",
"created_at",
"updated_at",
"deleted_at",
)
search_fields = ("name", "description")
list_filter = ("account", "created_by")
ordering = ("name",)
@admin.register(Intervention)
class InterventionAdmin(admin.ModelAdmin):
list_display = (
"name",
"short_name",
"intervention_category",
"code",
"impact_ref",
"created_by",
"created_at",
"updated_at",
"updated_by",
"deleted_at",
)
list_editable = ("impact_ref",)
search_fields = ("name", "description", "code", "impact_ref")
list_filter = (
"intervention_category__account",
"created_by",
)
ordering = ("name",)
@admin.register(InterventionAssignment)
class InterventionAssignmentAdmin(admin.ModelAdmin):
raw_id_fields = ("org_unit", "intervention")
list_display = (
"scenario",
"created_by",
"created_at",
)
list_filter = ("scenario", "intervention", "created_by")
search_fields = ("scenario__name", "org_unit__name")
@admin.register(Scenario)
class ScenarioAdmin(admin.ModelAdmin):
list_display = (
"name",
"account",
"created_by",
"created_at",
"updated_at",
"deleted_at",
)
search_fields = ("name", "description")
list_filter = ("account", "created_by")
ordering = ("name",)
class ScenarioRuleInterventionPropertiesInline(admin.TabularInline):
model = ScenarioRuleInterventionProperties
extra = 3
@admin.register(ScenarioRule)
class ScenarioRuleAdmin(admin.ModelAdmin):
list_display = (
"name",
"scenario",
"priority",
"created_by",
"created_at",
"updated_at",
)
search_fields = ("name",)
list_filter = ("scenario", "created_by")
ordering = ("scenario", "priority")
inlines = [ScenarioRuleInterventionPropertiesInline]
@admin.register(InterventionCostBreakdownLine)
class InterventionCostBreakdownLineAdmin(admin.ModelAdmin):
list_display = ("id", "intervention", "name", "unit_cost", "created_at")
search_fields = ("intervention",)
list_filter = ("intervention",)
ordering = ("id", "name")
@admin.register(Budget)
class BudgetAdmin(admin.ModelAdmin):
list_display = ("id", "scenario", "name", "cost_input", "assumptions", "updated_at")
search_fields = ("id", "name")
list_filter = ("scenario",)
ordering = ("id", "name", "updated_at")
@admin.register(BudgetSettings)
class BudgetSettingsAdmin(admin.ModelAdmin):
list_display = (
"id",
"account",
"local_currency",
"exchange_rate",
"inflation_rate",
)
search_fields = ("id", "local_currency")
ordering = ("id", "local_currency")
@admin.register(BudgetAssumptions)
class BudgetAssumptionsAdmin(admin.ModelAdmin):
list_display = ("id", "scenario", "intervention_assignment", "year")
search_fields = ("id", "scenario__name", "intervention_assignment__id")
list_filter = ("year",)
ordering = ("id",)
class ImpactProviderConfigForm(forms.ModelForm):
secret = forms.CharField(
widget=forms.PasswordInput(attrs={"autocomplete": "off"}, render_value=True),
required=False,
help_text=ImpactProviderConfig._meta.get_field("secret").help_text,
)
class Meta:
model = ImpactProviderConfig
fields = "__all__"
@admin.register(ImpactProviderConfig)
class ImpactProviderConfigAdmin(admin.ModelAdmin):
form = ImpactProviderConfigForm
formfield_overrides = {models.JSONField: {"widget": IasoJSONEditorWidget}}
list_display = ("id", "account", "provider_key")
list_filter = ("provider_key",)
search_fields = ("account__name",)
ordering = ("account__name",)
fieldsets = (
(None, {"fields": ("account", "provider_key")}),
("Provider configuration", {"fields": ("config", "secret")}),
("Caching", {"fields": ("cache_ttl_seconds",)}),
)
MAPPING_FILE_HELP = format_html(
"<p>JSON array of {{name, reference?, children?}} nodes.</p>"
"<details><summary>Expected file format</summary>"
'<pre style="white-space: pre-wrap;">{}</pre></details>',
IMPACT_MAPPING_HELP_TEXT,
)
class LoadImpactOrgUnitMappingsForm(forms.Form):
account = forms.ModelChoiceField(
queryset=Account.objects.order_by("name"),
help_text="Account whose default version's org units will be mapped.",
)
mapping_file = forms.FileField(
label="Mapping file",
help_text=MAPPING_FILE_HELP,
)
overwrite = forms.BooleanField(
required=False,
help_text="Overwrite existing mappings. Without this flag only unmapped org units are processed.",
)
@admin.register(ImpactOrgUnitMapping)
class ImpactOrgUnitMappingAdmin(admin.ModelAdmin):
list_display = ("id", "org_unit", "reference", "get_account")
list_editable = ("reference",)
autocomplete_fields = ("org_unit",)
search_fields = ("org_unit__name", "reference")
list_filter = ("org_unit__version__data_source__projects__account",)
ordering = ("org_unit__name",)
change_list_template = "admin/snt_malaria/impactorgunitmapping/change_list.html"
def get_queryset(self, request):
return (
super()
.get_queryset(request)
.select_related("org_unit__version__data_source")
.prefetch_related("org_unit__version__data_source__projects__account")
)
@admin.display(description="Account")
def get_account(self, obj):
accounts = {project.account.name for project in obj.org_unit.version.data_source.projects.all()}
return ", ".join(sorted(accounts)) if accounts else "-"
def get_urls(self):
urls = super().get_urls()
custom_urls = [
path(
"load-from-file/",
self.admin_site.admin_view(self.load_from_file_view),
name="snt_malaria_impactorgunitmapping_load_from_file",
),
]
return custom_urls + urls
def load_from_file_view(self, request):
changelist_url = reverse("admin:snt_malaria_impactorgunitmapping_changelist")
if request.method == "POST":
form = LoadImpactOrgUnitMappingsForm(request.POST, request.FILES)
if form.is_valid():
uploaded = form.cleaned_data["mapping_file"]
try:
mapping_nodes = json.load(uploaded)
except json.JSONDecodeError as e:
form.add_error("mapping_file", f"Invalid JSON: {e}")
else:
try:
counts = load_impact_org_unit_mappings(
account=form.cleaned_data["account"],
mapping_nodes=mapping_nodes,
overwrite=form.cleaned_data["overwrite"],
)
except MappingLoadError as e:
form.add_error(None, str(e))
else:
messages.success(
request,
(
f"Account '{form.cleaned_data['account'].name}': "
f"created {counts['created']}, updated {counts['updated']}, "
f"skipped {counts['skipped']} mapping(s)."
),
)
return redirect(changelist_url)
else:
form = LoadImpactOrgUnitMappingsForm()
fieldsets = [(None, {"fields": list(form.fields)})]
adminform = helpers.AdminForm(form, fieldsets, prepopulated_fields={}, model_admin=self)
context = {
**self.admin_site.each_context(request),
"title": "Load impact org unit mappings from file",
"adminform": adminform,
"errors": helpers.AdminErrorList(form, []),
"media": self.media + form.media,
"opts": self.model._meta,
"has_file_field": True,
"add": True,
"change": False,
"is_popup": False,
"save_as": False,
"show_save": True,
"show_save_and_add_another": False,
"show_save_and_continue": False,
"show_delete": False,
"has_add_permission": True,
"has_change_permission": True,
"has_delete_permission": False,
"has_view_permission": True,
"has_editable_inline_admin_formsets": False,
}
return render(request, "admin/change_form.html", context)
@admin.register(AccountSettings)
class AccountSettingsAdmin(admin.ModelAdmin):
list_display = ("id", "account", "focus_org_unit_type_id", "intervention_org_unit_type_id")
search_fields = ("account__name",)
ordering = ("account__name",)