diff --git a/docs/oauth.md b/docs/oauth.md index 382b40e7..0d5a7062 100644 --- a/docs/oauth.md +++ b/docs/oauth.md @@ -18,9 +18,11 @@ Note the Client ID and the Client Secret of the newly created client Go to the environment on heroku, and enter the following env variables: -- OAUTH_ACCESS_TOKEN_URL: https://sso.hep-verlag.ch/oauth/token -- OAUTH_API_BASE_URL: https://cms.hep-verlag.ch -- OAUTH_AUTHORIZE_URL: https://sso.hep-verlag.ch/oauth/authorize -- OAUTH_CLIENT_ID: `` -- OAUTH_CLIENT_SECRET: `` -- OAUTH_LOCAL_REDIRECT_URI: https:///api/oauth/callback/ +``` +OAUTH_ACCESS_TOKEN_URL=https://sso.hep-verlag.ch/oauth/token +OAUTH_API_BASE_URL=https://cms.hep-verlag.ch +OAUTH_AUTHORIZE_URL=https://sso.hep-verlag.ch/oauth/authorize +OAUTH_CLIENT_ID= +OAUTH_CLIENT_SECRET= +OAUTH_LOCAL_REDIRECT_URI=https:///api/oauth/callback/ +``` diff --git a/server/.env.example b/server/.env.example index dd6ddfd5..b5aa0b78 100644 --- a/server/.env.example +++ b/server/.env.example @@ -33,3 +33,4 @@ export OAUTH_AUTHORIZE_URL= export OAUTH_API_BASE_URL= export OAUTH_LOCAL_REDIRECT_URI= export LOGOUT_REDIRECT_URL= +export PLATFORM=myskillbox diff --git a/server/.env.heroku.example b/server/.env.heroku.example index d63fdfb9..d98ce393 100644 --- a/server/.env.heroku.example +++ b/server/.env.heroku.example @@ -35,3 +35,4 @@ TASKBASE_TOKEN: TASKBASE_USER: USE_AWS: True WEB_CONCURRENCY: 4 +PLATFORM: myskillbox diff --git a/server/core/settings.py b/server/core/settings.py index 2d331886..6c185e3e 100644 --- a/server/core/settings.py +++ b/server/core/settings.py @@ -366,7 +366,6 @@ WAGTAIL_SITE_NAME = 'skillbox' GRAPHQL_QUERIES_DIR = os.path.join(BASE_DIR, '..', 'client', 'src', 'graphql', 'gql', 'queries') GRAPHQL_MUTATIONS_DIR = os.path.join(GRAPHQL_QUERIES_DIR, '../mutations') - DEFAULT_FROM_EMAIL = 'myskillbox ' # Metanet Config @@ -400,6 +399,8 @@ AUTHLIB_OAUTH_CLIENTS = { } } +PLATFORM = os.environ.get('PLATFORM', 'myskillbox') + OAUTH_LOCAL_REDIRECT_URI = os.environ.get("OAUTH_LOCAL_REDIRECT_URI") TASKBASE_USER = os.environ.get("TASKBASE_USER") diff --git a/server/oauth/hep_client.py b/server/oauth/hep_client.py index c3a55669..46d9cec0 100644 --- a/server/oauth/hep_client.py +++ b/server/oauth/hep_client.py @@ -5,7 +5,7 @@ from datetime import timedelta from oauth.models import OAuth2Token from oauth.oauth_client import oauth, fetch_token -from users.licenses import MYSKILLBOX_LICENSES, is_myskillbox_product, TEACHER_KEY +from users.licenses import get_license_dict, is_myskillbox_product, TEACHER_KEY from users.models import License from core.logger import get_logger @@ -14,6 +14,7 @@ logger = get_logger(__name__) VALID_PRODUCT_STATES = ['waiting', 'paid', 'completed', 'shipped'] +licenses = get_license_dict() class HepClientException(Exception): pass @@ -144,13 +145,14 @@ class HepClient: return products def entry_to_product(self, entry, activation_date, status): - if is_myskillbox_product(entry['isbn']) and activation_date: + isbn = entry['isbn'] + if is_myskillbox_product(isbn) and activation_date: return { 'raw': entry, 'activated': activation_date, 'status': status, 'order_id': entry['id'], - 'license': MYSKILLBOX_LICENSES[entry['isbn']], + 'license': licenses[isbn], 'isbn': entry['isbn'] } diff --git a/server/oauth/tests/test_hep_client.py b/server/oauth/tests/test_hep_client.py index 876e7959..70b0ab6f 100644 --- a/server/oauth/tests/test_hep_client.py +++ b/server/oauth/tests/test_hep_client.py @@ -9,17 +9,18 @@ from oauth.factories import Oauth2TokenFactory from oauth.hep_client import HepClient, HepClientUnauthorizedException, HepClientNoTokenException, HepClientException from oauth.models import OAuth2Token from oauth.tests.test_oauth2token import REFRESH_DATA -from users.licenses import MYSKILLBOX_LICENSES +from users.licenses import get_valid_isbns, get_license_dict from users.models import License from users.tests.mock_hep_data_factory import MockResponse -ISBNS = list(MYSKILLBOX_LICENSES.keys()) +ISBNS = get_valid_isbns() +licenses = get_license_dict() STUDENT_ISBN = ISBNS[0] -STUDENT_LICENSE = MYSKILLBOX_LICENSES[STUDENT_ISBN] +STUDENT_LICENSE = licenses[STUDENT_ISBN] TEACHER_ISBN = ISBNS[-1] -TEACHER_LICENSE = MYSKILLBOX_LICENSES[TEACHER_ISBN] +TEACHER_LICENSE = licenses[TEACHER_ISBN] class HepClientTestCases(TestCase): diff --git a/server/users/licenses.py b/server/users/licenses.py index 4ec25f5c..0f3c35af 100644 --- a/server/users/licenses.py +++ b/server/users/licenses.py @@ -1,35 +1,75 @@ +from django.conf import settings + TEACHER_KEY = 'teacher' STUDENT_KEY = 'student' +MYSKILLBOX_PLATFORM = 'myskillbox' +MYKV_PLATFORM = 'mykv' MYSKILLBOX_LICENSES = { "978-3-0355-1397-4": { 'edition': STUDENT_KEY, 'duration': 4 * 365, - 'name': 'Student 4 years' + 'name': 'Student 4 years', + 'platform': MYSKILLBOX_PLATFORM, + 'default': True }, "978-3-0355-1860-3": { 'edition': STUDENT_KEY, 'duration': 365, - 'name': 'Student 1 year' + 'name': 'Student 1 year', + 'platform': MYSKILLBOX_PLATFORM }, "978-3-0355-1862-7": { 'edition': STUDENT_KEY, 'duration': 30, - 'name': 'Student test 1 month' + 'name': 'Student test 1 month', + 'platform': MYSKILLBOX_PLATFORM }, "978-3-0355-1861-0": { 'edition': TEACHER_KEY, 'duration': 30, - 'name': 'Teacher test 1 month' + 'name': 'Teacher test 1 month', + 'platform': MYSKILLBOX_PLATFORM }, "978-3-0355-1823-8": { 'edition': TEACHER_KEY, 'duration': 365, - 'name': 'Teacher 1 year' + 'name': 'Teacher 1 year', + 'platform': MYSKILLBOX_PLATFORM + }, + '978-3-0355-2189-4': { + 'edition': STUDENT_KEY, + 'duration': 30, + 'name': 'Student test 1 month', + 'platform': MYKV_PLATFORM, + 'default': True + }, + '978-3-0355-2188-7': { + 'edition': TEACHER_KEY, + 'duration': 30, + 'name': 'Student test 1 month', + 'platform': MYKV_PLATFORM } } +def get_license_dict(): + return { + k: v for k, v in MYSKILLBOX_LICENSES.items() if v.get('platform') == settings.PLATFORM + } + + +def get_valid_isbns(): + return list(get_license_dict().keys()) + + def is_myskillbox_product(isbn): - valid_isbns = list(MYSKILLBOX_LICENSES.keys()) - return isbn in valid_isbns + return isbn in get_valid_isbns() + + +def get_default_isbn(): + defaults = { + k: v for k, v in get_license_dict().items() if v.get('default', False) + } + default_isbns = list(defaults.keys()) + return default_isbns[0] diff --git a/server/users/management/commands/reset_license_duration.py b/server/users/management/commands/reset_license_duration.py index bec75ddc..6d0a65b7 100644 --- a/server/users/management/commands/reset_license_duration.py +++ b/server/users/management/commands/reset_license_duration.py @@ -3,12 +3,12 @@ from datetime import date, timedelta from django.utils.timezone import now from django.core.management.base import BaseCommand -from users.licenses import MYSKILLBOX_LICENSES +from users.licenses import get_license_dict from users.models import License, NO_DATE YEARLY_ISBNS = ["978-3-0355-1860-3", "978-3-0355-1823-8"] LONGER_DURATION = 455 - +licenses = get_license_dict() class Command(BaseCommand): help = """ @@ -27,7 +27,7 @@ class Command(BaseCommand): if isbn not in YEARLY_ISBNS: continue - standard_duration = MYSKILLBOX_LICENSES[isbn]['duration'] # use isbn from hep due to our bug + standard_duration = licenses[isbn]['duration'] # use isbn from hep due to our bug start_date = license.get_hep_start_date() duration_in_days = (license.expire_date - start_date).days if duration_in_days == standard_duration: diff --git a/server/users/managers.py b/server/users/managers.py index a4308a23..7a7071e4 100644 --- a/server/users/managers.py +++ b/server/users/managers.py @@ -9,8 +9,9 @@ from django.utils.translation import ugettext_lazy as _ from django.db import models from django.contrib.auth.models import UserManager as DjangoUserManager -from users.licenses import MYSKILLBOX_LICENSES +from users.licenses import get_license_dict +license_dict = get_license_dict() class RoleManager(models.Manager): use_in_migrations = True @@ -140,7 +141,7 @@ class LicenseManager(models.Manager): def create_license_for_role(self, licensee, activation_date, raw, role, order_id, isbn): Role = apps.get_model('users', 'Role') - expiry_date = activation_date + timedelta(MYSKILLBOX_LICENSES[isbn]['duration']) + expiry_date = activation_date + timedelta(license_dict[isbn]['duration']) if role == 'teacher': user_role = Role.objects.get_default_teacher_role() diff --git a/server/users/models.py b/server/users/models.py index e87b88dd..7057057f 100644 --- a/server/users/models.py +++ b/server/users/models.py @@ -15,7 +15,7 @@ from django.utils.timezone import is_aware, make_aware from django.utils.translation import ugettext_lazy as _ from core.mixins import GraphqlNodeMixin -from users.licenses import MYSKILLBOX_LICENSES +from users.licenses import get_license_dict, get_default_isbn from users.managers import LicenseManager, RoleManager, UserManager, UserRoleManager DEFAULT_SCHOOL_ID = 1 @@ -23,6 +23,8 @@ 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) +licenses = get_license_dict() + class User(AbstractUser): LICENSE_NONE = 'no-license' @@ -309,7 +311,7 @@ class License(models.Model): order_id = models.IntegerField(blank=False, null=False, default=-1) raw = models.TextField(default='') isbn = models.CharField(max_length=50, blank=False, null=False, - default=list(MYSKILLBOX_LICENSES.keys())[0]) # student license + default=get_default_isbn()) # student license created_at = models.DateTimeField(auto_now_add=True) hep_created_at = models.DateTimeField(default=AWARE_NO_DATETIME) new_api_raw = models.TextField(default='') @@ -365,7 +367,7 @@ class License(models.Model): if not is_aware(expiry_date): expiry_date = make_aware(expiry_date) - return expiry_date >= now >= expiry_date - timedelta(days=MYSKILLBOX_LICENSES[isbn]['duration']) + return expiry_date >= now >= expiry_date - timedelta(days=licenses[isbn]['duration']) def __str__(self): return f'License for role: {self.for_role}' diff --git a/server/users/services.py b/server/users/services.py index 809626bb..fe8debff 100644 --- a/server/users/services.py +++ b/server/users/services.py @@ -3,9 +3,8 @@ from datetime import timedelta from django.utils.timezone import now from core.factories import UserFactory -from users.factories import SchoolClassFactory, LicenseFactory -from users.licenses import MYSKILLBOX_LICENSES -from users.models import Role, UserRole, DEFAULT_SCHOOL_ID +from users.factories import SchoolClassFactory +from users.models import Role, UserRole def create_student(**kwargs):