Skip to content

Commit 1da45d6

Browse files
committed
Merge branch 'enhancement/missing-file-notice-not-500' into test
2 parents 637b866 + f99b034 commit 1da45d6

4 files changed

Lines changed: 72 additions & 28 deletions

File tree

hypha/apply/funds/views/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.contrib.auth.decorators import login_required
22
from django.contrib.auth.mixins import UserPassesTestMixin
3+
from django.http import Http404
34
from django.shortcuts import get_object_or_404, render
45
from django.utils.decorators import method_decorator
56
from django_filters.views import FilterView
@@ -56,7 +57,10 @@ def get_media(self, *args, **kwargs):
5657
path_to_file = generate_private_file_path(
5758
self.submission.pk, field_id, file_name
5859
)
59-
return self.storage.open(path_to_file)
60+
try:
61+
return self.storage.open(path_to_file)
62+
except Exception as e:
63+
raise Http404 from e
6064

6165
def test_func(self):
6266
permission, _ = has_permission(

hypha/apply/funds/views/submission_edit.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,23 @@ def get_form_kwargs(self):
253253
def get_placeholder_file(self, initial_file):
254254
if not isinstance(initial_file, list):
255255
return PlaceholderUploadedFile(
256-
initial_file.filename, size=initial_file.size, file_id=initial_file.name
256+
initial_file.filename,
257+
size=self._safe_file_size(initial_file),
258+
file_id=initial_file.name,
257259
)
258260
return [
259-
PlaceholderUploadedFile(f.filename, size=f.size, file_id=f.name)
261+
PlaceholderUploadedFile(
262+
f.filename, size=self._safe_file_size(f), file_id=f.name
263+
)
260264
for f in initial_file
261265
]
262266

267+
def _safe_file_size(self, stream_file):
268+
try:
269+
return stream_file.storage.size(stream_file.name)
270+
except Exception:
271+
return 0
272+
263273
def save_draft_and_refresh_page(self, form) -> HttpResponseRedirect:
264274
self.object.create_revision(draft=True, by=self.request.user)
265275
form.delete_temporary_files()

hypha/apply/stream_forms/files.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,27 @@ def size(self):
8282
return self.file.size
8383
return self.storage.size(self.name)
8484

85+
def _check_exists(self):
86+
"""Fetch modification time once; caches both existence and the date."""
87+
if not hasattr(self, "_exists_cache"):
88+
try:
89+
self._modification_time_cache = self.storage.get_modified_time(
90+
self.name
91+
).date()
92+
self._exists_cache = True
93+
except Exception:
94+
self._modification_time_cache = "–"
95+
self._exists_cache = False
96+
8597
@property
86-
def modification_time(self):
87-
# Wrap in a try for local developments where files might not always exist.
88-
try:
89-
modified_time = self.storage.get_modified_time(self.name).date()
90-
except FileNotFoundError:
91-
modified_time = "–"
98+
def exists(self):
99+
self._check_exists()
100+
return self._exists_cache
92101

93-
return modified_time
102+
@property
103+
def modification_time(self):
104+
self._check_exists()
105+
return self._modification_time_cache
94106

95107
def serialize(self):
96108
return {
Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
{% load i18n heroicons apply_tags %}
22

3-
<a class="flex justify-between items-center py-2 px-3 mb-2 max-w-2xl font-medium rounded-sm border transition-colors not-prose bg-base-200 hover:bg-base-300"
4-
href="{{ file.url }}"
5-
target="_blank"
6-
rel="noopener noreferrer"
7-
title="View file - {{ file.filename }}"
8-
>
9-
<span class="text-sm truncate">
10-
{% heroicon_mini "document-text" class="inline align-text-bottom" aria_hidden=true %}
11-
{{ file.filename|truncatechars_middle:45 }}
12-
<span class="text-xs text-fg-muted">
13-
<span class="sr-only">{% trans "uploaded" %}</span>
14-
<span aria-hidden></span>
15-
<relative-time datetime={{ file.modification_time|date:"c" }}>{{ file.modification_time|date:"SHORT_DATE_FORMAT" }}</relative-time>
3+
{% if file.exists %}
4+
<a class="flex justify-between items-center py-2 px-3 mb-2 max-w-2xl font-medium rounded-sm border transition-colors not-prose bg-base-200 hover:bg-base-300"
5+
href="{{ file.url }}"
6+
target="_blank"
7+
rel="noopener noreferrer"
8+
title="View file - {{ file.filename }}"
9+
>
10+
<span class="text-sm truncate">
11+
{% heroicon_mini "document-text" class="inline align-text-bottom" aria_hidden=true %}
12+
{{ file.filename|truncatechars_middle:45 }}
13+
<span class="text-xs text-fg-muted">
14+
<span class="sr-only">{% trans "uploaded" %}</span>
15+
<span aria-hidden></span>
16+
<relative-time datetime={{ file.modification_time|date:"c" }}>{{ file.modification_time|date:"SHORT_DATE_FORMAT" }}</relative-time>
17+
</span>
1618
</span>
17-
</span>
18-
<span>
19-
{% heroicon_mini "arrow-down" class="w-5 h-5" stoke_width=2 aria_hidden=true %}
20-
</span>
21-
</a>
19+
<span>
20+
{% heroicon_mini "arrow-down" class="w-5 h-5" stoke_width=2 aria_hidden=true %}
21+
</span>
22+
</a>
23+
{% else %}
24+
<div class="flex justify-between items-center py-2 px-3 mb-2 max-w-2xl font-medium rounded-sm border opacity-60 not-prose bg-base-200"
25+
title="{% trans "File not found" %} - {{ file.filename }}"
26+
>
27+
<span class="text-sm truncate">
28+
{% heroicon_mini "document-text" class="inline align-text-bottom" aria_hidden=true %}
29+
{{ file.filename|truncatechars_middle:45 }}
30+
<span class="text-xs text-error">
31+
<span aria-hidden></span>
32+
{% trans "Missing file" %}
33+
</span>
34+
</span>
35+
<span>
36+
{% heroicon_mini "exclamation-circle" class="w-5 h-5 text-error" aria_hidden=true %}
37+
</span>
38+
</div>
39+
{% endif %}

0 commit comments

Comments
 (0)