Beispiel #1
0
    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 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
Beispiel #3
0
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)
Beispiel #4
0
def archives(request, template_path="blog/archives.html"):
    try:
        page = request.GET.get("page", default="1")
        category = request.GET.get("category", default="")
        group = request.GET.get("group", default="")
        month = request.GET.get("month", default="")
        year = request.GET.get("year", default="")

        groups = []
        categories = []

        if group:
            group = api.get_group_by_slug(group)
            groups.append(group["id"])

        if category:
            category = api.get_category_by_slug(category)
            categories.append(category["id"])

        after = ""
        before = ""
        if year:
            year = int(year)
            if month:
                month = int(month)
                after = datetime(year=year, month=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_with_metadata(
            tags=tag_ids,
            tags_exclude=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"] = blog_title
        context["total_posts"] = total_posts

        return render(request, template_path, context)

    except Exception as e:
        return HttpResponse("Error: " + e, status=502)
    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
Beispiel #6
0
def upcoming(request):
    try:
        page_param = request.GET.get("page", default="1")

        events = api.get_category_by_slug("events")
        webinars = api.get_category_by_slug("webinars")

        articles, total_pages = api.get_articles(
            tags=tag_ids,
            tags_exclude=excluded_tags,
            page=page_param,
            categories=[events["id"], webinars["id"]],
        )

    except Exception as e:
        return HttpResponse("Error: " + e, status=502)

    context = get_index_context(page_param, articles, total_pages)
    context["title"] = blog_title

    return render(request, "blog/upcoming.html", context)
    def get_group(self, group_slug, page=1, category_slug=""):
        group = api.get_group_by_slug(group_slug)

        category = {}
        if category_slug:
            category = api.get_category_by_slug(category_slug)

        articles, metadata = api.get_articles(
            tags=self.tag_ids,
            tags_exclude=self.excluded_tags,
            page=page,
            groups=[group.get("id", "")],
            categories=[category.get("id", "")],
        )
        total_pages = metadata["total_pages"]

        context = get_group_page_context(page, articles, total_pages, group)
        context["title"] = self.blog_title
        context["category"] = {"slug": category_slug}

        return context
 def test_get_category_by_slug(self, get):
     slug = "articles"
     _ = api.get_category_by_slug(slug)
     get.assert_called_once_with("https://admin.insights.ubuntu.com/"
                                 "wp-json/wp/v2/categories?slug=articles"
                                 "&_embed=true")