From 201c0c9e70c07a4effe1d74751ae5d9151c5f399 Mon Sep 17 00:00:00 2001 From: Jarand Jensen Romestrand Date: Tue, 26 Sep 2023 18:32:16 +0200 Subject: [PATCH] Added model, serializer and viewset for model News Reaction --- .../migrations/0054_remove_news_reactions.py | 17 +++++++++++++++ app/content/models/news.py | 7 ------- app/emoji/models/news_reaction.py | 21 +++++++++++++++++++ app/emoji/serializers/news_reaction.py | 11 ++++++++++ app/emoji/views/news_reaction.py | 0 5 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 app/content/migrations/0054_remove_news_reactions.py create mode 100644 app/emoji/models/news_reaction.py create mode 100644 app/emoji/serializers/news_reaction.py create mode 100644 app/emoji/views/news_reaction.py diff --git a/app/content/migrations/0054_remove_news_reactions.py b/app/content/migrations/0054_remove_news_reactions.py new file mode 100644 index 000000000..498274efd --- /dev/null +++ b/app/content/migrations/0054_remove_news_reactions.py @@ -0,0 +1,17 @@ +# Generated by Django 4.0.8 on 2023-09-26 16:11 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('content', '0053_news_emojis_allowed_news_reactions'), + ] + + operations = [ + migrations.RemoveField( + model_name='news', + name='reactions', + ), + ] diff --git a/app/content/models/news.py b/app/content/models/news.py index ea8715b53..8bb199728 100644 --- a/app/content/models/news.py +++ b/app/content/models/news.py @@ -19,13 +19,6 @@ class News(BaseModel, OptionalImage, BasePermissionModel): ) body = models.TextField() emojis_allowed = models.BooleanField(default=False) - reactions = models.ForeignKey( - Reaction, - on_delete=models.SET_NULL, - blank=True, - null=True, - related_name="reactions", - ) write_access = AdminGroup.all() diff --git a/app/emoji/models/news_reaction.py b/app/emoji/models/news_reaction.py new file mode 100644 index 000000000..fe2820671 --- /dev/null +++ b/app/emoji/models/news_reaction.py @@ -0,0 +1,21 @@ +from django.db import models + +from app.common.enums import Groups +from app.common.permissions import BasePermissionModel +from app.content.models.news import News +from app.emoji.models.reaction import Reaction +from app.util.models import BaseModel + + +class NewsReaction(BaseModel, BasePermissionModel): + reaction = models.ForeignKey(Reaction, on_delete=models.CASCADE) + news = models.ForeignKey(News, on_delete=models.CASCADE) + + write_access = [Groups.TIHLDE] + + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["reaction", "news"], name="unique together: reaction and news" + ) + ] diff --git a/app/emoji/serializers/news_reaction.py b/app/emoji/serializers/news_reaction.py new file mode 100644 index 000000000..34a44fb3e --- /dev/null +++ b/app/emoji/serializers/news_reaction.py @@ -0,0 +1,11 @@ +from app.common.serializers import BaseModelSerializer +from app.emoji.models.news_reaction import NewsReaction + + +class UserNewsReactionSerializer(BaseModelSerializer): + class Meta: + model = NewsReaction + fields = ( + "reaction", + "news", + ) diff --git a/app/emoji/views/news_reaction.py b/app/emoji/views/news_reaction.py new file mode 100644 index 000000000..e69de29bb