2010-07-22 01:58:57 +04:00
|
|
|
import logging
|
2010-09-03 09:34:39 +04:00
|
|
|
import math
|
2010-07-27 22:35:26 +04:00
|
|
|
import os
|
2010-07-22 01:58:57 +04:00
|
|
|
|
2010-09-02 05:48:19 +04:00
|
|
|
from django.conf import settings
|
2010-07-22 01:58:57 +04:00
|
|
|
from django.db.models import Count
|
|
|
|
|
2010-08-03 00:44:22 +04:00
|
|
|
from celeryutils import task
|
2010-07-22 01:58:57 +04:00
|
|
|
|
2010-07-27 00:14:45 +04:00
|
|
|
import amo
|
2011-04-28 04:44:38 +04:00
|
|
|
from amo.decorators import set_modified_on
|
2010-10-12 23:11:31 +04:00
|
|
|
from amo.utils import resize_image
|
2010-08-21 03:19:56 +04:00
|
|
|
from tags.models import Tag
|
2010-07-22 01:58:57 +04:00
|
|
|
from . import cron # Pull in tasks run through cron.
|
2010-09-25 02:59:29 +04:00
|
|
|
from .models import (Collection, CollectionAddon, CollectionVote,
|
|
|
|
CollectionWatcher)
|
2010-07-22 01:58:57 +04:00
|
|
|
|
|
|
|
log = logging.getLogger('z.task')
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
2010-08-05 22:55:08 +04:00
|
|
|
def collection_votes(*ids, **kw):
|
2010-07-22 01:58:57 +04:00
|
|
|
log.info('[%s@%s] Updating collection votes.' %
|
|
|
|
(len(ids), collection_votes.rate_limit))
|
2010-08-05 22:55:08 +04:00
|
|
|
using = kw.get('using')
|
2010-07-22 01:58:57 +04:00
|
|
|
for collection in ids:
|
2010-08-05 22:55:08 +04:00
|
|
|
v = CollectionVote.objects.filter(collection=collection).using(using)
|
2010-07-22 01:58:57 +04:00
|
|
|
votes = dict(v.values_list('vote').annotate(Count('vote')))
|
2010-08-05 22:55:08 +04:00
|
|
|
c = Collection.objects.get(id=collection)
|
2010-09-03 09:34:39 +04:00
|
|
|
c.upvotes = up = votes.get(1, 0)
|
|
|
|
c.downvotes = down = votes.get(-1, 0)
|
|
|
|
try:
|
|
|
|
# Use log to limit the effect of the multiplier.
|
|
|
|
c.rating = (up - down) * math.log(up + down)
|
|
|
|
except ValueError:
|
|
|
|
c.rating = 0
|
2010-08-05 22:55:08 +04:00
|
|
|
c.save()
|
2010-07-27 22:35:26 +04:00
|
|
|
|
|
|
|
|
2010-07-31 04:18:25 +04:00
|
|
|
@task
|
2011-04-28 04:44:38 +04:00
|
|
|
@set_modified_on
|
2011-01-24 22:47:15 +03:00
|
|
|
def resize_icon(src, dst, **kw):
|
2010-07-31 04:18:25 +04:00
|
|
|
"""Resizes collection icons to 32x32"""
|
2010-08-21 01:10:46 +04:00
|
|
|
log.info('[1@None] Resizing icon: %s' % dst)
|
2010-08-18 00:37:03 +04:00
|
|
|
|
2010-07-31 04:18:25 +04:00
|
|
|
try:
|
2010-10-12 23:11:31 +04:00
|
|
|
resize_image(src, dst, (32, 32))
|
2011-04-28 04:44:38 +04:00
|
|
|
return True
|
2010-07-31 04:18:25 +04:00
|
|
|
except Exception, e:
|
|
|
|
log.error("Error saving collection icon: %s" % e)
|
2010-07-27 00:14:45 +04:00
|
|
|
|
2010-09-25 02:59:29 +04:00
|
|
|
|
2010-09-02 05:48:19 +04:00
|
|
|
@task
|
2011-01-24 22:47:15 +03:00
|
|
|
def delete_icon(dst, **kw):
|
2010-09-02 05:48:19 +04:00
|
|
|
log.info('[1@None] Deleting icon: %s.' % dst)
|
|
|
|
|
|
|
|
if not dst.startswith(settings.COLLECTIONS_ICON_PATH):
|
2010-09-04 02:21:54 +04:00
|
|
|
log.error("Someone tried deleting something they shouldn't: %s" % dst)
|
2010-09-02 05:48:19 +04:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(dst)
|
|
|
|
except Exception, e:
|
|
|
|
log.error("Error deleting icon: %s" % e)
|
|
|
|
|
2010-07-27 00:14:45 +04:00
|
|
|
|
|
|
|
@task
|
2010-08-11 07:33:08 +04:00
|
|
|
def collection_meta(*ids, **kw):
|
2010-07-27 00:14:45 +04:00
|
|
|
log.info('[%s@%s] Updating collection metadata.' %
|
|
|
|
(len(ids), collection_meta.rate_limit))
|
2010-08-11 07:33:08 +04:00
|
|
|
using = kw.get('using')
|
2010-07-27 00:14:45 +04:00
|
|
|
qs = (CollectionAddon.objects.filter(collection__in=ids)
|
2010-08-11 07:33:08 +04:00
|
|
|
.using(using).values_list('collection'))
|
2010-07-27 00:14:45 +04:00
|
|
|
counts = dict(qs.annotate(Count('id')))
|
|
|
|
persona_counts = dict(qs.filter(addon__type=amo.ADDON_PERSONA)
|
|
|
|
.annotate(Count('id')))
|
2010-08-21 03:19:56 +04:00
|
|
|
tags = (Tag.objects.not_blacklisted().values_list('id')
|
|
|
|
.annotate(cnt=Count('id')).filter(cnt__gt=1).order_by('-cnt'))
|
2010-08-11 07:33:08 +04:00
|
|
|
for c in Collection.objects.no_cache().filter(id__in=ids):
|
|
|
|
addon_count = counts.get(c.id, 0)
|
2010-08-19 23:11:12 +04:00
|
|
|
all_personas = addon_count == persona_counts.get(c.id, None)
|
2010-07-27 03:35:22 +04:00
|
|
|
addons = list(c.addons.values_list('id', flat=True))
|
2010-08-21 03:19:56 +04:00
|
|
|
c.top_tags = [t for t, _ in tags.filter(addons__in=addons)[:5]]
|
2010-08-11 07:33:08 +04:00
|
|
|
Collection.objects.filter(id=c.id).update(addon_count=addon_count,
|
|
|
|
all_personas=all_personas)
|
2010-07-27 03:35:22 +04:00
|
|
|
|
|
|
|
|
2010-09-25 02:59:29 +04:00
|
|
|
@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:
|
2011-05-31 01:40:06 +04:00
|
|
|
try:
|
|
|
|
watchers = (CollectionWatcher.objects.filter(collection=pk)
|
|
|
|
.using(using).count())
|
|
|
|
Collection.objects.filter(pk=pk).update(subscribers=watchers)
|
|
|
|
log.info('Updated collection watchers: %s' % pk)
|
|
|
|
except Exception, e:
|
|
|
|
log.error('Updating collection watchers failed: %s, %s' % (pk, e))
|
2010-09-25 02:59:29 +04:00
|
|
|
|
|
|
|
|
2010-07-27 03:35:22 +04:00
|
|
|
@task(rate_limit='10/m')
|
2011-01-24 22:47:15 +03:00
|
|
|
def cron_collection_meta(*addons, **kw):
|
2010-07-27 03:35:22 +04:00
|
|
|
collection_meta(*addons)
|