Ejemplo n.º 1
0
def accept_invitation(token):

    if 'password' not in request.json:
        bottle.abort(422, "A password is required for the new user")

    session = None
    try:
        session = db.Session()
        invitation = session.query(Invitation).filter_by(token=token).first()
        if not invitation:
            bottle.abort(404)

        invitation.accepted = True
        user = User(
            **{
                'email': invitation.email,
                'organization_id': invitation.organization_id,
                'password': request.json['password'],
                'last_accessed': datetime.now(),
                'access_token': create_unique_token(),
                'access_token_expiration': datetime.now() + timedelta(weeks=2)
            })
        session.add(user)
        session.commit()
        user_json = user.json()
    finally:
        if session:
            session.close()

    return user_json
Ejemplo n.º 2
0
def accept_invitation(token):

    if 'password' not in request.json:
        bottle.abort(422, "A password is required for the new user")

    session = None
    try:
        session = db.Session()
        invitation = session.query(Invitation).filter_by(token=token).first()
        if not invitation:
            bottle.abort(404)

        invitation.accepted = True
        user = User(**{
            'email': invitation.email,
            'organization_id': invitation.organization_id,
            'password': request.json['password'],
            'last_accessed': datetime.now(),
            'access_token': create_unique_token(),
            'access_token_expiration': datetime.now() + timedelta(weeks=2)
        })
        session.add(user)
        session.commit()
        user_json = user.json()
    finally:
        if session:
            session.close()

    return user_json
Ejemplo n.º 3
0
def send_invitation(organization_id):

    token = create_unique_token()
    subject = 'Bauble Invitation'
    to_email = request.json.get('email', None)
    if to_email is None or '@' not in to_email:
        bottle.abort(422, "An email recipient is required.")

    # make sure a user with this email address doesn't already have an account
    count = request.session.query(User).filter_by(email=to_email).count()
    if count > 0:
        bottle.abort(
            409, "A user with this email address already has a Bauble account")

    # if a message was provided then send it else use the default message
    try:
        print('request.json: ', request.json)
        if 'message' in request.json:
            email.send(
                request.json['message'], **{
                    'to': to_email,
                    'subject': subject,
                    'from': '*****@*****.**'
                })
        else:
            email.send_template(
                'default_invite.txt', {
                    'organization': request.organization.name,
                    'app_url': config.get("BAUBLE_APP_URL"),
                    'token': token
                }, **{
                    'to': request.json['email'],
                    'subject': subject,
                    'from': '*****@*****.**'
                })
    except smtplib.SMTPException as exc:
        print('exc: ', exc)
        bottle.abort(500, 'Could not send invitation email.')

    invitation = Invitation(
        **{
            'email': to_email,
            'organization_id': request.organization.id,
            'date_sent': datetime.now(),
            'invited_by_id': request.user.id,
            'message': request.json['message'] if 'message' in
            request.json else None,
            'token': token,
            'token_expiration': datetime.now() + timedelta(weeks=2)
        })

    request.session.add(invitation)
    request.session.commit()
Ejemplo n.º 4
0
def send_invitation(organization_id):

    token = create_unique_token()
    subject = 'Bauble Invitation'
    to_email = request.json.get('email', None)
    if to_email is None or '@' not in to_email:
        bottle.abort(422, "An email recipient is required.")

    # make sure a user with this email address doesn't already have an account
    count = request.session.query(User).filter_by(email=to_email).count()
    if count > 0:
        bottle.abort(409, "A user with this email address already has a Bauble account")

    # if a message was provided then send it else use the default message
    try:
        print('request.json: ', request.json)
        if 'message' in request.json:
            email.send(request.json['message'], **{
                'to': to_email,
                'subject': subject,
                'from': '*****@*****.**'
            })
        else:
            email.send_template('default_invite.txt', {
                'organization': request.organization.name,
                'app_url': config.get("BAUBLE_APP_URL"),
                'token': token
            }, **{
                'to': request.json['email'],
                'subject': subject,
                'from': '*****@*****.**'
            })
    except smtplib.SMTPException as exc:
        print('exc: ', exc)
        bottle.abort(500, 'Could not send invitation email.')

    invitation = Invitation(**{
        'email': to_email,
        'organization_id': request.organization.id,
        'date_sent': datetime.now(),
        'invited_by_id': request.user.id,
        'message': request.json['message'] if 'message' in request.json else None,
        'token': token,
        'token_expiration': datetime.now() + timedelta(weeks=2)
    })

    request.session.add(invitation)
    request.session.commit()
Ejemplo n.º 5
0
def create_password_reset_token():
    return create_unique_token(), datetime.now() + timedelta(days=1)
Ejemplo n.º 6
0
def create_access_token():
    return create_unique_token(), datetime.now() + timedelta(weeks=2)