pull all the creatured ids to avoid repeated calls

This commit is contained in:
Jeff Balogh 2010-07-19 13:56:17 -07:00
Родитель 3958e5eb9f
Коммит 947b596246
2 изменённых файлов: 29 добавлений и 7 удалений

Просмотреть файл

@ -286,6 +286,11 @@ class Addon(amo.models.ModelBase):
# Personas need categories for the JSON dump.
Category.transformer(personas)
# Store creatured apps on the add-on.
creatured = AddonCategory.creatured()
for addon in addons:
addon._creatured_apps = creatured.get(addon.id, [])
@amo.cached_property
def current_beta_version(self):
"""Retrieves the latest version of an addon, in the beta channel."""
@ -359,14 +364,16 @@ class Addon(amo.models.ModelBase):
return True
return False
@caching.cached_method
def is_category_featured(self, app, lang):
"""is add-on featured in any category for this app?"""
# XXX should probably take feature_locales under consideration, even
# though remora didn't do that
return AddonCategory.objects.filter(
addon=self, feature=True,
category__application__id=app.id).exists()
"""Is add-on featured in any category for this app?"""
return app.id in self._creatured_apps
@amo.cached_property(writable=True)
def _creatured_apps(self):
"""Return a list of the apps where this add-on is creatured."""
# This exists outside of is_category_featured so we can write the list
# in the transformer and avoid repeated .creatured() calls.
return AddonCategory.creatured().get(self.id, [])
@amo.cached_property
def tags_partitioned_by_developer(self):
@ -505,6 +512,17 @@ class AddonCategory(caching.CachingMixin, models.Model):
db_table = 'addons_categories'
unique_together = ('addon', 'category')
@classmethod
def creatured(cls):
"""Get a dict of {addon: [app,..]} for all creatured add-ons."""
qs = cls.objects.filter(feature=True)
f = lambda: qs.values_list('addon', 'category__application')
vals = caching.cached_with(qs, f, 'creatured')
rv = {}
for addon, app in vals:
rv.setdefault(addon, []).append(app)
return rv
class PledgeManager(amo.models.ManagerBase):

Просмотреть файл

@ -0,0 +1,4 @@
CREATE INDEX feature_addon_idx ON addons_categories (feature, addon_id);
-- This index is redundant.
ALTER TABLE addons_categories DROP KEY tag_id;