Esempio n. 1
0
        "/thank-you"
    ),
    view_func=download_thank_you,
)
app.add_url_rule("/getubuntu/releasenotes", view_func=releasenotes_redirect)
app.add_url_rule(
    "/search", "search", build_search_view(template_path="search.html")
)
app.add_url_rule(
    "/appliance/<regex('.+'):app>/<regex('.+'):device>",
    view_func=appliance_install,
)
# blog section

blog_views = BlogViews(
    api=Wordpress(session=session),
    excluded_tags=[3184, 3265, 3408],
    per_page=11,
)
app.add_url_rule(
    "/blog/topics/<regex('maas|design|juju|robotics|snapcraft'):slug>",
    view_func=BlogCustomTopic.as_view("blog_topic", blog_views=blog_views),
)
app.add_url_rule(
    "/blog/<regex('cloud-and-server|desktop|internet-of-things'):slug>",
    view_func=BlogCustomGroup.as_view("blog_group", blog_views=blog_views),
)
app.add_url_rule(
    "/blog/press-centre",
    view_func=BlogPressCentre.as_view("press_centre", blog_views=blog_views),
)
 def setUp(self):
     self.api = Wordpress(session=requests.Session())
     return super().setUp()
Esempio n. 3
0
from canonicalwebteam.flask_base.app import FlaskBase

from webapp.blueprint import jp_website
import webapp.template_utils as template_utils

session = talisker.requests.get_session()
app = FlaskBase(
    __name__,
    "jp.ubuntu.com",
    template_folder="../templates",
    static_folder="../static",
)

app.register_blueprint(jp_website)
blog_views = BlogViews(
    api=Wordpress(session=session), blog_title="Ubuntu blog", tag_ids=[3184]
)
app.register_blueprint(build_blueprint(blog_views), url_prefix="/blog")

# read releases.yaml
with open("releases.yaml") as releases:
    releases = yaml.load(releases, Loader=yaml.FullLoader)


# Template context
@app.context_processor
def context():
    return {
        "format_date": template_utils.format_date,
        "get_json_feed": template_utils.get_json_feed_content,
        "replace_admin": template_utils.replace_admin,
class TestWordpress(VCRTestCase):
    def setUp(self):
        self.api = Wordpress(session=requests.Session())
        return super().setUp()

    def test_get_articles(self):
        articles, metadata = self.api.get_articles()
        # I assume these numbers are only gonna grow
        self.assertTrue(int(metadata["total_pages"]) >= 258)
        self.assertTrue(int(metadata["total_posts"]) >= 3095)

        for article in articles:
            self.assertTrue("rendered" in article["content"])

        no_article = self.api.get_article(slug="nonexistent-slug")
        self.assertEqual(no_article, {})

        article = self.api.get_article(slug="testing-your-user-contract")
        self.assertEqual(
            article["title"]["rendered"], "Testing your user contract"
        )
        self.assertIn("<p>", article["content"]["rendered"])

    def test_get_users(self):
        user = self.api.get_user_by_id(id=217)
        self.assertEqual(user["name"], "Canonical")
        self.assertEqual(user["slug"], "canonical")

        user = self.api.get_user_by_username(username="******")
        self.assertEqual(user["name"], "Canonical")
        self.assertEqual(user["id"], 217)

    def test_get_media(self):
        media = self.api.get_media(id=89203)
        self.assertIn("link", media)

    def test_get_tags(self):
        tag = self.api.get_tag_by_slug(slug="design")
        self.assertEqual(tag["name"], "Design")
        self.assertEqual(tag["id"], 1239)
        self.assertTrue(tag["count"] >= 542)

        tag = self.api.get_tag_by_id(id=1239)
        self.assertEqual(tag["name"], "Design")
        self.assertEqual(tag["slug"], "design")
        self.assertTrue(tag["count"] >= 542)

        tag = self.api.get_tag_by_name(name="Design")
        # Note: This highlights a flaw in this method,
        # it's really just a search.
        # My hunch would be to remove it, but snapcraft needs it
        self.assertEqual(tag["name"], "app design")

    def test_get_categories(self):
        category = self.api.get_category_by_id(id=1453)
        self.assertEqual(category["name"], "Articles")
        self.assertEqual(category["slug"], "articles")

        category = self.api.get_category_by_slug(slug="articles")
        self.assertEqual(category["name"], "Articles")
        self.assertEqual(category["id"], 1453)

        categories = self.api.get_categories()
        self.assertEqual(len(categories), 45)
        for category in categories:
            self.assertIn("count", category)
            self.assertIn("name", category)

    def test_get_groups(self):
        group = self.api.get_group_by_id(id=3367)
        self.assertEqual(group["name"], "AI")
        self.assertEqual(group["slug"], "ai")

        group = self.api.get_group_by_slug(slug="ai")
        self.assertEqual(group["name"], "AI")
        self.assertEqual(group["id"], 3367)
Esempio n. 5
0
def init_blog(app, url_prefix):
    wordpress_api = Wordpress(session=talisker.requests.get_session())
    blog = build_blueprint(
        BlogViews(api=wordpress_api,
                  blog_title="Snapcraft Blog",
                  tag_ids=[2996]))

    @blog.route("/api/snap-posts/<snap>")
    def snap_posts(snap):
        try:
            blog_tags = wordpress_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 = 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:
                transformed_article = helpers.transform_article(article)

                if transformed_article["image"]:
                    featured_media = image_template(
                        url=transformed_article["image"]["source_url"],
                        alt="",
                        width="346",
                        height="231",
                        fill=True,
                        hi_def=True,
                        loading="auto",
                    )
                else:
                    featured_media = 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":
                    featured_media,
                })

        return flask.jsonify(articles)

    @blog.route("/api/series/<series>")
    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 = helpers.transform_article(article)
            articles.append({
                "slug": transformed_article["slug"],
                "title": transformed_article["title"]["rendered"],
            })

        return flask.jsonify(articles)

    @blog.context_processor
    def add_newsletter():
        newsletter_subscribed = flask.request.args.get("newsletter",
                                                       default=False,
                                                       type=bool)

        return {"newsletter_subscribed": newsletter_subscribed}

    app.register_blueprint(blog, url_prefix=url_prefix)