Skip to content

Commit

Permalink
Merge pull request #25 from stuartmaxwell/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmaxwell authored May 1, 2024
2 parents fab374f + 279acae commit c2cd7ae
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 93 deletions.
4 changes: 2 additions & 2 deletions djpress/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib import admin

# Register the models here.
from djpress.models import Category, Content
from djpress.models import Category, Post

admin.site.register(Category)
admin.site.register(Content)
admin.site.register(Post)
14 changes: 7 additions & 7 deletions djpress/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@
from django.contrib.syndication.views import Feed
from django.urls import reverse

from djpress.models import Content
from djpress.models import Post

if TYPE_CHECKING:
from django.db import models


class ContentFeed(Feed):
class PostFeed(Feed):
"""RSS feed for blog posts."""

title = "stuartm.nz"
link = "/rss"
description = "stuartm.nz updates"

def items(self: "ContentFeed") -> "models.QuerySet":
def items(self: "PostFeed") -> "models.QuerySet":
"""Return the most recent posts."""
return Content.post_objects.get_recent_published_content()
return Post.post_objects.get_recent_published_content()

def item_title(self: "ContentFeed", item: Content) -> str:
def item_title(self: "PostFeed", item: Post) -> str:
"""Return the title of the post."""
return item.title

def item_description(self: "ContentFeed", item: Content) -> str:
def item_description(self: "PostFeed", item: Post) -> str:
"""Return the description of the post."""
description = item.truncated_content_markdown
if item.is_truncated:
description += f'<p><a href="{self.item_link(item)}">Read more</a></p>'
return description

def item_link(self: "ContentFeed", item: Content) -> str:
def item_link(self: "PostFeed", item: Post) -> str:
"""Return the link to the post."""
return reverse("djpress:content_detail", args=[item.slug])
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 5.0.4 on 2024-05-01 10:32

from django.conf import settings
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('djpress', '0007_alter_content_managers'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.RenameModel(
old_name='Content',
new_name='Post',
),
migrations.AlterModelOptions(
name='category',
options={'verbose_name': 'category', 'verbose_name_plural': 'catregories'},
),
migrations.AlterModelOptions(
name='post',
options={'verbose_name': 'content', 'verbose_name_plural': 'contents'},
),
migrations.AlterModelManagers(
name='post',
managers=[
],
),
]
2 changes: 1 addition & 1 deletion djpress/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Models package for djpress app."""

from djpress.models.category import Category # noqa: F401
from djpress.models.content import Content # noqa: F401
from djpress.models.post import Post # noqa: F401
24 changes: 12 additions & 12 deletions djpress/models/content.py → djpress/models/post.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Content model."""
"""Post model."""

import logging
from typing import ClassVar
Expand Down Expand Up @@ -26,7 +26,7 @@


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

