Add created_at fields to license
This commit is contained in:
parent
28cdc3f4f8
commit
4be9858730
|
|
@ -77,7 +77,7 @@ class UserSettingAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
@admin.register(License)
|
@admin.register(License)
|
||||||
class LicenseAdmin(admin.ModelAdmin):
|
class LicenseAdmin(admin.ModelAdmin):
|
||||||
list_display = ('licensee', 'for_role', 'expire_date', 'isbn')
|
list_display = ('licensee', 'for_role', 'expire_date', 'isbn', 'hep_created_at')
|
||||||
list_filter = ('licensee', 'expire_date')
|
list_filter = ('licensee', 'expire_date')
|
||||||
raw_id_fields = ('licensee',)
|
raw_id_fields = ('licensee',)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ from django.utils.timezone import now
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
from users.licenses import MYSKILLBOX_LICENSES
|
from users.licenses import MYSKILLBOX_LICENSES
|
||||||
from users.models import License
|
from users.models import License, NO_DATE
|
||||||
|
|
||||||
YEARLY_ISBNS = ["978-3-0355-1860-3", "978-3-0355-1823-8"]
|
YEARLY_ISBNS = ["978-3-0355-1860-3", "978-3-0355-1823-8"]
|
||||||
LONGER_DURATION = 455
|
LONGER_DURATION = 455
|
||||||
|
|
@ -47,7 +47,7 @@ class Command(BaseCommand):
|
||||||
such it the longer duration was set as duration
|
such it the longer duration was set as duration
|
||||||
"""
|
"""
|
||||||
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 == NO_DATE
|
||||||
|
|
||||||
def _update_expiry_date(self, license: License, standard_duration: int, dry_run: bool):
|
def _update_expiry_date(self, license: License, standard_duration: int, dry_run: bool):
|
||||||
new_expiry_date = license.expire_date - timedelta(LONGER_DURATION - standard_duration)
|
new_expiry_date = license.expire_date - timedelta(LONGER_DURATION - standard_duration)
|
||||||
|
|
|
||||||
|
|
@ -147,15 +147,16 @@ class LicenseManager(models.Manager):
|
||||||
else:
|
else:
|
||||||
user_role = Role.objects.get_default_student_role()
|
user_role = Role.objects.get_default_student_role()
|
||||||
|
|
||||||
new_license = self._create_license_for_role(licensee, expiry_date, raw, user_role, order_id, isbn)
|
new_license = self._create_license_for_role(licensee, expiry_date, raw, user_role, order_id, isbn,
|
||||||
|
activation_date)
|
||||||
new_license.licensee.license_expiry_date = new_license.expire_date
|
new_license.licensee.license_expiry_date = new_license.expire_date
|
||||||
new_license.licensee.save()
|
new_license.licensee.save()
|
||||||
|
|
||||||
return new_license
|
return new_license
|
||||||
|
|
||||||
def _create_license_for_role(self, licensee, expiry_date, raw, role, order_id, isbn):
|
def _create_license_for_role(self, licensee, expiry_date, raw, role, order_id, isbn, activation_date):
|
||||||
return self.create(licensee=licensee, expire_date=expiry_date, raw=raw, for_role=role, order_id=order_id,
|
return self.create(licensee=licensee, expire_date=expiry_date, new_api_raw=raw, for_role=role, order_id=order_id,
|
||||||
isbn=isbn)
|
isbn=isbn, hep_created_at=activation_date)
|
||||||
|
|
||||||
def get_active_license_for_user(self, user):
|
def get_active_license_for_user(self, user):
|
||||||
licenses = self.filter(licensee=user, expire_date__gte=timezone.now()).order_by('-expire_date')
|
licenses = self.filter(licensee=user, expire_date__gte=timezone.now()).order_by('-expire_date')
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
# Generated by Django 2.2.24 on 2021-09-22 05:50
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
from django.db import migrations, models
|
||||||
|
from django.utils.timezone import utc
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('users', '0027_auto_20210414_2116'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='license',
|
||||||
|
name='hep_created_at',
|
||||||
|
field=models.DateTimeField(default=datetime.datetime(1, 1, 1, 0, 0, tzinfo=utc)),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='license',
|
||||||
|
name='new_api_raw',
|
||||||
|
field=models.TextField(default=''),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -1,25 +1,21 @@
|
||||||
# Generated by Django 2.2.24 on 2021-09-09 11:39
|
# Generated by Django 2.2.24 on 2021-09-22 05:50
|
||||||
|
|
||||||
from datetime import datetime
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
from django.utils.timezone import make_aware
|
|
||||||
|
|
||||||
from users.models import NO_DATE
|
from users.models import AWARE_NO_DATETIME
|
||||||
|
|
||||||
MIN_DATE = datetime.combine(NO_DATE, datetime.min.time())
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
('users', '0027_auto_20210414_2116'),
|
('users', '0028_auto_20210922_0550'),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='license',
|
model_name='license',
|
||||||
name='created_at',
|
name='created_at',
|
||||||
field=models.DateTimeField(auto_now_add=True, default=make_aware(MIN_DATE)),
|
field=models.DateTimeField(auto_now_add=True, default=AWARE_NO_DATETIME),
|
||||||
preserve_default=False,
|
preserve_default=False,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
@ -18,7 +18,9 @@ from users.licenses import MYSKILLBOX_LICENSES
|
||||||
from users.managers import LicenseManager, RoleManager, UserManager, UserRoleManager
|
from users.managers import LicenseManager, RoleManager, UserManager, UserRoleManager
|
||||||
|
|
||||||
DEFAULT_SCHOOL_ID = 1
|
DEFAULT_SCHOOL_ID = 1
|
||||||
NO_DATE = date(MINYEAR, 1, 1) # date to tag licenses without tag
|
NO_DATE = date(MINYEAR, 1, 1) # date to tag licenses without date
|
||||||
|
NO_DATETIME = datetime.combine(NO_DATE, datetime.min.time())
|
||||||
|
AWARE_NO_DATETIME = make_aware(NO_DATETIME)
|
||||||
|
|
||||||
|
|
||||||
class User(AbstractUser):
|
class User(AbstractUser):
|
||||||
|
|
@ -308,11 +310,11 @@ class License(models.Model):
|
||||||
isbn = models.CharField(max_length=50, blank=False, null=False,
|
isbn = models.CharField(max_length=50, blank=False, null=False,
|
||||||
default=list(MYSKILLBOX_LICENSES.keys())[0]) # student license
|
default=list(MYSKILLBOX_LICENSES.keys())[0]) # student license
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
hep_created_at = models.DateTimeField(default=AWARE_NO_DATETIME)
|
||||||
|
new_api_raw = models.TextField(default='')
|
||||||
|
|
||||||
objects = LicenseManager()
|
objects = LicenseManager()
|
||||||
|
|
||||||
NO_DATE = NO_DATE
|
|
||||||
|
|
||||||
def is_teacher_license(self):
|
def is_teacher_license(self):
|
||||||
return self.for_role.key == RoleManager.TEACHER_KEY
|
return self.for_role.key == RoleManager.TEACHER_KEY
|
||||||
|
|
||||||
|
|
@ -330,7 +332,7 @@ class License(models.Model):
|
||||||
date_string = hep_data['created_at']
|
date_string = hep_data['created_at']
|
||||||
return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S').date()
|
return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S').date()
|
||||||
|
|
||||||
return License.NO_DATE
|
return NO_DATE
|
||||||
|
|
||||||
def get_hep_isbn(self) -> str:
|
def get_hep_isbn(self) -> str:
|
||||||
hep_data = self._read_as_json(self.raw)
|
hep_data = self._read_as_json(self.raw)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue