Skip to content

Commit

Permalink
Support release report pdf attachment to news
Browse files Browse the repository at this point in the history
  • Loading branch information
GregKaleka committed Dec 11, 2024
1 parent eb60cd3 commit 74d6109
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __call__(self, value):


image_validator = FileTypeValidator(extensions=[".jpg", ".jpeg", ".png"])
pdf_validator = FileTypeValidator(extensions=[".pdf"])


@deconstructible
Expand All @@ -42,3 +43,6 @@ def __call__(self, value):

# 1 MB max file size
max_file_size_validator = MaxFileSizeValidator(max_size=1 * 1024 * 1024)

# 50 MB allowed for certain large files - to be used on staff-only fields
large_file_max_size_validator = MaxFileSizeValidator(max_size=50 * 1024 * 1024)
27 changes: 27 additions & 0 deletions news/migrations/0010_news_pdf_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.16 on 2024-12-11 21:17

import core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("news", "0009_alter_news_options"),
]

operations = [
migrations.AddField(
model_name="news",
name="pdf_attachment",
field=models.FileField(
blank=True,
null=True,
upload_to="news/files/%Y/%m/",
validators=[
core.validators.MaxFileSizeValidator(max_size=52428800),
core.validators.FileTypeValidator(extensions=[".pdf"]),
],
),
),
]
13 changes: 12 additions & 1 deletion news/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _

from core.validators import image_validator, max_file_size_validator
from core.validators import (
image_validator,
max_file_size_validator,
large_file_max_size_validator,
pdf_validator,
)

from . import acl

Expand Down Expand Up @@ -183,6 +188,12 @@ def author_needs_moderation(self):

class News(Entry):
news_type = "news"
pdf_attachment = models.FileField(
upload_to="news/files/%Y/%m/",
null=True,
blank=True,
validators=[large_file_max_size_validator, pdf_validator],
)

class Meta:
verbose_name = "News"
Expand Down
6 changes: 6 additions & 0 deletions templates/news/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ <h1 class="text-4xl">{{ entry.title }}</h1>
<div class="break-words">
{{ entry.content|urlize|url_target_blank:'text-sky-600 dark:text-sky-300 hover:text-orange dark:hover:text-orange'|linebreaks }}
</div>
{% if entry.news.pdf_attachment %}
<a href="{{ entry.news.pdf_attachment.url }}" class="text-sky-600 dark:text-sky-300 hover:text-orange dark:hover:text-orange">
{# Currently this field is only used for release reports. If that changes in the future, this text should be made dynamic. #}
Release Report
</a>
{% endif %}
</div>

</div>
Expand Down

0 comments on commit 74d6109

Please sign in to comment.