Skip to content
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
8 changes: 8 additions & 0 deletions hypha/apply/funds/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import UserPassesTestMixin
from django.http import Http404
from django.shortcuts import get_object_or_404, render
from django.utils.decorators import method_decorator
from django_filters.views import FilterView
Expand Down Expand Up @@ -56,6 +57,13 @@ def get_media(self, *args, **kwargs):
path_to_file = generate_private_file_path(
self.submission.pk, field_id, file_name
)
try:
if not self.storage.exists(path_to_file):
raise Http404
except Http404:
raise
except Exception as e:
raise Http404 from e
return self.storage.open(path_to_file)

def test_func(self):
Expand Down
14 changes: 12 additions & 2 deletions hypha/apply/funds/views/submission_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,23 @@ def get_form_kwargs(self):
def get_placeholder_file(self, initial_file):
if not isinstance(initial_file, list):
return PlaceholderUploadedFile(
initial_file.filename, size=initial_file.size, file_id=initial_file.name
initial_file.filename,
size=self._safe_file_size(initial_file),
file_id=initial_file.name,
)
return [
PlaceholderUploadedFile(f.filename, size=f.size, file_id=f.name)
PlaceholderUploadedFile(
f.filename, size=self._safe_file_size(f), file_id=f.name
)
for f in initial_file
]

def _safe_file_size(self, stream_file):
try:
return stream_file.storage.size(stream_file.name)
except Exception:
return 0

def save_draft_and_refresh_page(self, form) -> HttpResponseRedirect:
self.object.create_revision(draft=True, by=self.request.user)
form.delete_temporary_files()
Expand Down
26 changes: 19 additions & 7 deletions hypha/apply/stream_forms/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,27 @@ def size(self):
return self.file.size
return self.storage.size(self.name)

def _check_exists(self):
"""Fetch modification time once; caches both existence and the date."""
if not hasattr(self, "_exists_cache"):
try:
self._modification_time_cache = self.storage.get_modified_time(
self.name
).date()
self._exists_cache = True
except Exception:
self._modification_time_cache = "–"
self._exists_cache = False

@property
def modification_time(self):
# Wrap in a try for local developments where files might not always exist.
try:
modified_time = self.storage.get_modified_time(self.name).date()
except FileNotFoundError:
modified_time = "–"
def exists(self):
self._check_exists()
return self._exists_cache

return modified_time
@property
def modification_time(self):
self._check_exists()
return self._modification_time_cache

def serialize(self):
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
{% load i18n heroicons apply_tags %}

<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"
href="{{ file.url }}"
target="_blank"
rel="noopener noreferrer"
title="View file - {{ file.filename }}"
>
<span class="text-sm truncate">
{% heroicon_mini "document-text" class="inline align-text-bottom" aria_hidden=true %}
{{ file.filename|truncatechars_middle:45 }}
<span class="text-xs text-fg-muted">
<span class="sr-only">{% trans "uploaded" %}</span>
<span aria-hidden>•</span>
<relative-time datetime={{ file.modification_time|date:"c" }}>{{ file.modification_time|date:"SHORT_DATE_FORMAT" }}</relative-time>
{% if file.exists %}
<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"
href="{{ file.url }}"
target="_blank"
rel="noopener noreferrer"
title="View file - {{ file.filename }}"
>
<span class="text-sm truncate">
{% heroicon_mini "document-text" class="inline align-text-bottom" aria_hidden=true %}
{{ file.filename|truncatechars_middle:45 }}
<span class="text-xs text-fg-muted">
<span class="sr-only">{% trans "uploaded" %}</span>
<span aria-hidden>•</span>
<relative-time datetime={{ file.modification_time|date:"c" }}>{{ file.modification_time|date:"SHORT_DATE_FORMAT" }}</relative-time>
</span>
</span>
</span>
<span>
{% heroicon_mini "arrow-down" class="w-5 h-5" stoke_width=2 aria_hidden=true %}
</span>
</a>
<span>
{% heroicon_mini "arrow-down" class="w-5 h-5" stoke_width=2 aria_hidden=true %}
</span>
</a>
{% else %}
<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"
title="{% trans "File not found" %} - {{ file.filename }}"
>
<span class="text-sm truncate">
{% heroicon_mini "document-text" class="inline align-text-bottom" aria_hidden=true %}
{{ file.filename|truncatechars_middle:45 }}
<span class="text-xs text-error">
<span aria-hidden>•</span>
{% trans "Missing file" %}
</span>
</span>
<span>
{% heroicon_mini "exclamation-circle" class="w-5 h-5 text-error" aria_hidden=true %}
</span>
</div>
{% endif %}
Loading