示例#1
0
def articles_by_date():
    # Read query parameters.
    target_date = request.args.get('date')
    article_to_show_comments = request.args.get('view_comments_for')

    # Fetch the first and last articles in the series.
    first_article = services.get_first_article(uow.uow_instance)
    last_article = services.get_last_article(uow.uow_instance)

    if target_date is None:
        # No date query parameter, so return articles from day 1 of the series.
        target_date = first_article['date']
    else:
        # Convert target_date from string to date.
        target_date = date.fromisoformat(target_date)

    if article_to_show_comments is None:
        # No view-comments query parameter, so set to a non-existent article id.
        article_to_show_comments = -1
    else:
        # Convert article_to_show_comments from string to int.
        article_to_show_comments = int(article_to_show_comments)

    # Fetch article(s) for the target date. This call also returns the previous and next dates for articles immediately
    # before and after the target date.
    articles, previous_date, next_date = services.get_articles_by_date(
        target_date, uow.uow_instance)

    first_article_url = None
    last_article_url = None
    next_article_url = None
    prev_article_url = None

    if len(articles) > 0:
        # There's at least one article for the target date.
        if previous_date is not None:
            # There are articles on a previous date, so generate URLs for the 'previous' and 'first' navigation buttons.
            prev_article_url = url_for('news_bp.articles_by_date',
                                       date=previous_date.isoformat())
            first_article_url = url_for('news_bp.articles_by_date',
                                        date=first_article['date'].isoformat())

        # There are articles on a subsequent date, so generate URLs for the 'next' and 'last' navigation buttons.
        if next_date is not None:
            next_article_url = url_for('news_bp.articles_by_date',
                                       date=next_date.isoformat())
            last_article_url = url_for('news_bp.articles_by_date',
                                       date=last_article['date'].isoformat())

        # Construct urls for viewing article comments and adding comments.
        for article in articles:
            article['view_comment_url'] = url_for(
                'news_bp.articles_by_date',
                date=target_date,
                view_comments_for=article['id'])
            article['add_comment_url'] = url_for('news_bp.comment_on_article',
                                                 article=article['id'])

        # Generate the webpage to display the articles.
        return render_template(
            'news/articles.html',
            title='Articles',
            articles_title=target_date.strftime('%A %B %e %Y'),
            articles=articles,
            selected_articles=utilities.get_selected_articles(
                len(articles) * 2),
            tag_urls=utilities.get_tags_and_urls(),
            first_article_url=first_article_url,
            last_article_url=last_article_url,
            prev_article_url=prev_article_url,
            next_article_url=next_article_url,
            show_comments_for_article=article_to_show_comments)

    # No articles to show, so return the homepage.
    return redirect(url_for('home_bp.home'))
def test_get_first_article(in_memory_uow):
    article_as_dict = news_services.get_first_article(in_memory_uow)

    assert article_as_dict['id'] == 1