Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bericht attachment download functionality #1437

Open
wants to merge 2 commits into
base: mijn-services-hackathon-2024
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/open_inwoner/berichten/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.urls import path

from open_inwoner.berichten.views.bericht_detail import BerichtDownloadView

from .views import BerichtDetailView, BerichtListView, MarkBerichtUnreadView

app_name = "berichten"
Expand All @@ -11,5 +13,10 @@
MarkBerichtUnreadView.as_view(),
name="mark-bericht-unread",
),
path(
"<uuid:object_uuid>/download",
BerichtDownloadView.as_view(),
name="download-bericht-attachment",
),
path("", BerichtListView.as_view(), name="list"),
]
13 changes: 11 additions & 2 deletions src/open_inwoner/berichten/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
from .bericht_detail import BerichtDetailView, MarkBerichtUnreadView
from .bericht_detail import (
BerichtDetailView,
BerichtDownloadView,
MarkBerichtUnreadView,
)
from .bericht_list import BerichtListView

__all__ = ["BerichtDetailView", "BerichtListView", "MarkBerichtUnreadView"]
__all__ = [
"BerichtDetailView",
"BerichtListView",
"MarkBerichtUnreadView",
"BerichtDownloadView",
]
48 changes: 47 additions & 1 deletion src/open_inwoner/berichten/views/bericht_detail.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from django.http import HttpResponseRedirect
from django.http import Http404, HttpResponseRedirect, StreamingHttpResponse
from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
Expand All @@ -10,6 +10,7 @@

from open_inwoner.berichten.services import BerichtenService
from open_inwoner.berichten.views.mixins import BerichtAccessMixin
from open_inwoner.openzaak.models import ZGWApiGroupConfig
from open_inwoner.utils.views import CommonPageMixin

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -49,3 +50,48 @@ def get(self, *args, **kwargs):
service = BerichtenService()
service.update_object(self.kwargs["object_uuid"], {"geopend": False})
return HttpResponseRedirect(reverse("berichten:list"))


class BerichtDownloadView(BerichtAccessMixin):
def get(self, *args, **kwargs):
url = self.request.GET.get("url")
if not url:
logger.error("No URL provided")
raise Http404

if url not in self.bericht.bijlages:
logger.error("URL provided that is not a bijlage for the specified bericht")
raise Http404

try:
api_group = ZGWApiGroupConfig.objects.resolve_group_from_hints(url=url)
except ZGWApiGroupConfig.DoesNotExist:
logger.exception("Unable to resolve API group from bericht attachment url")
raise Http404

documenten_client = api_group.documenten_client
info_object = documenten_client._fetch_single_information_object(url=url)
logger.info(f"{info_object=}")
if not info_object:
logger.error("Could not find info object for bericht attachment")
raise Http404

content_stream = api_group.documenten_client.download_document(
info_object.inhoud
)
logger.info(f"{content_stream=} {content_stream.headers=}")

if not content_stream:
logger.error("Could not build content stream for bericht attachment")
raise Http404

headers = {
"Content-Disposition": f'attachment; filename="{info_object.bestandsnaam}"',
"Content-Type": info_object.formaat,
# TODO: We should be able to use info_object.bestandsomvang here, but we've seen instances
# in the wild where this is incorrectly set, which can trip up the browser. Better to just
# use the content-length given to us by the server.
"Content-Length": content_stream.headers.get("Content-Length"),
}
response = StreamingHttpResponse(content_stream, headers=headers)
return response
7 changes: 5 additions & 2 deletions src/open_inwoner/templates/pages/berichten/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ <h1 class="utrecht-heading-1" id="title">{{ bericht.onderwerp }}</h1>
{# Icon only if deadline exists #}
{% if bericht.einddatum_handelingstermijn %}
{% icon icon="warning_amber" icon_position="after" extra_classes="icon--danger" outlined=True %}
{% endif %}

<span class="dashboard__item-label">
<span> Einddatum: </span>
</span>
<span>{{ bericht.einddatum_handelingstermijn }}</span>
<span class="dashboard__item-label"><span id="remainingDays" class="utrecht-paragraph table__item--notification-danger"></span> <!-- Placeholder for remaining days --></span>
{% else %}
<span class="dashboard__item-label">Voor dit bericht is geen uiterste handelingstermijn</span>
{% endif %}
</p>
</li>
</ul>
Expand Down Expand Up @@ -89,7 +92,7 @@ <h2 class="utrecht-heading-2 " id="documents">Bijlagen</h2>
<span class="file__name">{{ attachment }} (PDF)</span>
<span class="file__date">{{ bericht.publicatiedatum }}</span>
</div>
<a href="https://www.maykinmedia.nl/media/filer_public/94/50/9450f9e9-70d1-4d00-99e4-08bc49fb3dc7/2194530_nl_2019.pdf" download="" class="link link--icon link--icon-position-before file__download link--primary file__download" aria-label="Download namefile" title="Download dit bestand" target="_blank">
<a href="{% url 'berichten:download-bericht-attachment' object_uuid=bericht.object_uuid %}?url={{ attachment|urlencode }}" download="" class="link link--icon link--icon-position-before file__download link--primary file__download" aria-label="Download namefile" title="Download dit bestand" target="_blank">
{% icon "download" outlined=True %}
</a>
</div>
Expand Down
Loading