diff --git a/lacommunaute/forum/factories.py b/lacommunaute/forum/factories.py index 4b1428622..eaa77bad4 100644 --- a/lacommunaute/forum/factories.py +++ b/lacommunaute/forum/factories.py @@ -17,6 +17,9 @@ class ForumFactory(BaseForumFactory): class Meta: skip_postgeneration_save = True + class Params: + with_image = factory.Trait(image=factory.django.ImageField(filename="banner.jpg")) + @factory.post_generation def with_public_perms(self, create, extracted, **kwargs): if not create or not extracted: @@ -41,4 +44,4 @@ def with_child(self, create, extracted, **kwargs): if not create or not extracted: return - ForumFactory(parent=self, with_public_perms=True) + ForumFactory(parent=self, with_public_perms=True, with_image=True) diff --git a/lacommunaute/forum/migrations/0013_alter_forum_image.py b/lacommunaute/forum/migrations/0013_alter_forum_image.py new file mode 100644 index 000000000..0991a4959 --- /dev/null +++ b/lacommunaute/forum/migrations/0013_alter_forum_image.py @@ -0,0 +1,27 @@ +# Generated by Django 5.0.6 on 2024-05-21 14:55 + +import storages.backends.s3 +from django.conf import settings +from django.db import migrations, models + +import lacommunaute.forum.models + + +class Migration(migrations.Migration): + dependencies = [ + ("forum", "0012_forum_short_description_alter_forum_kind"), + ] + + operations = [ + migrations.AlterField( + model_name="forum", + name="image", + field=models.ImageField( + storage=storages.backends.s3.S3Storage( + bucket_name=settings.AWS_STORAGE_BUCKET_NAME, file_overwrite=False + ), + upload_to="", + validators=[lacommunaute.forum.models.validate_image_size], + ), + ), + ] diff --git a/lacommunaute/forum/models.py b/lacommunaute/forum/models.py index 9b591ac5c..1e5abedde 100644 --- a/lacommunaute/forum/models.py +++ b/lacommunaute/forum/models.py @@ -1,15 +1,18 @@ import uuid +from django.conf import settings from django.contrib.auth.models import Group from django.contrib.contenttypes.fields import GenericRelation from django.db import models from django.urls import reverse from django.utils.functional import cached_property from machina.apps.forum.abstract_models import AbstractForum +from storages.backends.s3boto3 import S3Boto3Storage from lacommunaute.forum.enums import Kind as Forum_Kind from lacommunaute.forum_conversation.models import Topic from lacommunaute.forum_upvote.models import UpVote +from lacommunaute.utils.validators import validate_image_size class ForumQuerySet(models.QuerySet): @@ -28,6 +31,10 @@ class Forum(AbstractForum): short_description = models.CharField( max_length=400, blank=True, null=True, verbose_name="Description courte (SEO)" ) + image = models.ImageField( + storage=S3Boto3Storage(bucket_name=settings.AWS_STORAGE_BUCKET_NAME, file_overwrite=False), + validators=[validate_image_size], + ) upvotes = GenericRelation(UpVote, related_query_name="forum") diff --git a/lacommunaute/forum/tests/tests_model.py b/lacommunaute/forum/tests/tests_model.py index 1c887068f..d683a3026 100644 --- a/lacommunaute/forum/tests/tests_model.py +++ b/lacommunaute/forum/tests/tests_model.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.db import IntegrityError from django.test import TestCase @@ -57,3 +58,14 @@ def test_upvotes_count(self): self.assertEqual(forum.upvotes_count(), 0) forum.upvotes.create(voter=UserFactory()) self.assertEqual(forum.upvotes_count(), 1) + + def test_image_is_imagefield(self): + forum = ForumFactory() + self.assertEqual(forum.image.field.__class__.__name__, "ImageField") + + def test_image_url(self): + forum = ForumFactory(image="test.jpg") + self.assertEqual( + forum.image.url.split("?")[0], f"{settings.MEDIA_URL}{settings.AWS_STORAGE_BUCKET_NAME}/{forum.image.name}" + ) + self.assertIn("AWSAccessKeyId=", forum.image.url)