Commit aa4c5c9c authored by Birin Sanchez's avatar Birin Sanchez
Browse files

Add a CLI command to extend users' active subscription a number of


days.
Signed-off-by: Birin Sanchez's avatarBirin Sanchez <birin.sanchez@puri.sm>
parent df20f228
Pipeline #30346 passed with stage
in 29 seconds
from django.core.management.base import BaseCommand
from django.core.validators import EmailValidator, ValidationError
from django.conf import settings
from django.utils import timezone
from purist.models import User
from limitmonitor.common import parse_subscription, forced_update
class Command(BaseCommand):
help = "This command extends user's active subscription by the given number \
of days."
def add_arguments(self, parser):
parser.add_argument('email',
type=str,
help='This is the email of the user that will get \
the subscription extended.')
parser.add_argument('--days',
type=int,
help='Number of days to extend the subscription. \
Default is 5.',
default=5)
def handle(self, *args, **options):
ev = EmailValidator()
email = options['email']
extra_days = options['days']
try:
ev(email)
except ValidationError as e:
self.stdout.write(repr(e))
return
(username, domain) = email.split('@')
if domain != settings.SITE_DOMAIN:
msg = '{} does not belong to {} domain'
self.stdout.write(msg.format(email, settings.SITE_DOMAIN))
return
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
msg = 'User {} does not exist.'.format(email)
self.stdout.write(msg)
return
subs = user.get_woocommerce_subscriptions()
if subs is None:
msg = 'User {} does not have any subscriptions.'.format(email)
self.stdout.write(msg)
return
for sub in subs:
parsed_sub = parse_subscription(sub)
if parsed_sub['status'] == 'active':
break
else:
msg = 'User {} has *no* active subscriptions.'.format(email)
self.stdout.write(msg)
return
new_ren_date = (parsed_sub['next_renewal']
+ timezone.timedelta(days=extra_days))
data = {
'next_payment_date': new_ren_date.strftime('%Y-%m-%d %H:%M:%S')
}
result = user.woo_post_json(
'subscriptions/' + parsed_sub['external_key'], data
)
if result is None:
self.stdout.write('Error: Could not update WC subscription.')
return
forced_update(user)
msg = 'User {} subscription has been extended {} days.'.format(
email, extra_days
)
self.stdout.write(msg)
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