addons-server/apps/users/tasks.py

69 строки
1.7 KiB
Python

import os
from django.conf import settings
import commonware.log
import elasticutils
from celeryutils import task
from amo.decorators import set_modified_on
from amo.utils import resize_image
from .models import UserProfile
from . import search
task_log = commonware.log.getLogger('z.task')
@task
def delete_photo(dst, **kw):
task_log.debug('[1@None] Deleting photo: %s.' % dst)
if not dst.startswith(settings.USERPICS_PATH):
task_log.error("Someone tried deleting something they shouldn't: %s"
% dst)
return
try:
os.remove(dst)
except Exception, e:
task_log.error("Error deleting userpic: %s" % e)
@task
@set_modified_on
def resize_photo(src, dst, **kw):
"""Resizes userpics to 200x200"""
task_log.debug('[1@None] Resizing photo: %s' % dst)
try:
resize_image(src, dst, (200, 200))
return True
except Exception, e:
task_log.error("Error saving userpic: %s" % e)
@task
def index_users(ids, **kw):
es = elasticutils.get_es()
task_log.debug('Indexing users %s-%s [%s].' % (ids[0], ids[-1], len(ids)))
for c in UserProfile.objects.filter(id__in=ids):
UserProfile.index(search.extract(c), bulk=True, id=c.id)
es.flush_bulk(forced=True)
@task
def unindex_users(ids, **kw):
for id in ids:
task_log.debug('Removing user [%s] from search index.' % id)
UserProfile.unindex(id)
@task(rate_limit='15/m')
def update_user_ratings_task(data, **kw):
task_log.info("[%s@%s] Updating add-on author's ratings." %
(len(data), update_user_ratings_task.rate_limit))
for pk, rating in data:
rating = "%.2f" % round(rating, 2)
UserProfile.objects.filter(pk=pk).update(averagerating=rating)