コード例 #1
0
ファイル: views.py プロジェクト: georgejdanforth/trough
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
ファイル: views.py プロジェクト: georgejdanforth/trough
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
        )
    ))
コード例 #3
0
ファイル: views.py プロジェクト: georgejdanforth/trough
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())
    ))
コード例 #4
0
ファイル: views.py プロジェクト: georgejdanforth/trough
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())
    )
コード例 #5
0
ファイル: views.py プロジェクト: georgejdanforth/trough
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})
コード例 #6
0
ファイル: views.py プロジェクト: georgejdanforth/trough
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)