Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: separate quib model and change models name #97

Merged
merged 3 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/apps/comment/admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.contrib import admin

from .models import Comment
from .models import CommentModel

# Register your models here.


@admin.register(Comment)
@admin.register(CommentModel)
class CommentAdmin(admin.ModelAdmin):
list_display = ('quibbler', 'content', 'created_at')
search_fields = ('quibbler__username', 'content')
16 changes: 10 additions & 6 deletions backend/apps/comment/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from django.shortcuts import get_object_or_404
from rest_framework import serializers

from ...models import Comment
from ...models import CommentModel


class CommentSerializer(serializers.ModelSerializer):
path = serializers.CharField(required=False)

class Meta:
model = Comment
model = CommentModel
fields = '__all__'

def create(self, validated_data):
Expand All @@ -19,12 +19,16 @@ def create(self, validated_data):
}

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
parent_instance = get_object_or_404(CommentModel, path__match=path)
comment_instance: CommentModel = (
CommentModel.objects.create_child( # pyright: ignore
parent=parent_instance, **data
)
)
else:
comment_instance: Comment = Comment.objects.create_child(**data)
comment_instance: CommentModel = CommentModel.objects.create_child(
**data
) # pyright: ignore

comment_instance.save()
return comment_instance
4 changes: 2 additions & 2 deletions backend/apps/comment/api/v1/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from rest_framework import viewsets

from ...models import Comment
from ...models import CommentModel
from .serializers import CommentSerializer


class CommentViewSet(viewsets.ModelViewSet):
queryset = Comment.objects.all()
queryset = CommentModel.objects.all()
serializer_class = CommentSerializer
4 changes: 2 additions & 2 deletions backend/apps/comment/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.4 on 2024-12-13 12:02
# Generated by Django 5.1.4 on 2024-12-14 02:43

import django_ltree.fields
from django.db import migrations, models
Expand All @@ -12,7 +12,7 @@ class Migration(migrations.Migration):

