Move license to core

This commit is contained in:
Christian Cueni 2020-02-20 13:22:15 +01:00
parent 1152f1fd95
commit 96c0b3ee64
21 changed files with 125 additions and 179 deletions

View File

@ -31,10 +31,10 @@ class ApiAccessTestCase(TestCase):
def test_publicGraphqlEndpoint_shouldBeAccessibleWithoutLogin(self):
query= json.dumps({
'operationName': 'Login',
'operationName': 'LocalLogin',
'query': '''
mutation Login($input: LoginInput!){
login(input: $input) {
mutation LocalLogin($input: LocalLoginInput!){
localLogin(input: $input) {
success
errors {
field

View File

@ -17,9 +17,8 @@ from graphene.test import Client
from api.schema import schema
from core.factories import UserFactory
from core.hep_client import HepClient
from core.tests.mock_hep_data_factory import MockResponse, ME_DATA, VALID_TEACHERS_ORDERS
from registration.models import License
from users.models import User, Role, SchoolClass
from core.tests.mock_hep_data_factory import MockResponse, VALID_TEACHERS_ORDERS
from users.models import License, Role, SchoolClass
class CouponTests(TestCase):

View File

@ -30,7 +30,7 @@ class MiddlewareTestCase(TestCase):
def test_user_without_valid_license_cannot_see_private_api(self):
yesterday = timezone.now() - timedelta(1)
user = UserFactory(username='aschiman@ch.ch')
user = UserFactory(username='aschiman@ch.ch', hep_id=23)
user.license_expiry_date = yesterday
body = b'"{mutation {\\n addRoom}"'

View File

@ -28,7 +28,10 @@ def is_private_api_call_allowed(user, body):
body_unicode = body.decode('utf-8')
if not user.hep_id:
try:
if not user.hep_id:
return True
except AttributeError:
return True
if re.search(r"mutation\s*.*\s*logout\s*{", body_unicode) or re.search(r"query\s*.*\s*me\s*{", body_unicode)\

View File

@ -1,19 +0,0 @@
# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
#
# Created on 2019-10-10
# @author: chrigu <christian.cueni@iterativ.ch>
from django.contrib import admin
from registration.models import License
@admin.register(License)
class LicenseAdmin(admin.ModelAdmin):
list_display = ('licensee',)
list_filter = ('licensee',)
raw_id_fields = ('licensee',)

View File

@ -1,20 +0,0 @@
# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
#
# Created on 2019-10-08
# @author: chrigu <christian.cueni@iterativ.ch>
import random
import factory
from registration.models import License
class LicenseFactory(factory.django.DjangoModelFactory):
class Meta:
model = License

View File

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

View File

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

View File

@ -1,30 +0,0 @@
# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
#
# Created on 2019-10-23
# @author: chrigu <christian.cueni@iterativ.ch>
from django.conf import settings
from django.core.management import BaseCommand
# from registration.models import LicenseType
from users.models import Role
class Command(BaseCommand):
def handle(self, *args, **options):
try:
role = Role.objects.get(key=Role.objects.TEACHER_KEY)
except Role.DoesNotExist:
print("LicenseType requires that a Teacher Role exsits")
# LicenseType.objects.create(name='dummy_license',
# for_role=role,
# active=True,
# key='c1fa2e2a-2e27-480d-8469-2e88414c4ad8',
# description='dummy license')

View File

@ -1,35 +0,0 @@
# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2020 ITerativ GmbH. All rights reserved.
#
# Created on 27.01.20
# @author: chrigu <christian.cueni@iterativ.ch>
from datetime import timedelta
from django.utils import timezone
from django.db import models
from core.hep_client import TEACHER_EDITION_DURATION, STUDENT_EDITION_DURATION
from users.models import Role
class LicenseManager(models.Manager):
def create_license_for_role(self, licensee, activation_date, raw, role):
if role == 'teacher':
user_role = Role.objects.get_default_teacher_role()
expiry_date = activation_date + timedelta(TEACHER_EDITION_DURATION)
else:
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)
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)
def get_active_licenses_for_user(self, user):
return self.filter(licensee=user, expire_date__gte=timezone.now())\
.filter(expire_date__lt=timezone.now()).order_by('-expire_date')

View File

@ -0,0 +1,24 @@
# Generated by Django 2.1.15 on 2020-02-20 10:39
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('registration', '0003_auto_20200204_1331'),
]
operations = [
migrations.RemoveField(
model_name='license',
name='for_role',
),
migrations.RemoveField(
model_name='license',
name='licensee',
),
migrations.DeleteModel(
name='License',
),
]

View File

@ -1,36 +0,0 @@
# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
#
# Created on 2019-10-08
# @author: chrigu <christian.cueni@iterativ.ch>
from datetime import datetime
from django.db import models
from core.hep_client import HepClient
from registration.managers import LicenseManager
from users.managers import RoleManager
from users.models import Role, User
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="")
objects = LicenseManager()
def is_teacher_license(self):
return self.for_role.key == RoleManager.TEACHER_KEY
def is_valid(self):
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):
return 'License for role: %s' % self.for_role

View File

@ -14,7 +14,6 @@ from graphene import relay
from core.hep_client import HepClient, HepClientException
from core.models import AdminData
from users.user_signup_login_handler import handle_user_and_verify_products, UNKNOWN_ERROR
from users.models import User
class RegistrationError(graphene.ObjectType):

View File

@ -9,7 +9,6 @@
# @author: chrigu <christian.cueni@iterativ.ch>
from unittest.mock import patch
import requests
from django.contrib.sessions.middleware import SessionMiddleware
from django.test import TestCase, RequestFactory
from graphene.test import Client
@ -17,7 +16,7 @@ from graphene.test import Client
from api.schema import schema
from core.hep_client import HepClient
from core.tests.mock_hep_data_factory import ME_DATA, VALID_TEACHERS_ORDERS
from registration.models import License
from users.models import License
from users.models import User, Role, SchoolClass
INVALID_KEY_ME = dict(ME_DATA)

View File

@ -2,7 +2,7 @@ from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from .models import User, SchoolClass, Role, UserRole, UserSetting
from .models import User, SchoolClass, Role, UserRole, UserSetting, License
class SchoolClassInline(admin.TabularInline):
@ -59,3 +59,10 @@ admin.site.register(User, CustomUserAdmin)
class UserSettingAdmin(admin.ModelAdmin):
list_display = ('user', 'selected_class')
raw_id_fields = ('user', 'selected_class')
@admin.register(License)
class LicenseAdmin(admin.ModelAdmin):
list_display = ('licensee',)
list_filter = ('licensee',)
raw_id_fields = ('licensee',)

View File

@ -2,7 +2,7 @@ import random
import factory
from users.models import SchoolClass
from users.models import SchoolClass, License
class_types = ['DA', 'KV', 'INF', 'EE']
class_suffix = ['A', 'B', 'C', 'D', 'E']
@ -29,3 +29,8 @@ class SchoolClassFactory(factory.django.DjangoModelFactory):
# A list of groups were passed in, use them
for user in extracted:
self.users.add(user)
class LicenseFactory(factory.django.DjangoModelFactory):
class Meta:
model = License

View File

@ -1,10 +1,14 @@
from datetime import timedelta
from django.apps import apps
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.contrib.auth.models import UserManager as DjangoUserManager
from core.hep_client import HepClient
from core.hep_client import TEACHER_EDITION_DURATION, STUDENT_EDITION_DURATION
class RoleManager(models.Manager):
@ -107,3 +111,24 @@ class UserManager(DjangoUserManager):
user.hep_group_id = user_data['group_id']
user.save()
return user
class LicenseManager(models.Manager):
def create_license_for_role(self, licensee, activation_date, raw, role):
Role = apps.get_model('users', 'Role')
if role == 'teacher':
user_role = Role.objects.get_default_teacher_role()
expiry_date = activation_date + timedelta(TEACHER_EDITION_DURATION)
else:
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)
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)
def get_active_licenses_for_user(self, user):
return self.filter(licensee=user, expire_date__gte=timezone.now())\
.filter(expire_date__lt=timezone.now()).order_by('-expire_date')

View File

@ -0,0 +1,25 @@
# Generated by Django 2.1.15 on 2020-02-20 10:39
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('users', '0011_user_license_expiry_date'),
]
operations = [
migrations.CreateModel(
name='License',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('expire_date', models.DateField(null=True)),
('raw', models.TextField(default='')),
('for_role', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='users.Role')),
('licensee', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -1,4 +1,5 @@
import re
from datetime import datetime
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser, Permission
@ -6,7 +7,8 @@ from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils.translation import ugettext_lazy as _
from users.managers import RoleManager, UserRoleManager, UserManager
from core.hep_client import HepClient
from users.managers import RoleManager, UserRoleManager, UserManager, LicenseManager
DEFAULT_SCHOOL_ID = 1
@ -174,3 +176,22 @@ class UserRole(models.Model):
class UserSetting(models.Model):
user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE, related_name='user_setting')
selected_class = models.ForeignKey(SchoolClass, blank=True, null=True, on_delete=models.CASCADE)
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="")
objects = LicenseManager()
def is_teacher_license(self):
return self.for_role.key == RoleManager.TEACHER_KEY
def is_valid(self):
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):
return 'License for role: %s' % self.for_role

View File

@ -23,9 +23,8 @@ from core.factories import UserFactory
from core.hep_client import HepClient
from core.tests.mock_hep_data_factory import MockResponse, ME_DATA, VALID_STUDENT_ORDERS, VALID_TEACHERS_ORDERS, \
NOT_CONFIRMED_ME
from registration.factories import LicenseFactory
from registration.models import License
from users.models import Role, User, SchoolClass
from users.factories import LicenseFactory
from users.models import Role, User, SchoolClass, License
TOKEN = 'abcd12345!'

View File

@ -11,7 +11,7 @@ from django.conf import settings
from core.hep_client import HepClient, HepClientException
from core.models import AdminData
from registration.models import License
from users.models import License
from users.models import User, UserRole, Role, SchoolClass