Skip to content

Commit

Permalink
Merge pull request #14 from stuartmaxwell/dev
Browse files Browse the repository at this point in the history
Pages and Template Hierarchy
  • Loading branch information
stuartmaxwell authored Jun 15, 2024
2 parents 5394352 + 93aadee commit 50599ce
Show file tree
Hide file tree
Showing 17 changed files with 1,119 additions and 203 deletions.
17 changes: 17 additions & 0 deletions djpress/migrations/0003_post_menu_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.6 on 2024-06-12 11:43

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("djpress", "0002_rename_content_type_post_post_type"),
]

operations = [
migrations.AddField(
model_name="post",
name="menu_order",
field=models.IntegerField(default=0),
),
]
83 changes: 70 additions & 13 deletions djpress/models/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,80 @@
PUBLISHED_POSTS_CACHE_KEY = "published_posts"


class PagesManager(models.Manager):
"""Page custom manager."""

def get_queryset(self: "PagesManager") -> models.QuerySet:
"""Return the queryset for pages."""
return super().get_queryset().filter(post_type="page").order_by("-date")

def get_published_pages(self: "PagesManager") -> models.QuerySet:
"""Return all published pages.
For a page to be considered published, it must meet the following requirements:
- The status must be "published".
- The date must be less than or equal to the current date/time.
"""
return self.get_queryset().filter(
status="published",
date__lte=timezone.now(),
)

def get_published_page_by_slug(
self: "PagesManager",
slug: str,
) -> "Post":
"""Return a single published page.
Args:
slug (str): The slug of the page.
Returns:
Post: The published page.
"""
try:
page: Post = self.get_published_pages().get(slug=slug)
except Post.DoesNotExist as exc:
msg = "Page not found"
raise ValueError(msg) from exc

return page

def get_published_page_by_path(
self: "PagesManager",
path: str,
) -> "Post":
"""Return a single published post from a path.
For now, we'll only allow a top level path.
"""
# Check for a single item in the path
if path.count("/") > 0:
msg = "Invalid path"
raise ValueError(msg)

return self.get_published_page_by_slug(path)


class PostsManager(models.Manager):
"""Post custom manager."""

def get_queryset(self: "PostsManager") -> models.QuerySet:
"""Return the queryset for published posts."""
"""Return the queryset for posts."""
return super().get_queryset().filter(post_type="post").order_by("-date")

def get_published_posts(self: "PostsManager") -> models.QuerySet:
"""Returns all published posts.
For a post to be considered published, it must meet the following requirements:
- The status must be "published".
- The date must be less than or equal to the current date/time.
"""
return self.get_queryset().filter(
status="published",
date__lte=timezone.now(),
)

def get_recent_published_posts(self: "PostsManager") -> models.QuerySet:
"""Return recent published posts.
Expand All @@ -41,18 +108,6 @@ def get_recent_published_posts(self: "PostsManager") -> models.QuerySet:
: settings.RECENT_PUBLISHED_POSTS_COUNT
]

def get_published_posts(self: "PostsManager") -> models.QuerySet:
"""Returns all published posts.
For a post to be considered published, it must meet the following requirements:
- The status must be "published".
- The date must be less than or equal to the current date/time.
"""
return self.get_queryset().filter(
status="published",
date__lte=timezone.now(),
)

def _get_cached_recent_published_posts(self: "PostsManager") -> models.QuerySet:
"""Return the cached recent published posts queryset.
Expand Down Expand Up @@ -221,10 +276,12 @@ class Post(models.Model):
default="post",
)
categories = models.ManyToManyField(Category, blank=True)
menu_order = models.IntegerField(default=0)

# Managers
objects = models.Manager()
post_objects: "PostsManager" = PostsManager()
page_objects: "PagesManager" = PagesManager()

class Meta:
"""Meta options for the Post model."""
Expand Down
2 changes: 1 addition & 1 deletion djpress/static/djpress/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ body > header ul li a:hover {
main {
padding: 2em;
max-width: 992px;
margin: 0 auto;
margin: 0 auto 3em;
}

main > h1 {
Expand Down
2 changes: 1 addition & 1 deletion djpress/static/djpress/css/style.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions djpress/templates/djpress/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% load static %}
{% load djpress_tags %}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% blog_page_title post_text="| " %}{% blog_title %}</title>
<link rel="stylesheet" href="{% static 'djpress/css/style.css' %}">
</head>
<body>
<header>
<h1>{% blog_title_link %}</h1>
{% blog_pages %}
</header>

<main>

{% block main %}{% endblock main %}

</main>

<footer>
<p>Powered by
<a href="https://www.djangoproject.com/" title="The web framework for perfectionists with deadlines">Django</a>
and
<a href="#">DJPress</a>
</p>
</footer>
</body>
</html>
105 changes: 47 additions & 58 deletions djpress/templates/djpress/index.html
Original file line number Diff line number Diff line change
@@ -1,60 +1,49 @@
{% load static %}
{% extends "djpress/base.html" %}
{% load djpress_tags %}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% blog_title %}</title>
<link rel="stylesheet" href="{% static 'djpress/css/style.css' %}">
</head>
<body>
<header>
<h1>{% blog_title_link %}</h1>
{% blog_categories %}
</header>

<main>

{% have_posts as posts %}

{% category_name "h1" pre_text="View Posts in the " post_text=" Category" %}
{% author_name "h1" pre_text="View Posts by " %}

{% for post in posts %}

<article>
<header>
<h1>{% post_title_link %}</h1>
<p>By {% post_author_link %}. {% post_date_link %}</p>
</header>
<section>
{% post_content %}
</section>
<footer>
<p>Categories: {% post_categories_link "span" "badge" %}</p>
</footer>
</article>

{% empty %}

<p>No posts available.</p>

{% endfor %}

{% posts_nav_links %}

<br><br><br>

</main>

<footer>
<p>Powered by
<a href="https://www.djangoproject.com/" title="The web framework for perfectionists with deadlines">Django</a>
and
<a href="#">DJPress</a>
</p>
</footer>
</body>
</html>
{% block main %}

{% category_name "h1" pre_text="View Posts in the " post_text=" Category" %}
{% author_name "h1" pre_text="View Posts by " %}

{% if post %}

<article>
<header>
<h1>{% post_title_link %}</h1>
<p>By {% post_author_link %}. {% post_date_link %}</p>
</header>
<section>
{% post_content %}
</section>
<footer>
<p>Categories: {% post_categories_link "span" "badge" %}</p>
</footer>
</article>

{% else %}

{% for post in posts %}

<article>
<header>
<h1>{% post_title_link %}</h1>
<p>By {% post_author_link %}. {% post_date_link %}</p>
</header>
<section>
{% post_content %}
</section>
</article>

{% empty %}

<p>No posts available.</p>

{% endfor %}

{% endif %}


{% posts_nav_links %}

{% endblock main %}
21 changes: 21 additions & 0 deletions djpress/templates/djpress/single.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "djpress/base.html" %}
{% load djpress_tags %}

{% block main %}

<article>
<header>
<h1>{% post_title_link %}</h1>
<p>By {% post_author_link %}. {% post_date_link %}</p>
</header>
<section>
{% post_content %}
</section>
<footer>
<p>Categories: {% post_categories_link "span" "badge" %}</p>
</footer>
</article>

{% posts_nav_links %}

{% endblock main %}
Loading

0 comments on commit 50599ce

Please sign in to comment.