File: //home/arjun/projects/buyercall_new/buyercall/buyercall/blueprints/billing/tasks.py
from flask import current_app
from buyercall.app import create_celery_app
from buyercall.blueprints.user.models import User
from buyercall.blueprints.billing.models.credit_card import CreditCard
from buyercall.blueprints.billing.models.subscription import Subscription
from buyercall.blueprints.billing.models.coupon import Coupon
from buyercall.lib.flask_mailplus import _try_renderer_template
from buyercall.lib.util_ses_email import send_ses_email
from buyercall.extensions import db
celery = create_celery_app(current_app)
@celery.task()
def mark_old_credit_cards(**kwargs):
"""
Mark credit cards that are going to expire soon or have expired.
:return: Result of updating the records
"""
return CreditCard.mark_old_credit_cards()
@celery.task()
def expire_old_coupons(**kwargs):
"""
Invalidate coupons that are past their redeem date.
:return: Result of updating the records
"""
return Coupon.expire_old_coupons()
@celery.task()
def delete_users(ids, **kwargs):
"""
Delete users and potentially cancel their subscription.
:param ids: List of ids to be deleted
:type ids: list
:return: int
"""
return User.bulk_delete(ids)
@celery.task()
def delete_coupons(ids, **kwargs):
"""
Delete coupons both on the payment gateway and locally.
:param ids: List of ids to be deleted
:type ids: list
:return: int
"""
return Coupon.bulk_delete(ids)
@celery.task()
def send_limit_warning_email(user_id, partnership_name, partnership_logo, **kwargs):
user = User.query.get(user_id)
if user is None or user.limit_warning_count > 0:
return
ctx = {'user': user, 'company': partnership_name, 'logo': partnership_logo}
template_limit_warning = _try_renderer_template('user/mail/limit_warning', ext='html', **ctx)
template_limit_warning_admin = _try_renderer_template('user/mail/limit_warning_admin', ext='html', **ctx)
send_ses_email(recipients=[user.email],
p_id=user.partnership_id,
subject='[Warning] {} - Account usage limit'.format(partnership_name),
html=template_limit_warning
)
user.limit_warning_count = 1
db.session.commit()
send_ses_email(recipients=[celery.conf['ADMIN_EMAIL']],
p_id=user.partnership_id,
subject='[Warning] Subscriber account usage notification',
html=template_limit_warning_admin
)
@celery.task()
def send_second_limit_warning_email(user_id, partnership_name, partnership_logo, **kwargs):
user = User.query.get(user_id)
if user is None or user.limit_warning_count == 2:
return
ctx = {'user': user, 'company': partnership_name, 'logo': partnership_logo}
template_limit_2d_warning = _try_renderer_template('user/mail/limit_second_warning', ext='html', **ctx)
template_limit_2nd_warning_admin = _try_renderer_template('user/mail/limit_second_warning_admin', ext='html', **ctx)
send_ses_email(recipients=[user.email],
p_id=user.partnership_id,
subject='[Final Warning] {} - Account usage limit'.format(partnership_name),
html=template_limit_2d_warning
)
user.limit_warning_count = 2
db.session.commit()
send_ses_email(recipients=[celery.conf['MAIL_DEFAULT_SENDER']],
p_id=user.partnership_id,
subject='[Final Warning] Subscriber account usage notification',
html=template_limit_2nd_warning_admin)
@celery.task()
def reset_monthly_usage(**kwargs):
"""
Reset an account monthly usage in subscription table.
:return: Result of updating the records
"""
return Subscription.reset_monthly_usage()