Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

pull all the creatured ids to avoid repeated calls

  • Loading branch information...
commit 947b5962464ff88105f2b2f1fb03664ff2c22618 1 parent 3958e5e
Jeff Balogh authored
View
32 apps/addons/models.py
@@ -286,6 +286,11 @@ def transformer(addons):
# 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 @@ def _features():
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 Meta:
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):
View
4 migrations/51-addoncategory-feature-idx.sql
@@ -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;
Please sign in to comment.
Something went wrong with that request. Please try again.