Skip to content

Commit

Permalink
Merge pull request #3 from stuartmaxwell/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmaxwell authored Apr 25, 2024
2 parents b313948 + afb585a commit 603fd00
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 49 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Build and Deploy Docker Image

on:
push:
branches: [main]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.TOKEN_GITHUB }}

- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ github.repository }}:latest

- name: Trigger Rundeck
run: |
curl --request POST ${{ secrets.RUNDECK_WEBHOOK_URL }} --header 'Authorization: ${{ secrets.RUNDECK_WEBHOOK_TOKEN }}'
4 changes: 2 additions & 2 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
DB_PORT=(str, ""),
WHITENOISE_STATIC=(bool, False),
ADMIN_URL=(str, "admin"),
APP_NAME=(str, "stuartm.nz"),
)

environ.Env.read_env(Path(BASE_DIR / ".env"))

APP_NAME = "stuartm.nz"

SECRET_KEY = env("SECRET_KEY")

DEBUG = env("DEBUG")

ALLOWED_HOSTS = env("ALLOWED_HOSTS")

APP_NAME = env("APP_NAME")

ADMIN_URL = env("ADMIN_URL")

Expand Down
19 changes: 19 additions & 0 deletions djpress/migrations/0002_alter_content_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.4 on 2024-04-25 06:00

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('djpress', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='content',
name='date',
field=models.DateField(default=datetime.datetime.now),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.0.4 on 2024-04-25 06:01

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('djpress', '0002_alter_content_date'),
]

operations = [
migrations.AlterField(
model_name='content',
name='date',
field=models.DateTimeField(default=datetime.datetime.now),
),
migrations.AlterField(
model_name='content',
name='modified_date',
field=models.DateTimeField(auto_now=True),
),
]
19 changes: 19 additions & 0 deletions djpress/migrations/0004_alter_content_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.4 on 2024-04-25 07:01

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('djpress', '0003_alter_content_date_alter_content_modified_date'),
]

operations = [
migrations.AlterField(
model_name='content',
name='date',
field=models.DateTimeField(default=django.utils.timezone.now),
),
]
18 changes: 18 additions & 0 deletions djpress/migrations/0005_alter_content_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.4 on 2024-04-25 07:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('djpress', '0004_alter_content_date'),
]

operations = [
migrations.AlterField(
model_name='content',
name='slug',
field=models.SlugField(blank=True, unique=True),
),
]
49 changes: 39 additions & 10 deletions djpress/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from django.contrib.auth.models import User
from django.db import models
from django.utils import timezone
from django.utils.text import slugify


class Category(models.Model):
Expand All @@ -25,11 +27,11 @@ class Content(models.Model):
CONTENT_TYPE_CHOICES: ClassVar = [("post", "Post"), ("page", "Page")]

title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
slug = models.SlugField(unique=True, blank=True)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(auto_now_add=True)
modified_date = models.DateField(auto_now=True)
date = models.DateTimeField(default=timezone.now)
modified_date = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default="draft")
content_type = models.CharField(
max_length=10,
Expand All @@ -42,22 +44,49 @@ def __str__(self: "Content") -> str:
"""Return the string representation of the content."""
return self.title

def save(self: "Content", *args, **kwargs) -> None: # noqa: ANN002, ANN003
"""Override the save method to auto-generate the slug."""
if not self.slug:
self.slug = slugify(self.title)
if not self.slug or self.slug.strip("-") == "":
msg = "Invalid title. Unable to generate a valid slug."
raise ValueError(msg)
super().save(*args, **kwargs)

@classmethod
def get_published_posts(cls: type["Content"]) -> models.QuerySet:
"""Return all published posts ordered by date."""
return cls.objects.filter(status="published", content_type="post").order_by(
"-date",
)
"""Return all published posts.
Must have a date less than or equal to the current date/time, ordered by date in
descending order.
"""
return cls.objects.filter(
status="published",
content_type="post",
date__lte=timezone.now(),
).order_by("-date")

@classmethod
def get_published_post_by_slug(cls: type["Content"], slug: str) -> "Content":
"""Return a single published post based on its slug."""
return cls.objects.get(slug=slug, status="published", content_type="post")
"""Return a single published post.
Must have a date less than or equal to the current date/time based on its slug.
"""
return cls.objects.get(
slug=slug,
status="published",
content_type="post",
date__lte=timezone.now(),
)

@classmethod
def get_published_posts_by_category(
cls: type["Content"],
category: Category,
) -> models.QuerySet:
"""Return all published posts for a specific category."""
"""Return all published posts.
Must have a date less than or equal to the current date/time for a specific
category, ordered by date in descending order.
"""
return cls.get_published_posts().filter(categories=category)
11 changes: 1 addition & 10 deletions djpress/templates/djpress/category_posts.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ <h1>Category: {{ category.name }}</h1>
<p>{{ category.description }}</p>
<hr>
{% for post in posts %}
<div class="card my-4">
<div class="card-body">
<h2 class="card-title">{{ post.title }}</h2>
<p class="card-text">{{ post.content|truncatewords:50 }}</p>
<a href="{% url 'djpress:post_detail' post.slug %}" class="btn btn-primary">Read More</a>
</div>
<div class="card-footer text-muted">
Posted on {{ post.date }} by {{ post.author.first_name }}
</div>
</div>
{% include "djpress/snippets/post_summary.html" %}
{% empty %}
<p>No posts available in this category.</p>
{% endfor %}
Expand Down
17 changes: 1 addition & 16 deletions djpress/templates/djpress/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,7 @@

{% block content %}
{% for post in posts %}
<div class="card my-4">
<div class="card-body">
<h2 class="card-title">{{ post.title }}</h2>
<p class="card-text">{{ post.content|truncatewords:50 }}</p>
<p>
Categories:
{% for category in post.categories.all %}
<a href="{% url 'djpress:category_posts' category.slug %}" class="badge bg-primary">{{ category.name }}</a>
{% endfor %}
</p>
<a href="{% url 'djpress:post_detail' post.slug %}" class="btn btn-primary">Read More</a>
</div>
<div class="card-footer text-muted">
Posted on {{ post.date }} by {{ post.author.first_name }}
</div>
</div>
{% include "djpress/snippets/post_summary.html" %}
{% empty %}
<p>No posts available.</p>
{% endfor %}
Expand Down
15 changes: 15 additions & 0 deletions djpress/templates/djpress/snippets/post_summary.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="card my-4">
<div class="card-body">
<h2 class="card-title"><a href="{% url 'djpress:post_detail' post.slug %}">{{ post.title }}</a></h2>
<p class="card-text">{{ post.content|truncatewords:50 }}</p>
<p>
Categories:
{% for category in post.categories.all %}
<a href="{% url 'djpress:category_posts' category.slug %}" class="badge bg-primary">{{ category.name }}</a>
{% endfor %}
</p>
</div>
<div class="card-footer text-muted">
Posted on {{ post.date }} by {{ post.author.first_name }}
</div>
</div>
Loading

0 comments on commit 603fd00

Please sign in to comment.