Save correct license for user

This commit is contained in:
Christian Cueni 2021-09-09 09:37:25 +02:00
parent e77cb477c5
commit dbd5f92fbd
2 changed files with 21 additions and 3 deletions

View File

@ -0,0 +1,17 @@
from django.utils.timezone import now
from django.core.management.base import BaseCommand
from users.models import License
class Command(BaseCommand):
help = 'Reset licenses that have a duration that is longer than the standard duration'
def add_arguments(self, parser):
parser.add_argument('--dry-run', action='store_true', dest='save', default=False, help='Make dry-run')
def handle(self, *args, **options):
today = now().date()
for license in License.objects.filter(expire_date__gte=today):
print(license)

View File

@ -147,14 +147,15 @@ class LicenseManager(models.Manager):
else:
user_role = Role.objects.get_default_student_role()
new_license = self._create_license_for_role(licensee, expiry_date, raw, user_role, order_id)
new_license = self._create_license_for_role(licensee, expiry_date, raw, user_role, order_id, isbn)
new_license.licensee.license_expiry_date = new_license.expire_date
new_license.licensee.save()
return new_license
def _create_license_for_role(self, licensee, expiry_date, raw, role, order_id):
return self.create(licensee=licensee, expire_date=expiry_date, raw=raw, for_role=role, order_id=order_id)
def _create_license_for_role(self, licensee, expiry_date, raw, role, order_id, isbn):
return self.create(licensee=licensee, expire_date=expiry_date, raw=raw, for_role=role, order_id=order_id,
isbn=isbn)
def get_active_license_for_user(self, user):
licenses = self.filter(licensee=user, expire_date__gte=timezone.now()).order_by('-expire_date')