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

View documents in new tab #31

Merged
merged 2 commits into from
Sep 18, 2024
Merged
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
52 changes: 34 additions & 18 deletions app/web/cases/templates/cases/case_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,44 @@ <h2 class="h3">Bijlagen <small>({{ object.document_set.all|length }})</small></h
{% for document in object.document_set.all %}
{% if forloop.counter0 < 4 %}
<li>
<div><a href="{% url 'download_case_document' object.id document.id %}" target="_blank" rel="noopener noreferrer"><span>{{ document.name }}</span></a> <span>{{ document.extension }}</span><span> {{ document.uploaded|timezone:FRONTEND_TIMEZONE|date:DATE_FORMAT }} </span></div>
{% if not object.delete_request_date %}
<div>
<a href="{% url 'view_case_document' object.id document.id %}" target="_blank" rel="noopener noreferrer">
<span>{{ document.name }}</span>
</a>
<span>{{ document.extension }}</span>
<span> {{ document.uploaded|timezone:FRONTEND_TIMEZONE|date:DATE_FORMAT }} </span>
</div>

<button type="button" title="Bijlage aanpassen" class="text-link u-margin-top-none" data-handler="modal" data-contentid="modalDocumentUpdate-{{ document.id }}">
<span><svg class="button__icon" width="20" height="20" >
<use href="#edit-document" xlink:href="#edit-document" width="20" height="20"></use>
</svg></span>
</button>
<div role="dialog" class="modal" id="modalDocumentUpdate-{{ document.id }}">
<iframe class="modal--update-document" id="iFrameDocumentUpdate-{{ document.id }}" data-src="{% url 'update_case_document' object.id document.id %}" src="about:blank" title="Bijlage {{ document.name }} aanpassen"></iframe>
</div>
<button type="button" title="Bijlage verwijderen" class="text-link u-margin-top-none" data-handler="modal" data-contentid="modalDocumentDelete-{{ document.id }}">
<svg class="button__icon" width="20" height="20" >
<use href="#trash" xlink:href="#trash" width="20" height="20"></use>
</svg>
</button>
<div role="dialog" class="modal" id="modalDocumentDelete-{{ document.id }}">
<iframe class="modal--delete-document" id="iFrameDocumentDelete-{{ document.id }}" data-src="{% url 'delete_case_document' object.id document.id %}" src="about:blank" title="Verwijder bijlage {{ document.name }}"></iframe>
{% if not object.delete_request_date %}
<div>
<a class="grid__wrapper" href="{% url 'download_case_document' object.id document.id %}" target="_blank" rel="noopener noreferrer">
<button type="button" title="Bijlage downloaden" class="text-link u-margin-top-none">
<span>
<svg class="button__icon" width="20" height="20" >
<use href="#download" xlink:href="#download" width="20" height="20"></use>
</svg>
</span>
</button>
</a>
<button type="button" title="Bijlage aanpassen" class="text-link u-margin-top-none" data-handler="modal" data-contentid="modalDocumentUpdate-{{ document.id }}">
<span><svg class="button__icon" width="20" height="20" >
<use href="#edit-document" xlink:href="#edit-document" width="20" height="20"></use>
</svg></span>
</button>
<div role="dialog" class="modal" id="modalDocumentUpdate-{{ document.id }}">
<iframe class="modal--update-document" id="iFrameDocumentUpdate-{{ document.id }}" data-src="{% url 'update_case_document' object.id document.id %}" src="about:blank" title="Bijlage {{ document.name }} aanpassen"></iframe>
</div>
<button type="button" title="Bijlage verwijderen" class="text-link u-margin-top-none" data-handler="modal" data-contentid="modalDocumentDelete-{{ document.id }}">
<svg class="button__icon" width="20" height="20" >
<use href="#trash" xlink:href="#trash" width="20" height="20"></use>
</svg>
</button>
<div role="dialog" class="modal" id="modalDocumentDelete-{{ document.id }}">
<iframe class="modal--delete-document" id="iFrameDocumentDelete-{{ document.id }}" data-src="{% url 'delete_case_document' object.id document.id %}" src="about:blank" title="Verwijder bijlage {{ document.name }}"></iframe>
</div>
</div>
</div>
{% endif %}

</li>
{% endif %}
{% endfor %}
Expand Down
1 change: 1 addition & 0 deletions app/web/cases/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
path('<int:case_pk>/verwijder-bijlage/<int:pk>/', DocumentDelete.as_view(), name='delete_case_document'),

path('<int:case_pk>/download-bijlage/<int:document_pk>', download_document, name='download_case_document'),
path('<int:case_pk>/bekijk-bijlage/<int:document_pk>', view_document, name='view_case_document'),
]
46 changes: 37 additions & 9 deletions app/web/cases/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from operator import or_
from django.utils import timezone
from django.http.response import HttpResponse
import mimetypes

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -1263,13 +1264,19 @@ def post(self, request, *args, **kwargs):
return response


@user_passes_test(auth_test, user_type=[WONEN, BEGELEIDER, PB_FEDERATIE_BEHEERDER, WONINGCORPORATIE_MEDEWERKER])
def download_document(request, case_pk, document_pk):
def get_document_for_case(request, case_pk, document_pk):
"""
Utility function to retrieve and validate the document.
"""
qs = Case._default_manager.by_user(user=request.user)
case = qs.filter(pk=case_pk).first()

if not case:
raise PermissionDenied

document = get_object_or_404(Document, id=document_pk)

# Check permissions
if any(user_type in [WONEN, WONINGCORPORATIE_MEDEWERKER] for user_type in request.user.user_type_values):
form_status_list = [f[0] for f in case.casestatus_set.all().order_by('form').distinct().values_list('form')]
shared_in_forms = [f for f in document.forms if f in form_status_list]
Expand All @@ -1279,21 +1286,42 @@ def download_document(request, case_pk, document_pk):
if document.case != case:
raise PermissionDenied

# Check if file exists in storage
if not default_storage.exists(default_storage.generate_filename(document.uploaded_file.name)):
raise Http404()

with default_storage.open(document.uploaded_file.name, 'rb') as file:
response = HttpResponse(file.read(), content_type='application/octet-stream')
response['Content-Disposition'] = f'attachment; filename="{document.uploaded_file.name}"'
return response



return document


def serve_document(document, disposition_type):
"""
Utility function to serve a document with a specified Content-Disposition type.
"""
with default_storage.open(document.uploaded_file.name, 'rb') as file:
file_data = file.read()

# Guess the MIME type of the file
content_type = mimetypes.guess_type(document.uploaded_file.name)[0] or 'application/octet-stream'

response = HttpResponse(file_data, content_type=content_type)
response['Content-Disposition'] = f'{disposition_type}; filename="{document.uploaded_file.name}"'

return response


@user_passes_test(auth_test, user_type=[WONEN, BEGELEIDER, PB_FEDERATIE_BEHEERDER, WONINGCORPORATIE_MEDEWERKER])
def download_document(request, case_pk, document_pk):
# Retrieve and validate the document
document = get_document_for_case(request, case_pk, document_pk)

# Serve the document as an attachment (download)
return serve_document(document, 'attachment')


@user_passes_test(auth_test, user_type=[WONEN, BEGELEIDER, PB_FEDERATIE_BEHEERDER, WONINGCORPORATIE_MEDEWERKER])
def view_document(request, case_pk, document_pk):
# Retrieve and validate the document
document = get_document_for_case(request, case_pk, document_pk)

# Serve the document inline (for viewing)
return serve_document(document, 'inline')
Loading