Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
pull all the creatured ids to avoid repeated calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Balogh committed Jul 20, 2010
1 parent 3958e5e commit 947b596
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
32 changes: 25 additions & 7 deletions apps/addons/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):

Expand Down
4 changes: 4 additions & 0 deletions migrations/51-addoncategory-feature-idx.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 947b596

Please sign in to comment.