Skip to content

Commit

Permalink
Merge pull request #4 from nens/template_setup
Browse files Browse the repository at this point in the history
removed old method to generate xml files. Added template method
  • Loading branch information
JJFlorian authored Mar 18, 2024
2 parents dee707c + e9573f1 commit 1817de6
Show file tree
Hide file tree
Showing 44 changed files with 1,200 additions and 395 deletions.
6 changes: 4 additions & 2 deletions api/bro_import/bulk_import.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import traceback
import logging

import requests
from django.conf import settings

from api import models
from api.bro_import import config

logger = logging.getLogger(__name__)


class FetchBROIDsError(Exception):
"""Custom exception for errors during BRO IDs fetching."""
Expand Down Expand Up @@ -43,7 +45,7 @@ def run(self) -> None:
)
data_importer.run()
except requests.RequestException as e:
traceback.print_exc()
logger.exception(e)
raise DataImportError(f"Error fetching BRO IDs from {url}: {e}") from e

def _create_bro_ids_import_url(self) -> str:
Expand Down
25 changes: 0 additions & 25 deletions api/bro_upload/config.py

This file was deleted.

70 changes: 57 additions & 13 deletions api/bro_upload/delivery.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import logging
import time
import traceback
from typing import Any

from django.template.exceptions import TemplateDoesNotExist
from django.template.loader import render_to_string

from .. import models as api_models
from . import config, utils
from . import utils

logger = logging.getLogger(__name__)


class XMLGenerationError(Exception):
"""Exception raised when XML generation fails."""

pass