def get_queryset(self: "PostsManager") -> models.QuerySet:
"""Return the queryset for published posts."""
Expand Down Expand Up @@ -102,7 +102,7 @@ def _get_cached_recent_published_content(self: "PostsManager") -> models.QuerySe
def get_published_post_by_slug(
self: "PostsManager",
slug: str,
) -> "Content":
) -> "Post":
"""Return a single published post.
Must have a date less than or equal to the current date/time based on its slug.
Expand All @@ -117,7 +117,7 @@ def get_published_post_by_slug(
if not post:
try:
post = self._get_published_content().get(slug=slug)
except Content.DoesNotExist as exc:
except Post.DoesNotExist as exc:
msg = "Post not found"
raise ValueError(msg) from exc

Expand All @@ -139,8 +139,8 @@ def get_published_content_by_category(
)


class Content(models.Model):
"""Content model."""
class Post(models.Model):
"""Post model."""

STATUS_CHOICES: ClassVar = [("draft", "Draft"), ("published", "Published")]
CONTENT_TYPE_CHOICES: ClassVar = [("post", "Post"), ("page", "Page")]
Expand Down Expand Up @@ -169,11 +169,11 @@ class Meta:
verbose_name = "content"
verbose_name_plural = "contents"

def __str__(self: "Content") -> str:
def __str__(self: "Post") -> str:
"""Return the string representation of the content."""
return self.title

def save(self: "Content", *args, **kwargs) -> None: # noqa: ANN002, ANN003
def save(self: "Post", *args, **kwargs) -> None: # noqa: ANN002, ANN003
"""Override the save method to auto-generate the slug."""
if not self.slug:
self.slug = slugify(self.title)
Expand All @@ -182,7 +182,7 @@ def save(self: "Content", *args, **kwargs) -> None: # noqa: ANN002, ANN003
raise ValueError(msg)
super().save(*args, **kwargs)

def render_markdown(self: "Content", markdown_text: str) -> str:
def render_markdown(self: "Post", markdown_text: str) -> str:
"""Return the markdown text as HTML."""
html = md.convert(markdown_text)
logger.debug(f"Converted markdown to HTML: {html=}")
Expand All @@ -191,12 +191,12 @@ def render_markdown(self: "Content", markdown_text: str) -> str:
return html

@property
def content_markdown(self: "Content") -> str:
def content_markdown(self: "Post") -> str:
"""Return the content as HTML converted from Markdown."""
return self.render_markdown(self.content)

@property
def truncated_content_markdown(self: "Content") -> str:
def truncated_content_markdown(self: "Post") -> str:
"""Return the truncated content as HTML converted from Markdown."""
read_more_index = self.content.find(TRUNCATE_TAG)
if read_more_index != -1:
Expand All @@ -206,6 +206,6 @@ def truncated_content_markdown(self: "Content") -> str:
return self.render_markdown(truncated_content)

@property
def is_truncated(self: "Content") -> bool:
def is_truncated(self: "Post") -> bool:
"""Return whether the content is truncated."""
return TRUNCATE_TAG in self.content
8 changes: 4 additions & 4 deletions djpress/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
CATEGORY_CACHE_KEY,
Category,
)
from djpress.models.content import (
from djpress.models.post import (
PUBLISHED_CONTENT_CACHE_KEY,
Content,
Post,
)


Expand All @@ -21,8 +21,8 @@ def invalidate_category_cache(**kwargs) -> None: # noqa: ARG001, ANN003
cache.delete(CATEGORY_CACHE_KEY)


@receiver(post_save, sender=Content)
@receiver(post_delete, sender=Content)
@receiver(post_save, sender=Post)
@receiver(post_delete, sender=Post)
def invalidate_published_content_cache(**kwargs) -> None: # noqa: ARG001, ANN003
"""Invalidate the published posts cache."""
cache.delete(PUBLISHED_CONTENT_CACHE_KEY)
8 changes: 4 additions & 4 deletions djpress/templatetags/djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db import models

from config.settings import BLOG_TITLE
from djpress.models import Category, Content
from djpress.models import Category, Post

register = template.Library()

Expand All @@ -18,13 +18,13 @@ def get_categories() -> models.QuerySet[Category] | None:
@register.simple_tag
def get_recent_published_content() -> models.QuerySet[Category] | None:
"""Return recent published posts from the cache."""
return Content.post_objects.get_recent_published_content()
return Post.post_objects.get_recent_published_content()


@register.simple_tag
def get_single_published_content(slug: str) -> Content | None:
def get_single_published_content(slug: str) -> Post | None:
"""Return a single published post by slug."""
return Content.post_objects.get_published_post_by_slug(slug)
return Post.post_objects.get_published_post_by_slug(slug)


@register.simple_tag
Expand Down
10 changes: 5 additions & 5 deletions djpress/tests/test_feeds.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import pytest
from django.urls import reverse
from django.contrib.auth.models import User
from djpress.models import Content
from djpress.feeds import ContentFeed
from djpress.models import Post
from djpress.feeds import PostFeed


@pytest.mark.django_db
def test_latest_posts_feed(client):
user = User.objects.create_user(username="testuser", password="testpass")
Content.post_objects.create(
Post.post_objects.create(
title="Post 1", content="Content of post 1.", author=user, status="published"
)
Content.post_objects.create(
Post.post_objects.create(
title="Post 2", content="Content of post 2.", author=user, status="published"
)

Expand All @@ -35,7 +35,7 @@ def test_latest_posts_feed(client):
@pytest.mark.django_db
def test_truncated_posts_feed(client):
user = User.objects.create_user(username="testuser", password="testpass")
Content.post_objects.create(
Post.post_objects.create(
title="Post 1",
content="Content of post 1.<!--more-->Truncated content",
author=user,
Expand Down
Loading

0 comments on commit c2cd7ae

Please sign in to comment.