2011-12-19 06:33:13 +00:00
|
|
|
from time import sleep
|
2012-04-27 21:29:57 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2012-03-30 07:06:57 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
2011-12-19 06:33:13 +00:00
|
|
|
|
2012-03-30 07:06:57 +00:00
|
|
|
from celery.task import task
|
2011-12-19 06:33:13 +00:00
|
|
|
|
2012-02-16 18:19:36 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
|
|
from regluit.core import bookloader, models
|
2011-11-02 00:42:39 +00:00
|
|
|
from regluit.core import goodreads, librarything
|
2012-04-27 21:29:57 +00:00
|
|
|
from regluit.utils.localdatetime import now, date_today
|
|
|
|
|
2011-10-20 03:28:36 +00:00
|
|
|
|
2011-12-19 06:33:13 +00:00
|
|
|
@task
|
2012-02-16 18:19:36 +00:00
|
|
|
def populate_edition(isbn):
|
2011-12-19 06:33:13 +00:00
|
|
|
"""given an edition this task will populate the database with additional
|
|
|
|
information about related editions and subjects related to this edition
|
|
|
|
"""
|
2012-02-16 18:19:36 +00:00
|
|
|
bookloader.add_related(isbn)
|
|
|
|
edition=models.Edition.get_by_isbn(isbn)
|
|
|
|
if edition:
|
|
|
|
bookloader.add_openlibrary(edition.work)
|
2011-12-19 06:33:13 +00:00
|
|
|
return edition
|
2011-11-01 00:26:05 +00:00
|
|
|
|
|
|
|
@task
|
2012-02-16 18:19:36 +00:00
|
|
|
def load_goodreads_shelf_into_wishlist(user_id, shelf_name='all', goodreads_user_id=None, max_books=None,
|
2011-11-10 23:14:33 +00:00
|
|
|
expected_number_of_books=None):
|
2012-02-16 18:19:36 +00:00
|
|
|
user=User.objects.get(id=user_id)
|
2011-11-10 23:14:33 +00:00
|
|
|
return goodreads.load_goodreads_shelf_into_wishlist(user,shelf_name,goodreads_user_id,max_books, expected_number_of_books)
|
2012-02-16 18:19:36 +00:00
|
|
|
|
2011-11-02 00:42:39 +00:00
|
|
|
@task
|
2012-02-16 18:19:36 +00:00
|
|
|
def load_librarything_into_wishlist(user_id, lt_username, max_books=None):
|
|
|
|
user=User.objects.get(id=user_id)
|
2011-11-17 00:47:29 +00:00
|
|
|
return librarything.load_librarything_into_wishlist(user, lt_username, max_books)
|
2011-11-02 00:42:39 +00:00
|
|
|
|
2011-11-10 01:31:31 +00:00
|
|
|
@task
|
2011-11-10 17:33:22 +00:00
|
|
|
def fac(n, sleep_interval=None):
|
2012-03-29 20:10:14 +00:00
|
|
|
# used to test celery task execution
|
2011-11-10 01:31:31 +00:00
|
|
|
if not(isinstance(n,int) and n >= 0):
|
|
|
|
raise Exception("You can't calculate a factorial of %s " % (str(n)))
|
|
|
|
if n <= 1:
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
res = 1
|
|
|
|
for i in xrange(2,n+1):
|
|
|
|
res = res*i
|
2011-11-10 17:33:22 +00:00
|
|
|
fac.update_state(state="PROGRESS", meta={"current": i, "total": n})
|
|
|
|
if sleep_interval is not None:
|
|
|
|
sleep(sleep_interval)
|
2011-11-10 01:31:31 +00:00
|
|
|
return res
|
2012-03-29 20:10:14 +00:00
|
|
|
|
2012-03-31 00:31:54 +00:00
|
|
|
from django.core import mail
|
|
|
|
|
|
|
|
@task
|
|
|
|
def send_mail_task(subject, message, from_email, recipient_list,
|
2012-04-27 21:29:57 +00:00
|
|
|
fail_silently=False, auth_user=None, auth_password=None,
|
|
|
|
connection=None):
|
2012-03-31 00:31:54 +00:00
|
|
|
"""a task to drop django.core.mail.send_mail into """
|
|
|
|
return mail.send_mail(subject, message, from_email, recipient_list, fail_silently, auth_user, auth_password, connection)
|
|
|
|
|
|
|
|
|
2012-03-30 07:06:57 +00:00
|
|
|
from notification.engine import send_all
|
2012-04-27 21:29:57 +00:00
|
|
|
from notification import models as notification
|
2012-03-30 07:06:57 +00:00
|
|
|
|
|
|
|
@task
|
|
|
|
def emit_notifications():
|
|
|
|
logger.info('notifications emitting' )
|
2012-04-03 14:45:12 +00:00
|
|
|
return send_all()
|
2012-04-27 21:29:57 +00:00
|
|
|
|
|
|
|
@task
|
|
|
|
def report_new_ebooks(created=None): #created= creation date
|
|
|
|
if created:
|
|
|
|
period = (created, created+timedelta(days=1))
|
|
|
|
else:
|
|
|
|
period = (date_today()-timedelta(days=1), date_today())
|
|
|
|
works = models.Work.objects.filter(editions__ebooks__created__range = period).distinct()
|
|
|
|
for work in works:
|
|
|
|
notification.send_now(work.wished_by(), "wishlist_unglued_book_released", {'work':work}, True)
|
|
|
|
|
2012-04-03 14:45:12 +00:00
|
|
|
|