Check minimal date
This commit is contained in:
parent
321163e542
commit
ffb330223c
|
|
@ -21,6 +21,9 @@ MYSKILLBOX_STUDENT_EDITION_ISBN = "123-4-5678-9012-3"
|
||||||
TEACHER_EDITION_DURATION = 365
|
TEACHER_EDITION_DURATION = 365
|
||||||
STUDENT_EDITION_DURATION = 4*365
|
STUDENT_EDITION_DURATION = 4*365
|
||||||
|
|
||||||
|
TEACHER_KEY = 'teacher'
|
||||||
|
STUDENT_KEY = 'student'
|
||||||
|
|
||||||
|
|
||||||
class HepClientException(Exception):
|
class HepClientException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
@ -168,10 +171,10 @@ class HepClient:
|
||||||
}
|
}
|
||||||
|
|
||||||
if item['sku'] == MYSKILLBOX_TEACHER_EDITION_ISBN:
|
if item['sku'] == MYSKILLBOX_TEACHER_EDITION_ISBN:
|
||||||
product['edition'] = 'teacher'
|
product['edition'] = TEACHER_KEY
|
||||||
|
|
||||||
else:
|
else:
|
||||||
product['edition'] = 'student'
|
product['edition'] = STUDENT_KEY
|
||||||
|
|
||||||
products.append(product)
|
products.append(product)
|
||||||
|
|
||||||
|
|
@ -188,27 +191,48 @@ class HepClient:
|
||||||
if product['status'] != 'complete':
|
if product['status'] != 'complete':
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if product['edition'] == 'teacher':
|
if product['edition'] == TEACHER_KEY:
|
||||||
expiry_delta = product['activated'] + timedelta(TEACHER_EDITION_DURATION)
|
expiry_delta = product['activated'] + timedelta(TEACHER_EDITION_DURATION)
|
||||||
else:
|
else:
|
||||||
expiry_delta = product['activated'] + timedelta(STUDENT_EDITION_DURATION)
|
expiry_delta = product['activated'] + timedelta(STUDENT_EDITION_DURATION)
|
||||||
|
|
||||||
if HepClient.is_product_valid(expiry_delta):
|
if HepClient.is_product_active(expiry_delta, product['edition']):
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
active_products = list(filter(filter_valid_products, products))
|
active_products = list(filter(filter_valid_products, products))
|
||||||
print(active_products)
|
|
||||||
|
|
||||||
# todo can a teacher have multiple licenses?
|
|
||||||
# clarify with hep
|
|
||||||
if len(active_products) == 0:
|
if len(active_products) == 0:
|
||||||
return None
|
return None
|
||||||
elif len(active_products) == 1:
|
elif len(active_products) == 1:
|
||||||
return active_products[0]
|
return active_products[0]
|
||||||
|
else:
|
||||||
|
return self._select_from_teacher_products(active_products)
|
||||||
|
|
||||||
|
def _select_from_teacher_products(self, active_products):
|
||||||
|
teacher_edition = None
|
||||||
|
|
||||||
|
# select first teacher product, as they are all valid it does not matter which one
|
||||||
|
for product in active_products:
|
||||||
|
if product['edition'] == TEACHER_KEY:
|
||||||
|
teacher_edition = product
|
||||||
|
break
|
||||||
|
|
||||||
|
# select a student product, as they are all valid it does not matter which one
|
||||||
|
if not teacher_edition:
|
||||||
|
return active_products[0]
|
||||||
|
|
||||||
|
return teacher_edition
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def is_product_valid(expiry_date):
|
def is_product_active(expiry_date, edition):
|
||||||
return expiry_date >= datetime.now()
|
|
||||||
|
if edition == TEACHER_KEY:
|
||||||
|
duration = TEACHER_EDITION_DURATION
|
||||||
|
else:
|
||||||
|
duration = STUDENT_EDITION_DURATION
|
||||||
|
|
||||||
|
now = datetime.now()
|
||||||
|
|
||||||
|
return expiry_date >= now >= expiry_date - timedelta(days=duration)
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ class HepClientTestCases(TestCase):
|
||||||
relevant_product = self.hep_client._get_relevant_product(products)
|
relevant_product = self.hep_client._get_relevant_product(products)
|
||||||
self.assertEqual(relevant_product['raw']['id'], 0)
|
self.assertEqual(relevant_product['raw']['id'], 0)
|
||||||
|
|
||||||
def test_has_multiple_valid_products(self):
|
def test_has_multiple_valid_teacher_products_but_only_one_active(self):
|
||||||
products = [
|
products = [
|
||||||
{
|
{
|
||||||
'edition': 'teacher',
|
'edition': 'teacher',
|
||||||
|
|
@ -118,3 +118,40 @@ class HepClientTestCases(TestCase):
|
||||||
|
|
||||||
relevant_product = self.hep_client._get_relevant_product(products)
|
relevant_product = self.hep_client._get_relevant_product(products)
|
||||||
self.assertEqual(relevant_product['raw']['id'], 0)
|
self.assertEqual(relevant_product['raw']['id'], 0)
|
||||||
|
|
||||||
|
def test_has_valid_student_and_teacher_edition(self):
|
||||||
|
products = [
|
||||||
|
{
|
||||||
|
'edition': 'student',
|
||||||
|
'raw': {
|
||||||
|
'id': 0
|
||||||
|
},
|
||||||
|
'activated': self.now - timedelta(7),
|
||||||
|
'status': 'complete'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'edition': 'teacher',
|
||||||
|
'raw': {
|
||||||
|
'id': 1
|
||||||
|
},
|
||||||
|
'activated': self.now - timedelta(7),
|
||||||
|
'status': 'complete'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
relevant_product = self.hep_client._select_from_teacher_products(products)
|
||||||
|
self.assertEqual(relevant_product['raw']['id'], 1)
|
||||||
|
|
||||||
|
def test_product_is_active(self):
|
||||||
|
|
||||||
|
expiry_date = self.now + timedelta(3)
|
||||||
|
|
||||||
|
is_active = HepClient.is_product_active(expiry_date, 'teacher')
|
||||||
|
self.assertTrue(is_active)
|
||||||
|
|
||||||
|
def test_product_is_not_active(self):
|
||||||
|
|
||||||
|
expiry_date = self.now - timedelta(3 * TEACHER_EDITION_DURATION)
|
||||||
|
|
||||||
|
is_active = HepClient.is_product_active(expiry_date, 'teacher')
|
||||||
|
self.assertFalse(is_active)
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,8 @@ class License(models.Model):
|
||||||
return self.for_role.key == RoleManager.TEACHER_KEY
|
return self.for_role.key == RoleManager.TEACHER_KEY
|
||||||
|
|
||||||
def is_valid(self):
|
def is_valid(self):
|
||||||
return HepClient.is_product_valid(datetime(self.expire_date.year, self.expire_date.month, self.expire_date.day))
|
return HepClient.is_product_active(datetime(self.expire_date.year, self.expire_date.month, self.expire_date.day),
|
||||||
|
self.for_role.key)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'License for role: %s' % self.for_role
|
return 'License for role: %s' % self.for_role
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue