From fbc441d1eaa4c9fd00e961acefe0b84a1b0cda5c Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Thu, 9 Sep 2021 13:22:44 +0200 Subject: [PATCH] Save new expiry_dates to models --- .../commands/reset_license_duration.py | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/server/users/management/commands/reset_license_duration.py b/server/users/management/commands/reset_license_duration.py index 9b66f3d5..795f5593 100644 --- a/server/users/management/commands/reset_license_duration.py +++ b/server/users/management/commands/reset_license_duration.py @@ -1,5 +1,5 @@ import json -from datetime import datetime, date +from datetime import datetime, date, timedelta from django.utils.timezone import now from django.core.management.base import BaseCommand @@ -18,7 +18,7 @@ class Command(BaseCommand): """ 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): today = now().date() @@ -28,16 +28,16 @@ class Command(BaseCommand): if isbn not in YEARLY_ISBNS: 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() duration_in_days = (license.expire_date - start_date).days - if duration_in_days == defined_duration: + if duration_in_days == standard_duration: continue 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): - 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: """ @@ -48,3 +48,21 @@ class Command(BaseCommand): """ return user_join_delta == LONGER_DURATION or duration_in_days == LONGER_DURATION \ 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()