138 lines
4.1 KiB
Python
138 lines
4.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# ITerativ GmbH
|
|
# http://www.iterativ.ch/
|
|
#
|
|
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
|
|
#
|
|
# Created on 2019-10-01
|
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
|
|
|
import graphene
|
|
from django.conf import settings
|
|
from django.contrib.auth import authenticate, login
|
|
from graphene import relay
|
|
|
|
from core.hep_client import HepClient
|
|
from registration.models import License
|
|
from users.managers import UserRoleManager
|
|
from users.models import MagentoToken, User, Role, UserRole, SchoolClass
|
|
|
|
|
|
class LoginError(graphene.ObjectType):
|
|
field = graphene.String()
|
|
|
|
|
|
class Login(relay.ClientIDMutation):
|
|
class Input:
|
|
username_input = graphene.String()
|
|
password_input = graphene.String()
|
|
|
|
success = graphene.Boolean()
|
|
errors = graphene.List(LoginError) # todo: change for consistency
|
|
|
|
@classmethod
|
|
def mutate_and_get_payload(cls, root, info, **kwargs):
|
|
|
|
username = kwargs.get('username_input')
|
|
password = kwargs.get('password_input')
|
|
|
|
# get token
|
|
# wrong password
|
|
#
|
|
# is email verified
|
|
# -> show screen
|
|
# local license
|
|
# -> show coupon
|
|
# get hep orders
|
|
# -> show coupon
|
|
# save role information
|
|
# login
|
|
|
|
if settings.USE_LOCAL_REGISTRATION:
|
|
user = authenticate(username=username, password=password)
|
|
if user is None:
|
|
error = LoginError(field='invalid_credentials')
|
|
return cls(success=False, errors=[error])
|
|
|
|
else:
|
|
hep_client = HepClient()
|
|
|
|
# Todo network error catch error
|
|
token = hep_client.customer_token(username, password)
|
|
try:
|
|
user = User.objects.get(email=username)
|
|
except User.DoesNotExist:
|
|
user = User.objects.create_user_from_hep(token)
|
|
# ISBN "123-4-5678-9012-3"
|
|
|
|
magento_token, created = MagentoToken.objects.get_or_create(user=user)
|
|
magento_token.token = token['token']
|
|
magento_token.save()
|
|
|
|
if not hep_client.is_email_verified(username):
|
|
# Todo handle unverifed emails
|
|
pass
|
|
|
|
try:
|
|
license = License.objects.get(licensee=user)
|
|
except License.DoesNotExist:
|
|
product = hep_client.myskillbox_product_for_customer(settings.HEP_ADMIN_TOKEN, user.hep_id)
|
|
|
|
if product:
|
|
license = License.objects.create_license_for_role(user, product['activated'],
|
|
product['raw'], product['edition'])
|
|
else:
|
|
# todo go to shop
|
|
pass
|
|
|
|
if license.for_role.key == Role.objects.TEACHER_KEY:
|
|
UserRole.objects.create_role_for_user(user, license.for_role.key)
|
|
default_class_name = SchoolClass.generate_default_group_name()
|
|
default_class = SchoolClass.objects.create(name=default_class_name)
|
|
user.school_classes.add(default_class)
|
|
|
|
# if teacher create class
|
|
# if student add to class if exists???
|
|
|
|
# no orders
|
|
# network errors
|
|
# Todo get orders from magento
|
|
# check items
|
|
# show buy or create license
|
|
|
|
|
|
|
|
# check items
|
|
# show buy page
|
|
|
|
if not license.is_valid():
|
|
pass
|
|
|
|
|
|
# show page
|
|
|
|
#
|
|
|
|
# user_license = None
|
|
#
|
|
# try:
|
|
# user_license = License.objects.get(licensee=user)
|
|
# except License.DoesNotExist:
|
|
# # current users have no license, allow them to login
|
|
# pass
|
|
#
|
|
# if user_license is not None and not user_license.license_type.active:
|
|
# error = LoginError(field='license_inactive')
|
|
# return cls(success=False, errors=[error])
|
|
|
|
login(info.context, user)
|
|
return cls(success=True, errors=[])
|
|
|
|
|
|
class UserMutations:
|
|
login = Login.Field()
|
|
|
|
|
|
|