|
| 1 | +from base64 import b64encode |
1 | 2 | from typing import Protocol
|
2 | 3 |
|
3 | 4 | from django.conf import settings
|
4 | 5 | from django.core.mail import send_mail
|
5 | 6 | from django.db import transaction
|
6 | 7 | from django.db.models import OuterRef, Q, Subquery
|
| 8 | +from django.utils import timezone |
| 9 | +from django.utils.translation import gettext_lazy as _ |
| 10 | + |
| 11 | +from zgw_consumers.client import build_client |
| 12 | +from zgw_consumers.constants import APITypes |
| 13 | +from zgw_consumers.models import Service |
7 | 14 |
|
8 | 15 | from openarchiefbeheer.accounts.models import User
|
| 16 | +from openarchiefbeheer.config.models import ArchiveConfig |
9 | 17 | from openarchiefbeheer.emails.models import EmailConfig
|
10 | 18 | from openarchiefbeheer.emails.render_backend import get_sandboxed_backend
|
| 19 | +from openarchiefbeheer.utils.results_store import ResultStore |
11 | 20 | from openarchiefbeheer.zaken.models import Zaak
|
12 | 21 |
|
13 | 22 | from .constants import InternalStatus, ListRole
|
@@ -121,3 +130,117 @@ def resync_items_and_zaken() -> None:
|
121 | 130 | Zaak.objects.filter(url=OuterRef("_zaak_url")).values("pk")[:1]
|
122 | 131 | )
|
123 | 132 | )
|
| 133 | + |
| 134 | + |
| 135 | +def create_zaak_for_report( |
| 136 | + destruction_list: DestructionList, store: ResultStore |
| 137 | +) -> None: |
| 138 | + config = ArchiveConfig.get_solo() |
| 139 | + |
| 140 | + zrc_service = Service.objects.get(api_type=APITypes.zrc) |
| 141 | + zrc_client = build_client(zrc_service) |
| 142 | + |
| 143 | + with zrc_client: |
| 144 | + if not destruction_list.zaak_destruction_report_url: |
| 145 | + response = zrc_client.post( |
| 146 | + "zaken", |
| 147 | + headers={ |
| 148 | + "Accept-Crs": "EPSG:4326", |
| 149 | + "Content-Crs": "EPSG:4326", |
| 150 | + }, |
| 151 | + json={ |
| 152 | + "bronorganisatie": config.bronorganisatie, |
| 153 | + "omschrijving": _("Destruction report of list: %(list_name)s") |
| 154 | + % {"list_name": destruction_list.name}, |
| 155 | + "zaaktype": config.zaaktype, |
| 156 | + "vertrouwelijkheidaanduiding": "openbaar", |
| 157 | + "startdatum": timezone.now().date().isoformat(), |
| 158 | + "verantwoordelijkeOrganisatie": config.bronorganisatie, |
| 159 | + "archiefnominatie": "blijvend_bewaren", |
| 160 | + }, |
| 161 | + timeout=settings.REQUESTS_DEFAULT_TIMEOUT, |
| 162 | + ) |
| 163 | + response.raise_for_status() |
| 164 | + new_zaak = response.json() |
| 165 | + |
| 166 | + destruction_list.zaak_destruction_report_url = new_zaak["url"] |
| 167 | + destruction_list.save() |
| 168 | + |
| 169 | + if not store.has_created_resource("resultaten"): |
| 170 | + response = zrc_client.post( |
| 171 | + "resultaten", |
| 172 | + json={ |
| 173 | + "zaak": destruction_list.zaak_destruction_report_url, |
| 174 | + "resultaattype": config.resultaattype, |
| 175 | + }, |
| 176 | + ) |
| 177 | + response.raise_for_status() |
| 178 | + store.add_created_resource("resultaten", response.json()["url"]) |
| 179 | + |
| 180 | + if not store.has_created_resource("statussen"): |
| 181 | + response = zrc_client.post( |
| 182 | + "statussen", |
| 183 | + json={ |
| 184 | + "zaak": destruction_list.zaak_destruction_report_url, |
| 185 | + "statustype": config.statustype, |
| 186 | + "datum_status_gezet": timezone.now().date().isoformat(), |
| 187 | + }, |
| 188 | + ) |
| 189 | + response.raise_for_status() |
| 190 | + store.add_created_resource("statussen", response.json()["url"]) |
| 191 | + |
| 192 | + |
| 193 | +def create_eio_destruction_report( |
| 194 | + destruction_list: DestructionList, store: ResultStore |
| 195 | +) -> None: |
| 196 | + if store.has_created_resource("enkelvoudiginformatieobjecten"): |
| 197 | + return |
| 198 | + |
| 199 | + config = ArchiveConfig.get_solo() |
| 200 | + |
| 201 | + drc_service = Service.objects.get(api_type=APITypes.drc) |
| 202 | + drc_client = build_client(drc_service) |
| 203 | + |
| 204 | + with drc_client, destruction_list.destruction_report.open("rb") as f_report: |
| 205 | + response = drc_client.post( |
| 206 | + "enkelvoudiginformatieobjecten", |
| 207 | + json={ |
| 208 | + "bronorganisatie": config.bronorganisatie, |
| 209 | + "creatiedatum": timezone.now().date().isoformat(), |
| 210 | + "titel": _("Destruction report of list: %(list_name)s") |
| 211 | + % {"list_name": destruction_list.name}, |
| 212 | + "auteur": "Open Archiefbeheer", |
| 213 | + "taal": "nld", |
| 214 | + "formaat": "text/csv", |
| 215 | + "inhoud": b64encode(f_report.read()).decode("utf-8"), |
| 216 | + "informatieobjecttype": config.informatieobjecttype, |
| 217 | + "indicatie_gebruiksrecht": False, |
| 218 | + }, |
| 219 | + ) |
| 220 | + response.raise_for_status() |
| 221 | + new_document = response.json() |
| 222 | + |
| 223 | + store.add_created_resource("enkelvoudiginformatieobjecten", new_document["url"]) |
| 224 | + |
| 225 | + |
| 226 | +def attach_report_to_zaak( |
| 227 | + destruction_list: DestructionList, store: ResultStore |
| 228 | +) -> None: |
| 229 | + if store.has_created_resource("zaakinformatieobjecten"): |
| 230 | + return |
| 231 | + |
| 232 | + zrc_service = Service.objects.get(api_type=APITypes.zrc) |
| 233 | + zrc_client = build_client(zrc_service) |
| 234 | + |
| 235 | + with zrc_client: |
| 236 | + response = zrc_client.post( |
| 237 | + "zaakinformatieobjecten", |
| 238 | + json={ |
| 239 | + "zaak": destruction_list.zaak_destruction_report_url, |
| 240 | + "informatieobject": store.get_created_resources( |
| 241 | + "enkelvoudiginformatieobjecten" |
| 242 | + )[0], |
| 243 | + }, |
| 244 | + ) |
| 245 | + response.raise_for_status() |
| 246 | + store.add_created_resource("zaakinformatieobjecten", response.json()["url"]) |
0 commit comments