|
25 | 25 | from django.db.models.query_utils import Q |
26 | 26 | from django.utils import dateformat, timezone |
27 | 27 | from label_studio_sdk.converter import Converter |
28 | | -from tasks.models import Annotation, Task |
| 28 | +from tasks.models import Annotation, AnnotationDraft, Task |
29 | 29 |
|
30 | 30 | ONLY = 'only' |
31 | 31 | EXCLUDE = 'exclude' |
@@ -96,27 +96,29 @@ def _get_filtered_annotations_queryset(self, annotation_filter_options=None): |
96 | 96 | }) |
97 | 97 | """ |
98 | 98 | queryset = Annotation.objects.all() |
99 | | - if not isinstance(annotation_filter_options, dict): |
100 | | - return queryset |
101 | | - |
102 | | - q_list = [] |
103 | | - if annotation_filter_options.get('usual'): |
104 | | - q_list.append(Q(was_cancelled=False, ground_truth=False)) |
105 | | - if annotation_filter_options.get('ground_truth'): |
106 | | - q_list.append(Q(ground_truth=True)) |
107 | | - if annotation_filter_options.get('skipped'): |
108 | | - q_list.append(Q(was_cancelled=True)) |
109 | | - if not q_list: |
110 | | - return queryset |
111 | | - |
112 | | - q = reduce(lambda x, y: x | y, q_list) |
113 | | - annotations_qs = queryset.filter(q) |
114 | | - |
| 99 | + if isinstance(annotation_filter_options, dict): |
| 100 | + q_list = [] |
| 101 | + if annotation_filter_options.get('usual'): |
| 102 | + q_list.append(Q(was_cancelled=False, ground_truth=False)) |
| 103 | + if annotation_filter_options.get('ground_truth'): |
| 104 | + q_list.append(Q(ground_truth=True)) |
| 105 | + if annotation_filter_options.get('skipped'): |
| 106 | + q_list.append(Q(was_cancelled=True)) |
| 107 | + if q_list: |
| 108 | + q = reduce(lambda x, y: x | y, q_list) |
| 109 | + queryset = queryset.filter(q) |
| 110 | + |
| 111 | + # pre-select completed_by user info |
| 112 | + queryset = queryset.select_related('completed_by') |
115 | 113 | # prefetch reviews in LSE |
116 | | - if hasattr(annotations_qs.model, 'reviews'): |
117 | | - annotations_qs = annotations_qs.prefetch_related('reviews') |
| 114 | + if hasattr(queryset.model, 'reviews'): |
| 115 | + from reviews.models import AnnotationReview |
| 116 | + |
| 117 | + queryset = queryset.prefetch_related( |
| 118 | + Prefetch('reviews', queryset=AnnotationReview.objects.select_related('created_by')) |
| 119 | + ) |
118 | 120 |
|
119 | | - return annotations_qs |
| 121 | + return queryset |
120 | 122 |
|
121 | 123 | @staticmethod |
122 | 124 | def _get_export_serializer_option(serialization_options): |
@@ -153,13 +155,12 @@ def get_task_queryset(self, ids, annotation_filter_options): |
153 | 155 |
|
154 | 156 | return ( |
155 | 157 | Task.objects.filter(id__in=ids) |
| 158 | + .select_related('file_upload') # select_related more efficient for regular foreign-key relationship |
156 | 159 | .prefetch_related( |
157 | | - Prefetch( |
158 | | - 'annotations', |
159 | | - queryset=annotations_qs, |
160 | | - ) |
| 160 | + Prefetch('annotations', queryset=annotations_qs), |
| 161 | + Prefetch('drafts', queryset=AnnotationDraft.objects.select_related('user')), |
| 162 | + 'comment_authors', |
161 | 163 | ) |
162 | | - .prefetch_related('predictions', 'drafts', 'comment_authors', 'file_upload') |
163 | 164 | ) |
164 | 165 |
|
165 | 166 | def get_export_data(self, task_filter_options=None, annotation_filter_options=None, serialization_options=None): |
|
0 commit comments