addons-server/apps/users/tasks.py

64 строки
1.5 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.info('[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.info('[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):
if not settings.USE_ELASTIC:
return
es = elasticutils.get_es()
task_log.info('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):
if not settings.USE_ELASTIC:
return
for id in ids:
task_log.info('Removing user [%s] from search index.' % id)
UserProfile.unindex(id)