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

Commit

Permalink
update collection followers in a task
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Balogh committed Sep 24, 2010
1 parent 394e60b commit 0ede431
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
22 changes: 17 additions & 5 deletions apps/bandwagon/models.py
Expand Up @@ -63,7 +63,7 @@ def publishable_by(self, user):
"""Collections that are publishable by a user."""
owned_by = Q(author=user.id)
publishable_by = Q(users=user.id)
return self.filter(owned_by|publishable_by)
return self.filter(owned_by | publishable_by)


class Collection(amo.models.ModelBase):
Expand Down Expand Up @@ -168,7 +168,8 @@ def get_url_path(self):
args=[self.author_username, self.slug])

def get_img_dir(self):
return os.path.join(settings.COLLECTIONS_ICON_PATH, str(self.id / 1000))
return os.path.join(settings.COLLECTIONS_ICON_PATH,
str(self.id / 1000))

def upvote_url(self):
return reverse('collections.vote',
Expand All @@ -183,8 +184,8 @@ def edit_url(self):
args=[self.author_username, self.slug])

def watch_url(self):
return reverse('collections.watch',
args=[self.author_username, self.slug])
return reverse('collections.watch',
args=[self.author_username, self.slug])

def delete_url(self):
return reverse('collections.delete',
Expand Down Expand Up @@ -395,6 +396,17 @@ def flush_urls(self):
urls = ['*/user/%d/' % self.user_id]
return urls

@staticmethod
def post_save_or_delete(sender, instance, **kw):
from . import tasks
tasks.collection_watchers(instance.collection_id, using='default')


models.signals.post_save.connect(CollectionWatcher.post_save_or_delete,
sender=CollectionWatcher)
models.signals.post_delete.connect(CollectionWatcher.post_save_or_delete,
sender=CollectionWatcher)


class CollectionUser(models.Model):
collection = models.ForeignKey(Collection)
Expand Down Expand Up @@ -428,7 +440,7 @@ def post_save_or_delete(sender, instance, **kwargs):
models.signals.post_save.connect(CollectionVote.post_save_or_delete,
sender=CollectionVote)
models.signals.post_delete.connect(CollectionVote.post_save_or_delete,
sender=CollectionVote)
sender=CollectionVote)


class SyncedCollection(Collection):
Expand Down
15 changes: 14 additions & 1 deletion apps/bandwagon/tasks.py
Expand Up @@ -12,7 +12,8 @@
import amo
from tags.models import Tag
from . import cron # Pull in tasks run through cron.
from .models import Collection, CollectionAddon, CollectionVote
from .models import (Collection, CollectionAddon, CollectionVote,
CollectionWatcher)

log = logging.getLogger('z.task')

Expand Down Expand Up @@ -49,6 +50,7 @@ def resize_icon(src, dst):
except Exception, e:
log.error("Error saving collection icon: %s" % e)


@task
def delete_icon(dst):
log.info('[1@None] Deleting icon: %s.' % dst)
Expand Down Expand Up @@ -84,6 +86,17 @@ def collection_meta(*ids, **kw):
all_personas=all_personas)


@task
def collection_watchers(*ids, **kw):
log.info('[%s@%s] Updating collection watchers.' %
(len(ids), collection_watchers.rate_limit))
using = kw.get('using')
for pk in ids:
watchers = (CollectionWatcher.objects.filter(collection=pk)
.using(using).count())
Collection.objects.get(pk=pk).update(subscribers=watchers)


@task(rate_limit='10/m')
def cron_collection_meta(*addons):
collection_meta(*addons)
21 changes: 13 additions & 8 deletions apps/bandwagon/tests/test_models.py
Expand Up @@ -6,8 +6,9 @@

import amo
from addons.models import Addon, AddonRecommendation
from bandwagon.models import (Collection, CollectionUser, SyncedCollection,
RecommendedCollection)
from bandwagon.models import (Collection, CollectionUser, CollectionWatcher,
SyncedCollection, RecommendedCollection)
from bandwagon import tasks
from users.models import UserProfile


Expand All @@ -17,12 +18,8 @@ def get_addons(c):


class TestCollections(test_utils.TestCase):
fixtures = ('base/apps',
'base/users',
'base/addon_3615',
'base/collections',
'bandwagon/test_models'
)
fixtures = ['base/apps', 'base/users', 'base/addon_3615',
'base/collections', 'bandwagon/test_models']

def setUp(self):
self.user = UserProfile.objects.create(username='uhhh', email='uh@hh')
Expand Down Expand Up @@ -129,6 +126,14 @@ def test_slug_dupe(self):
c = Collection.objects.create(author=self.user, slug='boom')
eq_(c.slug, 'boom-2')

def test_watchers(self):
def check(num):
eq_(Collection.objects.get(id=512).subscribers, num)
tasks.collection_watchers(512)
check(0)
CollectionWatcher.objects.create(collection_id=512, user=self.user)
check(1)


class TestRecommendations(test_utils.TestCase):
fixtures = ['base/addon-recs']
Expand Down
2 changes: 2 additions & 0 deletions migrations/80-collection-triggers.sql
@@ -0,0 +1,2 @@
DROP TRIGGER IF EXISTS collections_update_subscriber_count_delete;
DROP TRIGGER IF EXISTS collections_update_subscriber_count_insert;

0 comments on commit 0ede431

Please sign in to comment.