Пример #1
0
    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 test_building_index_context_with_params_as_strings(
         self, get_media, get_user, get_category_by_id, get_group_by_id):
     get_media.return_value = "test_image"
     get_user.return_value = "test_author"
     get_category_by_id.return_value = "test_category"
     get_group_by_id.return_value = "test_group"
     articles = [
         {
             "featured_media": "test",
             "author": "test",
             "categories": [1, 2],
             "group": [1],
             "tags": ["test"],
         },
         {
             "featured_media": "test2",
             "author": "test2",
             "categories": [2, 3],
             "group": [1],
             "tags": ["test2"],
         },
     ]
     context = get_index_context("1", articles, "2")
     expected_context = {
         "current_page":
         1,
         "total_pages":
         2,
         "featured_articles": [],
         "articles": [
             {
                 "author": "test_author",
                 "categories": [1, 2],
                 "display_category": "test_category",
                 "featured_media": "test",
                 "group": "test_group",
                 "image": "test_image",
                 "tags": ["test"],
             },
             {
                 "author": "test_author",
                 "categories": [2, 3],
                 "display_category": "test_category",
                 "featured_media": "test2",
                 "group": "test_group",
                 "image": "test_image",
                 "tags": ["test2"],
             },
         ],
         "groups": {
             1: "test_group"
         },
         "used_categories": {
             1: "test_category",
             2: "test_category",
             3: "test_category",
         },
     }
     self.maxDiff = None
     self.assertEqual(context, expected_context)
Пример #3
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)
Пример #4
0
    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)
Пример #5
0
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_building_index_context_without_api(self):
     articles = [
         {
             "featured_media": "test",
             "author": "test",
             "categories": [1, 2],
             "group": [1],
             "tags": ["test"],
         },
         {
             "featured_media": "test2",
             "author": "test2",
             "categories": [2, 3],
             "group": [1],
             "tags": ["test2"],
         },
     ]
     context = get_index_context(1, articles, 2)
     expected_context = {
         "current_page": 1,
         "total_pages": 2,
         "articles": [
             {
                 "author": None,
                 "categories": [1, 2],
                 "featured_media": "test",
                 "group": 1,
                 "image": None,
                 "tags": ["test"],
             },
             {
                 "author": None,
                 "categories": [2, 3],
                 "featured_media": "test2",
                 "group": 1,
                 "image": None,
                 "tags": ["test2"],
             },
         ],
         "groups": {1: None},
         "used_categories": {1: None, 2: None, 3: None},
     }
     self.assertEqual(context, expected_context)
Пример #7
0
    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"])
Пример #8
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)
Пример #9
0
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)