Add license check to user model
This commit is contained in:
parent
dc1cb86bb8
commit
249abff36b
|
|
@ -17,6 +17,10 @@ DEFAULT_SCHOOL_ID = 1
|
|||
|
||||
|
||||
class User(AbstractUser):
|
||||
LICENSE_NONE = 'no-license'
|
||||
LICENSE_VALID = 'valid-license'
|
||||
LICENSE_EXPIRED = 'expired-license'
|
||||
|
||||
last_module = models.ForeignKey('books.Module', related_name='+', on_delete=models.SET_NULL, null=True)
|
||||
recent_modules = models.ManyToManyField('books.Module', related_name='+', through='books.RecentModule')
|
||||
last_topic = models.ForeignKey('books.Topic', related_name='+', on_delete=models.SET_NULL, null=True)
|
||||
|
|
@ -120,6 +124,14 @@ class User(AbstractUser):
|
|||
# todo: just mocked for now, link to license before release
|
||||
return True
|
||||
|
||||
def get_license_status(self):
|
||||
if not self.license_expiry_date:
|
||||
return User.LICENSE_NONE
|
||||
now = datetime.now()
|
||||
if self.license_expiry_date >= now:
|
||||
return User.LICENSE_VALID
|
||||
return User.LICENSE_NONE
|
||||
|
||||
class Meta:
|
||||
ordering = ['pk', ]
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
from datetime import timedelta, datetime
|
||||
|
||||
from core.factories import UserFactory
|
||||
from core.tests.base_test import SkillboxTestCase
|
||||
from users.models import User
|
||||
|
||||
|
||||
class LicenseExpiredTestCase(SkillboxTestCase):
|
||||
def setUp(self) -> None:
|
||||
self.user = UserFactory(username='some_user')
|
||||
|
||||
def test_license_expired(self):
|
||||
|
||||
now = datetime.now()
|
||||
self.user.license_expiry_date = now - timedelta(days=1)
|
||||
self.user.save()
|
||||
self.assertTrue(self.user.get_license_status(), User.LICENSE_EXPIRED)
|
||||
|
||||
def test_license_valid(self):
|
||||
now = datetime.now()
|
||||
self.user.license_expiry_date = now + timedelta(days=1)
|
||||
self.user.save()
|
||||
self.assertTrue(self.user.get_license_status(), User.LICENSE_VALID)
|
||||
|
||||
def test_license_none(self):
|
||||
self.assertTrue(self.user.get_license_status(), User.LICENSE_NONE)
|
||||
Loading…
Reference in New Issue