move share counts to the transformer (bug 593253)

This commit is contained in:
Jeff Balogh 2010-10-21 13:18:22 -07:00
Родитель ffa2e957e2
Коммит 8009c8ab45
7 изменённых файлов: 40 добавлений и 19 удалений

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

@ -15,6 +15,7 @@ import mongoengine
from tower import ugettext_lazy as _
import amo.models
import sharing.utils as sharing
from amo.fields import DecimalCharField
from amo.utils import send_mail, urlparams, sorted_groupby, JSONEncoder
from amo.urlresolvers import reverse
@ -201,6 +202,9 @@ class Addon(amo.models.ModelBase):
_current_version = models.ForeignKey(Version, related_name='___ignore',
db_column='current_version', null=True)
# This gets overwritten in the transformer.
share_counts = collections.defaultdict(int)
objects = AddonManager()
class Meta:
@ -368,6 +372,9 @@ class Addon(amo.models.ModelBase):
for addon in addons:
addon._creatured_apps = creatured.get(addon.id, [])
# Attach sharing stats.
sharing.attach_share_counts(AddonShareCountTotal, 'addon', addon_dict)
@amo.cached_property
def current_beta_version(self):
"""Retrieves the latest version of an addon, in the beta channel."""
@ -519,13 +526,6 @@ class Addon(amo.models.ModelBase):
def has_eula(self):
return self.eula
@caching.cached_method
def share_counts(self):
rv = collections.defaultdict(int)
rv.update(AddonShareCountTotal.objects.filter(addon=self)
.values_list('service', 'count'))
return rv
@classmethod
def _last_updated_queries(cls):
"""

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

@ -14,6 +14,7 @@ import caching.base as caching
import amo
import amo.models
import sharing.utils as sharing
from amo.utils import sorted_groupby
from amo.urlresolvers import reverse
from addons.models import Addon, AddonRecommendation
@ -113,6 +114,9 @@ class Collection(amo.models.ModelBase):
help_text='Custom index for the add-ons in this collection')
recommended_collection = models.ForeignKey('self', null=True)
# This gets overwritten in the transformer.
share_counts = collections.defaultdict(int)
objects = CollectionManager()
top_tags = TopTags()
@ -229,13 +233,6 @@ class Collection(amo.models.ModelBase):
else:
return settings.MEDIA_URL + 'img/amo2009/icons/collection.png'
@caching.cached_method
def share_counts(self):
rv = collections.defaultdict(int)
rv.update(CollectionShareCountTotal.objects.filter(collection=self)
.values_list('service', 'count'))
return rv
def get_recommendations(self):
"""Get a collection of recommended add-ons for this collection."""
if self.recommended_collection:
@ -318,6 +315,9 @@ class Collection(amo.models.ModelBase):
UserProfile.objects.filter(id__in=author_ids))
for c in collections:
c.author = authors.get(c.author_id)
c_dict = dict((c.pk, c) for c in collections)
sharing.attach_share_counts(CollectionShareCountTotal, 'collection',
c_dict)
@staticmethod
def post_save(sender, instance, **kwargs):

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

@ -317,10 +317,12 @@ class SearchToolsFilter(AddonFilter):
('popular', _lazy(u'Downloads')),
('rating', _lazy(u'Rating')))
class SearchExtensionsFilter(AddonFilter):
opts = (('popular', _lazy(u'Most Popular')),
('created', _lazy(u'Recently Added')),)
def search_tools(request, category=None):
APP, TYPE = request.APP, amo.ADDON_SEARCH
qs = Category.objects.filter(application=APP.id, type=TYPE)

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

@ -12,11 +12,10 @@ def sharing_widget(context, obj, condensed=False):
services = list(sharing.SERVICES_LIST)
share_counts = obj.share_counts()
counts = {}
for service in services:
short = service.shortname
counts[short] = service.count_term(share_counts[short])
counts[short] = service.count_term(obj.share_counts[short])
c.update({
'condensed': condensed,

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

@ -48,10 +48,10 @@ class SharingModelsTestCase(test.TestCase):
def test_share_count(self):
addon = Addon.objects.get(id=3615)
eq_(addon.share_counts()[DIGG.shortname], 29)
eq_(addon.share_counts[DIGG.shortname], 29)
# total count with no shares
eq_(addon.share_counts()[FACEBOOK.shortname], 0,
eq_(addon.share_counts[FACEBOOK.shortname], 0,
'Total count with no shares must be 0')

17
apps/sharing/utils.py Normal file
Просмотреть файл

@ -0,0 +1,17 @@
import collections
def attach_share_counts(StatsModel, key, objects):
"""
Populate obj.share_counts for each obj in the dict objects.
* StatsModel is used to run the query.
* key is the name of the foreign key.
* objects is a dict of {obj.id: obj}.
"""
for obj in objects.values():
obj.share_counts = collections.defaultdict(int)
qs = (StatsModel.objects.filter(**{'%s__in' % key: objects})
.values_list(key, 'service', 'count'))
for pk, service, count in qs:
objects[pk].share_counts[service] = count

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

@ -308,8 +308,11 @@ class UserProfile(amo.models.ModelBase):
def special_collection(self, type_, defaults):
from bandwagon.models import Collection
c, _ = Collection.objects.get_or_create(
c, new = Collection.objects.get_or_create(
author=self, type=type_, defaults=defaults)
if new:
# Do an extra query to make sure this gets transformed.
c = Collection.objects.get(id=c.id)
return c
@staticmethod