Example #1
0
def homepage():
    page_param = flask.request.args.get('page', default=1, type=int)

    try:
        articles, total_pages = api.get_articles(page=page_param)
    except ApiError as api_error:
        return flask.abort(502, str(api_error))

    for article in articles:
        try:
            featured_image = api.get_media(article['featured_media'])
        except ApiError:
            featured_image = None

        try:
            author = api.get_user(article['author'])
        except ApiError:
            author = None

        article = logic.transform_article(article,
                                          featured_image=featured_image,
                                          author=author)

    context = {
        'current_page': page_param,
        'total_pages': int(total_pages),
        'articles': articles,
    }

    return flask.render_template('blog/index.html', **context)
Example #2
0
def article(slug):
    try:
        articles = api.get_article(slug)
    except ApiCircuitBreaker:
        return flask.abort(503)
    except ApiError as api_error:
        return flask.abort(502, str(api_error))

    if not articles:
        flask.abort(404, "Article not found")

    article = articles[0]

    try:
        author = api.get_user(article["author"])
    except ApiError:
        author = None

    transformed_article = logic.transform_article(article,
                                                  author=author,
                                                  optimise_images=True)

    tags = article["tags"]
    tag_names = []
    try:
        tag_names_response = api.get_tags_by_ids(tags)
    except ApiError:
        tag_names_response = None

    if tag_names_response:
        for tag in tag_names_response:
            tag_names.append({"id": tag["id"], "name": tag["name"]})

    is_in_series = logic.is_in_series(tag_names)

    try:
        related_articles, total_pages = api.get_articles(tags=tags,
                                                         per_page=3,
                                                         exclude=article["id"])
    except ApiError:
        related_articles = None

    if related_articles:
        for related_article in related_articles:
            related_article = logic.transform_article(related_article)

    newsletter_subscribed = flask.request.args.get("newsletter",
                                                   default=False,
                                                   type=bool)

    context = {
        "article": transformed_article,
        "related_articles": related_articles,
        "tags": tag_names,
        "is_in_series": is_in_series,
        "newsletter_subscribed": newsletter_subscribed,
    }

    return flask.render_template("blog/article.html", **context)
Example #3
0
def article(slug):
    try:
        articles = api.get_article(slug)
    except ApiCircuitBreaker:
        return flask.abort(503)
    except ApiError as api_error:
        return flask.abort(502, str(api_error))

    if not articles:
        flask.abort(404, "Article not found")

    article = articles[0]

    try:
        author = api.get_user(article["author"])
    except ApiError:
        author = None

    transformed_article = logic.transform_article(
        article, author=author, optimise_images=True
    )

    tags = article["tags"]
    tag_names = []
    try:
        tag_names_response = api.get_tags_by_ids(tags)
    except ApiError:
        tag_names_response = None

    if tag_names_response:
        for tag in tag_names_response:
            tag_names.append({"id": tag["id"], "name": tag["name"]})

    is_in_series = logic.is_in_series(tag_names)

    try:
        related_articles, total_pages = api.get_articles(
            tags=tags, per_page=3, exclude=article["id"]
        )
    except ApiError:
        related_articles = None

    if related_articles:
        for related_article in related_articles:
            related_article = logic.transform_article(related_article)

    context = {
        "article": transformed_article,
        "related_articles": related_articles,
        "tags": tag_names,
        "is_in_series": is_in_series,
    }

    return flask.render_template("blog/article.html", **context)
Example #4
0
def article(slug):
    try:
        articles = api.get_article(slug)
    except ApiError as api_error:
        return flask.abort(502, str(api_error))

    if not articles:
        flask.abort(404, 'Article not found')

    article = articles[0]

    try:
        author = api.get_user(article['author'])
    except ApiError:
        author = None

    transformed_article = logic.transform_article(article, author=author)

    tags = article['tags']

    try:
        related_articles, total_pages = api.get_articles(tags=tags,
                                                         per_page=3,
                                                         exclude=article['id'])
    except ApiError:
        related_articles = None

    if related_articles:
        for related_article in related_articles:
            related_article = logic.transform_article(related_article)

    context = {
        'article': transformed_article,
        'related_articles': related_articles
    }

    return flask.render_template('blog/article.html', **context)
Example #5
0
def homepage():
    BLOG_CATEGORIES_ENABLED = (
        flask.current_app.config["BLOG_CATEGORIES_ENABLED"] == "true"
    )
    page_param = flask.request.args.get("page", default=1, type=int)

    # Feature flag
    if BLOG_CATEGORIES_ENABLED:
        filter = flask.request.args.get("filter", default=None, type=str)

        if filter == "all":
            filter = None

        try:
            categories_list = api.get_categories()
        except ApiError:
            categories_list = None

        categories = logic.whitelist_categories(categories_list)

        filter_category = next(
            (
                item["id"]
                for item in categories
                if item["name"].lower() == filter
            ),
            None,
        )
    else:
        filter_category = None
        categories = None
        filter = None

    try:
        articles, total_pages = api.get_articles(
            page=page_param, category=filter_category
        )
    except ApiError as api_error:
        return flask.abort(502, str(api_error))

    category_cache = {}

    for article in articles:
        # XXX Luke 10-09-2018
        # Until the blog api returns smaller images
        # preventing this, should speed the page up
        # try:
        #    featured_image = api.get_media(article["featured_media"])
        # except ApiError:
        featured_image = None

        try:
            author = api.get_user(article["author"])
        except ApiError:
            author = None

        # Feature flag
        if BLOG_CATEGORIES_ENABLED:
            category_ids = article["categories"]

            for category_id in category_ids:
                if category_id not in category_cache:
                    category_cache[category_id] = {}

        article = logic.transform_article(
            article, featured_image=featured_image, author=author
        )

    # Feature flag
    if BLOG_CATEGORIES_ENABLED:
        for key, category in category_cache.items():
            try:
                resolved_category = api.get_category_by_id(key)
            except ApiError:
                resolved_category = None

            category_cache[key] = resolved_category

    context = {
        "current_page": page_param,
        "total_pages": int(total_pages),
        "articles": articles,
        "categories": categories,
        "used_categories": category_cache,
        "filter": filter,
    }

    return flask.render_template("blog/index.html", **context)
