Skip to content
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
16 changes: 11 additions & 5 deletions apps/dataset/serializers/document_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import openpyxl
from celery_once import AlreadyQueued
from django.core import validators
from django.db import transaction
from django.db import transaction, models
from django.db.models import QuerySet, Count
from django.db.models.functions import Substr, Reverse
from django.http import HttpResponse
Expand All @@ -28,7 +28,7 @@
from rest_framework import serializers
from xlwt import Utils

from common.db.search import native_search, native_page_search
from common.db.search import native_search, native_page_search, get_dynamics_model
from common.event import ListenerManagement
from common.event.common import work_thread_pool
from common.exception.app_exception import AppApiException
Expand Down Expand Up @@ -443,11 +443,17 @@ def get_query_set(self):
else:
query_set = query_set.filter(status__iregex='^[2n]*$')
order_by = self.data.get('order_by', '')
order_by_query_set = QuerySet(model=get_dynamics_model(
{'char_length': models.CharField(), 'paragraph_count': models.IntegerField(),
"update_time": models.IntegerField(), 'create_time': models.DateTimeField()}))
if order_by:
query_set = query_set.order_by(order_by)
order_by_query_set = order_by_query_set.order_by(order_by)
else:
query_set = query_set.order_by('-create_time', 'id')
return query_set
order_by_query_set = order_by_query_set.order_by('-create_time', 'id')
return {
'document_custom_sql': query_set,
'order_by_query': order_by_query_set
}

def list(self, with_valid=False):
if with_valid:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is mostly clean, but there are some minor improvements that can be made:

  1. In the get_query_set method, you may want to ensure that the dynamic model retrieved by get_dynamics_model has all the fields specified ('char_length', 'paragraph_count', "update_time", 'create_time') defined and accessible.

  2. When returning the results, instead of using a dictionary with keys 'document_custom_sql' and 'order_by_query', it might make more sense to directly define these variables within the function scope for clarity:

def get_query_set(self):
        # existing logic...

        return query_set, order_by_query_set
  1. If possible, consider moving away from using hardcoded field names like 'status'. You could set them up dynamically based on configuration settings so they're easier to change later without modifying many places in your codebase.

These changes won't significantly impact functionality but will improve readability and maintainability slightly.

Expand Down
4 changes: 4 additions & 0 deletions apps/dataset/sql/list_document.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
SELECT * from (
SELECT
"document".* ,
to_json("document"."meta") as meta,
to_json("document"."status_meta") as status_meta,
(SELECT "count"("id") FROM "paragraph" WHERE document_id="document"."id") as "paragraph_count"
FROM
"document" "document"
${document_custom_sql}
) temp
${order_by_query}