add article date field

This commit is contained in:
Tobias Brunner 2025-07-04 15:51:44 +02:00
parent 470887c34e
commit 6351da70ee
No known key found for this signature in database
6 changed files with 42 additions and 19 deletions

View file

@ -45,7 +45,7 @@ class ArticleAdmin(admin.ModelAdmin):
"image_preview",
"is_published",
"is_featured",
"created_at",
"article_date",
)
list_filter = (
"is_published",
@ -54,11 +54,12 @@ class ArticleAdmin(admin.ModelAdmin):
"related_service",
"related_consulting_partner",
"related_cloud_provider",
"created_at",
"article_date",
)
search_fields = ("title", "excerpt", "content", "meta_keywords")
prepopulated_fields = {"slug": ("title",)}
readonly_fields = ("created_at", "updated_at")
ordering = ("-article_date",)
def image_preview(self, obj):
"""Display image preview in admin list view"""

View file

@ -0,0 +1,22 @@
# Generated by Django 5.2 on 2025-07-04 13:48
import django.utils.timezone
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("services", "0038_add_plan_ordering_and_best"),
]
operations = [
migrations.AddField(
model_name="article",
name="article_date",
field=models.DateField(
default=django.utils.timezone.now,
help_text="Date of the article publishing",
),
),
]

View file

@ -3,6 +3,7 @@ from django.urls import reverse
from django.utils.text import slugify
from django.contrib.auth.models import User
from django_prose_editor.fields import ProseEditorField
from django.utils import timezone
from .base import validate_image_size
from .services import Service
from .providers import CloudProvider, ConsultingPartner
@ -23,6 +24,9 @@ class Article(models.Model):
help_text="Title picture for the article",
)
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="articles")
article_date = models.DateField(
default=timezone.now, help_text="Date of the article publishing"
)
# Relations to other models
related_service = models.ForeignKey(

View file

@ -16,9 +16,7 @@
<div class="d-flex justify-content-center align-items-center gap-3 text-sm">
<span>By {{ article.author.get_full_name|default:article.author.username }}</span>
<span></span>
<span>{{ article.created_at|date:"M d, Y" }}</span>
{% if article.updated_at != article.created_at %}
{% endif %}
<span>{{ article.article_date|date:"M d, Y" }}</span>
</div>
</div>
</header>

View file

@ -169,7 +169,7 @@
By {{ article.author.get_full_name|default:article.author.username }}
</span>
<span class="text-muted ms-2">
{{ article.created_at|date:"M d, Y" }}
{{ article.article_date|date:"M d, Y" }}
</span>
</p>
</div>

View file

@ -41,7 +41,7 @@ def article_list(request):
# Order articles: featured first, then by creation date (newest first)
articles = articles.order_by(
"-is_featured", # Featured first (True before False)
"-created_at", # Newest first
"-article_date", # Newest first
)
# Create base querysets for each filter type that apply all OTHER current filters
@ -137,15 +137,13 @@ def article_detail(request, slug):
"author",
"related_service",
"related_consulting_partner",
"related_cloud_provider"
"related_cloud_provider",
).filter(is_published=True),
slug=slug,
)
# Get related articles (same service, partner, or provider)
related_articles = Article.objects.filter(
is_published=True
).exclude(id=article.id)
related_articles = Article.objects.filter(is_published=True).exclude(id=article.id)
if article.related_service:
related_articles = related_articles.filter(
@ -164,10 +162,10 @@ def article_detail(request, slug):
related_articles = related_articles.filter(
related_service__isnull=True,
related_consulting_partner__isnull=True,
related_cloud_provider__isnull=True
related_cloud_provider__isnull=True,
)
related_articles = related_articles.order_by("-created_at")[:3]
related_articles = related_articles.order_by("-article_date")[:3]
context = {
"article": article,