diff --git a/server/oauth/user_signup_login_handler.py b/server/oauth/user_signup_login_handler.py index d77a9063..628b7cae 100644 --- a/server/oauth/user_signup_login_handler.py +++ b/server/oauth/user_signup_login_handler.py @@ -9,51 +9,49 @@ UNKNOWN_ERROR = 'unknown_error' NO_VALID_LICENSE = 'no_valid_license' -def handle_user_and_verify_products(user_data): +def handle_user_and_verify_products(user_data, token): hep_client = HepClient() user = User.objects.get_or_create_hep_user(user_data) - try: - if not hep_client.is_email_verified(user_data): - return user, EMAIL_NOT_VERIFIED - except HepClientException: - return user, UNKNOWN_ERROR + # try: + # if not hep_client.is_email_verified(user_data): + # return user, EMAIL_NOT_VERIFIED + # except HepClientException: + # return user, UNKNOWN_ERROR license = License.objects.get_active_license_for_user(user) - # if not license: - # license, error_msg = check_and_create_licenses(hep_client, user) - # - # if error_msg: - # return user, error_msg - # - # create_role_for_user(user, license.for_role.key) - # + if not license: + license, error_msg = check_and_create_licenses(hep_client, user, token) + + if error_msg: + return user, error_msg + + create_role_for_user(user, license.for_role.key) + if not license.is_valid(): return user, NO_VALID_LICENSE return user, None -def check_and_create_licenses(hep_client, user): - # try: - # admin_token = AdminData.objects.get_admin_token() - # product = hep_client.myskillbox_product_for_customer(admin_token, user.hep_id) - # except HepClientException: - # return None, UNKNOWN_ERROR +def check_and_create_licenses(hep_client, user, token): + try: + product = hep_client.active_myskillbox_product_for_customer(token=token) + except HepClientException: + return None, UNKNOWN_ERROR - # if product: - # license = License.objects.create_license_for_role(user, product['activated'], product['raw'], - # product['license']['edition'], - # product['order_id'], product['isbn']) - # # todo handle no license case - # else: - # return None, NO_VALID_LICENSE + if product: + license = License.objects.create_license_for_role(user, product['activated'], product['raw'], + product['license']['edition'], + product['order_id'], product['isbn']) + # todo handle no license case + else: + return None, NO_VALID_LICENSE - # return license, None + return license, None - return None, None def create_role_for_user(user, role_key): diff --git a/server/oauth/views.py b/server/oauth/views.py index f8cc45e7..c7396b5a 100644 --- a/server/oauth/views.py +++ b/server/oauth/views.py @@ -19,7 +19,7 @@ def authorize(request): token = oauth.hep.authorize_access_token(request) user_data = hep_client.user_details(token=token) - user, status_msg = handle_user_and_verify_products(user_data) + user, status_msg = handle_user_and_verify_products(user_data, token) user.sync_with_hep_data(user_data) if user and status_msg != EMAIL_NOT_VERIFIED: diff --git a/server/users/models.py b/server/users/models.py index 43586e14..ab86f03e 100644 --- a/server/users/models.py +++ b/server/users/models.py @@ -7,6 +7,7 @@ from django.contrib.auth import get_user_model from django.contrib.auth.models import AbstractUser, Permission from django.core.exceptions import ObjectDoesNotExist from django.db import models +from django.utils.timezone import make_aware from django.utils.translation import ugettext_lazy as _ from django.utils import timezone @@ -308,8 +309,8 @@ class License(models.Model): return self.for_role.key == RoleManager.TEACHER_KEY def is_valid(self): - return License.is_product_active( - datetime(self.expire_date.year, self.expire_date.month, self.expire_date.day), self.isbn) + date = make_aware(datetime(self.expire_date.year, self.expire_date.month, self.expire_date.day)) + return License.is_product_active(date, self.isbn) @staticmethod def is_product_active(expiry_date, isbn):