示例#1
0
def login():
    email = request.json_data.get('email')
    password = request.json_data.get('password')

    user = User.query.filter_by(email=email).first()

    if not user:
        return Responses.error(f'User not found with email: {email}',
                               HTTPStatus.UNAUTHORIZED.value)

    if not bcrypt.check_password_hash(user.password, password):
        return Responses.error(
            f'Invalid password for user with email: {email}',
            HTTPStatus.UNAUTHORIZED.value)

    access_token = create_access_token(user)
    refresh_token = create_refresh_token(user)

    store_token(access_token)
    store_token(refresh_token)

    return Responses.json_response(
        {
            'access_token': access_token,
            'refresh_token': refresh_token
        }, HTTPStatus.OK.value)
示例#2
0
def logout():
    jti = get_raw_jwt().get('jti', '')
    user_id = get_jwt_identity()
    try:
        blacklist_token(jti, user_id)
        return Responses.ok()
    except TokenNotFound:
        return Responses.error(f'Token not found with JTI: {jti}',
                               HTTPStatus.NOT_FOUND.value)
示例#3
0
def manage_topic_feeds(topic_id):
    try:
        CustomTopic.for_user(get_jwt_identity()).filter(CustomTopic.id == topic_id).one()
    except NoResultFound:
        return Responses.error('No such topic for user.', HTTPStatus.NOT_FOUND.value)

    to_add, to_remove = [], []
    for feed_id, included in request.json_data.get('feed_ids', {}).items():
        if included:
            to_add.append(int(feed_id))
        else:
            to_remove.append(int(feed_id))

    existing = flatten(
        db.session.query(custom_topic_feed.c.feed_id).filter(
            custom_topic_feed.c.custom_topic_id == topic_id
        ).all()
    )

    to_add = list(set(to_add) - set(existing))
    to_remove = list(set(to_remove) & set(existing))

    try:
        if to_add:
            db.session.execute(
                custom_topic_feed.insert().values(
                    list(itertools.product([topic_id], to_add))
                )
            )

        if to_remove:
            db.session.execute(
                custom_topic_feed
                .delete()
                .where(custom_topic_feed.c.custom_topic_id == topic_id)
                .where(custom_topic_feed.c.feed_id.in_(to_remove))
            )

        db.session.commit()

    except IntegrityError:
        pass

    return Responses.ok()