Beispiel #1
0
    def mutate_and_get_payload(root, info, **input):
        "Login check"
        if not info.context.user.is_anonymous:
            raise GraphQLError('Already logged in.')
        # remove password from input to allow spaces in password
        password = input.pop('password')
        input = clean_input(input)
        "Extra username validation"
        if ' ' in input.get('username'):
            raise GraphQLError('Invalid username format.')
        "Email validation"
        try:
            validate_email(input.get('email'))
        except ValidationError:
            raise GraphQLError('Invalid email format.')
        "Checks to see if the user exists in the database"
        if UserModel.objects.filter(username=input.get('username')).first():
            raise GraphQLError('Username already taken.')
        if UserModel.objects.filter(email=input.get('email')).first():
            raise GraphQLError('Email already taken.')
        "Checks name format"
        if not validate_name(input.get('first_name')) or not validate_name(
                input.get('last_name')):
            raise GraphQLError('Invalid first/last name.')

        confirmation_link = input.pop('confirmation_link')

        "Convert username to lowercase"
        input['username'] = input.get('username').lower()

        new_user = UserModel(**input)
        new_user.set_password(password)
        new_user.save()

        # Email Authentication
        # try:
        #     server = smtplib.SMTP('smtp.gmail.com:587')
        #     server.ehlo()
        #     server.starttls()
        #     server.login("EMAIL", "PASSWORD")
        #     message = 'Subject: {}\n\n{}'.format(
        #         'Email Confirmation',
        #         'Please click the link below to confirm your email address:\n' + confirmation_link
        #     )
        #     server.sendmail("EMAIL", input.get('email'), message)
        #     server.quit()
        # except:
        #     print('Email failed to send.')

        return Register(user=new_user)
Beispiel #2
0
    def mutate_and_get_payload(root, info, **input):
        password = input.pop('password')
        cleaned_input = clean_input(input)
        cleaned_input['username'] = cleaned_input.get('username').lower()
        username = cleaned_input.get('username')

        # check if username exists
        if UserModel.objects.filter(username=username).exists():
            raise GraphQLError('Username Taken.')

        new_user = UserModel(username=username)
        new_user.set_password(password)
        new_user.save()

        UserSubscriptions.broadcast(group='users-subscription',
                                    payload={
                                        "type":
                                        SpecialMutationTypes.REGISTER.name,
                                        "username": username
                                    })

        return Register(user=new_user)