示例#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 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()
示例#4
0
def get_feed_items():
    user = User.query.filter_by(id=get_jwt_identity()).one()
    order_by = FeedItem.pubdate.desc()

    if 'saved' in request.query_params:
        feed_items = FeedItem.for_saved(user.id)
        order_by = SavedFeedItem.id.desc()
    elif 'feed_id' in request.query_params:
        feed_items = FeedItem.for_feed(user.id, request.query_params['feed_id'])
    elif 'topic_id' in request.query_params:
        feed_items = FeedItem.for_topic(user.id, request.query_params['topic_id'])
    else:
        feed_items = FeedItem.for_user(user.id)

    return Responses.json_response((
        {'is_saved': bool(feed_item_id), **feed_item.to_dict(user=user)}
        for feed_item, feed_item_id in (
            feed_items
            .order_by(order_by)
            .paginate(
                page=int(request.query_params.get('page', 1)),
                per_page=constants.MAX_ITEMS_PER_PAGE
            )
            .items
        )
    ))
示例#5
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()
示例#6
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()
示例#7
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()
示例#8
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()
示例#9
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()
示例#10
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()
示例#11
0
def get_custom_topics():
    for_feed = request.query_params.get('for_feed')
    if for_feed:
        topic_feeds = (
            db.session
            .query(custom_topic_feed)
            .filter(custom_topic_feed.c.feed_id == for_feed)
            .subquery()
        )

        return Responses.json_response((
            {'added': bool(feed_id), **topic.to_dict()} for topic, feed_id in (
                db.session
                .query(CustomTopic, topic_feeds.c.feed_id)
                .filter(CustomTopic.user_id == get_jwt_identity())
                .outerjoin(topic_feeds, topic_feeds.c.custom_topic_id == CustomTopic.id)
                .all()
            )
        ))

    return Responses.json_response((
        map(methodcaller('to_dict'), CustomTopic.for_user(get_jwt_identity()).all())
    ))
示例#12
0
def get_feeds():
    for_topic = request.query_params.get('for_topic')
    if for_topic:
        topic_feeds = (
            db.session
            .query(custom_topic_feed)
            .filter(custom_topic_feed.c.custom_topic_id == for_topic)
            .subquery()
        )

        return Responses.json_response((
            {'added': bool(topic_id), **feed.to_dict()} for feed, topic_id in (
                db.session
                .query(Feed, topic_feeds.c.custom_topic_id)
                .join(user_feed, user_feed.c.feed_id == Feed.id)
                .filter(user_feed.c.user_id == get_jwt_identity())
                .outerjoin(topic_feeds, topic_feeds.c.feed_id == Feed.id)
                .all()
            )
        ))

    return Responses.json_response(
        map(methodcaller('to_dict'), Feed.for_user(get_jwt_identity()).all())
    )
示例#13
0
def is_valid_feed():
    is_valid = False
    feed_url = request.query_params.get('url')

    if feed_url:
        try:
            Feed.query.filter_by(feed_url=FeedParser.clean_url(feed_url)).one()
            is_valid = True
        except NoResultFound:
            feed_response = requests.get(feed_url)
            if feed_response.ok:
                try:
                    FeedParser(feed_url, feed_response.content, parse_metadata=True)
                    is_valid = True
                except ValueError:
                    pass

    return Responses.json_response({'is_valid': is_valid})
示例#14
0
def refresh_access_token():
    user = User.query.filter_by(id=get_jwt_identity()).one()
    access_token = create_access_token(user)
    store_token(access_token)
    return Responses.json_response({'access_token': access_token},
                                   HTTPStatus.OK.value)
示例#15
0
def create_user():
    user = User(**request.json_data)
    db.session.add(user)
    db.session.commit()

    return Responses.ok()