Commit 07ccebf5 authored by Birin Sanchez's avatar Birin Sanchez
Browse files

Adds a celery task to process subscription renewals. It will only act


on subscriptions that are active using forced_updated on those users.
Signed-off-by: Birin Sanchez's avatarBirin Sanchez <birin.sanchez@puri.sm>
parent 02d2b829
Pipeline #13863 passed with stage
in 38 seconds
......@@ -5,7 +5,7 @@ from django.utils import timezone
from django.contrib.auth.hashers import make_password
from purist.models import get_woo_connection, AccountType
from purist.limitmonitor import ParserContainer, ServicesContainer
from limitmonitor.models import ExternalCredit, ExternalBundle
from limitmonitor.models import ExternalCredit, ExternalBundle, Limit
from limitmonitor.tunnel import TunnelManager
logger = logging.getLogger(__name__)
......@@ -163,8 +163,10 @@ def forced_update(user):
parsed_sub = parse_subscription(sub)
if parsed_sub['bundle_key'] not in settings.WOOSUB1_PRODUCT_LIST:
logger.info('Subscription {} does not belong '
'to Librem One'.format(parsed_sub['bundle_key']))
logger.info('Subscription {} for user {} does not belong '
'to {}'.format(parsed_sub['bundle_key'],
user.username,
settings.SITE_TITLE))
continue # Skip this subscription as is not a product we care of
ext_cred = subscription_was_processed(parsed_sub, overwrite=True)
......@@ -264,3 +266,36 @@ def delete_account(user, purge_n=0, purge=False):
ldap_user = user.get_ldap()
ldap_user.delete()
user.delete()
def get_users_expired_subs(date_limit=None):
"""Returns the list of users for which the service renewal_date is
older than date_limit. If date_limit is not specified
timezone.now() will be used. It will omit invited users, inactive
users and users created with magic carts.
"""
magic_carts = [0, 999, 5000, 5999]
if date_limit is None:
limit = timezone.now()
else:
limit = date_limit
limits = Limit.objects.filter(renewal_date__lte=limit)
users = []
for limit in limits:
user = limit.user
if hasattr(user, 'chosenreward'):
is_magic = user.chosenreward.reward in magic_carts
else:
is_magic = False
if (
user not in users and user.is_active
and user.account_type != AccountType.INVITED
and not is_magic
):
users.append(user)
return users
......@@ -2,7 +2,8 @@ from django.conf import settings
from celery.utils.log import get_task_logger
from cart.models import ChosenReward
from limitmonitor.common import parse_subscription, get_user_subscriptions, \
subscription_was_processed, get_services_from_bundle
subscription_was_processed, get_services_from_bundle, forced_update, \
get_users_expired_subs
logger = get_task_logger(__name__)
......@@ -69,3 +70,10 @@ def process_pending_registrations():
for reg in pending_regs:
process_pending_registrations_user(reg.user)
def process_renewals():
expired_users = get_users_expired_subs()
for user in expired_users:
forced_update(user)
......@@ -38,3 +38,8 @@ def purge_users_pending_cart_task():
@shared_task
def process_pending_registrations():
subscription.process_pending_registrations()
@shared_task
def process_renewals():
subscription.process_renewals()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment