def homepage(): page_param = flask.request.args.get("page", default=1, type=int) try: if page_param == "1": featured_articles, total_pages = api.get_articles( tags=tag_ids, tags_exclude=excluded_tags, page=page_param, sticky="true", per_page=3, ) featured_article_ids = [ article["id"] for article in featured_articles ] articles, total_pages = api.get_articles( tags=tag_ids, tags_exclude=excluded_tags, exclude=featured_article_ids, page=page_param, ) else: articles, total_pages = api.get_articles( tags=tag_ids, page=page_param ) featured_articles = [] except Exception: return flask.abort(502) context = get_index_context(page_param, articles, total_pages) return flask.render_template("blog/index.html", **context)
def latest_news(request): try: latest_pinned_articles = api.get_articles( tags=tag_ids, exclude=excluded_tags, page=1, per_page=1, sticky=True, ) # check if the number of returned articles is 0 if len(latest_pinned_articles[0]) == 0: latest_articles = api.get_articles( tags=tag_ids, exclude=excluded_tags, page=1, per_page=4, sticky=False, ) else: latest_articles = api.get_articles( tags=tag_ids, exclude=excluded_tags, page=1, per_page=3, sticky=False, ) except Exception: return JsonResponse({"Error": "An error ocurred"}, status=502) return JsonResponse({ "latest_articles": latest_articles, "latest_pinned_articles": latest_pinned_articles, })
def latest_news(): try: latest_pinned_articles = api.get_articles( tags=tag_ids, exclude=excluded_tags, page=1, per_page=1, sticky=True, ) # check if the number of returned articles is 0 if len(latest_pinned_articles[0]) == 0: latest_articles = api.get_articles( tags=tag_ids, exclude=excluded_tags, page=1, per_page=4, sticky=False, ) else: latest_articles = api.get_articles( tags=tag_ids, exclude=excluded_tags, page=1, per_page=3, sticky=False, ) except Exception: return flask.jsonify({"Error": "An error ocurred"}), 502 return flask.jsonify( { "latest_articles": latest_articles, "latest_pinned_articles": latest_pinned_articles, } )
def get_index(self, page=1, category="", enable_upcoming=True): categories = [] if category: category_resolved = api.get_category_by_slug(category) if category_resolved: categories.append(category_resolved.get("id", "")) upcoming = [] featured_articles = [] if page == 1: featured_articles, _ = api.get_articles( tags=self.tag_ids, tags_exclude=self.excluded_tags, page=page, sticky="true", per_page=3, ) if enable_upcoming: # Maybe we can get the IDs since there is no chance # this going to move events = api.get_category_by_slug("events") webinars = api.get_category_by_slug("webinars") date_after = (datetime.now() - relativedelta(months=6)).isoformat() upcoming, _ = api.get_articles( tags=self.tag_ids, tags_exclude=self.excluded_tags, page=page, per_page=3, categories=[events["id"], webinars["id"]], after=date_after, ) articles, metadata = api.get_articles( tags=self.tag_ids, tags_exclude=self.excluded_tags, exclude=[article["id"] for article in featured_articles], page=page, per_page=self.per_page, categories=categories, ) total_pages = metadata["total_pages"] context = get_index_context( page, articles, total_pages, featured_articles=featured_articles, upcoming=upcoming, ) context["title"] = self.blog_title context["category"] = {"slug": category} context["upcoming"] = upcoming return context
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 group(request, slug, template_path): try: page_param = request.GET.get("page", default="1") category_param = request.GET.get("category", default="") group = api.get_group_by_slug(slug) group_id = group["id"] category_id = "" if category_param != "": category = api.get_category_by_slug(category_param) category_id = category["id"] articles, total_pages = api.get_articles( tags=tag_ids, tags_exclude=excluded_tags, page=page_param, groups=[group_id], categories=[category_id], ) except Exception as e: return HttpResponse("Error: " + e, status=502) context = get_group_page_context(page_param, articles, total_pages, group) context["title"] = blog_title context["category"] = {"slug": category_param} return render(request, template_path, context)
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 test_get_topic_page_context(self): articles, total_pages = api.get_articles() featured, _ = api.get_articles(sticky=True) # TODO: TDD refactor of function signature, to include # get the context by topic id # https://github.com/canonical-web-and-design/canonicalwebteam.blog/issues/69 topic_context = get_topic_page_context(1, articles, "2") self.assertEqual(topic_context["current_page"], 1) self.assertEqual(topic_context["total_pages"], 2) for article in topic_context["articles"]: self.assertIsNotNone(article["author"]["name"]) self.assertIsNotNone(article["image"]) self.assertIsNotNone(article["topic"])
def test_get_articles(self, get): get.return_value = SuccessMockResponse() _, metadata = api.get_articles() self.assertEqual(metadata["total_pages"], "12") self.assertEqual(metadata["total_posts"], "52") get.assert_called_once_with("https://admin.insights.ubuntu.com/" "wp-json/wp/v2/posts?" "per_page=12&page=1" "&_embed=true")
def test_get_articles_with_metadata(self, get): get.return_value = SuccessMockResponse() articles, metadata = api.get_articles() self.assertIsNotNone(metadata["total_pages"]) self.assertIsNotNone(metadata["total_posts"]) get.assert_called_once_with("https://admin.insights.ubuntu.com/" "wp-json/wp/v2/posts?per_page=12&page=1" "&_embed=true")
def test_get_index_context(self): articles, total_pages = api.get_articles() featured, _ = api.get_articles(sticky=True) index_context = get_index_context(1, articles, "2", featured) self.assertEqual(index_context["current_page"], 1) self.assertEqual(index_context["total_pages"], 2) for article in index_context["articles"]: self.assertIsNotNone(article["author"]["name"]) self.assertIsNotNone(article["image"]) self.assertIsNotNone(article["group"]) for article in index_context["featured_articles"]: self.assertIsNotNone(article["author"]["name"]) self.assertIsNotNone(article["image"]) self.assertIsNotNone(article["group"])
def test_getting_all_articles(self, get): get.return_value = MockResponse() article = api.get_articles() get.assert_called_once_with("https://admin.insights.ubuntu.com/" + "wp-json/wp/v2/posts?" + "per_page=12" + "&tags=" + "&page=1" + "&group=" + "&tags_exclude=" + "&categories=" + "&exclude=" + "&author=") self.assertEqual(article, (["hello_test"], 12))
def get_archives(self, page=1, group="", month="", year="", category=""): groups = [] categories = [] if group: group = api.get_group_by_slug(group) if not group: return None groups.append(group["id"]) if category: category_slugs = category.split(",") for slug in category_slugs: category = api.get_category_by_slug(slug) categories.append(category["id"]) after = None before = None if year: year = int(year) if month: after = datetime(year=year, month=int(month), day=1) before = after + relativedelta(months=1) else: after = datetime(year=year, month=1, day=1) before = datetime(year=year, month=12, day=31) articles, metadata = api.get_articles( tags=self.tag_ids, tags_exclude=self.excluded_tags, page=page, groups=groups, categories=categories, after=after, before=before, ) total_pages = metadata["total_pages"] total_posts = metadata["total_posts"] if group: context = get_group_page_context( page, articles, total_pages, group ) else: context = get_index_context(page, articles, total_pages) context["title"] = self.blog_title context["total_posts"] = total_posts return context
def homepage(): page_param = flask.request.args.get("page", default=1, type=int) try: articles, total_pages = api.get_articles(tags=tags_id, page=page_param) except Exception: return flask.abort(502) context = get_index_context(page_param, articles, total_pages) return flask.render_template("blog/index.html", **context)
def get_index_feed(self, uri, path): articles, _ = api.get_articles(tags=self.tag_ids, tags_exclude=self.excluded_tags) feed = feeds.build_feed( uri=uri, path=path, title=self.blog_title, description=self.feed_description, articles=articles, ) return feed.rss_str()
def get_latest_article(self): articles, _ = api.get_articles( tags=self.tag_ids, tags_exclude=self.excluded_tags, page=1, per_page=1, ) if not articles: return {} return get_article_context(articles[0], self.tag_ids, self.excluded_tags)
def index(request): page_param = request.GET.get("page", default="1") category_param = request.GET.get("category", default="") try: if page_param == "1": featured_articles, total_pages = api.get_articles( tags=tag_ids, tags_exclude=excluded_tags, page=page_param, sticky="true", per_page=3, ) featured_article_ids = [ article["id"] for article in featured_articles ] articles, total_pages = api.get_articles( tags=tag_ids, tags_exclude=excluded_tags, exclude=featured_article_ids, page=page_param, ) else: articles, total_pages = api.get_articles( tags=tag_ids, page=page_param, tags_exclude=excluded_tags) featured_articles = [] except Exception as e: return HttpResponse("Error: " + e, status=502) context = get_index_context(page_param, articles, total_pages, featured_articles=featured_articles) context["title"] = blog_title context["category"] = {"slug": category_param} return render(request, "blog/index.html", context)
def index(request): page_param = request.GET.get("page", default=1) try: articles, total_pages = api.get_articles(tags=tags_id, exclude=excluded_tags, page=page_param) except Exception: return HttpResponse(status=502) context = get_index_context(page_param, articles, total_pages) context["title"] = blog_title return render(request, "blog/index.html", context)
def test_getting_articles_from_last_year(self, get): get.return_value = MockResponse() before = date(year=2007, month=12, day=5) after = before - timedelta(days=365) article = api.get_articles(after=after, before=before) get.assert_called_once_with("https://admin.insights.ubuntu.com/" + "wp-json/wp/v2/posts?" + "per_page=12" + "&tags=" + "&page=1" + "&group=" + "&tags_exclude=" + "&categories=" + "&exclude=" + "&author=" + "&before=2007-12-05" + "&after=2006-12-05") self.assertEqual(article, (["hello_test"], 12))
def get_topic(self, topic_slug, page=1): tag = api.get_tag_by_slug(topic_slug) articles, metadata = api.get_articles( tags=self.tag_ids + [tag["id"]], tags_exclude=self.excluded_tags, page=page, ) total_pages = metadata["total_pages"] context = get_topic_page_context(page, articles, total_pages) context["title"] = self.blog_title return context
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 test_get_group_page_context(self): articles, total_pages = api.get_articles() featured, _ = api.get_articles(sticky=True) # TODO: TDD refactor of function signature, to get # the groups based on a group id # https://github.com/canonical-web-and-design/canonicalwebteam.blog/issues/68 group_context = get_group_page_context(1, articles, "2", "test", featured) self.assertEqual(group_context["current_page"], 1) self.assertEqual(group_context["total_pages"], 2) self.assertEqual(group_context["group"], "test") for article in group_context["articles"]: self.assertIsNotNone(article["author"]["name"]) self.assertIsNotNone(article["image"]) self.assertIsNotNone(article["group"]) for article in group_context["featured_articles"]: self.assertIsNotNone(article["author"]["name"]) self.assertIsNotNone(article["image"]) self.assertIsNotNone(article["group"])
def get_latest_news(self, limit=3, tag_ids=None, group_ids=None): latest_pinned_articles, _ = api.get_articles( tags=tag_ids or self.tag_ids, tags_exclude=self.excluded_tags, groups=group_ids, page=1, per_page=1, sticky=True, ) latest_articles, _ = api.get_articles( tags=tag_ids or self.tag_ids, tags_exclude=self.excluded_tags, groups=group_ids, exclude=[article["id"] for article in latest_pinned_articles], page=1, per_page=limit, sticky=False, ) return { "latest_articles": latest_articles, "latest_pinned_articles": latest_pinned_articles, }
def get_tag(self, slug, page=1): tag = api.get_tag_by_slug(slug) if not tag: return None articles, metadata = api.get_articles( tags=[tag["id"]], tags_exclude=self.excluded_tags, page=page ) total_pages = metadata["total_pages"] context = get_topic_page_context(page, articles, total_pages) context["title"] = self.blog_title context["tag"] = tag return context
def get_upcoming(self, page=1): events = api.get_category_by_slug("events") webinars = api.get_category_by_slug("webinars") articles, metadata = api.get_articles( tags=self.tag_ids, tags_exclude=self.excluded_tags, page=page, categories=[events["id"], webinars["id"]], ) total_pages = metadata["total_pages"] context = get_index_context(page, articles, total_pages) context["title"] = self.blog_title return context
def topic(request, slug, template_path): try: page_param = request.GET.get("page", default="1") tag = api.get_tag_by_slug(slug) articles, total_pages = api.get_articles( tags=tag_ids + [tag["id"]], tags_exclude=excluded_tags, page=page_param, ) except Exception as e: return HttpResponse("Error: " + e, status=502) context = get_topic_page_context(page_param, articles, total_pages) context["title"] = blog_title return render(request, template_path, context)
def get_topic_feed(self, topic_slug, uri, path): tag = api.get_tag_by_slug(topic_slug) if not tag: return None articles, _ = api.get_articles(tags=[tag["id"]], tags_exclude=self.excluded_tags) title = f"{tag['name']} - {self.blog_title}" feed = feeds.build_feed( uri=uri, path=path, title=title, description=self.feed_description, articles=articles, ) return feed.rss_str()
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 author(request, username): try: author = api.get_user_by_username(username) articles, total_pages = api.get_articles( tags=tag_ids, tags_exclude=excluded_tags, per_page=5, author=author["id"], ) context = { "title": blog_title, "author": author, "latest_articles": articles, } return render(request, "blog/author.html", context) except Exception as e: return HttpResponse("Error: " + e, status=502)
def get_author(self, username, page=1): author = api.get_user_by_username(username) if not author: return None articles, metadata = api.get_articles( tags=self.tag_ids, tags_exclude=self.excluded_tags, page=page, author=author["id"], ) context = get_index_context(page, articles, metadata.get("total_pages")) context["title"] = self.blog_title context["author"] = author context["total_posts"] = metadata.get("total_posts", 0) return context