Skip to content

Commit

Permalink
added indent level for comments and made test for this
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsNyl committed Oct 7, 2023
1 parent 184db8c commit 0a26d30
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/content/factories/comment_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Meta:
body = factory.Faker("paragraph", nb_sentences=10)
author = factory.SubFactory(UserFactory)
parent = None
indent_level = 0
content_object = factory.SubFactory(EventFactory)

@factory.lazy_attribute
Expand All @@ -33,6 +34,7 @@ class Meta:
body = factory.Faker("paragraph", nb_sentences=10)
author = factory.SubFactory(UserFactory)
parent = None
indent_level = 0
content_object = factory.SubFactory(NewsFactory)

@factory.lazy_attribute
Expand Down
18 changes: 18 additions & 0 deletions app/content/migrations/0057_comment_indent_level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.8 on 2023-10-07 08:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('content', '0056_alter_comment_options_news_allow_comments'),
]

operations = [
migrations.AddField(
model_name='comment',
name='indent_level',
field=models.IntegerField(default=0),
),
]
2 changes: 2 additions & 0 deletions app/content/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Comment(BaseModel, BasePermissionModel):

body = models.TextField()

indent_level = models.IntegerField(default=0)

author = models.ForeignKey(
User,
blank=True,
Expand Down
28 changes: 27 additions & 1 deletion app/content/serializers/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class Meta:
"created_at",
"updated_at",
"author",
"children",
"indent_level",
"parent",
"children"
)

def to_representation(self, instance):
Expand All @@ -45,9 +47,24 @@ def create(self, validated_data):
content_id = validated_data.pop("content_id")
author = validated_data.pop("author")
body = validated_data.pop("body")
parent = validated_data.pop("parent")

if parent and parent.indent_level >= 3:
# TODO: make exception for many indents in thread
raise Exception("For mange kommentarer i tråden.")

if content_type == ContentType.EVENT:
event = Event.objects.get(id=content_id)

if parent:
created_comment = event.comments.create(
author=author,
body=body,
parent=parent,
indent_level=parent.indent_level + 1
)
return created_comment

created_comment = event.comments.create(
author=author,
body=body
Expand All @@ -56,6 +73,15 @@ def create(self, validated_data):

if content_type == ContentType.NEWS:
news = News.objects.get(id=content_id)

if parent:
created_comment = news.comments.create(
author=author,
body=body,
parent=parent,
indent_level=parent.indent_level + 1
)

created_comment = news.comments.create(
author=author,
body=body
Expand Down
13 changes: 10 additions & 3 deletions app/content/views/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ def create(self, request, *args, **kwargs):
)

if serializer.is_valid():
comment = super().perform_create(serializer)
serializer = CommentSerializer(comment, context={"request": request})
return Response(serializer.data, status=status.HTTP_201_CREATED)
try:
comment = super().perform_create(serializer)
serializer = CommentSerializer(comment, context={"request": request})
return Response(serializer.data, status=status.HTTP_201_CREATED)
except Exception as oversized_thread:
capture_exception(oversized_thread)
return Response(
{"detail": "Denne kommentartråden er for lang."},
status=status.HTTP_400_BAD_REQUEST
)

return Response(
{"detail": serializer.error}, status=status.HTTP_400_BAD_REQUEST
Expand Down
52 changes: 49 additions & 3 deletions app/tests/content/test_comment_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_comment_data(
return {
"body": "test comment body text",
"author": user.user_id if user else None,
"parent": parent,
"parent": parent.id if parent else None,
"content_type": content_type,
"content_id": content_id,
"allow_comments": allow_comments
Expand All @@ -42,7 +42,7 @@ def test_retrieve_comment_as_user(user):
@pytest.mark.django_db
def test_create_comment_on_event_as_member(member, event):
"""
An user of TIHLDE should be able to create an coment on an event.
An user of TIHLDE should be able to create a comment on an event.
"""

client = get_api_client(user=member)
Expand All @@ -53,10 +53,56 @@ def test_create_comment_on_event_as_member(member, event):
assert response.status_code == status.HTTP_201_CREATED


@pytest.mark.django_db
def test_create_child_comment_on_event_as_member(member, event, event_comment):
"""
An user of TIHLDE should be able to create a child comment on an event.
"""

client = get_api_client(user=member)
data = get_comment_data(
ContentType.EVENT,
event.id,
user=member,
parent=event_comment
)

response = client.post(API_COMMENTS_BASE_URL, data)
data = response.data

assert response.status_code == status.HTTP_201_CREATED
assert data["parent"] == event_comment.id
assert data["indent_level"] == 1


@pytest.mark.django_db
def test_create_oversized_thread_on_event_as_member(member, event, event_comment):
"""
An user of TIHLDE should not be able to create a child comment
on an event which have achieved maximum thread length.
"""

event_comment.indent_level = 3

event_comment.save()

client = get_api_client(user=member)
data = get_comment_data(
ContentType.EVENT,
event.id,
user=member,
parent=event_comment
)

response = client.post(API_COMMENTS_BASE_URL, data)

assert response.status_code == status.HTTP_400_BAD_REQUEST


@pytest.mark.django_db
def test_create_comment_on_news_as_member(member, news):
"""
An user of TIHLDE should be able to create an coment on news.
An user of TIHLDE should be able to create a comment on news.
"""

client = get_api_client(user=member)
Expand Down

0 comments on commit 0a26d30

Please sign in to comment.