Skip to content

Commit e5d9e96

Browse files
committed
Improved queries using ExpertReportAnnotation
1 parent d49b506 commit e5d9e96

File tree

1 file changed

+33
-71
lines changed

1 file changed

+33
-71
lines changed

tigaserver_app/models.py

Lines changed: 33 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -968,26 +968,6 @@ def movelab_score(self) -> Optional[int]:
968968
return None
969969
return max_movelab_annotation.tiger_certainty_category
970970

971-
@property
972-
def is_validated_by_two_experts_and_superexpert(self) -> bool:
973-
"""
974-
This is used to locate reports validated by two experts and the superexpert. This commonly
975-
happens as a consequence of detecting some reports validated 2 times by the same expert; to solve
976-
this situation, one of the two repeated annotations is removed. As a consequence, the report
977-
is validated by 2 experts and 1 superexpert
978-
:return: True if the report is validqted by exactly 2 experts and 1 superexpert
979-
"""
980-
return (
981-
ExpertReportAnnotation.objects.filter(
982-
report=self, user__groups__name="expert", validation_complete=True
983-
).count()
984-
== 2
985-
and ExpertReportAnnotation.objects.filter(
986-
report=self, user__groups__name="superexpert", validation_complete=True
987-
).count()
988-
== 1
989-
)
990-
991971
@property
992972
def crowd_score(self):
993973
if self.type != self.TYPE_ADULT:
@@ -2071,20 +2051,19 @@ def get_final_expert_score_aegypti(self):
20712051
return -3
20722052

20732053
def get_final_expert_status(self):
2074-
result = 1
2075-
if ExpertReportAnnotation.objects.filter(report=self, user__groups__name='superexpert', validation_complete=True, revise=True, status=-1).exists():
2076-
result = -1
2077-
elif ExpertReportAnnotation.objects.filter(report=self, user__groups__name='superexpert', validation_complete=True, revise=True, status=0).exists():
2078-
result = 0
2079-
elif ExpertReportAnnotation.objects.filter(report=self, user__groups__name='superexpert', validation_complete=True, revise=True, status=1).exists():
2080-
result = 1
2081-
elif ExpertReportAnnotation.objects.filter(report=self, user__groups__name='expert', validation_complete=True, status=-1).exists():
2082-
result = -1
2083-
elif ExpertReportAnnotation.objects.filter(report=self, user__groups__name='expert', validation_complete=True, status=0).exists():
2084-
result = 0
2085-
elif ExpertReportAnnotation.objects.filter(report=self, user__groups__name='expert', validation_complete=True, status=1).exists():
2086-
result = 1
2087-
return result
2054+
finished_annotations = self.expert_report_annotations.filter(validation_complete=True)
2055+
2056+
super_expert_annotations = finished_annotations.filter(user__groups__name='superexpert', revise=True)
2057+
super_expert_status = super_expert_annotations.aggregate(min_status=models.Min('status'))
2058+
2059+
if super_expert_status['min_status'] is not None:
2060+
return super_expert_status['min_status']
2061+
2062+
expert_status = finished_annotations.filter(user__groups__name='expert').aggregate(min_status=models.Min('status'))
2063+
if expert_status['min_status'] is not None:
2064+
return expert_status['min_status']
2065+
2066+
return 1
20882067

20892068
def get_final_expert_status_text(self):
20902069
return dict(STATUS_CATEGORIES)[self.get_final_expert_status()]
@@ -2097,10 +2076,9 @@ def get_is_expert_validated(self):
20972076
return ExpertReportAnnotation.objects.filter(report=self, user__groups__name='expert', validation_complete=True).count() >= 3
20982077

20992078
def get_tags_bootstrap_superexpert(self):
2100-
these_annotations = ExpertReportAnnotation.objects.filter(report=self)
21012079
result = ''
21022080
s = set()
2103-
for ano in these_annotations:
2081+
for ano in self.expert_report_annotations.all():
21042082
tags = ano.tags.all()
21052083
for tag in tags:
21062084
if not tag in s:
@@ -2112,10 +2090,9 @@ def get_tags_bootstrap_superexpert(self):
21122090
return result
21132091

21142092
def get_tags_bootstrap(self):
2115-
these_annotations = ExpertReportAnnotation.objects.filter(report=self)
21162093
result = ''
21172094
s = set()
2118-
for ano in these_annotations:
2095+
for ano in self.expert_report_annotations.all():
21192096
tags = ano.tags.all()
21202097
for tag in tags:
21212098
if not tag in s:
@@ -2130,54 +2107,42 @@ def get_single_report_view_link(self):
21302107
return result
21312108

