Ejemplo n.º 1
0
def invite():
    user_pending = None

    try:
        body = request.json
        if body is None:
            return error_view(400, "invalid JSON in body")

        email = body.get('email')
        if email is None:
            return error_view(400, "invalid email value")

        # check if mail is already used
        if User.exists_from_email(email):
            return error_view(500, f"email already used by an existing user")

        if UserRequest.exists(email):
            return error_view(500, f"a request for this email already exists")

        if UserPending.exists_from_email(email):
            return error_view(
                500, f"a user with this email has already been invited")

        # create a new pending user in database
        user_pending = UserPending.new(email)
        user_pending.insert()

        # send a mail with the token
        default_channel = Channel.get(Channel.DEFAULT)
        template = INVITE_TEMPLATE
        template.set_format(token=user_pending.token)
        send(user_pending.email, default_channel, template)

        return user_pending_created_view(user_pending)

    except ObjectNotFound as o:
        return error_view(404, str(o))

    except (MailSendingError, TelegramSendingError):
        # in case the mail cannot be sent, abort the invitation and delete the pending user in database
        if user_pending is not None:
            user_pending.delete()

        return error_view(500, f"error sending the invitation")
Ejemplo n.º 2
0
def request_access():
    body = request.json
    if body is None:
        return error_view(400, "invalid JSON in body")

    email = body.get('email')
    if email is None:
        return error_view(400, "invalid email value")

    if User.exists_from_email(email):
        return error_view(500, "email unavailable")

    if UserPending.exists_from_email(email):
        return error_view(500, "an invitation has already been sent to this email")

    if UserRequest.exists(email):
        return error_view(500, "a request for this email has already been sent")

    user_request = UserRequest.new(email)
    user_request.insert()

    return user_request_created_view(user_request)