update collection followers in a task

This commit is contained in:
Jeff Balogh 2010-09-24 15:59:29 -07:00
Родитель 394e60b441
Коммит 0ede431412
4 изменённых файлов: 46 добавлений и 14 удалений

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

@ -63,7 +63,7 @@ class CollectionManager(amo.models.ManagerBase):
"""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):
@ -168,7 +168,8 @@ class Collection(amo.models.ModelBase):
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',
@ -183,8 +184,8 @@ class Collection(amo.models.ModelBase):
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',
@ -395,6 +396,17 @@ class CollectionWatcher(amo.models.ModelBase):
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)
@ -428,7 +440,7 @@ class CollectionVote(models.Model):
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):

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

@ -12,7 +12,8 @@ from PIL import Image
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')
@ -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)
@ -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)

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

@ -6,8 +6,9 @@ import test_utils
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
@ -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')
@ -129,6 +126,14 @@ class TestCollections(test_utils.TestCase):
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']

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

@ -0,0 +1,2 @@
DROP TRIGGER IF EXISTS collections_update_subscriber_count_delete;
DROP TRIGGER IF EXISTS collections_update_subscriber_count_insert;