65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# ITerativ GmbH
|
|
# http://www.iterativ.ch/
|
|
#
|
|
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
|
|
#
|
|
# Created on 2019-10-08
|
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
|
import graphene
|
|
from django.contrib.auth import login
|
|
from graphene import relay
|
|
|
|
from core.hep_client import HepClient, HepClientException, HepClientUnauthorizedException
|
|
from users.user_signup_login_handler import handle_user_and_verify_products, UNKNOWN_ERROR
|
|
|
|
|
|
class RegistrationError(graphene.ObjectType):
|
|
field = graphene.String()
|
|
|
|
|
|
class Registration(relay.ClientIDMutation):
|
|
class Input:
|
|
confirmation_key_input = graphene.String()
|
|
|
|
success = graphene.Boolean()
|
|
message = graphene.String()
|
|
errors = graphene.List(RegistrationError) # todo: change for consistency
|
|
|
|
@classmethod
|
|
def mutate_and_get_payload(cls, root, info, **kwargs):
|
|
confirmation_key = kwargs.get('confirmation_key_input')
|
|
|
|
hep_client = HepClient()
|
|
|
|
try:
|
|
user_data = hep_client.customer_activate(confirmation_key)
|
|
except HepClientUnauthorizedException:
|
|
return cls.return_registration_msg('invalid_key')
|
|
except HepClientException:
|
|
return cls.return_registration_msg('unknown_error')
|
|
|
|
user, status_msg = handle_user_and_verify_products(user_data)
|
|
|
|
if user:
|
|
login(info.context, user)
|
|
|
|
if status_msg:
|
|
return cls.return_registration_msg(status_msg)
|
|
|
|
return cls(success=True, errors=[], message='success')
|
|
|
|
@classmethod
|
|
def return_registration_msg(cls, message):
|
|
# even if the user has no valid license treat it like a success
|
|
if message == UNKNOWN_ERROR:
|
|
error = RegistrationError(field=message)
|
|
return cls(success=False, errors=[error], message='')
|
|
|
|
return cls(success=True, errors=[], message=message)
|
|
|
|
|
|
class RegistrationMutations:
|
|
registration = Registration.Field()
|