update collection followers in a task
This commit is contained in:
Родитель
394e60b441
Коммит
0ede431412
|
@ -63,7 +63,7 @@ class CollectionManager(amo.models.ManagerBase):
|
||||||
"""Collections that are publishable by a user."""
|
"""Collections that are publishable by a user."""
|
||||||
owned_by = Q(author=user.id)
|
owned_by = Q(author=user.id)
|
||||||
publishable_by = Q(users=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):
|
class Collection(amo.models.ModelBase):
|
||||||
|
@ -168,7 +168,8 @@ class Collection(amo.models.ModelBase):
|
||||||
args=[self.author_username, self.slug])
|
args=[self.author_username, self.slug])
|
||||||
|
|
||||||
def get_img_dir(self):
|
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):
|
def upvote_url(self):
|
||||||
return reverse('collections.vote',
|
return reverse('collections.vote',
|
||||||
|
@ -183,8 +184,8 @@ class Collection(amo.models.ModelBase):
|
||||||
args=[self.author_username, self.slug])
|
args=[self.author_username, self.slug])
|
||||||
|
|
||||||
def watch_url(self):
|
def watch_url(self):
|
||||||
return reverse('collections.watch',
|
return reverse('collections.watch',
|
||||||
args=[self.author_username, self.slug])
|
args=[self.author_username, self.slug])
|
||||||
|
|
||||||
def delete_url(self):
|
def delete_url(self):
|
||||||
return reverse('collections.delete',
|
return reverse('collections.delete',
|
||||||
|
@ -395,6 +396,17 @@ class CollectionWatcher(amo.models.ModelBase):
|
||||||
urls = ['*/user/%d/' % self.user_id]
|
urls = ['*/user/%d/' % self.user_id]
|
||||||
return urls
|
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):
|
class CollectionUser(models.Model):
|
||||||
collection = models.ForeignKey(Collection)
|
collection = models.ForeignKey(Collection)
|
||||||
|
@ -428,7 +440,7 @@ class CollectionVote(models.Model):
|
||||||
models.signals.post_save.connect(CollectionVote.post_save_or_delete,
|
models.signals.post_save.connect(CollectionVote.post_save_or_delete,
|
||||||
sender=CollectionVote)
|
sender=CollectionVote)
|
||||||
models.signals.post_delete.connect(CollectionVote.post_save_or_delete,
|
models.signals.post_delete.connect(CollectionVote.post_save_or_delete,
|
||||||
sender=CollectionVote)
|
sender=CollectionVote)
|
||||||
|
|
||||||
|
|
||||||
class SyncedCollection(Collection):
|
class SyncedCollection(Collection):
|
||||||
|
|
|
@ -12,7 +12,8 @@ from PIL import Image
|
||||||
import amo
|
import amo
|
||||||
from tags.models import Tag
|
from tags.models import Tag
|
||||||
from . import cron # Pull in tasks run through cron.
|
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')
|
log = logging.getLogger('z.task')
|
||||||
|
|
||||||
|
@ -49,6 +50,7 @@ def resize_icon(src, dst):
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
log.error("Error saving collection icon: %s" % e)
|
log.error("Error saving collection icon: %s" % e)
|
||||||
|
|
||||||
|
|
||||||
@task
|
@task
|
||||||
def delete_icon(dst):
|
def delete_icon(dst):
|
||||||
log.info('[1@None] Deleting icon: %s.' % dst)
|
log.info('[1@None] Deleting icon: %s.' % dst)
|
||||||
|
@ -84,6 +86,17 @@ def collection_meta(*ids, **kw):
|
||||||
all_personas=all_personas)
|
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')
|
@task(rate_limit='10/m')
|
||||||
def cron_collection_meta(*addons):
|
def cron_collection_meta(*addons):
|
||||||
collection_meta(*addons)
|
collection_meta(*addons)
|
||||||
|
|
|
@ -6,8 +6,9 @@ import test_utils
|
||||||
|
|
||||||
import amo
|
import amo
|
||||||
from addons.models import Addon, AddonRecommendation
|
from addons.models import Addon, AddonRecommendation
|
||||||
from bandwagon.models import (Collection, CollectionUser, SyncedCollection,
|
from bandwagon.models import (Collection, CollectionUser, CollectionWatcher,
|
||||||
RecommendedCollection)
|
SyncedCollection, RecommendedCollection)
|
||||||
|
from bandwagon import tasks
|
||||||
from users.models import UserProfile
|
from users.models import UserProfile
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,12 +18,8 @@ def get_addons(c):
|
||||||
|
|
||||||
|
|
||||||
class TestCollections(test_utils.TestCase):
|
class TestCollections(test_utils.TestCase):
|
||||||
fixtures = ('base/apps',
|
fixtures = ['base/apps', 'base/users', 'base/addon_3615',
|
||||||
'base/users',
|
'base/collections', 'bandwagon/test_models']
|
||||||
'base/addon_3615',
|
|
||||||
'base/collections',
|
|
||||||
'bandwagon/test_models'
|
|
||||||
)
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.user = UserProfile.objects.create(username='uhhh', email='uh@hh')
|
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')
|
c = Collection.objects.create(author=self.user, slug='boom')
|
||||||
eq_(c.slug, 'boom-2')
|
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):
|
class TestRecommendations(test_utils.TestCase):
|
||||||
fixtures = ['base/addon-recs']
|
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;
|
Загрузка…
Ссылка в новой задаче