Skip to content

Commit

Permalink
add blog templates
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Aug 13, 2024
1 parent eea4c24 commit b0881d7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/pallets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ def __init__(self, **kwargs: t.Any) -> None:
class BlogPost(BasePage):
content_prefix = "blog"
__tablename__ = "blog_post"
title: orm.Mapped[str]
content: orm.Mapped[str]
author_path: orm.Mapped[str | None] = orm.mapped_column(sa.ForeignKey(Person.path))
author: orm.Mapped[Person | None] = orm.relationship()
author_name: orm.Mapped[str]
published: orm.Mapped[datetime]
updated: orm.Mapped[datetime]
updated: orm.Mapped[datetime | None]
tags: orm.Mapped[list[str]] = orm.mapped_column(sa.JSON, default=list)

def __init__(self, **kwargs: t.Any) -> None:
kwargs["author_path"] = kwargs.pop("author")
path = kwargs["path"].partition("/")[2]
published = kwargs["published"]
kwargs["path"] = f"{published:%Y/%m}/{path}"
Expand Down
10 changes: 10 additions & 0 deletions src/pallets/templates/blog/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "layout.html" %}

{% block page %}
{% for post in posts %}
<article>
<h3><a href="{{ url_for(".blog_post", path=post.path) }}">{{ post.title }}</a></h3>
Posted by {{ post.author_name }} on {{ post.published.strftime("%Y-%m-%d") }}
</article>
{% endfor %}
{% endblock %}
8 changes: 8 additions & 0 deletions src/pallets/templates/blog/post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends "layout.html" %}

{% block page %}
<h1>{{ page.title }}</h1>
Posted by {{ page.author_name }} on {{ page.published.strftime("%Y-%m-%d") }}
<hr>
{{ page.content_html | safe }}
{% endblock %}
1 change: 1 addition & 0 deletions src/pallets/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<ul>
<li><a href="/donate">Donate</a></li>
<li><a href="/ecosystem">Ecosystem</a></li>
<li><a href="/blog/">Blog</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
Expand Down
11 changes: 10 additions & 1 deletion src/pallets/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path

import sqlalchemy as sa
from flask import Blueprint
from flask import current_app
from flask import redirect
Expand Down Expand Up @@ -73,11 +74,19 @@ def project_redirect(path: str) -> Response:
return redirect(url_for(".project", path=path))


@bp.route("/blog/")
def blog_index() -> str:
posts = db.session.scalars(
sa.select(models.BlogPost).order_by(models.BlogPost.published.desc())
)
return render_template("blog/index.html", posts=posts)


@bp.route("/blog/<path:path>")
def blog_post(path: str) -> str:
obj = db.session.get(models.BlogPost, path)

if obj is None:
raise NotFound()

return render_template("blog.html", page=obj)
return render_template("blog/post.html", page=obj)

0 comments on commit b0881d7

Please sign in to comment.