Re-order structure
This commit is contained in:
parent
91fa976317
commit
549bf1ef28
|
|
@ -6,7 +6,7 @@ from graphene_django.views import GraphQLView
|
|||
|
||||
from api.schema_public import schema
|
||||
|
||||
from core.views import PrivateGraphQLView, ConfirmationKeyDisplayView
|
||||
from core.views import PrivateGraphQLView
|
||||
|
||||
app_name = 'api'
|
||||
urlpatterns = [
|
||||
|
|
@ -21,6 +21,5 @@ if settings.DEBUG:
|
|||
urlpatterns += [url(r'^graphiql-public', csrf_exempt(GraphQLView.as_view(schema=schema, graphiql=True,
|
||||
pretty=True)))]
|
||||
urlpatterns += [url(r'^graphiql', csrf_exempt(PrivateGraphQLView.as_view(graphiql=True, pretty=True)))]
|
||||
urlpatterns += [url(r'^confirmation', ConfirmationKeyDisplayView.as_view(), name='confirmation_key_display')]
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,28 +3,13 @@ from django.db import models
|
|||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
from core.hep_client import HepClient
|
||||
|
||||
DEFAULT_PK = 1
|
||||
|
||||
|
||||
class AdminDataManager(models.Manager):
|
||||
hep_client = HepClient()
|
||||
|
||||
def update_admin_token(self):
|
||||
admin_token = self.hep_client.fetch_admin_token(settings.HEP_ADMIN_USER, settings.HEP_ADMIN_PASSWORD)
|
||||
|
||||
admin_data, created = self.get_or_create(pk=DEFAULT_PK)
|
||||
admin_data.hep_admin_token = admin_token
|
||||
admin_data.save()
|
||||
return admin_data.hep_admin_token
|
||||
return ''
|
||||
|
||||
def get_admin_token(self):
|
||||
try:
|
||||
admin_token = self.get(pk=DEFAULT_PK)
|
||||
if admin_token.updated_at < timezone.now() + timedelta(hours=1):
|
||||
admin_token = self.update_admin_token()
|
||||
except self.model.DoesNotExist:
|
||||
admin_token = self.update_admin_token()
|
||||
|
||||
return admin_token
|
||||
return ''
|
||||
|
|
|
|||
|
|
@ -416,7 +416,7 @@ AUTHLIB_OAUTH_CLIENTS = {
|
|||
'authorize_url': os.environ.get("OAUTH_AUTHORIZE_URL"),
|
||||
'api_base_url': os.environ.get("OAUTH_API_BASE_URL"),
|
||||
'client_kwargs': {
|
||||
'scope': 'email',
|
||||
'scope': 'orders',
|
||||
'token_endpoint_auth_method': 'client_secret_post',
|
||||
'token_placement': 'header',
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ from django.views.decorators.csrf import ensure_csrf_cookie
|
|||
from django.views.generic import TemplateView
|
||||
from graphene_django.views import GraphQLView
|
||||
|
||||
from core.hep_client import HepClient
|
||||
from core.models import AdminData
|
||||
|
||||
|
||||
|
|
@ -27,20 +26,3 @@ def home(request):
|
|||
print('Can not connect to dev server at http://localhost:8080:', e)
|
||||
|
||||
return render(request, 'index.html', {})
|
||||
|
||||
|
||||
class ConfirmationKeyDisplayView(TemplateView):
|
||||
template_name = 'confirmation_key.html'
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
|
||||
email = self.request.GET.get('email', '')
|
||||
|
||||
hep_client = HepClient()
|
||||
admin_token = AdminData.objects.get_admin_token()
|
||||
hep_user = hep_client.customers_search(admin_token, email)
|
||||
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['confirmation_key'] = hep_user['confirmation']
|
||||
context['hep_id'] = hep_user['id']
|
||||
return context
|
||||
|
|
|
|||
|
|
@ -4,42 +4,11 @@ from django.conf import settings
|
|||
import logging
|
||||
import requests
|
||||
|
||||
from core.oauth import oauth
|
||||
from oauth.oauth_client import oauth
|
||||
from users.models import License
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
TEACHER_KEY = 'teacher'
|
||||
STUDENT_KEY = 'student'
|
||||
|
||||
MYSKILLBOX_LICENSES = {
|
||||
"978-3-0355-1397-4": {
|
||||
'edition': STUDENT_KEY,
|
||||
'duration': 4 * 365,
|
||||
'name': 'Student 4 years'
|
||||
},
|
||||
"978-3-0355-1860-3": {
|
||||
'edition': STUDENT_KEY,
|
||||
'duration': 455,
|
||||
'name': 'Student 1 year'
|
||||
},
|
||||
"978-3-0355-1862-7": {
|
||||
'edition': STUDENT_KEY,
|
||||
'duration': 30,
|
||||
'name': 'Student test 1 month'
|
||||
},
|
||||
"978-3-0355-1861-0": {
|
||||
'edition': TEACHER_KEY,
|
||||
'duration': 30,
|
||||
'name': 'Teacher test 1 month'
|
||||
},
|
||||
"978-3-0355-1823-8": {
|
||||
'edition': TEACHER_KEY,
|
||||
'duration': 455,
|
||||
'name': 'Teacher 1 year'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class HepClientException(Exception):
|
||||
pass
|
||||
|
||||
|
|
@ -48,6 +17,10 @@ class HepClientUnauthorizedException(Exception):
|
|||
pass
|
||||
|
||||
|
||||
class HepClientNoTokenException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class HepClient:
|
||||
URL = settings.HEP_URL
|
||||
WEBSITE_ID = 1
|
||||
|
|
@ -56,14 +29,19 @@ class HepClient:
|
|||
'content-type': 'application/json'
|
||||
}
|
||||
|
||||
def _call(self, url, token, method='get', data=None):
|
||||
def _call(self, url, method='get', data=None, request=None, token=None):
|
||||
|
||||
request_url = f'{self.URL}{url}'
|
||||
|
||||
token_parameters = {
|
||||
'token': token,
|
||||
'request': request
|
||||
}
|
||||
|
||||
if method == 'post':
|
||||
response = requests.post(request_url, json=data)
|
||||
elif method == 'get':
|
||||
response = oauth.hep.get(url, token=token)
|
||||
response = oauth.hep.get(url, **token_parameters)
|
||||
elif method == 'put':
|
||||
response = requests.put(request_url, data=data)
|
||||
|
||||
|
|
@ -78,8 +56,10 @@ class HepClient:
|
|||
def is_email_verified(self, user_data):
|
||||
return user_data['email_verified_at'] is not None
|
||||
|
||||
def user_details(self, token):
|
||||
response = self._call('/api/auth/user', token)
|
||||
def user_details(self, request=None, token=None):
|
||||
if request is None and token is None:
|
||||
raise HepClientNoTokenException
|
||||
response = self._call('/api/auth/user', request=request, token=token)
|
||||
return response.json()['data']
|
||||
|
||||
def customers_search(self, admin_token, email):
|
||||
|
|
@ -167,7 +147,7 @@ class HepClient:
|
|||
|
||||
expiry_delta = product['activated'] + timedelta(product['license']['duration'])
|
||||
|
||||
if HepClient.is_product_active(expiry_delta, product['isbn']):
|
||||
if License.is_product_active(expiry_delta, product['isbn']):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
|
@ -196,8 +176,3 @@ class HepClient:
|
|||
|
||||
return teacher_edition
|
||||
|
||||
@staticmethod
|
||||
def is_product_active(expiry_date, isbn):
|
||||
now = datetime.now()
|
||||
|
||||
return expiry_date >= now >= expiry_date - timedelta(days=MYSKILLBOX_LICENSES[isbn]['duration'])
|
||||
|
|
@ -2,7 +2,20 @@
|
|||
from authlib.integrations.django_client import OAuth
|
||||
from django.conf import settings
|
||||
|
||||
oauth = OAuth()
|
||||
from oauth.models import OAuth2Token
|
||||
|
||||
|
||||
def fetch_token(name, request):
|
||||
try:
|
||||
token = OAuth2Token.objects.get(
|
||||
user=request.user
|
||||
)
|
||||
return token.to_token()
|
||||
except (OAuth2Token.DoesNotExist, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
oauth = OAuth(fetch_token=fetch_token)
|
||||
oauth.register(
|
||||
name='hep',
|
||||
client_id=settings.AUTHLIB_OAUTH_CLIENTS['hep']['client_id'],
|
||||
|
|
@ -17,4 +30,3 @@ oauth.register(
|
|||
client_kwargs=settings.AUTHLIB_OAUTH_CLIENTS['hep']['client_kwargs'],
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
from core.hep_client import HepClient, HepClientException
|
||||
from core.models import AdminData
|
||||
from oauth.hep_client import HepClient, HepClientException
|
||||
from users.models import License
|
||||
from users.models import User, UserRole, Role, SchoolClass
|
||||
|
||||
|
|
@ -23,18 +23,18 @@ def handle_user_and_verify_products(user_data, ):
|
|||
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.is_valid():
|
||||
# return user, NO_VALID_LICENSE
|
||||
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.is_valid():
|
||||
return user, NO_VALID_LICENSE
|
||||
|
||||
return user, None
|
||||
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
from django.conf import settings
|
||||
from django.shortcuts import redirect
|
||||
|
||||
from core.hep_client import HepClient
|
||||
from core.oauth import oauth
|
||||
from oauth.hep_client import HepClient
|
||||
from oauth.oauth_client import oauth
|
||||
from oauth.models import OAuth2Token
|
||||
from users.user_signup_login_handler import handle_user_and_verify_products, EMAIL_NOT_VERIFIED
|
||||
from oauth.user_signup_login_handler import handle_user_and_verify_products, EMAIL_NOT_VERIFIED
|
||||
from django.contrib.auth import login as dj_login
|
||||
|
||||
|
||||
|
|
@ -17,7 +17,7 @@ def login(request):
|
|||
def authorize(request):
|
||||
hep_client = HepClient()
|
||||
token = oauth.hep.authorize_access_token(request)
|
||||
user_data = hep_client.user_details(token)
|
||||
user_data = hep_client.user_details(token=token)
|
||||
|
||||
user, status_msg = handle_user_and_verify_products(user_data)
|
||||
user.sync_with_hep_data(user_data)
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@ import graphene
|
|||
from django.contrib.auth import login
|
||||
from graphene import relay
|
||||
|
||||
from core.hep_client import HepClient, HepClientException
|
||||
# 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, NO_VALID_LICENSE
|
||||
from oauth.user_signup_login_handler import handle_user_and_verify_products, UNKNOWN_ERROR, NO_VALID_LICENSE
|
||||
|
||||
|
||||
class Registration(relay.ClientIDMutation):
|
||||
|
|
@ -30,29 +30,29 @@ class Registration(relay.ClientIDMutation):
|
|||
confirmation_key = kwargs.get('confirmation_key')
|
||||
user_id = kwargs.get('user_id')
|
||||
|
||||
hep_client = HepClient()
|
||||
admin_token = AdminData.objects.get_admin_token()
|
||||
# hep_client = HepClient()
|
||||
# admin_token = AdminData.objects.get_admin_token()
|
||||
|
||||
try:
|
||||
hep_client.customer_activate(confirmation_key, user_id)
|
||||
user_data = hep_client.customers_by_id(admin_token, user_id)
|
||||
# double check if user has verified his email. If the "confirmation" field is present, the email address
|
||||
# is not verified.
|
||||
if 'confirmation' in user_data:
|
||||
return cls.return_fail_registration_msg('invalid_key')
|
||||
except HepClientException:
|
||||
return cls.return_fail_registration_msg('unknown_error')
|
||||
# try:
|
||||
# hep_client.customer_activate(confirmation_key, user_id)
|
||||
# user_data = hep_client.customers_by_id(admin_token, user_id)
|
||||
# # double check if user has verified his email. If the "confirmation" field is present, the email address
|
||||
# # is not verified.
|
||||
# if 'confirmation' in user_data:
|
||||
# return cls.return_fail_registration_msg('invalid_key')
|
||||
# except HepClientException:
|
||||
# return cls.return_fail_registration_msg('unknown_error')
|
||||
#
|
||||
# user, status_msg = handle_user_and_verify_products(user_data)
|
||||
|
||||
user, status_msg = handle_user_and_verify_products(user_data)
|
||||
|
||||
if user:
|
||||
login(info.context, user)
|
||||
|
||||
if status_msg:
|
||||
if status_msg == NO_VALID_LICENSE:
|
||||
return cls(success=True, message=NO_VALID_LICENSE)
|
||||
else:
|
||||
return cls.return_fail_registration_msg(status_msg)
|
||||
# if user:
|
||||
# login(info.context, user)
|
||||
#
|
||||
# if status_msg:
|
||||
# if status_msg == NO_VALID_LICENSE:
|
||||
# return cls(success=True, message=NO_VALID_LICENSE)
|
||||
# else:
|
||||
# return cls.return_fail_registration_msg(status_msg)
|
||||
|
||||
return cls(success=True, message='success')
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
TEACHER_KEY = 'teacher'
|
||||
STUDENT_KEY = 'student'
|
||||
|
||||
MYSKILLBOX_LICENSES = {
|
||||
"978-3-0355-1397-4": {
|
||||
'edition': STUDENT_KEY,
|
||||
'duration': 4 * 365,
|
||||
'name': 'Student 4 years'
|
||||
},
|
||||
"978-3-0355-1860-3": {
|
||||
'edition': STUDENT_KEY,
|
||||
'duration': 365,
|
||||
'name': 'Student 1 year'
|
||||
},
|
||||
"978-3-0355-1862-7": {
|
||||
'edition': STUDENT_KEY,
|
||||
'duration': 30,
|
||||
'name': 'Student test 1 month'
|
||||
},
|
||||
"978-3-0355-1861-0": {
|
||||
'edition': TEACHER_KEY,
|
||||
'duration': 30,
|
||||
'name': 'Teacher test 1 month'
|
||||
},
|
||||
"978-3-0355-1823-8": {
|
||||
'edition': TEACHER_KEY,
|
||||
'duration': 365,
|
||||
'name': 'Teacher 1 year'
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ 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 MYSKILLBOX_LICENSES
|
||||
from users.licenses import MYSKILLBOX_LICENSES
|
||||
|
||||
|
||||
class RoleManager(models.Manager):
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import random
|
||||
import re
|
||||
from datetime import datetime, timedelta, date
|
||||
import string
|
||||
from datetime import date, datetime
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import AbstractUser, Permission
|
||||
|
|
@ -9,7 +9,7 @@ from django.core.exceptions import ObjectDoesNotExist
|
|||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from core.hep_client import HepClient, MYSKILLBOX_LICENSES
|
||||
from users.licenses import MYSKILLBOX_LICENSES
|
||||
from users.managers import RoleManager, UserRoleManager, UserManager, LicenseManager
|
||||
|
||||
DEFAULT_SCHOOL_ID = 1
|
||||
|
|
@ -306,9 +306,15 @@ class License(models.Model):
|
|||
return self.for_role.key == RoleManager.TEACHER_KEY
|
||||
|
||||
def is_valid(self):
|
||||
return HepClient.is_product_active(
|
||||
return License.is_product_active(
|
||||
datetime(self.expire_date.year, self.expire_date.month, self.expire_date.day), self.isbn)
|
||||
|
||||
@staticmethod
|
||||
def is_product_active(expiry_date, isbn):
|
||||
now = datetime.now()
|
||||
|
||||
return expiry_date >= now >= expiry_date - timedelta(days=MYSKILLBOX_LICENSES[isbn]['duration'])
|
||||
|
||||
def __str__(self):
|
||||
return f'License for role: {self.for_role}'
|
||||
|
||||
|
|
|
|||
|
|
@ -3,8 +3,7 @@ from django.conf import settings
|
|||
from django.contrib.auth import authenticate, login
|
||||
from graphene import relay
|
||||
|
||||
from core.hep_client import HepClient, HepClientUnauthorizedException, HepClientException
|
||||
from users.user_signup_login_handler import handle_user_and_verify_products, UNKNOWN_ERROR, EMAIL_NOT_VERIFIED
|
||||
# from users.user_signup_login_handler import handle_user_and_verify_products, UNKNOWN_ERROR, EMAIL_NOT_VERIFIED
|
||||
|
||||
|
||||
class BetaLogin(relay.ClientIDMutation):
|
||||
|
|
@ -40,32 +39,32 @@ class Login(relay.ClientIDMutation):
|
|||
@classmethod
|
||||
def mutate_and_get_payload(cls, root, info, **kwargs):
|
||||
|
||||
hep_client = HepClient()
|
||||
token = kwargs.get('token_input')
|
||||
|
||||
try:
|
||||
user_data = hep_client.customer_me(token)
|
||||
except HepClientUnauthorizedException:
|
||||
return cls.return_login_message('invalid_credentials')
|
||||
except HepClientException:
|
||||
return cls.return_login_message(UNKNOWN_ERROR)
|
||||
|
||||
# use in auth
|
||||
user, status_msg = handle_user_and_verify_products(user_data)
|
||||
user.sync_with_hep_data(user_data)
|
||||
|
||||
if user and status_msg != EMAIL_NOT_VERIFIED:
|
||||
login(info.context, user)
|
||||
|
||||
if status_msg:
|
||||
return cls.return_login_message(status_msg)
|
||||
# hep_client = HepClient()
|
||||
# token = kwargs.get('token_input')
|
||||
#
|
||||
# try:
|
||||
# user_data = hep_client.customer_me(token)
|
||||
# except HepClientUnauthorizedException:
|
||||
# return cls.return_login_message('invalid_credentials')
|
||||
# except HepClientException:
|
||||
# return cls.return_login_message(UNKNOWN_ERROR)
|
||||
#
|
||||
# # use in auth
|
||||
# user, status_msg = handle_user_and_verify_products(user_data)
|
||||
# user.sync_with_hep_data(user_data)
|
||||
#
|
||||
# if user and status_msg != EMAIL_NOT_VERIFIED:
|
||||
# login(info.context, user)
|
||||
#
|
||||
# if status_msg:
|
||||
# return cls.return_login_message(status_msg)
|
||||
|
||||
return cls(success=True, message='success')
|
||||
|
||||
@classmethod
|
||||
def return_login_message(cls, message):
|
||||
if message == EMAIL_NOT_VERIFIED or message == UNKNOWN_ERROR or message == 'invalid_credentials':
|
||||
raise Exception(message)
|
||||
# if message == EMAIL_NOT_VERIFIED or message == UNKNOWN_ERROR or message == 'invalid_credentials':
|
||||
# raise Exception(message)
|
||||
return cls(success=True, message=message)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue