-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added model, serializer and view for comment
- Loading branch information
Showing
15 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import factory | ||
|
||
from factory.django import DjangoModelFactory | ||
|
||
from app.content.models.comment import Comment | ||
from app.content.factories.user_factory import UserFactory | ||
|
||
|
||
class CommentFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Comment | ||
|
||
body = factory.Faker("paragraph", nb_sentences=10) | ||
author = factory.SubFactory(UserFactory) | ||
parent = None | ||
|
37 changes: 37 additions & 0 deletions
37
app/content/migrations/0054_event_allow_comments_comment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Generated by Django 4.0.8 on 2023-09-26 15:11 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('contenttypes', '0002_remove_content_type_name'), | ||
('content', '0053_event_contact_person'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='event', | ||
name='allow_comments', | ||
field=models.BooleanField(default=True), | ||
), | ||
migrations.CreateModel( | ||
name='Comment', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('updated_at', models.DateTimeField(auto_now=True)), | ||
('body', models.TextField()), | ||
('object_id', models.PositiveIntegerField()), | ||
('author', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='comments', to=settings.AUTH_USER_MODEL)), | ||
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')), | ||
('parent', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='content.comment', verbose_name='parent')), | ||
], | ||
options={ | ||
'abstract': False, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.0.8 on 2023-09-26 15:42 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('content', '0054_event_allow_comments_comment'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name='comment', | ||
options={'ordering': ('-created_at',)}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
get_strike_description, | ||
get_strike_strike_size, | ||
) | ||
from app.content.models.comment import Comment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.db import models | ||
from django.contrib.contenttypes.fields import GenericForeignKey | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from app.util.models import BaseModel | ||
from app.common.permissions import BasePermissionModel | ||
from app.common.enums import Groups | ||
from app.content.models.user import User | ||
|
||
|
||
class Comment(BaseModel, BasePermissionModel): | ||
write_access = (Groups.TIHLDE, ) | ||
read_access = (Groups.TIHLDE, ) | ||
|
||
body = models.TextField() | ||
|
||
author = models.ForeignKey( | ||
User, | ||
blank=True, | ||
null=True, | ||
default=None, | ||
on_delete=models.SET_NULL, | ||
related_name="comments" | ||
) | ||
|
||
parent = models.ForeignKey( | ||
"self", | ||
blank=True, | ||
null=True, | ||
default=None, | ||
on_delete=models.SET_NULL, | ||
related_name="children", | ||
verbose_name="parent" | ||
) | ||
|
||
content_type = models.ForeignKey( | ||
ContentType, | ||
on_delete=models.CASCADE | ||
) | ||
object_id = models.PositiveIntegerField() | ||
content_object = GenericForeignKey() | ||
|
||
class Meta: | ||
ordering = ("-created_at",) | ||
|
||
def __str__(self): | ||
return f"Comment by: {self.author.first_name} {self.author.last_name} - created: {self.created_at}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from rest_framework import serializers | ||
|
||
from app.content.models.comment import Comment | ||
from app.content.models.event import Event | ||
from app.content.serializers.user import DefaultUserSerializer | ||
|
||
class CommentSerializer(serializers.ModelSerializer): | ||
author = DefaultUserSerializer(read_only=True, required=False) | ||
|
||
class Meta: | ||
model = Comment | ||
fields = ( | ||
"id", | ||
"body", | ||
"created_at", | ||
"updated_at", | ||
"author", | ||
"parent" | ||
) | ||
|
||
|
||
class CommentCreateAndUpdateSerializer(serializers.ModelSerializer): | ||
content_type = serializers.CharField() | ||
content_id = serializers.FloatField() | ||
|
||
class Meta: | ||
model = Comment | ||
fields = ( | ||
"body", | ||
"author", | ||
"parent", | ||
"content_type", | ||
"content_id" | ||
) | ||
|
||
def create(self, validated_data): | ||
print("inside create") | ||
print(validated_data) | ||
content_type = validated_data.pop("content_type") | ||
content_id = validated_data.pop("content_id") | ||
author = validated_data.pop("author") | ||
body = validated_data.pop("body") | ||
|
||
if content_type == "event": | ||
event = Event.objects.get(id=int(content_id)) | ||
created_comment = event.comments.create( | ||
author=author, | ||
body=body | ||
) | ||
print(created_comment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from app.common.viewsets import BaseViewSet | ||
from app.common.mixins import ActionMixin | ||
from app.common.pagination import BasePagination | ||
from app.content.serializers.comment import CommentSerializer, CommentCreateAndUpdateSerializer | ||
from app.content.models.comment import Comment | ||
from app.common.permissions import BasicViewPermission | ||
|
||
|
||
class CommentViewSet(BaseViewSet, ActionMixin): | ||
serializer_class = CommentSerializer | ||
# permission_classes = [BasicViewPermission] | ||
queryset = Comment.objects.all() | ||
pagination_class = BasePagination | ||
|
||
def retrieve(self, request, pk): | ||
try: | ||
comment = self.get_object() | ||
print(comment) | ||
except Exception as e: | ||
print(e) | ||
|
||
def create(self, request, *args, **kwargs): | ||
data = request.data | ||
print(data) | ||
|
||
serializer = CommentCreateAndUpdateSerializer( | ||
data=data, context={"request": request} | ||
) | ||
|
||
if serializer.is_valid(): | ||
print("valid") | ||
comment = super().perform_create(serializer) | ||
print("perform create good") | ||
print(comment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pytest | ||
|
||
from app.util.test_utils import get_api_client | ||
|
||
|
||
API_COMMENTS_BASE_URL = "/comments/" | ||
|
||
def get_comments_url_detail(comment=None): | ||
return f"{API_COMMENTS_BASE_URL}{comment.id}/" | ||
|
||
def get_comment_data(content_type, content_id, user=None, parent=None): | ||
return { | ||
"body": "test comment body text", | ||
"author": user.user_id, | ||
"parent": None, | ||
"content_type": content_type, | ||
"content_id": content_id | ||
} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_retrieve_comment_as_user(user): | ||
pass | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_create_comment_as_user(user, event): | ||
client = get_api_client(user=user) | ||
data = get_comment_data("event", event.id, user=user) | ||
|
||
response = client.post(API_COMMENTS_BASE_URL, data) |