Skip to main content

Flask extension and Django App to add a nice blog to your website

Project description

Canonical blog extension

This extension allows you to add a simple frontend section to your flask app. All the articles are pulled from Canonical's Wordpress back-end through the JSON API.

This extension provides a blueprint with 3 routes:

  • "/": that returns the list of articles
  • "/": the article page
  • "/feed": provides a RSS feed for the page.

How to install

To install this extension as a requirement in your project, you can use PIP;

pip install canonicalwebteam.blog

See also the documentation for (pip install)[https://pip.pypa.io/en/stable/reference/pip_install/].

How to use

Templates

The module expects HTML templates at blog/index.html, blog/article.html, blog/blog-card.html, blog/archives.html, blog/upcoming.html and blog/author.html.

An example of these templates can be found at https://github.com/canonical-websites/jp.ubuntu.com/tree/master/templates/blog.

Flask

In your app you can then:

    import flask
    from canonicalwebteam.blog import BlogViews
    from canonicalwebteam.blog.flask import build_blueprint

    app = flask.Flask(__name__)

    # ...

    blog_views = BlogViews()
    app.register_blueprint(build_blueprint(blog_views), url_prefix="/blog")

You can customise the blog through the following optional arguments:

    blog_views = BlogViews(
        blog_title="Blog",
        tag_ids=[1, 12, 112],
        exclude_tags=[26, 34],
        feed_description="The Ubuntu Blog Feed",
    )
    app.register_blueprint(build_blueprint(blog_views), url_prefix="/blog")

BlogExtension() accepts five parameters;

  • (obj) app
  • (str) "The title of your blog"
  • (arr) Array of tags from blog entries on which to filter e.g ['1234', '4321']
  • (str) Language string e.g. "lang:en"
  • (str) "/path/to/display/your/blog"

Django

  • Add the blog module as a dependency to your Django project
  • Load it at the desired path (e.g. "/blog") in the urls.py file
from django.urls import path, include
urlpatterns = [path("blog/", include("canonicalwebteam.blog.django.urls"))]
  • In your Django project settings (settings.py) you have to specify the following parameters:
BLOG_CONFIG = {
    # the id for tags that should be fetched for this blog
    "TAGS_ID": [3184],
    # the title of the blog
    "BLOG_TITLE": "TITLE OF THE BLOG",
    # the tag name for generating a feed
    "TAG_NAME": "TAG NAME FOR GENERATING A FEED",
}
  • Run your project and verify that the blog is displaying at the path you specified (e.g. '/blog')

Groups pages

  • Group pages are optional and can be enabled by using the view canonicalwebteam.blog.django.views.group. The view takes the group slug to fetch data for and a template path to load the correct template from.
  • Group pages can be filtered by category, by adding a category=CATEGORY_NAME query parameter to the URL (e.g. http://localhost:8080/blog/cloud-and-server?category=articles).
from canonicalwebteam.blog.django.views import group

urlpatterns = [
    url(r"blog", include("canonicalwebteam.blog.django.urls")),
    url(
        r"blog/cloud-and-server",
        group,
        {
            "slug": "cloud-and-server",
            "template_path": "blog/cloud-and-server.html"
        }
    )

Topic pages

  • Topic pages are optional as well and can be enabled by using the view canonicalwebteam.blog.django.views.topic. The view takes the topic slug to fetch data for and a template path to load the correct template from.

urls.py

path(
		r"blog/topics/kubernetes",
		topic,
		{"slug": "kubernetes", "template_path": "blog/kubernetes.html"},
		name="topic",
),

Development

The blog extension leverages poetry for dependency management.

Regenerate setup.py

poetry install
poetry run poetry-setup

Testing

All tests can be run with poetry run pytest.

Regenerating Fixtures

All API calls are caught with VCR and saved as fixtures in the fixtures directory. If the API updates, all fixtures can easily be updated by just removing the fixtures directory and rerunning the tests.

To do this run rm -rf fixtures && poetry run pytest.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

canonicalwebteam.blog-3.1.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

canonicalwebteam.blog-3.1.1-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file canonicalwebteam.blog-3.1.1.tar.gz.

File metadata

  • Download URL: canonicalwebteam.blog-3.1.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.6.8 Linux/5.0.0-25-generic

File hashes

Hashes for canonicalwebteam.blog-3.1.1.tar.gz
Algorithm Hash digest
SHA256 c632b6fb2958046f94e406929cf23a4b72b1ac2af88ba55e44168ea27c595a3e
MD5 b67d7779848ce6d5efb8a3c331508928
BLAKE2b-256 c2ac175d75a2bb6eb8c1668e24c9d6f48d84f86898b965c8ddc373a2806ae178

See more details on using hashes here.

File details

Details for the file canonicalwebteam.blog-3.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for canonicalwebteam.blog-3.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4b587d57a71c323002144351e94347c26129060d56a1bc1d28d4cb64555435f5
MD5 8bfcce3a28a9879126a1aa154ddd9247
BLAKE2b-256 4a9e03e107d1a836423aff7bb8bfd70158ac42d114f1bdd2239a53575eee6767

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page