Save new expiry_dates to models

This commit is contained in:
Christian Cueni 2021-09-09 13:22:44 +02:00
parent adba163c77
commit fbc441d1ea
1 changed files with 23 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import json import json
from datetime import datetime, date from datetime import datetime, date, timedelta
from django.utils.timezone import now from django.utils.timezone import now
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
@ -18,7 +18,7 @@ class Command(BaseCommand):
""" """
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('--dry-run', action='store_true', dest='save', default=False, help='Make dry-run') parser.add_argument('--dry-run', action='store_true', dest='dry_run', default=False, help='Make dry-run')
def handle(self, *args, **options): def handle(self, *args, **options):
today = now().date() today = now().date()
@ -28,16 +28,16 @@ class Command(BaseCommand):
if isbn not in YEARLY_ISBNS: if isbn not in YEARLY_ISBNS:
continue continue
defined_duration = MYSKILLBOX_LICENSES[isbn]['duration'] # use isbn from hep due to our bug standard_duration = MYSKILLBOX_LICENSES[isbn]['duration'] # use isbn from hep due to our bug
start_date = license.get_hep_start_date() start_date = license.get_hep_start_date()
duration_in_days = (license.expire_date - start_date).days duration_in_days = (license.expire_date - start_date).days
if duration_in_days == defined_duration: if duration_in_days == standard_duration:
continue continue
user_join_delta = (license.expire_date - license.licensee.date_joined.date()).days user_join_delta = (license.expire_date - license.licensee.date_joined.date()).days
if self._is_change_candidate(user_join_delta, duration_in_days, start_date): if self._is_change_candidate(user_join_delta, duration_in_days, start_date):
print(license.id, duration_in_days, defined_duration, license.expire_date, start_date, user_join_delta) self._update_expiry_date(license, standard_duration, options['dry_run'])
def _is_change_candidate(self, user_join_delta: int, duration_in_days: int, start_date: date) -> bool: def _is_change_candidate(self, user_join_delta: int, duration_in_days: int, start_date: date) -> bool:
""" """
@ -48,3 +48,21 @@ class Command(BaseCommand):
""" """
return user_join_delta == LONGER_DURATION or duration_in_days == LONGER_DURATION \ return user_join_delta == LONGER_DURATION or duration_in_days == LONGER_DURATION \
or start_date == License.NO_DATE or start_date == License.NO_DATE
def _update_expiry_date(self, license: License, standard_duration: int, dry_run: bool):
new_expiry_date = license.expire_date - timedelta(LONGER_DURATION - standard_duration)
user_expire_date = license.licensee.license_expiry_date
if user_expire_date == license.expire_date:
print(f'Resetting user expire_date for userID {license.licensee.id} from'
f' {license.licensee.license_expiry_date} to {new_expiry_date}')
if not dry_run:
license.licensee.license_expiry_date = new_expiry_date
license.licensee.save()
print(f'Resetting expiry date for license ID {license.id} ({license.expire_date}) to {new_expiry_date}')
if not dry_run:
license.expire_date = new_expiry_date
license.save()