Skip to content

Commit

Permalink
Fixed some bugs where content_type was not saved
Browse files Browse the repository at this point in the history
  • Loading branch information
JarandJR committed Sep 28, 2023
1 parent bfdb1c9 commit e98663f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 25 deletions.
20 changes: 20 additions & 0 deletions app/emoji/migrations/0004_alter_reaction_unique_together.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.0.8 on 2023-09-28 15:05

from django.conf import settings
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('contenttypes', '0002_remove_content_type_name'),
('emoji', '0003_rename_id_reaction_reaction_id'),
]

operations = [
migrations.AlterUniqueTogether(
name='reaction',
unique_together={('user', 'object_id', 'content_type')},
),
]
1 change: 1 addition & 0 deletions app/emoji/models/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Reaction(BaseModel, BasePermissionModel):
read_access = [Groups.TIHLDE]

class Meta:
unique_together = ("user", "object_id", "content_type")
verbose_name = "Reaction"
verbose_name_plural = "Reactions"

Expand Down
5 changes: 4 additions & 1 deletion app/emoji/serializers/reaction.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from django.contrib.contenttypes.models import ContentType
from rest_framework import serializers

from app.common.serializers import BaseModelSerializer
from app.emoji.models.reaction import Reaction


class ReactionSerializer(BaseModelSerializer):
content_type = serializers.CharField(source="content_type.model", read_only=True)
content_type = serializers.PrimaryKeyRelatedField(
queryset=ContentType.objects.all()
)
object_id = serializers.IntegerField()

class Meta:
Expand Down
80 changes: 56 additions & 24 deletions app/emoji/views/reaction.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.response import Response
Expand All @@ -11,16 +12,23 @@


class ReactionViewSet(BaseViewSet):

serializer_class = ReactionSerializer
permission_classes = [BasicViewPermission]
pagination_class = BasePagination

def create(self, request, *args, **kwargs):
content_type = request.data.get("content_type")
content_type_str = request.data.get("content_type")
try:
content_type = ContentType.objects.get(model=content_type_str)
except ContentType.DoesNotExist:
return Response(
{"detail": "Fant ikke content type"},
status=status.HTTP_400_BAD_REQUEST,
)

if content_type == "news":
if content_type_str == "news":
content_object = get_object_or_404(News, id=request.data.get("object_id"))

if not content_object.emojis_allowed:
return Response(
{"detail": "Reaksjoner er ikke tillatt her."},
Expand All @@ -31,18 +39,35 @@ def create(self, request, *args, **kwargs):
{"detail": "Content type er ikke støttet"},
status=status.HTTP_400_BAD_REQUEST,
)
serializer = ReactionSerializer(data=request.data, context={"request": request})
if serializer.is_valid():
super().perform_create(serializer)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(
{"detail": "Du har ikke tillattelse til å reagere"},
status=status.HTTP_400_BAD_REQUEST,
)
try:
request.data["content_type"] = content_type.id
serializer = ReactionSerializer(
data=request.data, context={"request": request}
)
if serializer.is_valid():
super().perform_create(serializer)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(
{"detail": "Du har ikke tillattelse til å reagere"},
status=status.HTTP_403_FORBIDDEN,
)
except ValueError:
return Response(
{"detail": "Klarte ikke lagre reaksjon"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

def update(self, request, *args, **kwargs):
content_type = request.data.get("content_type")
if content_type == "news":
content_type_str = request.data.get("content_type")
try:
content_type = ContentType.objects.get(model=content_type_str)
except ContentType.DoesNotExist:
return Response(
{"detail": "Fant ikke content type"},
status=status.HTTP_400_BAD_REQUEST,
)

if content_type_str == "news":
content_object = get_object_or_404(News, id=request.data.get("object_id"))

if not content_object.emojis_allowed:
Expand All @@ -56,19 +81,26 @@ def update(self, request, *args, **kwargs):
status=status.HTTP_400_BAD_REQUEST,
)

reaction = self.get_object()
serializer = ReactionSerializer(
reaction, data=request.data, context={"request": request}
)
try:
reaction = self.get_object()
request.data["content_type"] = content_type.id
serializer = ReactionSerializer(
reaction, data=request.data, context={"request": request}
)

if serializer.is_valid():
super().perform_update(serializer)
return Response(serializer.data, status=status.HTTP_200_OK)
if serializer.is_valid():
super().perform_update(serializer)
return Response(serializer.data, status=status.HTTP_200_OK)

return Response(
{"detail": "Du har ikke tillatelse til å endre reaksjon"},
status=status.HTTP_400_BAD_REQUEST,
)
return Response(
{"detail": "Du har ikke tillatelse til å endre reaksjon"},
status=status.HTTP_400_BAD_REQUEST,
)
except ValueError:
return Response(
{"detail": "Klarte ikke lagre reaksjon"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

def get_queryset(self):
queryset = Reaction.objects.all()
Expand Down
21 changes: 21 additions & 0 deletions app/tests/emoji/test_reaction_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ def test_that_a_member_can_change_reaction_on_news(news_reaction):
assert response.status_code == status.HTTP_200_OK


@pytest.mark.django_db
def test_that_a_member_can_not_react_on_news_more_than_once(news_reaction):
"""A member should not be able to leave more than one reaction on the same news page"""
url = _get_reactions_url()
client = get_api_client(user=news_reaction.user)
data = _get_reactions_put_data(news_reaction)
response = client.post(url, data)

assert response.status_code == status.HTTP_403_FORBIDDEN


@pytest.mark.django_db
def test_that_a_member_can_remove_their_reaction_on_a_news(news_reaction):
"""A member should be able to remove their reaction on a news page"""
url = _get_reactions_detailed_url(news_reaction)
client = get_api_client(user=news_reaction.user)
response = client.delete(url)

assert response.status_code == status.HTTP_200_OK


@pytest.mark.django_db
def test_that_a_non_member_cannot_react_on_news(user, news, default_client):
"""A non-member should not be able to leave a reaction on a news page"""
Expand Down

0 comments on commit e98663f

Please sign in to comment.