operations = [
migrations.CreateModel(
name='Comment',
name='CommentModel',
fields=[
(
'id',
Expand Down
18 changes: 9 additions & 9 deletions backend/apps/comment/migrations/0002_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.4 on 2024-12-13 12:02
# Generated by Django 5.1.4 on 2024-12-14 02:43

import django.contrib.postgres.indexes
import django.db.models.deletion
Expand All @@ -16,38 +16,38 @@ class Migration(migrations.Migration):

operations = [
migrations.AddField(
model_name='comment',
model_name='commentmodel',
name='downvotes',
field=models.ManyToManyField(
blank=True,
related_name='downvotes',
to='user.profile',
to='user.profilemodel',
verbose_name='downvotes',
),
),
migrations.AddField(
model_name='comment',
model_name='commentmodel',
name='quibbler',
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to='user.profile',
to='user.profilemodel',
verbose_name='quibbler',
),
),
migrations.AddField(
model_name='comment',
model_name='commentmodel',
name='upvotes',
field=models.ManyToManyField(
blank=True,
related_name='upvotes',
to='user.profile',
to='user.profilemodel',
verbose_name='upvotes',
),
),
migrations.AddIndex(
model_name='comment',
model_name='commentmodel',
index=django.contrib.postgres.indexes.GistIndex(
fields=['path'], name='comment_com_path_d1388c_gist'
fields=['path'], name='comment_com_path_d124df_gist'
),
),
]
10 changes: 5 additions & 5 deletions backend/apps/comment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
from django.utils.translation import gettext_lazy as _
from django_ltree.models import TreeModel

from apps.user.models import Profile
from apps.user.models import ProfileModel
from common.mixins.model_mixins import CreatedAtMixin

# Create your models here.


class Comment(CreatedAtMixin, TreeModel):
class CommentModel(CreatedAtMixin, TreeModel):
quibbler = models.ForeignKey(
Profile, on_delete=models.CASCADE, verbose_name=_('quibbler')
ProfileModel, on_delete=models.CASCADE, verbose_name=_('quibbler')
)
content = models.TextField(_('content'))
upvotes = models.ManyToManyField(
Profile, related_name='upvotes', blank=True, verbose_name=_('upvotes')
ProfileModel, related_name='upvotes', blank=True, verbose_name=_('upvotes')
)
downvotes = models.ManyToManyField(
Profile, related_name='downvotes', blank=True, verbose_name=_('downvotes')
ProfileModel, related_name='downvotes', blank=True, verbose_name=_('downvotes')
)

@property
Expand Down
Empty file added backend/apps/quib/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions backend/apps/quib/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.contrib import admin

from .models import QuibModel

# Register your models here.


@admin.register(QuibModel)
class QuibModelAdmin(admin.ModelAdmin):
list_display = ('title', 'quiblet', 'quibber', 'is_public', 'created_at')
search_fields = ('title', 'quiblet__name', 'quibber__name')
Empty file.
Empty file.
21 changes: 21 additions & 0 deletions backend/apps/quib/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from rest_framework import serializers

from apps.quiblet.api.v1.serializers import QuibletSerializer, QuibletSlimSerializer

from ...models import QuibModel


class QuibSerializer(serializers.ModelSerializer):
quiblet = QuibletSerializer(read_only=True)

class Meta:
model = QuibModel
fields = '__all__'


class QuibSlimSerializer(serializers.ModelSerializer):
quiblet = QuibletSlimSerializer(read_only=True)

class Meta:
model = QuibModel
exclude = ('quibber',)
8 changes: 8 additions & 0 deletions backend/apps/quib/api/v1/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework.routers import DefaultRouter

from .viewsets import QuibViewSet

router = DefaultRouter()
router.register(r'', QuibViewSet)

urlpatterns = router.urls
13 changes: 13 additions & 0 deletions backend/apps/quib/api/v1/viewsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from rest_framework import viewsets

from ...models import QuibModel
from .serializers import QuibSerializer, QuibSlimSerializer


class QuibViewSet(viewsets.ModelViewSet):
queryset = QuibModel.objects.all()

def get_serializer_class(self): # pyright: ignore
if self.action == 'list':
return QuibSlimSerializer
return QuibSerializer
6 changes: 6 additions & 0 deletions backend/apps/quib/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class QuibConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'apps.quib'
73 changes: 73 additions & 0 deletions backend/apps/quib/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Generated by Django 5.1.4 on 2024-12-14 02:43

import dynamic_filenames
import shortuuid.django_fields
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
('comment', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='QuibModel',
fields=[
(
'created_at',
models.DateTimeField(auto_now_add=True, verbose_name='create at'),
),
('is_public', models.BooleanField(default=True, verbose_name='is public')),
(
'id',
shortuuid.django_fields.ShortUUIDField(
alphabet='abcdefghijklmnopqrstuvwxyz0123456789',
editable=False,
length=7,
max_length=7,
prefix='',
primary_key=True,
serialize=False,
verbose_name='id',
),
),
('title', models.CharField(max_length=255, verbose_name='title')),
(
'slug',
models.SlugField(
blank=True, editable=False, max_length=25, verbose_name='slug'
),
),
('content', models.TextField(verbose_name='content')),
(
'cover',
models.ImageField(
blank=True,
null=True,
upload_to=dynamic_filenames.FilePattern(
filename_pattern='cover/{uuid:s}{ext}'
),
verbose_name='cover',
),
),
(
'comments',
models.ManyToManyField(
blank=True,
related_name='comments',
to='comment.commentmodel',
verbose_name='comments',
),
),
],
options={
'verbose_name': 'Quib',
'verbose_name_plural': 'Quibs',
'ordering': ['-created_at'],
},
),
]
58 changes: 58 additions & 0 deletions backend/apps/quib/migrations/0002_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Generated by Django 5.1.4 on 2024-12-14 02:43

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
('quib', '0001_initial'),
('quiblet', '0001_initial'),
('user', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='quibmodel',
name='downvotes',
field=models.ManyToManyField(
blank=True,
related_name='downvoted_quibs',
to='user.profilemodel',
verbose_name='downvotes',
),
),
migrations.AddField(
model_name='quibmodel',
name='quibber',
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name='quibs',
to='user.profilemodel',
verbose_name='quibber',
),
),
migrations.AddField(
model_name='quibmodel',
name='quiblet',
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name='quibs',
to='quiblet.quibletmodel',
verbose_name='quiblet',
),
),
migrations.AddField(
model_name='quibmodel',
name='upvotes',
field=models.ManyToManyField(
blank=True,
related_name='upvoted_quibs',
to='user.profilemodel',
verbose_name='upvotes',
),
),
]
17 changes: 17 additions & 0 deletions backend/apps/quib/migrations/0003_remove_quibmodel_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1.4 on 2024-12-14 02:47

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('quib', '0002_initial'),
]

operations = [
migrations.RemoveField(
model_name='quibmodel',
name='comments',
),
]
24 changes: 24 additions & 0 deletions backend/apps/quib/migrations/0004_quibmodel_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.4 on 2024-12-14 02:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('comment', '0002_initial'),
('quib', '0003_remove_quibmodel_comments'),
]

operations = [
migrations.AddField(
model_name='quibmodel',
name='comments',
field=models.ManyToManyField(
blank=True,
related_name='comments',
to='comment.commentmodel',
verbose_name='comments',
),
),
]
Empty file.
Loading
Loading