コード例 #1
0
    def test_hi_def(self):
        markup = image_template(
            url=non_asset_url,
            alt="test",
            width="1920",
            height="1080",
            hi_def=True,
        )
        markup_asset = image_template(
            url=asset_url, alt="test", width="1920", height="1080", hi_def=True
        )

        # Check the markup includes srcset
        self.assertTrue(markup.find("srcset="))
        self.assertTrue(markup.find("data-srcset"))
        # Check x2 is present
        self.assertTrue(markup.find("x2"))
        # Check width and height are double
        self.assertTrue(markup.find("3840"))
        self.assertTrue(markup.find("2160"))

        self.assertTrue(markup_asset.find("srcset="))
        self.assertTrue(markup_asset.find("data-srcset"))
        self.assertTrue(markup_asset.find("x2"))
        self.assertTrue(markup_asset.find("w%3D3840%26h%3D2160"))
コード例 #2
0
 def test_returns_string(self):
     markup = image_template(
         url=asset_url,
         alt="test",
         width="1920",
         height="1080",
         hi_def=False,
     )
     self.assertTrue(isinstance(markup, str))
コード例 #3
0
    def test_height_is_optional(self):
        image = image_template(
            url=non_asset_url,
            alt="test",
            width="1920",
            hi_def=True,
        )

        self.assertNotIn("height=", image)
        self.assertNotIn("h_auto", image)
コード例 #4
0
 def test_classes(self):
     markup = image_template(
         url=asset_url,
         alt="test",
         width="1920",
         height="1080",
         hi_def=False,
         attrs={"class": "test-title"},
     )
     # Check custom class exists
     self.assertIn('class="test-title"', markup)
コード例 #5
0
 def test_attributes(self):
     markup = image_template(
         url=asset_url,
         alt="test",
         width="1920",
         height="1080",
         hi_def=False,
         attrs={"id": "test", "title": "test title"},
     )
     self.assertIn('id="test"', markup)
     self.assertIn('title="test title"', markup)
コード例 #6
0
 def test_optional_fill(self):
     markup = image_template(
         url=asset_url,
         alt="test",
         width="1920",
         height="1080",
         loading="auto",
         fill=True,
         hi_def=False,
     )
     # Check c_fill is present
     self.assertIn("c_fill", markup)
コード例 #7
0
 def test_optional_lazy(self):
     markup = image_template(
         url=asset_url,
         alt="test",
         width="1920",
         height="1080",
         loading="auto",
         attrs={"class": "test-title"},
         hi_def=False,
     )
     # Check lazyload class is not present
     self.assertIn('class="test-title"', markup)
コード例 #8
0
 def test_e_sharpen(self):
     markup = image_template(
         url=asset_url,
         alt="test",
         width="1920",
         height="1080",
         loading="auto",
         fill=True,
         e_sharpen=True,
         hi_def=False,
     )
     # Check e_sharpen is present
     self.assertIn("e_sharpen", markup)
コード例 #9
0
    def snap_posts(snap):
        try:
            blog_tags = blog_api.get_tag_by_slug(f"sc:snap:{snap}")
        except NotFoundError:
            blog_tags = None

        blog_articles = None
        articles = []

        if blog_tags:
            snapcraft_tag = blog_api.get_tag_by_slug("snapcraft.io")

            try:
                blog_articles, total_pages = blog_api.get_articles(
                    tags=blog_tags["id"],
                    tags_exclude=[3184, 3265, 3408],
                    per_page=3 - len(articles),
                )
            except RequestException:
                blog_articles = []

            for article in blog_articles:
                if article["image"]:
                    featured_media = image_template(
                        url=article["image"]["source_url"],
                        alt="",
                        width="346",
                        height="231",
                        fill=True,
                        hi_def=True,
                        loading="auto",
                    )
                else:
                    featured_media = None

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

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

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

        return flask.jsonify(articles)
コード例 #10
0
    def _apply_image_template(self,
                              content,
                              width,
                              height=None,
                              use_e_sharpen=False):
        """Apply image template to the img tags

        :param content: String to replace url
        :param width: Default width of the image
        :param height: Default height of the image

        :returns: HTML images templated
        """

        soup = BeautifulSoup(content, "html.parser")
        for image in soup.findAll("img"):
            if not image.get("src") or "http" not in image.get("src"):
                continue

            img_width = (image.get("width") if image.get("width") is not None
                         and image.get("width").isdigit() else None)

            img_height = (image.get("height")
                          if image.get("height") is not None
                          and image.get("height").isdigit() else None)

            new_image = BeautifulSoup(
                image_template(
                    url=image.get("src"),
                    alt="",
                    width=img_width or width,
                    height=img_height or height,
                    hi_def=True,
                    fill=True,
                    e_sharpen=use_e_sharpen,
                    loading="lazy",
                ),
                "html.parser",
            )
            image.replace_with(new_image)

        return str(soup)
コード例 #11
0
ファイル: views.py プロジェクト: claude743/congenial-tribble
    def snap_posts(snap):
        try:
            blog_tags = blog_api.get_tag_by_name(f"sc:snap:{snap}")
        except NotFoundError:
            blog_tags = None

        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 = blog_api.get_tag_by_name("snapcraft.io")

            try:
                blog_articles, total_pages = blog_api.get_articles(
                    tags=blog_tags["id"],
                    tags_exclude=[3184, 3265, 3408],
                    per_page=3 - len(articles),
                )
            except RequestException:
                blog_articles = []

            for article in blog_articles:
                if article["image"]:
                    featured_media = image_template(
                        url=article["image"]["source_url"],
                        alt="",
                        width="346",
                        height="231",
                        fill=True,
                        hi_def=True,
                        loading="auto",
                    )
                else:
                    featured_media = None

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

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

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

        return flask.jsonify(articles)
コード例 #12
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 = []

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

        if 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:
            try:
                blog_articles, total_pages = wordpress_api.get_articles(
                    blog_tags["id"], 3 - len(articles))
            except Exception:
                blog_articles = []

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

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

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

        return flask.jsonify(articles)