def get_article_context(article, related_tag_ids=[]):
    """
    Build the content for the article page
    :param article: Article to create context for
    """

    author = api.get_user(article["author"])

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

    tags = article["tags"]
    tag_names = []
    tag_names_response = api.get_tags_by_ids(tags)

    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)

    all_related_articles, total_pages = api.get_articles(
        tags=tags, per_page=3, exclude=[article["id"]]
    )

    related_articles = []
    if all_related_articles:
        for related_article in all_related_articles:
            if set(related_tag_ids).issubset(related_article["tags"]):
                related_articles.append(
                    logic.transform_article(related_article)
                )

    for group_id in article["group"]:
        if group_id not in group_cache:
            resolved_group = api.get_group_by_id(group_id)
            group_cache[group_id] = resolved_group
            article["group"] = resolved_group
            break
        else:
            article["group"] = group_cache[group_id]

    return {
        "article": transformed_article,
        "related_articles": related_articles,
        "tags": tag_names,
        "is_in_series": is_in_series,
    }
def get_article_context(article, related_tag_ids=[], excluded_tags=[]):
    """
    Build the content for the article page
    :param article: Article to create context for
    """
    transformed_article = get_complete_article(article)

    tags = logic.get_embedded_tags(article["_embedded"])
    is_in_series = logic.is_in_series(tags)

    all_related_articles, _ = api.get_articles(
        tags=[tag["id"] for tag in tags],
        tags_exclude=excluded_tags,
        per_page=3,
        exclude=[article["id"]],
    )

    related_articles = []
    for related_article in all_related_articles:
        if set(related_tag_ids) <= set(related_article["tags"]):
            related_articles.append(logic.transform_article(related_article))

    return {
        "article": transformed_article,
        "related_articles": related_articles,
        "tags": tags,
        "is_in_series": is_in_series,
    }
def get_complete_article(article, group=None):
    """
    This returns any given article from the wordpress API
    as an object that includes all information for the templates,
    some of which will be fetched from the Wordpress API
    """
    featured_images = logic.get_embedded_featured_media(article["_embedded"])
    featured_image = {}
    if featured_images:
        featured_image = featured_images[0]
    author = logic.get_embedded_author(article["_embedded"])
    categories = logic.get_embedded_categories(article["_embedded"])

    for category in categories:
        if "display_category" not in article:
            article["display_category"] = category

    if group:
        article["group"] = group
    else:
        article["group"] = logic.get_embedded_group(article["_embedded"])

    return logic.transform_article(
        article, featured_image=featured_image, author=author
    )
Example #4
0
    def snap_posts(snap):
        try:
            blog_tags = wordpress_api.get_tag_by_name(f"sc:snap:{snap}")
        except Exception:
            blog_tags = None

        blog_articles = None
        articles = []

        if blog_tags:
            try:
                blog_articles, total_pages = wordpress_api.get_articles(
                    blog_tags["id"], 3)
            except Exception:
                blog_articles = []

            for article in blog_articles:
                transformed_article = logic.transform_article(
                    article, featured_image=None, author=None)
                articles.append({
                    "slug":
                    transformed_article["slug"],
                    "title":
                    transformed_article["title"]["rendered"],
                })

        return flask.jsonify(articles)
    def snap_series(series):
        blog_articles = None
        articles = []

        try:
            blog_articles, total_pages = wordpress_api.get_articles(series)
        except RequestException:
            blog_articles = []

        for article in blog_articles:
            transformed_article = logic.transform_article(article,
                                                          featured_image=None,
                                                          author=None)
            articles.append({
                "slug": transformed_article["slug"],
                "title": transformed_article["title"]["rendered"],
            })

        return flask.jsonify(articles)
def get_complete_article(article, group=None):
    """
    This returns any given article from the wordpress API
    as an object that includes all information for the templates,
    some of which will be fetched from the Wordpress API
    """
    featured_image = api.get_media(article["featured_media"])

    author = api.get_user(article["author"])

    category_ids = article["categories"]

    # Can these calls be bundled?
    first_item = True
    for category_id in category_ids:
        if category_id not in category_cache:
            resolved_category = api.get_category_by_id(category_id)
            category_cache[category_id] = resolved_category
        if first_item:
            article["display_category"] = category_cache[category_id]
            first_item = False

    if group:
        article["group"] = group
    else:
        first_item = True
        for group_id in article["group"]:
            if group_id not in group_cache:
                resolved_group = api.get_group_by_id(group_id)
                group_cache[group_id] = resolved_group
            if first_item:
                article["group"] = group_cache[group_id]
                first_item = False

    return logic.transform_article(
        article, featured_image=featured_image, author=author
    )
    def snap_posts(snap):
        blog_tags = wordpress_api.get_tag_by_name(f"sc:snap:{snap}")
        blog_articles = None
        articles = []

        third_party_blogs = get_yaml("blog/content/blog-posts.yaml")

        if third_party_blogs and snap in third_party_blogs:
            post = third_party_blogs[snap]
            cdn_image = "/".join([
                "https://res.cloudinary.com",
                "canonical",
                "image",
                "fetch",
                "f_auto,q_auto,fl_sanitize,w_346,h_231,c_fill",
                post["image"],
            ])
            brand_image = "https://assets.ubuntu.com/v1/aae0f33a-omgubuntu.svg"
            image_element = "".join([
                f'<img src="{cdn_image}" ',
                'style="display:block">',
                f'<img src="{brand_image}" ',
                'class="p-blog-post__source" />',
            ])
            articles.append({
                "slug": post["uri"],
                "title": post["title"],
                "image": image_element,
            })

        if blog_tags:
            snapcraft_tag = wordpress_api.get_tag_by_name("snapcraft.io")

            try:
                blog_articles, total_pages = wordpress_api.get_articles(
                    blog_tags["id"], 3 - len(articles))
            except RequestException:
                blog_articles = []

            for article in blog_articles:
                try:
                    featured_media = wordpress_api.get_media(
                        article["featured_media"])
                    if featured_media:
                        featured_media = image_template(
                            url=featured_media["source_url"],
                            alt="",
                            width="346",
                            height="231",
                            fill=True,
                            hi_def=True,
                            loading="auto",
                        )
                except RequestException:
                    featured_media = None

                transformed_article = logic.transform_article(
                    article, featured_image=featured_media, author=None)

                url = f"/blog/{transformed_article['slug']}"

                if snapcraft_tag["id"] not in transformed_article["tags"]:
                    url = f"https://ubuntu.com{url}"

                articles.append({
                    "slug":
                    url,
                    "title":
                    transformed_article["title"]["rendered"],
                    "image":
                    transformed_article["image"],
                })

        return flask.jsonify(articles)