add support for article specific og image

This commit is contained in:
Tobias Brunner 2025-07-08 16:02:12 +02:00
parent 1d190fe182
commit 6b689704b0
No known key found for this signature in database
5 changed files with 49 additions and 6 deletions

View file

@ -69,8 +69,8 @@ class ArticleAdmin(admin.ModelAdmin):
(
"Images",
{
"fields": ("image_library",),
"description": "Select an image from the Image Library.",
"fields": ("image_library", "og_image"),
"description": "Select an image from the Image Library and optionally upload a specific Open Graph image for social sharing.",
},
),
(

View file

@ -0,0 +1,25 @@
# Generated by Django 5.2 on 2025-07-08 13:53
import hub.services.models.base
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("services", "0044_add_svg_support"),
]
operations = [
migrations.AddField(
model_name="article",
name="og_image",
field=models.ImageField(
blank=True,
help_text="Optional Open Graph image for social sharing (max 1MB). If not provided, the article's main image will be used.",
null=True,
upload_to="article_og_images/",
validators=[hub.services.models.base.validate_image_size],
),
),
]

View file

@ -51,6 +51,15 @@ class Article(ImageReference):
help_text="Link this article to a cloud provider",
)
# Open Graph image for social sharing
og_image = models.ImageField(
upload_to="article_og_images/",
blank=True,
null=True,
validators=[validate_image_size],
help_text="Optional Open Graph image for social sharing (max 1MB). If not provided, the article's main image will be used."
)
# Publishing controls
is_published = models.BooleanField(
default=False, help_text="Only published articles are visible to users"
@ -90,6 +99,15 @@ class Article(ImageReference):
return self.image_library.image
return None
@property
def get_og_image(self):
"""Returns the Open Graph image for social sharing"""
# Use specific OG image if available
if self.og_image:
return self.og_image
# Fall back to main article image
return self.get_image
@property
def related_to(self):
"""Returns a string describing what this article is related to"""

View file

@ -63,9 +63,9 @@ def social_meta_tags(context):
article = context["article"]
title = f"Servala - {article.title}"
description = article.excerpt
# Use article image if available, otherwise default
if article.get_image:
image_url = request.build_absolute_uri(article.get_image.url)
# Use OG image if available, otherwise fall back to article image, then default
if article.get_og_image:
image_url = request.build_absolute_uri(article.get_og_image.url)
# Determine og:type based on view
og_type = "website" # default