21322109
def get_who_has(self):
2133-
result = ''
2134-
these_annotations = ExpertReportAnnotation.objects.filter(report=self)
2135-
i = these_annotations.count()
2136-
for ano in these_annotations:
2137-
result += ano.user.username + (': validated' if ano.validation_complete else ': pending')
2138-
i -= 1
2139-
if i > 0:
2140-
result += ', '
2141-
return result
2110+
result = []
2111+
for ano in self.expert_report_annotations.all().select_related('user'):
2112+
result.append(
2113+
ano.user.username + (': validated' if ano.validation_complete else ': pending')
2114+
)
2115+
2116+
return ", ".join(result)
21422117

21432118
def get_expert_has_been_assigned_long_validation(self):
2144-
these_annotations = ExpertReportAnnotation.objects.filter(report=self)
2145-
for ano in these_annotations:
2119+
for ano in self.expert_report_annotations.all().select_related('user'):
21462120
if not ano.user.userstat.is_superexpert():
21472121
if ano.simplified_annotation == False:
21482122
return True
21492123
return False
21502124

21512125
def get_who_has_count(self):
2152-
these_annotations = ExpertReportAnnotation.objects.filter(report=self)
2153-
i = these_annotations.count()
2154-
return i
2155-
2156-
def get_annotations(self):
2157-
these_annotations = ExpertReportAnnotation.objects.filter(report=self)
2158-
return these_annotations
2126+
return self.expert_report_annotations.all().count()
21592127

21602128
def get_expert_recipients(self):
21612129
result = []
2162-
these_annotations = ExpertReportAnnotation.objects.filter(report=self)
2163-
for ano in these_annotations:
2130+
for ano in self.expert_report_annotations.all().select_related('user'):
21642131
if not ano.user.userstat.is_superexpert():
21652132
result.append(ano.user.username)
21662133
return '+'.join(result)
21672134

21682135
def get_superexpert_completed_recipients(self):
21692136
result = []
2170-
these_annotations = ExpertReportAnnotation.objects.filter(report=self)
2171-
for ano in these_annotations:
2137+
for ano in self.expert_report_annotations.all().select_related('user'):
21722138
if ano.validation_complete and ano.user.userstat.is_superexpert():
21732139
result.append(ano.user.username)
21742140
return '+'.join(result)
21752141

21762142
def get_expert_recipients_names(self):
21772143
result = []
2178-
these_annotations = ExpertReportAnnotation.objects.filter(report=self)
21792144

2180-
for ano in these_annotations:
2145+
for ano in self.expert_report_annotations.all().select_related('user'):
21812146
if not ano.user.userstat.is_superexpert():
21822147
if ano.user.first_name and ano.user.last_name:
21832148
result.append(ano.user.first_name + ' ' + ano.user.last_name)
@@ -2186,15 +2151,12 @@ def get_expert_recipients_names(self):
21862151
return '+'.join(result)
21872152

21882153
def get_who_has_bootstrap(self):
2189-
result = ''
2190-
these_annotations = ExpertReportAnnotation.objects.filter(report=self)
2191-
i = these_annotations.count()
2192-
for ano in these_annotations:
2193-
result += '<span class="label ' + ('label-success' if ano.validation_complete else 'label-warning') + '" data-toggle="tooltip" data-placement="bottom" title="' + (('validated by ' + ano.user.username) if ano.validation_complete else ('pending with ' + ano.user.username)) + '">' + ano.user.username + ' <span class="glyphicon ' + ('glyphicon-check' if ano.validation_complete else 'glyphicon-time') + '"></span></span>'
2194-
i -= 1
2195-
if i > 0:
2196-
result += ' '
2197-
return result
2154+
result = []
2155+
for ano in self.expert_report_annotations.all().select_related("user"):
2156+
result.append(
2157+
'<span class="label ' + ('label-success' if ano.validation_complete else 'label-warning') + '" data-toggle="tooltip" data-placement="bottom" title="' + (('validated by ' + ano.user.username) if ano.validation_complete else ('pending with ' + ano.user.username)) + '">' + ano.user.username + ' <span class="glyphicon ' + ('glyphicon-check' if ano.validation_complete else 'glyphicon-time') + '"></span></span>'
2158+
)
2159+
return ' '.join(result)
21982160

21992161
def get_final_photo_url_for_notification(self):
22002162
if ExpertReportAnnotation.objects.filter(report=self, user__groups__name='superexpert', validation_complete=True, revise=True).exists():

0 commit comments

Comments
 (0)