Example #6
0
def homepage():
    BLOG_CATEGORIES_ENABLED = (
        flask.current_app.config["BLOG_CATEGORIES_ENABLED"] == "true")

    page_param = flask.request.args.get("page", default=1, type=int)

    # Feature flag
    if BLOG_CATEGORIES_ENABLED:
        filter = flask.request.args.get("filter", default=None, type=str)

        if filter == "all":
            filter = None

        try:
            categories_list = api.get_categories()
        except ApiError:
            categories_list = []

        categories = logic.whitelist_categories(categories_list)

        filter_category = next(
            (item["id"]
             for item in categories if item["name"].lower() == filter),
            None,
        )
    else:
        filter_category = None
        categories = None
        filter = None

    try:
        # Anything after page 1 needs to be offset by the page -11
        # because the newsletter takes a spot on the first page
        # The only way to do this with wordpress is to set a specific
        # item offset `offset` from the start of the collection
        get_page = 1
        page_offset = 0

        if page_param > 1:
            get_page = 1
            page_offset = (12 * (page_param - 1)) - 1

        articles, total_pages = api.get_articles(page=get_page,
                                                 category=filter_category,
                                                 offset=page_offset)
    except ApiCircuitBreaker:
        return flask.abort(503)
    except ApiError as api_error:
        return flask.abort(502, str(api_error))

    category_cache = {}

    for article in articles:
        try:
            featured_image = api.get_media(article["featured_media"])
        except ApiError:
            featured_image = None

        try:
            author = api.get_user(article["author"])
        except ApiError:
            author = None

        # Feature flag
        if BLOG_CATEGORIES_ENABLED:
            category_ids = article["categories"]

            for category_id in category_ids:
                if category_id not in category_cache:
                    category_cache[category_id] = {}

        article = logic.transform_article(article,
                                          featured_image=featured_image,
                                          author=author)

    # Feature flag
    if BLOG_CATEGORIES_ENABLED:
        for key, category in category_cache.items():
            try:
                resolved_category = api.get_category_by_id(key)
            except ApiError:
                resolved_category = None

            category_cache[key] = resolved_category

    if page_param == 1 and len(articles) > 2:
        articles.insert(2, "newsletter")
        if len(articles) == 13:
            articles.pop(12)

    newsletter_subscribed = flask.request.args.get("newsletter",
                                                   default=False,
                                                   type=bool)

    context = {
        "current_page": page_param,
        "total_pages": int(total_pages),
        "articles": articles,
        "categories": categories,
        "used_categories": category_cache,
        "filter": filter,
        "newsletter_subscribed": newsletter_subscribed,
    }

    return flask.render_template("blog/index.html", **context)
Example #7
0
def homepage():
    BLOG_CATEGORIES_ENABLED = (
        flask.current_app.config["BLOG_CATEGORIES_ENABLED"] == "true")
    page_param = flask.request.args.get("page", default=1, type=int)

    # Feature flag
    if BLOG_CATEGORIES_ENABLED:
        filter = flask.request.args.get("filter", default=None, type=str)

        if filter == "all":
            filter = None

        try:
            categories_list = api.get_categories()
        except ApiError:
            categories_list = []

        categories = logic.whitelist_categories(categories_list)

        filter_category = next(
            (item["id"]
             for item in categories if item["name"].lower() == filter),
            None,
        )
    else:
        filter_category = None
        categories = None
        filter = None

    try:
        articles, total_pages = api.get_articles(page=page_param,
                                                 category=filter_category)
    except ApiCircuitBreaker:
        return flask.abort(503)
    except ApiError as api_error:
        return flask.abort(502, str(api_error))

    category_cache = {}

    for article in articles:
        try:
            featured_image = api.get_media(article["featured_media"])
        except ApiError:
            featured_image = None

        try:
            author = api.get_user(article["author"])
        except ApiError:
            author = None

        # Feature flag
        if BLOG_CATEGORIES_ENABLED:
            category_ids = article["categories"]

            for category_id in category_ids:
                if category_id not in category_cache:
                    category_cache[category_id] = {}

        article = logic.transform_article(article,
                                          featured_image=featured_image,
                                          author=author)

    # Feature flag
    if BLOG_CATEGORIES_ENABLED:
        for key, category in category_cache.items():
            try:
                resolved_category = api.get_category_by_id(key)
            except ApiError:
                resolved_category = None

            category_cache[key] = resolved_category

    context = {
        "current_page": page_param,
        "total_pages": int(total_pages),
        "articles": articles,
        "categories": categories,
        "used_categories": category_cache,
        "filter": filter,
    }

    return flask.render_template("blog/index.html", **context)