-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: comment model in Ltree structure (#96)
- Loading branch information
1 parent
bc7f8d7
commit 164ec9b
Showing
29 changed files
with
460 additions
and
198 deletions.
There are no files selected for viewing
Empty file.
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,11 @@ | ||
from django.contrib import admin | ||
|
||
from .models import Comment | ||
|
||
# Register your models here. | ||
|
||
|
||
@admin.register(Comment) | ||
class CommentAdmin(admin.ModelAdmin): | ||
list_display = ('quibbler', 'content', 'created_at') | ||
search_fields = ('quibbler__username', 'content') |
Empty file.
Empty file.
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,30 @@ | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import serializers | ||
|
||
from ...models import Comment | ||
|
||
|
||
class CommentSerializer(serializers.ModelSerializer): | ||
path = serializers.CharField(required=False) | ||
|
||
class Meta: | ||
model = Comment | ||
fields = '__all__' | ||
|
||
def create(self, validated_data): | ||
data = { | ||
'quibbler': validated_data.get('quibbler') | ||
or self.context['request'].user_profile, | ||
'content': validated_data['content'], | ||
} | ||
|
||
if path := validated_data.get('path'): | ||
parent_instance = get_object_or_404(Comment, path__match=path) | ||
comment_instance: Comment = Comment.objects.create_child( | ||
parent=parent_instance, **data | ||
) | ||
else: | ||
comment_instance: Comment = Comment.objects.create_child(**data) | ||
|
||
comment_instance.save() | ||
return comment_instance |
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,8 @@ | ||
from rest_framework import routers | ||
|
||
from .viewsets import CommentViewSet | ||
|
||
router = routers.DefaultRouter() | ||
router.register(r'', CommentViewSet) | ||
|
||
urlpatterns = router.urls |
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,9 @@ | ||
from rest_framework import viewsets | ||
|
||
from ...models import Comment | ||
from .serializers import CommentSerializer | ||
|
||
|
||
class CommentViewSet(viewsets.ModelViewSet): | ||
queryset = Comment.objects.all() | ||
serializer_class = CommentSerializer |
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class CommentConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'apps.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,38 @@ | ||
# Generated by Django 5.1.4 on 2024-12-13 12:02 | ||
|
||
import django_ltree.fields | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Comment', | ||
fields=[ | ||
( | ||
'id', | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name='ID', | ||
), | ||
), | ||
('path', django_ltree.fields.PathField(unique=True)), | ||
( | ||
'created_at', | ||
models.DateTimeField(auto_now_add=True, verbose_name='create at'), | ||
), | ||
('content', models.TextField(verbose_name='content')), | ||
], | ||
options={ | ||
'verbose_name': 'Comment', | ||
'verbose_name_plural': 'Comments', | ||
}, | ||
), | ||
] |
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,53 @@ | ||
# Generated by Django 5.1.4 on 2024-12-13 12:02 | ||
|
||
import django.contrib.postgres.indexes | ||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
('comment', '0001_initial'), | ||
('user', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='comment', | ||
name='downvotes', | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name='downvotes', | ||
to='user.profile', | ||
verbose_name='downvotes', | ||
), | ||
), | ||
migrations.AddField( | ||
model_name='comment', | ||
name='quibbler', | ||
field=models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to='user.profile', | ||
verbose_name='quibbler', | ||
), | ||
), | ||
migrations.AddField( | ||
model_name='comment', | ||
name='upvotes', | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name='upvotes', | ||
to='user.profile', | ||
verbose_name='upvotes', | ||
), | ||
), | ||
migrations.AddIndex( | ||
model_name='comment', | ||
index=django.contrib.postgres.indexes.GistIndex( | ||
fields=['path'], name='comment_com_path_d1388c_gist' | ||
), | ||
), | ||
] |
Empty file.
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 django.contrib.postgres import indexes as idx | ||
from django.db import models | ||
from django.utils.translation import gettext_lazy as _ | ||
from django_ltree.models import TreeModel | ||
|
||
from apps.user.models import Profile | ||
from common.mixins.model_mixins import CreatedAtMixin | ||
|
||
# Create your models here. | ||
|
||
|
||
class Comment(CreatedAtMixin, TreeModel): | ||
quibbler = models.ForeignKey( | ||
Profile, on_delete=models.CASCADE, verbose_name=_('quibbler') | ||
) | ||
content = models.TextField(_('content')) | ||
upvotes = models.ManyToManyField( | ||
Profile, related_name='upvotes', blank=True, verbose_name=_('upvotes') | ||
) | ||
downvotes = models.ManyToManyField( | ||
Profile, related_name='downvotes', blank=True, verbose_name=_('downvotes') | ||
) | ||
|
||
@property | ||
def children_count(self): | ||
return self.children().count() | ||
|
||
def __str__(self) -> str: | ||
return f"Comment by {self.quibbler.username}" | ||
|
||
class Meta: # pyright: ignore | ||
indexes = [idx.GistIndex(fields=['path'])] | ||
verbose_name = 'Comment' | ||
verbose_name_plural = 'Comments' |
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
Oops, something went wrong.