2013-06-03 16:31:39 +00:00
|
|
|
"""
|
|
|
|
external library imports
|
|
|
|
"""
|
2012-03-30 07:06:57 +00:00
|
|
|
import logging
|
2011-12-19 06:33:13 +00:00
|
|
|
|
2012-03-30 07:06:57 +00:00
|
|
|
from celery.task import task
|
2013-06-03 16:31:39 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
from time import sleep
|
2011-12-19 06:33:13 +00:00
|
|
|
|
2013-06-03 16:31:39 +00:00
|
|
|
"""
|
|
|
|
django imports
|
|
|
|
"""
|
2012-07-09 17:08:49 +00:00
|
|
|
from django.conf import settings
|
2013-06-03 16:31:39 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.core.mail import send_mail
|
|
|
|
from notification.engine import send_all
|
|
|
|
from notification import models as notification
|
2012-02-16 18:19:36 +00:00
|
|
|
|
2013-06-03 16:31:39 +00:00
|
|
|
"""
|
|
|
|
regluit imports
|
|
|
|
"""
|
|
|
|
from regluit.core import (
|
|
|
|
bookloader,
|
|
|
|
models,
|
|
|
|
goodreads,
|
2014-06-05 23:31:14 +00:00
|
|
|
librarything,
|
2015-01-23 23:52:35 +00:00
|
|
|
mobigen
|
2013-06-03 16:31:39 +00:00
|
|
|
)
|
2015-01-23 18:11:19 +00:00
|
|
|
from regluit.core.models import Campaign, Acq, Gift
|
2012-11-21 16:09:07 +00:00
|
|
|
from regluit.core.signals import deadline_impending
|
2014-09-04 22:33:20 +00:00
|
|
|
from regluit.core.parameters import RESERVE, REWARDS, THANKS
|
2013-03-01 22:43:43 +00:00
|
|
|
|
2012-04-27 21:29:57 +00:00
|
|
|
from regluit.utils.localdatetime import now, date_today
|
|
|
|
|
2013-06-03 16:31:39 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
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
|
|
|
|
|
|
|
@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,
|
2012-07-09 17:08:49 +00:00
|
|
|
connection=None, override_from_email=True):
|
|
|
|
"""a task to drop django.core.mail.send_mail into """
|
|
|
|
# NOTE: since we are currently using Amazon SES, which allows email to be sent only from validated email
|
|
|
|
# addresses, we force from_email to be one of the validated address unless override_from_email is FALSE
|
2012-07-10 20:06:57 +00:00
|
|
|
try:
|
2012-07-10 20:03:36 +00:00
|
|
|
if override_from_email:
|
|
|
|
try:
|
|
|
|
from_email = settings.DEFAULT_FROM_EMAIL
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
r= send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=auth_user,
|
2012-06-30 01:03:56 +00:00
|
|
|
auth_password=auth_password, connection=connection)
|
2012-07-10 19:57:44 +00:00
|
|
|
except:
|
|
|
|
r=logger.info('failed to send message:' + message)
|
|
|
|
return r
|
|
|
|
|
2012-06-30 01:03:56 +00:00
|
|
|
#task to update the status of active campaigns
|
|
|
|
@task
|
|
|
|
def update_active_campaign_status():
|
|
|
|
"""update the status of all active campaigns -- presumed to be run at midnight Eastern time"""
|
2012-11-13 18:15:38 +00:00
|
|
|
return [c.update_status(send_notice=True, ignore_deadline_for_success=True, process_transactions=True) for c in Campaign.objects.filter(status='Active') ]
|
2013-03-01 22:43:43 +00:00
|
|
|
|
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())
|
2016-10-04 17:22:01 +00:00
|
|
|
works = models.Work.objects.filter(editions__ebooks__created__range=period).distinct()
|
2012-04-27 21:29:57 +00:00
|
|
|
for work in works:
|
2016-10-04 17:22:01 +00:00
|
|
|
# only notify if a new ebooks are active, don't notify person if the book was just wished
|
|
|
|
# ebooks() only returns active ebooks
|
|
|
|
for ebook in work.ebooks().filter(created__range=period):
|
|
|
|
notification.send_now(
|
|
|
|
work.wished_by(excluding=period),
|
|
|
|
"wishlist_unglued_book_released",
|
|
|
|
{'work':work},
|
|
|
|
True
|
|
|
|
)
|
|
|
|
break
|
2012-11-21 16:09:07 +00:00
|
|
|
|
|
|
|
@task
|
|
|
|
def notify_ending_soon():
|
2013-12-20 22:33:55 +00:00
|
|
|
c_active = Campaign.objects.filter(status='Active', type=REWARDS)
|
2012-11-21 16:09:07 +00:00
|
|
|
for c in c_active:
|
|
|
|
if c.deadline - now() < timedelta(7) and c.deadline - now() >= timedelta(6):
|
|
|
|
"""
|
|
|
|
if the campaign is still active and there's only a week left until it closes, send reminder notification
|
|
|
|
"""
|
|
|
|
deadline_impending.send(sender=None, campaign=c)
|
|
|
|
|
2013-08-27 03:56:01 +00:00
|
|
|
@task
|
|
|
|
def watermark_acq(acq):
|
2013-09-06 02:54:11 +00:00
|
|
|
acq.get_watermarked()
|
2013-11-08 17:13:34 +00:00
|
|
|
|
2014-08-30 16:04:50 +00:00
|
|
|
@task
|
2014-09-04 22:33:20 +00:00
|
|
|
def process_ebfs(campaign):
|
|
|
|
if campaign.type == THANKS:
|
|
|
|
if campaign.use_add_ask:
|
|
|
|
campaign.add_ask_to_ebfs()
|
|
|
|
else:
|
2016-08-24 19:41:29 +00:00
|
|
|
campaign.revert_asks()
|
|
|
|
campaign.make_mobis()
|
|
|
|
|
2016-03-29 17:06:23 +00:00
|
|
|
|
|
|
|
@task
|
2016-08-24 19:41:29 +00:00
|
|
|
def make_mobi(ebookfile):
|
|
|
|
return ebookfile.make_mobi()
|
2014-08-30 16:04:50 +00:00
|
|
|
|
2013-11-08 17:13:34 +00:00
|
|
|
@task
|
|
|
|
def refresh_acqs():
|
|
|
|
in_10_min = now() + timedelta(minutes=10)
|
|
|
|
acqs = Acq.objects.filter(refreshed=False, refreshes__lt=in_10_min)
|
|
|
|
logger.info('refreshing %s acqs' % acqs.count())
|
|
|
|
for acq in acqs:
|
|
|
|
for hold in acq.holds:
|
|
|
|
# create a 1 day reserve on the acq
|
|
|
|
reserve_acq = Acq.objects.create(
|
|
|
|
user = hold.user,
|
|
|
|
work = hold.work,
|
|
|
|
license = RESERVE,
|
|
|
|
lib_acq = acq,
|
|
|
|
)
|
|
|
|
# the post_save handler takes care of pushing expires vis acq.expires_in
|
|
|
|
|
|
|
|
# notify the user with the hold
|
|
|
|
if 'example.org' not in reserve_acq.user.email:
|
|
|
|
notification.send([reserve_acq.user], "library_reserve", {'acq':reserve_acq})
|
|
|
|
# delete the hold
|
|
|
|
hold.delete()
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
acq.refreshed = True
|
2014-05-07 22:37:19 +00:00
|
|
|
|
2015-01-23 23:52:35 +00:00
|
|
|
@task
|
|
|
|
def convert_to_mobi(input_url, input_format="application/epub+zip"):
|
|
|
|
return mobigen.convert_to_mobi(input_url, input_format)
|
|
|
|
|
|
|
|
@task
|
|
|
|
def generate_mobi_ebook_for_edition(edition):
|
|
|
|
return mobigen.generate_mobi_ebook_for_edition(edition)
|
2014-06-05 23:31:14 +00:00
|
|
|
|
2014-05-07 22:37:19 +00:00
|
|
|
from postmonkey import PostMonkey, MailChimpException
|
|
|
|
pm = PostMonkey(settings.MAILCHIMP_API_KEY)
|
|
|
|
|
|
|
|
@task
|
|
|
|
def ml_subscribe_task(profile, **kwargs):
|
|
|
|
try:
|
|
|
|
if not profile.on_ml:
|
|
|
|
return pm.listSubscribe(id=settings.MAILCHIMP_NEWS_ID, email_address=profile.user.email, **kwargs)
|
|
|
|
except Exception, e:
|
|
|
|
logger.error("error subscribing to mailchimp list %s" % (e))
|
|
|
|
return False
|
2015-01-23 18:11:19 +00:00
|
|
|
|
|
|
|
@task
|
|
|
|
def notify_unclaimed_gifts():
|
|
|
|
unclaimed = Gift.objects.filter(used=None)
|
|
|
|
for gift in unclaimed:
|
|
|
|
"""
|
2017-12-14 21:24:26 +00:00
|
|
|
send notice every 7 days, but stop at 10x
|
2015-01-23 18:11:19 +00:00
|
|
|
"""
|
|
|
|
unclaimed_duration = (now() - gift.acq.created ).days
|
2017-12-14 21:24:26 +00:00
|
|
|
if unclaimed_duration > 70:
|
|
|
|
return
|
2015-01-23 18:11:19 +00:00
|
|
|
if unclaimed_duration > 0 and unclaimed_duration % 7 == 0 : # first notice in 7 days
|
|
|
|
notification.send_now([gift.acq.user], "purchase_gift_waiting", {'gift':gift}, True)
|
|
|
|
notification.send_now([gift.giver], "purchase_notgot_gift", {'gift':gift}, True)
|