Example #1
0
def test_get_articles_by_date_with_non_existent_date(in_memory_repo):
    target_date = date.fromisoformat('2020-03-06')

    articles_as_dict, prev_date, next_date = news_services.get_articles_by_date(target_date, in_memory_repo)

    # Check that there are no articles dated 2020-03-06.
    assert len(articles_as_dict) == 0
Example #2
0
def test_get_articles_by_date_with_one_date(in_memory_repo):
    target_date = date.fromisoformat('2020-02-28')

    articles_as_dict, prev_date, next_date = news_services.get_articles_by_date(target_date, in_memory_repo)

    assert len(articles_as_dict) == 1
    assert articles_as_dict[0]['id'] == 1

    assert prev_date is None
    assert next_date == date.fromisoformat('2020-02-29')
Example #3
0
def test_get_articles_by_date_with_multiple_dates(in_memory_repo):
    target_date = date.fromisoformat('2020-03-01')

    articles_as_dict, prev_date, next_date = news_services.get_articles_by_date(target_date, in_memory_repo)

    # Check that there are 3 articles dated 2020-03-01.
    assert len(articles_as_dict) == 3

    # Check that the article ids for the the articles returned are 3, 4 and 5.
    article_ids = [article['id'] for article in articles_as_dict]
    assert set([3, 4, 5]).issubset(article_ids)

    # Check that the dates of articles surrounding the target_date are 2020-02-29 and 2020-03-05.
    assert prev_date == date.fromisoformat('2020-02-29')
    assert next_date == date.fromisoformat('2020-03-05')
Example #4
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'))