2010-07-22 01:58:57 +04:00
|
|
|
import logging
|
2010-07-27 22:35:26 +04:00
|
|
|
import os
|
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-31 04:18:25 +04:00
|
|
|
from easy_thumbnails import processors
|
|
|
|
from PIL import Image
|
2010-07-22 01:58:57 +04:00
|
|
|
|
2010-07-27 00:14:45 +04:00
|
|
|
import amo
|
2010-07-27 03:35:22 +04:00
|
|
|
from tags.models import AddonTag
|
2010-07-22 01:58:57 +04:00
|
|
|
from . import cron # Pull in tasks run through cron.
|
2010-07-27 00:14:45 +04:00
|
|
|
from .models import Collection, CollectionAddon, CollectionVote
|
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)
|
|
|
|
c.upvotes = votes.get(1, 0)
|
|
|
|
c.downvotes = votes.get(-1, 0)
|
|
|
|
c.save()
|
2010-07-27 22:35:26 +04:00
|
|
|
|
|
|
|
|
2010-07-31 04:18:25 +04:00
|
|
|
@task
|
|
|
|
def resize_icon(src, dest):
|
|
|
|
"""Resizes collection icons to 32x32"""
|
|
|
|
try:
|
|
|
|
im = Image.open(src)
|
|
|
|
im = processors.scale_and_crop(im, (32, 32))
|
|
|
|
im.save(dest)
|
|
|
|
os.remove(src)
|
|
|
|
except Exception, e:
|
|
|
|
log.error("Error saving collection icon: %s" % e)
|
2010-07-27 00:14:45 +04:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def collection_meta(*ids):
|
|
|
|
log.info('[%s@%s] Updating collection metadata.' %
|
|
|
|
(len(ids), collection_meta.rate_limit))
|
|
|
|
qs = (CollectionAddon.objects.filter(collection__in=ids)
|
|
|
|
.values_list('collection'))
|
|
|
|
counts = dict(qs.annotate(Count('id')))
|
|
|
|
persona_counts = dict(qs.filter(addon__type=amo.ADDON_PERSONA)
|
|
|
|
.annotate(Count('id')))
|
2010-07-27 03:35:22 +04:00
|
|
|
tags = (AddonTag.objects.values_list('tag').annotate(cnt=Count('tag'))
|
|
|
|
.filter(cnt__gt=1).order_by('-cnt'))
|
2010-07-27 00:14:45 +04:00
|
|
|
for c in Collection.objects.filter(id__in=ids):
|
|
|
|
c.addon_count = counts.get(c.id, 0)
|
|
|
|
c.all_personas = c.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))
|
|
|
|
c.top_tags = [t for t, _ in tags.filter(addon__in=addons)[:5]]
|
2010-07-27 00:14:45 +04:00
|
|
|
c.save()
|
2010-07-27 03:35:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
@task(rate_limit='10/m')
|
|
|
|
def cron_collection_meta(*addons):
|
|
|
|
collection_meta(*addons)
|