-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
528 lines (451 loc) · 16 KB
/
models.py
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import logging
import uuid as _uuid
from datetime import date
from typing import TYPE_CHECKING, Iterable, Optional
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.db.models import QuerySet
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from timeline_logger.models import TimelineLog
from openarchiefbeheer.accounts.models import User
from openarchiefbeheer.config.models import ArchiveConfig
from openarchiefbeheer.utils.results_store import ResultStore
from openarchiefbeheer.zaken.utils import delete_zaak_and_related_objects
from .assignment_logic import STATE_MANAGER
from .constants import (
DestructionListItemAction,
InternalStatus,
ListItemStatus,
ListRole,
ListStatus,
ReviewDecisionChoices,
ZaakActionType,
)
from .exceptions import ZaakArchiefactiedatumInFuture, ZaakNotFound
if TYPE_CHECKING:
from openarchiefbeheer.zaken.models import Zaak
logger = logging.getLogger(__name__)
class DestructionList(models.Model):
name = models.CharField(_("name"), max_length=200, unique=True)
uuid = models.UUIDField(_("uuid"), default=_uuid.uuid4, unique=True)
author = models.ForeignKey(
"accounts.User",
on_delete=models.CASCADE,
related_name="created_lists",
verbose_name=_("author"),
help_text=_("Creator of destruction list."),
)
created = models.DateTimeField(auto_now_add=True)
end = models.DateTimeField(
_("end"),
blank=True,
null=True,
help_text=_(
"The timestamp at which all the cases in the list have been deleted."
),
)
contains_sensitive_info = models.BooleanField(
verbose_name=_("contains sensitive information"),
help_text=_(
"Specify whether this destruction list contains privacy sensitive data. "
"If set to true, the report of destruction will NOT contain case "
"descriptions or the remarks by the reviewers."
),
default=True,
)
assignee = models.ForeignKey(
"accounts.User",
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name="assigned_lists",
verbose_name=_("assignee"),
help_text=_("Currently assigned user."),
)
status = models.CharField(
_("status"),
default=ListStatus.new,
choices=ListStatus.choices,
max_length=80,
)
status_changed = models.DateTimeField(
_("status changed"),
help_text=_("Tracks when the status was changed."),
blank=True,
null=True,
)
zaak_destruction_report_url = models.URLField(
_("zaak destruction report URL"),
help_text=_(
"The URL of the case containing the destruction report for this destruction list."
),
blank=True,
)
processing_status = models.CharField(
_("processing status"),
choices=InternalStatus.choices,
max_length=80,
help_text=_(
"Field used to track the status of the deletion of a destruction list."
),
default=InternalStatus.new,
)
planned_destruction_date = models.DateField(
_("planned destruction date"),
help_text=_("Date from which this destruction list can be deleted."),
blank=True,
null=True,
)
logs = GenericRelation(TimelineLog, related_query_name="destruction_list")
class Meta:
verbose_name = _("destruction list")
verbose_name_plural = _("destruction lists")
def __str__(self):
return self.name
@staticmethod
def assign(assignee: "DestructionListAssignee") -> None:
assignee.assign()
def set_status(self, status: str) -> None:
self.status = status
self.status_changed = timezone.now()
self.save()
def add_items(self, zaken: Iterable["Zaak"]) -> list["DestructionListItem"]:
return DestructionListItem.objects.bulk_create(
[DestructionListItem(destruction_list=self, zaak=zaak) for zaak in zaken]
)
def get_author(self) -> "DestructionListAssignee":
return self.assignees.get(role=ListRole.author)
def get_assignee(self, user: User) -> Optional["DestructionListAssignee"]:
return self.assignees.filter(user=user).first()
def get_current_assignee(self) -> Optional["DestructionListAssignee"]:
return self.assignees.filter(user=self.assignee).first()
def assign_next(self) -> None:
STATE_MANAGER[self.status].assign_next(self)
def reassign(self) -> None:
STATE_MANAGER[self.status].reassign(self)
def has_short_review_process(self) -> bool:
zaaktypes_urls = (
self.items.all()
.select_related("zaak")
.values_list("zaak__zaaktype", flat=True)
.distinct()
)
config = ArchiveConfig.get_solo()
return all(
[zaaktype in config.zaaktypes_short_process for zaaktype in zaaktypes_urls]
)
def has_failures(self) -> bool:
return any(
[
status == InternalStatus.failed
for status in self.items.values_list("processing_status", flat=True)
]
)
def all_items_can_be_deleted_by_date(self, destruction_date: date) -> bool:
return not self.items.filter(
zaak__archiefactiedatum__gt=destruction_date,
status=ListItemStatus.suggested,
).exists()
def abort_destruction(self) -> None:
self.set_status(ListStatus.new)
self.planned_destruction_date = None
self.processing_status = InternalStatus.new
self.save()
class DestructionListItem(models.Model):
destruction_list = models.ForeignKey(
DestructionList,
on_delete=models.CASCADE,
related_name="items",
verbose_name=_("destruction list"),
)
zaak = models.ForeignKey(
"zaken.Zaak",
on_delete=models.SET_NULL,
related_name="items",
verbose_name=_("zaak"),
blank=True,
null=True,
)
_zaak_url = models.URLField(
_("zaak url"),
max_length=1000,
help_text=_(
"Keep a relation to the zaak for when the zaken cached are being re-synced."
),
blank=True,
)
status = models.CharField(
_("status"),
default=ListItemStatus.suggested,
choices=ListItemStatus.choices,
max_length=80,
)
extra_zaak_data = models.JSONField(
verbose_name=_("extra zaak data"),
help_text=_("Additional information of the zaak"),
null=True,
blank=True,
)
processing_status = models.CharField(
_("processing status"),
choices=InternalStatus.choices,
max_length=80,
help_text=_(
"Field used to track the status of the deletion of a destruction list item."
),
default=InternalStatus.new,
)
internal_results = models.JSONField(
verbose_name=_("internal result"),
help_text=_(
"When this item gets processed, "
"the URL of the resources deleted from Open Zaak get stored here."
),
default=dict,
)
class Meta:
verbose_name = _("destruction list item")
verbose_name_plural = _("destruction list items")
unique_together = ("destruction_list", "zaak")
def __str__(self):
if self.zaak:
return f"{self.destruction_list}: {self.zaak.identificatie}"
return f"{self.destruction_list}: (deleted)"
def save(self, *args, **kwargs):
if self.zaak:
self._zaak_url = self.zaak.url
return super().save(*args, **kwargs)
def set_processing_status(self, status: InternalStatus) -> None:
self.processing_status = status
self.save()
def _delete_zaak(self):
if not self.zaak:
logger.error("Could not find the zaak. Aborting deletion.")
raise ZaakNotFound()
if self.zaak.archiefactiedatum > date.today():
logger.error(
"Trying to delete zaak with archiefactiedatum in the future. Aborting deletion."
)
raise ZaakArchiefactiedatumInFuture()
store = ResultStore(store=self)
store.clear_traceback()
delete_zaak_and_related_objects(zaak=self.zaak, result_store=store)
self.zaak.delete()
def process_deletion(self) -> None:
self.processing_status = InternalStatus.processing
self.save()
try:
self._delete_zaak()
except Exception:
self.set_processing_status(InternalStatus.failed)
return
self.zaak = None
self.processing_status = InternalStatus.succeeded
self._zaak_url = ""
self.save()
class DestructionListAssignee(models.Model):
destruction_list = models.ForeignKey(
DestructionList,
on_delete=models.CASCADE,
related_name="assignees",
verbose_name=_("destruction list"),
)
user = models.ForeignKey(
"accounts.User",
on_delete=models.PROTECT,
verbose_name=_("user"),
help_text=_("The user assigned to the destruction list."),
)
assigned_on = models.DateTimeField(_("assigned on"), blank=True, null=True)
role = models.CharField(
_("role"),
default=ListRole.reviewer,
choices=ListRole.choices,
max_length=80,
)
class Meta:
verbose_name = _("destruction list assignee")
verbose_name_plural = _("destruction list assignees")
unique_together = ("destruction_list", "user")
def __str__(self):
return f"{self.user} ({self.destruction_list})"
def assign(self) -> None:
from .signals import user_assigned
# TODO Log assignment
self.destruction_list.assignee = self.user
self.assigned_on = timezone.now()
self.destruction_list.save()
self.save()
user_assigned.send(sender=self.__class__, assignee=self)
class DestructionListReview(models.Model):
destruction_list = models.ForeignKey(
DestructionList,
on_delete=models.CASCADE,
related_name="reviews",
verbose_name=_("destruction list"),
)
author = models.ForeignKey(
"accounts.User",
on_delete=models.CASCADE,
related_name="created_reviews",
verbose_name=_("author"),
help_text=_("User who created the review."),
)
decision = models.CharField(
_("decision"),
choices=ReviewDecisionChoices.choices,
max_length=80,
)
created = models.DateTimeField(auto_now_add=True)
list_feedback = models.TextField(
_("list feedback"),
max_length=2000,
blank=True,
help_text=_("Feedback about the destruction list as a whole."),
)
class Meta:
verbose_name = _("destruction list review")
verbose_name_plural = _("destruction list reviews")
def __str__(self):
return f"Review for {self.destruction_list} ({self.author})"
class DestructionListItemReview(models.Model):
destruction_list = models.ForeignKey(
DestructionList,
on_delete=models.CASCADE,
related_name="item_reviews",
verbose_name=_("destruction list"),
)
destruction_list_item = models.ForeignKey(
DestructionListItem,
on_delete=models.CASCADE,
related_name="reviews",
verbose_name=_("destruction list item"),
)
review = models.ForeignKey(
DestructionListReview,
on_delete=models.CASCADE,
related_name="item_reviews",
verbose_name=_("review"),
)
feedback = models.TextField(
_("feedback"),
max_length=2000,
blank=False,
help_text=_("What needs to be changed about the case."),
)
class Meta:
verbose_name = _("destruction list item review")
verbose_name_plural = _("destruction list item reviews")
def __str__(self):
return f"Case review for {self.destruction_list} ({self.destruction_list_item})"
class ReviewResponse(models.Model):
review = models.ForeignKey(
DestructionListReview,
on_delete=models.CASCADE,
related_name="responses",
verbose_name=_("review"),
)
comment = models.TextField(
_("comment"),
max_length=2000,
blank=True,
help_text=_("The response of the author of the destruction list to a review."),
)
created = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name = _("review response")
verbose_name_plural = _("review responses")
def __str__(self):
return f"Response to {self.review}"
@property
def items_responses(self) -> QuerySet["ReviewItemResponse"]:
return ReviewItemResponse.objects.filter(review_item__review=self.review)
@staticmethod
def _derive_status(items_statuses: list[str]) -> str:
if all([item_status == InternalStatus.new for item_status in items_statuses]):
return InternalStatus.new
if all(
[item_status == InternalStatus.succeeded for item_status in items_statuses]
):
return InternalStatus.succeeded
if any(
[item_status == InternalStatus.failed for item_status in items_statuses]
):
return InternalStatus.failed
if any(
[item_status == InternalStatus.processing for item_status in items_statuses]
):
return InternalStatus.processing
if any(
[item_status == InternalStatus.queued for item_status in items_statuses]
):
return InternalStatus.queued
return InternalStatus.processing
@property
def processing_status(self) -> str:
items_statuses = self.items_responses.values_list(
"processing_status", flat=True
)
return self._derive_status(items_statuses)
class ReviewItemResponse(models.Model):
review_item = models.ForeignKey(
DestructionListItemReview,
on_delete=models.CASCADE,
related_name="item_responses",
verbose_name=_("review item"),
)
action_item = models.CharField(
_("action item"),
choices=DestructionListItemAction.choices,
max_length=80,
)
action_zaak_type = models.CharField(
_("action zaak type"),
choices=ZaakActionType.choices,
max_length=80,
help_text=_("What type of change to do on the case. "),
blank=True,
)
action_zaak = models.JSONField(
_("action case"),
blank=True,
help_text=_("Fields that should be changed on the case."),
null=True,
)
created = models.DateTimeField(auto_now_add=True)
comment = models.TextField(
_("comment"),
max_length=2000,
blank=True,
help_text=_(
"The response of the author of the destruction list to "
"feedback of the reviewer on a specific case."
),
)
processing_status = models.CharField(
_("processing status"),
choices=InternalStatus.choices,
max_length=80,
help_text=_(
"Field used to track the status of the changes that should be made to a destruction list item and the corresponding case."
),
default=InternalStatus.new,
)
class Meta:
verbose_name = _("review item response")
verbose_name_plural = _("review item responses")
def __str__(self):
return f"Response to {self.review_item}"
def process(self) -> None:
if self.processing_status == InternalStatus.succeeded:
return
self.processing_status = InternalStatus.processing
self.save()
destruction_list_item = self.review_item.destruction_list_item
if self.action_item == DestructionListItemAction.remove:
destruction_list_item.status = ListItemStatus.removed
destruction_list_item.save()
destruction_list_item.zaak.update_data(self.action_zaak)
self.processing_status = InternalStatus.succeeded
self.save()