Just include comments on document object

This commit is contained in:
shamoon
2023-03-17 15:08:12 -07:00
parent 9d17727cb6
commit 78cb8cff69
14 changed files with 147 additions and 85 deletions

View File

@@ -0,0 +1,40 @@
# Generated by Django 4.1.5 on 2023-03-17 21:23
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("documents", "1033_alter_documenttype_options_alter_tag_options_and_more"),
]
operations = [
migrations.AlterField(
model_name="comment",
name="document",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="comments",
to="documents.document",
verbose_name="document",
),
),
migrations.AlterField(
model_name="comment",
name="user",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="comments",
to=settings.AUTH_USER_MODEL,
verbose_name="user",
),
),
]

View File

@@ -652,7 +652,7 @@ class Comment(models.Model):
Document,
blank=True,
null=True,
related_name="documents",
related_name="comments",
on_delete=models.CASCADE,
verbose_name=_("document"),
)
@@ -661,7 +661,7 @@ class Comment(models.Model):
User,
blank=True,
null=True,
related_name="users",
related_name="comments",
on_delete=models.SET_NULL,
verbose_name=_("user"),
)

View File

@@ -16,7 +16,6 @@ from rest_framework import serializers
from rest_framework.fields import SerializerMethodField
from . import bulk_edit
from .models import Comment
from .models import Correspondent
from .models import Document
from .models import DocumentType
@@ -383,7 +382,7 @@ class DocumentSerializer(OwnedObjectSerializer, DynamicFieldsModelSerializer):
archived_file_name = SerializerMethodField()
created_date = serializers.DateField(required=False)
n_comments = SerializerMethodField()
num_comments = serializers.IntegerField(read_only=True)
owner = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(),
@@ -400,9 +399,6 @@ class DocumentSerializer(OwnedObjectSerializer, DynamicFieldsModelSerializer):
else:
return None
def get_n_comments(self, obj):
return Comment.objects.filter(document=obj).count()
def to_representation(self, instance):
doc = super().to_representation(instance)
if self.truncate_content:
@@ -448,7 +444,8 @@ class DocumentSerializer(OwnedObjectSerializer, DynamicFieldsModelSerializer):
"owner",
"permissions",
"set_permissions",
"n_comments",
"comments",
"num_comments",
)

View File

@@ -230,7 +230,7 @@ class DocumentViewSet(
GenericViewSet,
):
model = Document
queryset = Document.objects.all()
queryset = Document.objects.annotate(num_comments=Count("comments"))
serializer_class = DocumentSerializer
pagination_class = StandardPagination
permission_classes = (IsAuthenticated, PaperlessObjectPermissions)
@@ -251,6 +251,7 @@ class DocumentViewSet(
"modified",
"added",
"archive_serial_number",
"num_comments",
)
def get_queryset(self):