Skip to content

Commit baedd04

Browse files
committed
Update news add to use image compression
1 parent de75c12 commit baedd04

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

news/forms.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from django import forms
2+
from django.conf import settings
23
from .models import BlogPost, Entry, Link, News, Poll, Video
4+
from .utils import downsize_uploaded_image
35

46

57
class EntryForm(forms.ModelForm):
@@ -9,6 +11,12 @@ class Meta:
911
model = Entry
1012
fields = ["title", "publish_at", "content", "image"]
1113

14+
def clean_image(self):
15+
image = self.cleaned_data.get("image", None)
16+
if image and image.size > settings.DOWNSCALE_IMAGE_THRESHOLD:
17+
return downsize_uploaded_image(image)
18+
return image
19+
1220
def save(self, *args, commit=True, **kwargs):
1321
instance = super().save(*args, commit=False, **kwargs)
1422
# Automatically approve unapproved news that do not require moderation

news/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
from core.validators import (
1414
attachment_validator,
15-
image_validator,
16-
max_file_size_validator,
1715
large_file_max_size_validator,
1816
)
1917

@@ -85,7 +83,7 @@ class AlreadyApprovedError(Exception):
8583
upload_to="news/%Y/%m/",
8684
null=True,
8785
blank=True,
88-
validators=[image_validator, max_file_size_validator],
86+
validators=[],
8987
)
9088
created_at = models.DateTimeField(default=now)
9189
approved_at = models.DateTimeField(null=True, blank=True)

0 commit comments

Comments
 (0)