mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Cache the computed average rating
This commit is contained in:
parent
b4efa96292
commit
99adf52046
@ -73,6 +73,7 @@ class TagTreeItem(object): # {{{
|
|||||||
is_searchable=category_key not in ['search'])
|
is_searchable=category_key not in ['search'])
|
||||||
elif self.type == self.TAG:
|
elif self.type == self.TAG:
|
||||||
self.tag = data
|
self.tag = data
|
||||||
|
self.cached_average_rating = None
|
||||||
|
|
||||||
self.tooltip = tooltip or ''
|
self.tooltip = tooltip or ''
|
||||||
|
|
||||||
@ -119,6 +120,8 @@ class TagTreeItem(object): # {{{
|
|||||||
return self.tag.avg_rating
|
return self.tag.avg_rating
|
||||||
if not self.children:
|
if not self.children:
|
||||||
return self.tag.avg_rating # leaf node, avg_rating is correct
|
return self.tag.avg_rating # leaf node, avg_rating is correct
|
||||||
|
if self.cached_average_rating:
|
||||||
|
return self.cached_average_rating
|
||||||
total = num = 0
|
total = num = 0
|
||||||
for child in self.children:
|
for child in self.children:
|
||||||
r = child.average_rating
|
r = child.average_rating
|
||||||
@ -129,7 +132,8 @@ class TagTreeItem(object): # {{{
|
|||||||
total += 1
|
total += 1
|
||||||
num += self.tag.avg_rating
|
num += self.tag.avg_rating
|
||||||
try:
|
try:
|
||||||
return num/float(total)
|
self.cached_average_rating = num/float(total)
|
||||||
|
return self.cached_average_rating
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user