Skip to content

Commit

Permalink
fix(forum_attachment) catch missing file error
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 21, 2023
1 parent 4291adf commit 9af14ff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
{% if post.attachments.exists and user_can_download_files %}
<div class="row attachments mt-3 mt-md-5">
{% for attachment in post.attachments.all %}
{% if not attachment|is_image %}
<div class="col-md-12 attachment">
<a href="{% url 'forum_conversation:attachment' pk=attachment.id %}"><i class="fa fa-file"></i>&nbsp;{{ attachment.filename }} ({{ attachment.file.size|filesizeformat }})</a>
{% if attachment.comment %}
<p class="text-muted"><em>{{ attachment.comment }}</em></p>
{% endif %}
</div>
{% endif%}
{% if attachment|is_available %}
{% if not attachment|is_image %}
<div class="col-md-12 attachment">
<a href="{% url 'forum_conversation:attachment' pk=attachment.id %}"><i class="fa fa-file"></i>&nbsp;{{ attachment.filename }} ({{ attachment.file.size|filesizeformat }})</a>
{% if attachment.comment %}
<p class="text-muted"><em>{{ attachment.comment }}</em></p>
{% endif %}
</div>
{% endif%}
{% else %}
<p class="text-muted"><i class="ri-file-damage-fill"></i>&nbsp;{{ attachment.filename }}</p>
{% endif %}
{% endfor %}
</div>
{% endif %}
5 changes: 5 additions & 0 deletions lacommunaute/utils/templatetags/attachments_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ def is_image(object):
return True
else:
return False


@register.filter
def is_available(object):
return object.file.field.storage.exists(object.file.name)
30 changes: 30 additions & 0 deletions lacommunaute/utils/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime, timedelta
from unittest.mock import patch

from django.core.files.storage import default_storage
from django.core.files.uploadedfile import SimpleUploadedFile
from django.template import Context, Template
from django.template.defaultfilters import date, time
Expand Down Expand Up @@ -64,6 +65,35 @@ def test_is_not_an_image(self):
)
self.assertEqual(out, "False")

@override_settings(DEFAULT_FILE_STORAGE="django.core.files.storage.FileSystemStorage")
def test_is_available(self):
f = SimpleUploadedFile("test.png", force_bytes("file_content"))
attachment = AttachmentFactory(post=self.post, file=f)

out = Template("{% load attachments_tags %}" "{{ attachment|is_available }}").render(
Context(
{
"attachment": attachment,
}
)
)
self.assertEqual(out, "True")

@override_settings(DEFAULT_FILE_STORAGE="django.core.files.storage.FileSystemStorage")
def test_is_not_available(self):
f = SimpleUploadedFile("test.png", force_bytes("file_content"))
attachment = AttachmentFactory(post=self.post, file=f)

with patch.object(default_storage, "exists", return_value=False):
out = Template("{% load attachments_tags %}" "{{ attachment|is_available }}").render(
Context(
{
"attachment": attachment,
}
)
)
self.assertEqual(out.strip(), "False")


class SettingsContextProcessorsTest(TestCase):
@override_settings(ALLOWED_HOSTS=["allowed.com"])
Expand Down

0 comments on commit 9af14ff

Please sign in to comment.