You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

40 lines
1.2 KiB

from datetime import timedelta
from django.core.management.base import BaseCommand
from apps.payment.models import CoursePayment
from apps.course.models import Course
class Command(BaseCommand):
help = 'Fix access duration in all paid courses'
def add_arguments(self, parser):
parser.add_argument('access_duration', type=int, help='New access duration',)
def handle(self, *args, **options):
access_duration = options.get('access_duration')
for course in Course.objects.filter(price__gt=0):
course.access_duration = access_duration
course.save()
for payment in CoursePayment.objects.filter(status__in=CoursePayment.PW_PAID_STATUSES):
payment.access_expire = payment.created_at.date() + timedelta(days=payment.course.access_duration)
payment.save()
'''
TEST
select c.id, c.title, cp.access_duration
from
course_course c,
(select cp.course_id, count(*), cp.access_expire - date_trunc('day', p.created_at) access_duration
from payment_coursepayment cp,
payment_payment p
where p.id = cp.payment_ptr_id and p.status = 0
group by cp.course_id, cp.access_expire - date_trunc('day', p.created_at)) cp
where cp.course_id = c.id
order by c.title
'''