diff --git a/feed_posts/enums.py b/feed_posts/enums.py index 844b0163e2c25f0e2340db5a4ecf293cbb882e0b..67a750ab93a244df45b0733c6f783e84db484ddb 100644 --- a/feed_posts/enums.py +++ b/feed_posts/enums.py @@ -6,3 +6,9 @@ from django.db.models import TextChoices class VisibilityLevel(TextChoices): PUBLIC = ("PUBLIC",) REGISTERED_USERS_ONLY = ("REGISTERED_USERS_ONLY",) + + +@strawberry.enum +class AspectRatio(TextChoices): + SIXTEEN_NINE = ("SIXTEEN_NINE",) + ONE_ONE = ("ONE_ONE",) diff --git a/feed_posts/migrations/0016_feedpostimage_aspect_ratio.py b/feed_posts/migrations/0016_feedpostimage_aspect_ratio.py new file mode 100644 index 0000000000000000000000000000000000000000..2e31b4630c31ff59ca6d60218865fcd9aef7170c --- /dev/null +++ b/feed_posts/migrations/0016_feedpostimage_aspect_ratio.py @@ -0,0 +1,23 @@ +# Generated by Django 5.0.8 on 2024-09-23 09:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("feed_posts", "0015_alter_feedpost_language_code"), + ] + + operations = [ + migrations.AddField( + model_name="feedpostimage", + name="aspect_ratio", + field=models.CharField( + blank=True, + choices=[("SIXTEEN_NINE", "Sixteen Nine"), ("ONE_ONE", "One One")], + max_length=20, + null=True, + ), + ), + ] diff --git a/feed_posts/models.py b/feed_posts/models.py index 8e2d448c60bc31fc56d0650f440f8e77d4cdd366..0e1cee9ee5e35744665c1d2b1ae90b0b0a1996c6 100644 --- a/feed_posts/models.py +++ b/feed_posts/models.py @@ -13,7 +13,7 @@ from rest_framework.exceptions import ValidationError from comments.models import Comment from comments.models.commentable_notification_context import CommentNotificationMixin, CommentNotificationContext -from feed_posts.enums import VisibilityLevel +from feed_posts.enums import VisibilityLevel, AspectRatio from feed_posts.helpers import upload_to_feed_post_images_directory, upload_to_new_feed_post_images_directory from openbook import settings from openbook_auth.models import User, UserRole @@ -167,6 +167,7 @@ class FeedPostImage(ModelWithUUID): order = models.PositiveIntegerField() uploaded_at = models.DateTimeField(editable=False, auto_now_add=True) alt_text = models.TextField(max_length=settings.CONTENT_IMAGE_ALT_TEXT_MAX_LENGTH, blank=True, null=True) + aspect_ratio = models.CharField(max_length=20, choices=AspectRatio.choices, blank=True, null=True) class Meta: ordering = ["order"] diff --git a/feed_posts/repositories.py b/feed_posts/repositories.py index 5168d9fb74d94b6e2bed21923040a74e032d861d..a02d3ea72505a5c157774a1df70e892a913fe9ef 100644 --- a/feed_posts/repositories.py +++ b/feed_posts/repositories.py @@ -149,6 +149,7 @@ class FeedPostRepository: image=obj["image"], order=obj["order"], alt_text=obj.get("alt_text") or None, + aspect_ratio=obj.get("aspect_ratio") or None, ) image.save() @@ -177,6 +178,7 @@ class FeedPostRepository: if image_data: image.order = image_data.get("order", image.order) image.alt_text = image_data.get("alt_text", image.alt_text) + image.aspect_ratio = image_data.get("aspect_ratio", image.aspect_ratio) image.save() if new_images is not None: diff --git a/feed_posts/schema/types.py b/feed_posts/schema/types.py index f224c32d12a3296fcd84e125435c5d67ec8383a9..65309aad95e1af6d92a319b5ba523b67a7fdf317 100644 --- a/feed_posts/schema/types.py +++ b/feed_posts/schema/types.py @@ -6,7 +6,7 @@ import strawberry_django from strawberry.file_uploads import Upload from comments.models.enabled_commentable_type import EnabledCommentableType -from feed_posts.enums import VisibilityLevel +from feed_posts.enums import VisibilityLevel, AspectRatio from feed_posts.models import ( FeedPost as FeedPostModel, FeedPostReaction as FeedPostReactionModel, @@ -28,6 +28,7 @@ class FeedPostImage: image_blurhash: str order: int alt_text: Optional[str] + aspect_ratio: Optional[AspectRatio] @strawberry_django.type(FeedPostModel) @@ -106,6 +107,7 @@ class FeedPostImageInput: image: Upload order: int alt_text: Optional[str] = strawberry.UNSET + aspect_ratio: Optional[AspectRatio] = strawberry.UNSET @strawberry.input @@ -129,6 +131,7 @@ class FeedPostImageMetadata: id: uuid.UUID order: int alt_text: Optional[str] = strawberry.UNSET + aspect_ratio: Optional[AspectRatio] = strawberry.UNSET @strawberry.input diff --git a/feed_posts/serializers.py b/feed_posts/serializers.py index 3dfddc53a9b625a8f45f6ec3336fbd313744fc07..374aa769e55641ac6d5753f1085006dab903883a 100644 --- a/feed_posts/serializers.py +++ b/feed_posts/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from feed_posts.enums import VisibilityLevel +from feed_posts.enums import VisibilityLevel, AspectRatio from feed_posts.validators import ( validate_topic_exists_by_id, validate_feed_post_category_exists_by_id, @@ -23,6 +23,7 @@ class FeedPostImageSerializer(serializers.Serializer): alt_text = serializers.CharField( required=False, max_length=settings.CONTENT_IMAGE_ALT_TEXT_MAX_LENGTH, allow_blank=True ) + aspect_ratio = serializers.ChoiceField(required=False, choices=AspectRatio.choices) class FeedPostImageMetadataSerializer(serializers.Serializer): @@ -31,6 +32,7 @@ class FeedPostImageMetadataSerializer(serializers.Serializer): alt_text = serializers.CharField( required=False, max_length=settings.CONTENT_IMAGE_ALT_TEXT_MAX_LENGTH, allow_blank=True ) + aspect_ratio = serializers.ChoiceField(required=False, choices=AspectRatio.choices) class CreateFeedPostSerializer(serializers.Serializer): diff --git a/feed_posts/tests/helpers.py b/feed_posts/tests/helpers.py index 7c8b7f28f3c0bae85c498cff9b061a07c3d94072..cb99c26d5c34fc504cf3bb3bedcaf63604dfd4a7 100644 --- a/feed_posts/tests/helpers.py +++ b/feed_posts/tests/helpers.py @@ -40,6 +40,7 @@ def create_feed_post(user, input): image order altText + aspectRatio } } } @@ -84,6 +85,7 @@ def update_feed_post(user, input): image order altText + aspectRatio } } } diff --git a/feed_posts/tests/test_graphql.py b/feed_posts/tests/test_graphql.py index 09a9ed2c9efe4849e4b0f36153dfabb125482608..32d62f8b12418e242fcf5eba17085a6689a07dea 100644 --- a/feed_posts/tests/test_graphql.py +++ b/feed_posts/tests/test_graphql.py @@ -8,7 +8,7 @@ from django.core.files.uploadedfile import SimpleUploadedFile from django.test import AsyncClient, TestCase from faker import Faker -from feed_posts.enums import VisibilityLevel +from feed_posts.enums import VisibilityLevel, AspectRatio from feed_posts.repositories import FeedPostRepository from feed_posts.tests.helpers import ( create_feed_post, @@ -150,6 +150,7 @@ class TestFeedPosts(TestCase): "image": SimpleUploadedFile(name="image_2.jpg", content=simple_jpeg_bytes), "order": 1, "altText": "image description", + "aspectRatio": AspectRatio.ONE_ONE, } ], "location": "Munich, Germany", @@ -171,6 +172,7 @@ class TestFeedPosts(TestCase): "image": SimpleUploadedFile(name="image_3.jpg", content=simple_jpeg_bytes), "order": 0, "altText": "image description", + "aspectRatio": AspectRatio.ONE_ONE, } ], "location": "Munich, Germany", @@ -421,7 +423,14 @@ class TestFeedPosts(TestCase): post_id = mutation_response.data["createFeedPost"]["id"] images = mutation_response.data["createFeedPost"]["images"] - existing_images = [{"id": images[0]["id"], "order": 9, "altText": "new image description"}] + existing_images = [ + { + "id": images[0]["id"], + "order": 9, + "altText": "new image description", + "aspectRatio": AspectRatio.SIXTEEN_NINE, + } + ] input_data = update_input_data( post_id=post_id, input_data=self.registered_users_only_feed_post_update_input_data,