Add cronjob for license-sync

This commit is contained in:
Christian Cueni 2020-02-20 14:21:52 +01:00
parent 96c0b3ee64
commit 0df6cb9d93
8 changed files with 98 additions and 10 deletions

View File

@ -164,6 +164,10 @@ class HepClient:
if 'status' in order_item:
status = order_item['status']
order_id = -1
if 'order_id' in order_item:
status = order_item['order_id']
for item in order_item['items']:
if item['sku'] == settings.MYSKILLBOX_TEACHER_EDITION_ISBN or \
item['sku'] == settings.MYSKILLBOX_STUDENT_EDITION_ISBN:
@ -171,7 +175,8 @@ class HepClient:
product = {
'raw': item,
'activated': self._get_item_activation(order_item),
'status': status
'status': status,
'order_id': order_id
}
if item['sku'] == settings.MYSKILLBOX_TEACHER_EDITION_ISBN:

View File

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2020 ITerativ GmbH. All rights reserved.
#
# Created on 20.02.20
# @author: chrigu <christian.cueni@iterativ.ch>
from django.conf import settings

View File

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2020 ITerativ GmbH. All rights reserved.
#
# Created on 20.02.20
# @author: chrigu <christian.cueni@iterativ.ch>
from django.conf import settings

View File

@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2020 ITerativ GmbH. All rights reserved.
#
# Created on 20.02.20
# @author: chrigu <christian.cueni@iterativ.ch>
import os
import shutil
from django.conf import settings
from django.core import management
from django.core.management import BaseCommand
from django.db import connection
from core import hep_client
from core.models import AdminData
from users.models import User, License
class Command(BaseCommand):
def ensure_clean_dir(self, folder):
path = os.path.join(settings.MEDIA_ROOT, folder)
if os.path.exists(path):
shutil.rmtree(path)
if not os.path.exists(path):
os.makedirs(path)
def handle(self, *args, **options):
"Update licenses via cronjob"
admin_token = AdminData.objects.get_admin_token()
hep_users = User.objects.filter(hep_id__isnull=False)
for hep_user in hep_users:
product = hep_client.myskillbox_product_for_customer(admin_token, hep_user.hep_id)
if License.objects.filter(licensee=hep_users, order_id=product['order_id']).count() == 0:
License.objects.create_license_for_role(hep_user, product['activated'], product['raw'],
product['edition'], product['order_id'])

View File

@ -115,7 +115,7 @@ class UserManager(DjangoUserManager):
class LicenseManager(models.Manager):
def create_license_for_role(self, licensee, activation_date, raw, role):
def create_license_for_role(self, licensee, activation_date, raw, role, order_id):
Role = apps.get_model('users', 'Role')
if role == 'teacher':
user_role = Role.objects.get_default_teacher_role()
@ -124,10 +124,14 @@ class LicenseManager(models.Manager):
user_role = Role.objects.get_default_student_role()
expiry_date = activation_date + timedelta(STUDENT_EDITION_DURATION)
return self._create_license_for_role(licensee, expiry_date, raw, user_role)
new_license = self._create_license_for_role(licensee, expiry_date, raw, user_role, order_id)
new_license.licensee.license_expiry_date = new_license.expire_date
new_license.licensee.save()
def _create_license_for_role(self, licensee, expiry_date, raw, role):
return self.create(licensee=licensee, expire_date=expiry_date, raw=raw, for_role=role)
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 get_active_licenses_for_user(self, user):
return self.filter(licensee=user, expire_date__gte=timezone.now())\

View File

@ -0,0 +1,18 @@
# Generated by Django 2.1.15 on 2020-02-20 13:05
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('users', '0012_license'),
]
operations = [
migrations.AddField(
model_name='license',
name='order_id',
field=models.IntegerField(default=-1),
),
]

View File

@ -182,7 +182,8 @@ class License(models.Model):
for_role = models.ForeignKey(Role, blank=False, null=True, on_delete=models.CASCADE)
licensee = models.ForeignKey(User, blank=False, null=True, on_delete=models.CASCADE)
expire_date = models.DateField(blank=False, null=True,)
raw = models.TextField(default="")
order_id = models.IntegerField(blank=False, null=False, default=-1)
raw = models.TextField(default='')
objects = LicenseManager()

View File

@ -63,10 +63,8 @@ def check_and_create_licenses(hep_client, user):
return None, UNKNOWN_ERROR
if product:
license = License.objects.create_license_for_role(user, product['activated'],
product['raw'], product['edition'])
user.license_expiry_date = license.expire_date
user.save()
license = License.objects.create_license_for_role(user, product['activated'], product['raw'],
product['edition'], product['order_id'])
# todo handle no license case
else:
return None, NO_VALID_LICENSE