Beispiel #1
0
def unfollow_feed(feed_id):
    user_id = get_jwt_identity()
    try:
        db.session.execute(
            user_feed
            .delete()
            .where(user_feed.c.user_id == user_id)
            .where(user_feed.c.feed_id == feed_id)
        )

        db.session.execute(
            custom_topic_feed
            .delete()
            .where(custom_topic_feed.c.feed_id == feed_id)
            .where(
                custom_topic_feed.c.custom_topic_id.in_(
                    db.session.query(CustomTopic.id)
                    .filter(CustomTopic.user_id == user_id)
                    .join(
                        custom_topic_feed,
                        custom_topic_feed.c.custom_topic_id == CustomTopic.id
                    )
                    .join(Feed, Feed.id == custom_topic_feed.c.feed_id)
                    .filter(Feed.id == feed_id)
                    .subquery()
                )
            )
        )

        db.session.commit()
    except IntegrityError:
        pass

    return Responses.ok()
Beispiel #2
0
def add_feed():
    feed_url = request.json_data.get('feed_url')
    feed = get_or_create_feed(feed_url)
    user = User.query.filter_by(id=get_jwt_identity()).one()
    user.feeds.append(feed)
    db.session.commit()

    return Responses.ok()
Beispiel #3
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)
Beispiel #4
0
def remove_saved_feed_item(feed_item_id):

    SavedFeedItem.query.filter(
        SavedFeedItem.feed_item_id == feed_item_id,
        SavedFeedItem.user_id == get_jwt_identity()
    ).delete()

    db.session.commit()

    return Responses.ok()
Beispiel #5
0
def delete_custom_topic(custom_topic_id):
    try:
        CustomTopic.query.filter_by(
            id=custom_topic_id,
            user_id=get_jwt_identity()
        ).delete()
        db.session.commit()
    except IntegrityError:
        pass

    return Responses.ok()
Beispiel #6
0
def add_custom_topic():
    # noinspection PyArgumentList
    topic = CustomTopic(
        name=request.json_data.get('name'),
        user_id=get_jwt_identity()
    )

    db.session.add(topic)
    db.session.commit()

    return Responses.ok()
Beispiel #7
0
def save_feed_item(feed_item_id):
    saved_feed_item = SavedFeedItem(
        user_id=get_jwt_identity(),
        feed_item_id=feed_item_id
    )

    try:
        db.session.add(saved_feed_item)
        db.session.commit()
    except IntegrityError:
        pass

    return Responses.ok()
Beispiel #8
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()
Beispiel #9
0
def create_user():
    user = User(**request.json_data)
    db.session.add(user)
    db.session.commit()

    return Responses.ok()