Fix timezones

This commit is contained in:
Christian Cueni 2021-05-11 16:32:03 +02:00
parent 5c8c836a2b
commit 4b1ddd2627
3 changed files with 31 additions and 32 deletions

View File

@ -9,51 +9,49 @@ UNKNOWN_ERROR = 'unknown_error'
NO_VALID_LICENSE = 'no_valid_license' 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() hep_client = HepClient()
user = User.objects.get_or_create_hep_user(user_data) user = User.objects.get_or_create_hep_user(user_data)
try: # try:
if not hep_client.is_email_verified(user_data): # if not hep_client.is_email_verified(user_data):
return user, EMAIL_NOT_VERIFIED # return user, EMAIL_NOT_VERIFIED
except HepClientException: # except HepClientException:
return user, UNKNOWN_ERROR # return user, UNKNOWN_ERROR
license = License.objects.get_active_license_for_user(user) license = License.objects.get_active_license_for_user(user)
# if not license: if not license:
# license, error_msg = check_and_create_licenses(hep_client, user) license, error_msg = check_and_create_licenses(hep_client, user, token)
#
# if error_msg: if error_msg:
# return user, error_msg return user, error_msg
#
# create_role_for_user(user, license.for_role.key) create_role_for_user(user, license.for_role.key)
#
if not license.is_valid(): if not license.is_valid():
return user, NO_VALID_LICENSE return user, NO_VALID_LICENSE
return user, None return user, None
def check_and_create_licenses(hep_client, user): def check_and_create_licenses(hep_client, user, token):
# try: try:
# admin_token = AdminData.objects.get_admin_token() product = hep_client.active_myskillbox_product_for_customer(token=token)
# product = hep_client.myskillbox_product_for_customer(admin_token, user.hep_id) except HepClientException:
# except HepClientException: return None, UNKNOWN_ERROR
# return None, UNKNOWN_ERROR
# if product: if product:
# license = License.objects.create_license_for_role(user, product['activated'], product['raw'], license = License.objects.create_license_for_role(user, product['activated'], product['raw'],
# product['license']['edition'], product['license']['edition'],
# product['order_id'], product['isbn']) product['order_id'], product['isbn'])
# # todo handle no license case # todo handle no license case
# else: else:
# return None, NO_VALID_LICENSE return None, NO_VALID_LICENSE
# return license, None return license, None
return None, None
def create_role_for_user(user, role_key): def create_role_for_user(user, role_key):

View File

@ -19,7 +19,7 @@ def authorize(request):
token = oauth.hep.authorize_access_token(request) token = oauth.hep.authorize_access_token(request)
user_data = hep_client.user_details(token=token) 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) user.sync_with_hep_data(user_data)
if user and status_msg != EMAIL_NOT_VERIFIED: if user and status_msg != EMAIL_NOT_VERIFIED:

View File

@ -7,6 +7,7 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser, Permission from django.contrib.auth.models import AbstractUser, Permission
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.db import models from django.db import models
from django.utils.timezone import make_aware
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils import timezone from django.utils import timezone
@ -308,8 +309,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 License.is_product_active( date = make_aware(datetime(self.expire_date.year, self.expire_date.month, self.expire_date.day))
datetime(self.expire_date.year, self.expire_date.month, self.expire_date.day), self.isbn) return License.is_product_active(date, self.isbn)
@staticmethod @staticmethod
def is_product_active(expiry_date, isbn): def is_product_active(expiry_date, isbn):