class XMLValidationError(Exception):
Expand Down Expand Up @@ -41,9 +53,6 @@ def __init__(
self.upload_task_instance = upload_task_instance
self.bro_username = bro_username
self.bro_password = bro_password
self.xml_generator_class = config.xml_generator_mapping.get(
self.upload_task_instance.registration_type
)

def process(self) -> None:
# Generate the XML file.
Expand All @@ -69,7 +78,7 @@ def process(self) -> None:

def _generate_xml_file(self) -> str:
try:
generator = self.xml_generator_class(
generator = XMLGenerator(
self.upload_task_instance.registration_type,
self.upload_task_instance.request_type,
self.upload_task_instance.metadata,
Expand All @@ -78,7 +87,7 @@ def _generate_xml_file(self) -> str:
return generator.create_xml_file()

except Exception as e:
traceback.print_exc()
logger.exception(e)
raise RuntimeError(f"Error generating XML file: {e}") from e

def _validate_xml_file(self, xml_file: str) -> None:
Expand Down Expand Up @@ -123,23 +132,21 @@ def _deliver_xml_file(self, xml_file: str) -> str:

return delivery_url

def _check_delivery(self, delivery_url: str) -> str | None:
def _check_delivery(self, delivery_url: str) -> bool:
"""Checks the delivery status."""

delivery_info = utils.check_delivery_status(
delivery_url, self.bro_username, self.bro_password
)

errors = delivery_info.json()["brondocuments"][0]["errors"]
errors = delivery_info["brondocuments"][0]["errors"]

if errors:
raise DeliveryError(f"Errors found after delivering the XML file: {errors}")

else:
delivery_status = delivery_info.json()["status"]
delivery_brondocument_status = delivery_info.json()["brondocuments"][0][
"status"
]
delivery_status = delivery_info["status"]
delivery_brondocument_status = delivery_info["brondocuments"][0]["status"]

if (
delivery_status == "DOORGELEVERD"
Expand All @@ -149,3 +156,40 @@ def _check_delivery(self, delivery_url: str) -> str | None:

else:
return False


class XMLGenerator:
"""XML generator based on Django Templates."""

def __init__(
self,
registration_type: str,
request_type: str,
metadata: dict[str, Any],
sourcedocs_data: dict[str, Any],
) -> None:
self.metadata = metadata
self.sourcedocs_data = sourcedocs_data
self.template_filepath = f"{request_type}_{registration_type}.html"

def create_xml_file(self) -> str:
"""Fills in the provided data into the templates"""
try:
rendered_xml = render_to_string(
self.template_filepath,
{
"metadata": self.metadata,
"sourcedocs_data": self.sourcedocs_data,
},
)
return rendered_xml

except TemplateDoesNotExist as e:
logger.exception(e)
raise XMLGenerationError(
"De aangeleverde combinatie van request type en registratie type is niet mogelijk. Als de combinatie in de BRO wel mogelijk is, vraag dan deze combinatie aan bij Nelen & Schuurmans."
) from e

except Exception as e:
logger.exception(e)
raise XMLGenerationError(e) from e
17 changes: 17 additions & 0 deletions api/bro_upload/templates/delete_GMN_Closure.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<deleteRequest xmlns="http://www.broservices.nl/xsd/isgmn/1.0"
xmlns:brocom="http://www.broservices.nl/xsd/brocommon/3.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.broservices.nl/xsd/isgmn/1.0 https://schema.broservices.nl/xsd/isgmn/1.0/isgmn-messages.xsd">
<brocom:requestReference>{{ metadata.requestReference }}</brocom:requestReference>
<brocom:deliveryAccountableParty>{{ metadata.deliveryAccountableParty }}</brocom:deliveryAccountableParty>
<brocom:broId>{{ metadata.broId }}</brocom:broId>
<brocom:qualityRegime>{{ metadata.qualityRegime }}</brocom:qualityRegime>
<correctionReason codeSpace="urn:bro:gmn:CorrectionReason">{{ metadata.correctionReason }}</correctionReason>
<sourceDocument>
<GMN_Closure gml:id="id_0001">
<endDateMonitoring>
<brocom:date>{{ sourcedocs_data.eventDate }}</brocom:date>
</endDateMonitoring>
</GMN_Closure>
</sourceDocument>
</deleteRequest>
28 changes: 28 additions & 0 deletions api/bro_upload/templates/delete_GMN_MeasuringPoint.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<deleteRequest xmlns="http://www.broservices.nl/xsd/isgmn/1.0"
xmlns:brocom="http://www.broservices.nl/xsd/brocommon/3.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.broservices.nl/xsd/isgmn/1.0 https://schema.broservices.nl/xsd/isgmn/1.0/isgmn-messages.xsd">
<brocom:requestReference>{{ metadata.requestReference }}</brocom:requestReference>
<brocom:deliveryAccountableParty>{{ metadata.deliveryAccountableParty }}</brocom:deliveryAccountableParty>
<brocom:broId>{{ metadata.broId }}</brocom:broId>
<brocom:qualityRegime>{{ metadata.qualityRegime }}</brocom:qualityRegime>
<correctionReason codeSpace="urn:bro:gmn:CorrectionReason">{{ metadata.correctionReason }}</correctionReason>
<sourceDocument>
<GMN_MeasuringPoint gml:id="id_0001">
<eventDate>
<brocom:date>{{ sourcedocs_data.eventDate }}</brocom:date>
</eventDate>
<measuringPoint>
<MeasuringPoint gml:id="id_0002">
<measuringPointCode>{{ sourcedocs_data.measuringPointCode }}</measuringPointCode>
<monitoringTube>
<GroundwaterMonitoringTube gml:id="id_0003">
<broId>{{ sourcedocs_data.broId }}</broId>
<tubeNumber>{{ sourcedocs_data.tubeNumber }}</tubeNumber>
</GroundwaterMonitoringTube>
</monitoringTube>
</MeasuringPoint>
</measuringPoint>
</GMN_MeasuringPoint>
</sourceDocument>
</deleteRequest>
18 changes: 18 additions & 0 deletions api/bro_upload/templates/delete_GMN_MeasuringPointEndDate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<deleteRequest xmlns="http://www.broservices.nl/xsd/isgmn/1.0"
xmlns:brocom="http://www.broservices.nl/xsd/brocommon/3.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.broservices.nl/xsd/isgmn/1.0 https://schema.broservices.nl/xsd/isgmn/1.0/isgmn-messages.xsd">
<brocom:requestReference>{{ metadata.requestReference }}</brocom:requestReference>
<brocom:deliveryAccountableParty>{{ metadata.deliveryAccountableParty }}</brocom:deliveryAccountableParty>
<brocom:broId>{{ metadata.broId }}</brocom:broId>
<brocom:qualityRegime>{{ metadata.qualityRegime }}</brocom:qualityRegime>
<correctionReason codeSpace="urn:bro:gmn:CorrectionReason">{{ metadata.correctionReason }}</correctionReason>
<sourceDocument>
<GMN_MeasuringPointEndDate gml:id="id_0001">
<eventDate>
<brocom:date>{{ sourcedocs_data.eventDate }}</brocom:date>
</eventDate>
<measuringPointCode>{{ sourcedocs_data.measuringPointCode }}</measuringPointCode>
</GMN_MeasuringPointEndDate>
</sourceDocument>
</deleteRequest>
28 changes: 28 additions & 0 deletions api/bro_upload/templates/delete_GMN_TubeReference.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<deleteRequest xmlns="http://www.broservices.nl/xsd/isgmn/1.0"
xmlns:brocom="http://www.broservices.nl/xsd/brocommon/3.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.broservices.nl/xsd/isgmn/1.0 https://schema.broservices.nl/xsd/isgmn/1.0/isgmn-messages.xsd">
<brocom:requestReference>{{ metadata.requestReference }}</brocom:requestReference>
<brocom:deliveryAccountableParty>{{ metadata.deliveryAccountableParty }}</brocom:deliveryAccountableParty>
<brocom:broId>{{ metadata.broId }}</brocom:broId>
<brocom:qualityRegime>{{ metadata.qualityRegime }}</brocom:qualityRegime>
<correctionReason codeSpace="urn:bro:gmn:CorrectionReason">{{ metadata.correctionReason }}</correctionReason>
<sourceDocument>
<GMN_TubeReference gml:id="id_0001">
<eventDate>
<brocom:date>{{ sourcedocs_data.eventDate }}</brocom:date>
</eventDate>
<measuringPoint>
<MeasuringPoint gml:id="id_0002">
<measuringPointCode>{{ sourcedocs_data.evemeasuringPointCodentDate }}</measuringPointCode>
<monitoringTube>
<GroundwaterMonitoringTube gml:id="id_0003">
<broId>{{ sourcedocs_data.broId }}</broId>
<tubeNumber>{{ sourcedocs_data.tubeNumber }}</tubeNumber>
</GroundwaterMonitoringTube>
</monitoringTube>
</MeasuringPoint>
</measuringPoint>
</GMN_TubeReference>
</sourceDocument>
</deleteRequest>
28 changes: 28 additions & 0 deletions api/bro_upload/templates/insert_GMN_MeasuringPoint.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<insertRequest xmlns="http://www.broservices.nl/xsd/isgmn/1.0"
xmlns:brocom="http://www.broservices.nl/xsd/brocommon/3.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.broservices.nl/xsd/isgmn/1.0 https://schema.broservices.nl/xsd/isgmn/1.0/isgmn-messages.xsd">
<brocom:requestReference>{{ metadata.requestReference }}</brocom:requestReference>
<brocom:deliveryAccountableParty>{{ metadata.deliveryAccountableParty }}</brocom:deliveryAccountableParty>
<brocom:broId>{{ metadata.broId }}</brocom:broId>
<brocom:qualityRegime>{{ metadata.qualityRegime }}</brocom:qualityRegime>
<correctionReason codeSpace="urn:bro:gmn:CorrectionReason">{{ metadata.correctionReason }}</correctionReason>
<sourceDocument>
<GMN_MeasuringPoint gml:id="id_0001">
<eventDate>
<brocom:date>{{ sourcedocs_data.eventDate }}</brocom:date>
</eventDate>
<measuringPoint>
<MeasuringPoint gml:id="id_0002">
<measuringPointCode>{{ sourcedocs_data.evenmeasuringPointCodetDate }}</measuringPointCode>
<monitoringTube>
<GroundwaterMonitoringTube gml:id="id_0003">
<broId>{{ sourcedocs_data.broId }}</broId>
<tubeNumber>{{ sourcedocs_data.tubeNumber }}</tubeNumber>
</GroundwaterMonitoringTube>
</monitoringTube>
</MeasuringPoint>
</measuringPoint>
</GMN_MeasuringPoint>
</sourceDocument>
</insertRequest>
28 changes: 28 additions & 0 deletions api/bro_upload/templates/insert_GMN_TubeReference.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<insertRequest xmlns="http://www.broservices.nl/xsd/isgmn/1.0"
xmlns:brocom="http://www.broservices.nl/xsd/brocommon/3.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.broservices.nl/xsd/isgmn/1.0 https://schema.broservices.nl/xsd/isgmn/1.0/isgmn-messages.xsd">
<brocom:requestReference>{{ metadata.requestReference }}</brocom:requestReference>
<brocom:deliveryAccountableParty>{{ metadata.deliveryAccountableParty }}</brocom:deliveryAccountableParty>
<brocom:broId>{{ metadata.broId }}</brocom:broId>
<brocom:qualityRegime>{{ metadata.qualityRegime }}</brocom:qualityRegime>
<correctionReason codeSpace="urn:bro:gmn:CorrectionReason">{{ metadata.correctionReason }}</correctionReason>
<sourceDocument>
<GMN_TubeReference gml:id="id_0001">
<eventDate>
<brocom:date>{{ sourcedocs_data.eventDate }}</brocom:date>
</eventDate>
<measuringPoint>
<MeasuringPoint gml:id="id_0002">
<measuringPointCode>{{ sourcedocs_data.measuringPointCode }}</measuringPointCode>
<monitoringTube>
<GroundwaterMonitoringTube gml:id="id_0003">
<broId>{{ sourcedocs_data.broId }}</broId>
<tubeNumber>{{ sourcedocs_data.tubeNumber }}</tubeNumber>
</GroundwaterMonitoringTube>
</monitoringTube>
</MeasuringPoint>
</measuringPoint>
</GMN_TubeReference>
</sourceDocument>
</insertRequest>
20 changes: 20 additions & 0 deletions api/bro_upload/templates/move_GMN_Closure.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<moveRequest xmlns="http://www.broservices.nl/xsd/isgmn/1.0"
xmlns:brocom="http://www.broservices.nl/xsd/brocommon/3.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.broservices.nl/xsd/isgmn/1.0 https://schema.broservices.nl/xsd/isgmn/1.0/isgmn-messages.xsd">
<brocom:requestReference>{{ metadata.requestReference }}</brocom:requestReference>
<brocom:deliveryAccountableParty>{{ metadata.deliveryAccountableParty }}</brocom:deliveryAccountableParty>
<brocom:broId>{{ metadata.broId }}</brocom:broId>
<brocom:qualityRegime>{{ metadata.qualityRegime }}</brocom:qualityRegime>
<correctionReason codeSpace="urn:bro:gmn:CorrectionReason">{{ metadata.correctionReason }}</correctionReason>
<sourceDocument>
<GMN_Closure gml:id="id_0001">
<endDateMonitoring>
<brocom:date>{{ sourcedocs_data.eventDate }}</brocom:date>
</endDateMonitoring>
</GMN_Closure>
</sourceDocument>
<dateToBeCorrected>
<brocom:date>{{ sourcedocs_data.dateToBeCorrected }}</brocom:date>
</dateToBeCorrected>
</moveRequest>
31 changes: 31 additions & 0 deletions api/bro_upload/templates/move_GMN_MeasuringPoint.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<moveRequest xmlns="http://www.broservices.nl/xsd/isgmn/1.0"
xmlns:brocom="http://www.broservices.nl/xsd/brocommon/3.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.broservices.nl/xsd/isgmn/1.0 https://schema.broservices.nl/xsd/isgmn/1.0/isgmn-messages.xsd">
<brocom:requestReference>{{ metadata.requestReference }}</brocom:requestReference>
<brocom:deliveryAccountableParty>{{ metadata.deliveryAccountableParty }}</brocom:deliveryAccountableParty>
<brocom:broId>{{ metadata.broId }}</brocom:broId>
<brocom:qualityRegime>{{ metadata.qualityRegime }}</brocom:qualityRegime>
<correctionReason codeSpace="urn:bro:gmn:CorrectionReason">{{ metadata.correctionReason }}</correctionReason>
<sourceDocument>
<GMN_MeasuringPoint gml:id="id_0001">
<eventDate>
<brocom:date>{{ sourcedocs_data.eventDate }}</brocom:date>
</eventDate>
<measuringPoint>
<MeasuringPoint gml:id="id_0002">
<measuringPointCode>{{ sourcedocs_data.measuringPointCode }}</measuringPointCode>
<monitoringTube>
<GroundwaterMonitoringTube gml:id="id_0003">
<broId>{{ sourcedocs_data.broId }}</broId>
<tubeNumber>{{ sourcedocs_data.tubeNumber }}</tubeNumber>
</GroundwaterMonitoringTube>
</monitoringTube>
</MeasuringPoint>
</measuringPoint>
</GMN_MeasuringPoint>
</sourceDocument>
<dateToBeCorrected>
<brocom:date>{{ sourcedocs_data.dateToBeCorrected }}</brocom:date>
</dateToBeCorrected>
</moveRequest>
21 changes: 21 additions & 0 deletions api/bro_upload/templates/move_GMN_MeasuringPointEndDate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<moveRequest xmlns="http://www.broservices.nl/xsd/isgmn/1.0"
xmlns:brocom="http://www.broservices.nl/xsd/brocommon/3.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.broservices.nl/xsd/isgmn/1.0 https://schema.broservices.nl/xsd/isgmn/1.0/isgmn-messages.xsd">
<brocom:requestReference>{{ metadata.requestReference }}</brocom:requestReference>
<brocom:deliveryAccountableParty>{{ metadata.deliveryAccountableParty }}</brocom:deliveryAccountableParty>
<brocom:broId>{{ metadata.broId }}</brocom:broId>
<brocom:qualityRegime>{{ metadata.qualityRegime }}</brocom:qualityRegime>
<correctionReason codeSpace="urn:bro:gmn:CorrectionReason">{{ metadata.correctionReason }}</correctionReason>
<sourceDocument>
<GMN_MeasuringPointEndDate gml:id="id_0001">
<eventDate>
<brocom:date>{{ sourcedocs_data.eventDate }}</brocom:date>
</eventDate>
<measuringPointCode>{{ sourcedocs_data.measuringPointCode }}</measuringPointCode>
</GMN_MeasuringPointEndDate>
</sourceDocument>
<dateToBeCorrected>
<brocom:date>{{ sourcedocs_data.dateToBeCorrected }}</brocom:date>
</dateToBeCorrected>
</moveRequest>
Loading

0 comments on commit 1817de6

Please sign in to comment.