Skip to content

Commit 5e873f6

Browse files
committed
Added opengraph_tags property on blog.Entry model
1 parent 92ca24f commit 5e873f6

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

blog/models.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from django.conf import settings
44
from django.core.cache import caches
55
from django.db import models
6+
from django.templatetags.static import static
67
from django.test import RequestFactory
78
from django.utils import timezone
89
from django.utils.cache import _generate_cache_header_key
10+
from django.utils.formats import date_format
911
from django.utils.translation import gettext_lazy as _
1012
from django_hosts.resolvers import reverse
1113
from docutils.core import publish_parts
@@ -150,6 +152,10 @@ def is_published(self):
150152

151153
is_published.boolean = True
152154

155+
@property
156+
def pub_date_localized(self):
157+
return date_format(self.pub_date)
158+
153159
def save(self, *args, **kwargs):
154160
self.summary_html = ContentFormat.to_html(self.content_format, self.summary)
155161
self.body_html = ContentFormat.to_html(self.content_format, self.body)
@@ -171,6 +177,25 @@ def invalidate_cached_entry(self):
171177
)
172178
cache.delete(cache_key)
173179

180+
@property
181+
def opengraph_tags(self):
182+
return {
183+
"og:type": "article",
184+
"og:title": self.headline,
185+
"og:description": _("Posted by {author} on {pub_date}").format(
186+
author=self.author, pub_date=self.pub_date_localized
187+
),
188+
"og:article:published_time": self.pub_date.isoformat(),
189+
"og:article:author": self.author,
190+
"og:image": static("img/logos/django-logo-negative.png"),
191+
"og:image:alt": "Django logo",
192+
"og:url": self.get_absolute_url(),
193+
"og:site_name": "Django Project",
194+
"twitter:card": "summary",
195+
"twitter:creator": "djangoproject",
196+
"twitter:site": "djangoproject",
197+
}
198+
174199

175200
class EventQuerySet(EntryQuerySet):
176201
def past(self):

blog/tests.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from contextlib import redirect_stderr
2-
from datetime import timedelta
2+
from datetime import date, timedelta
33
from io import StringIO
44

55
from django.contrib.auth.models import User
66
from django.core.files.base import ContentFile
77
from django.test import TestCase
88
from django.urls import reverse
9-
from django.utils import timezone
9+
from django.utils import timezone, translation
1010

1111
from .models import ContentFormat, Entry, Event, ImageUpload
1212
from .sitemaps import WeblogSitemap
@@ -126,6 +126,12 @@ def test_header_base_level_markdown(self):
126126
)
127127
self.assertHTMLEqual(entry.body_html, '<h3 id="s-test">test</h3>')
128128

129+
def test_pub_date_localized(self):
130+
entry = Entry(pub_date=date(2005, 7, 21))
131+
self.assertEqual(entry.pub_date_localized, "July 21, 2005")
132+
with translation.override("nn"):
133+
self.assertEqual(entry.pub_date_localized, "21. juli 2005")
134+
129135

130136
class EventTestCase(DateTimeMixin, TestCase):
131137
def test_manager_past_future(self):

djangoproject/templates/blog/entry_detail.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
{% block title %}{{ object.headline|escape }} | Weblog{% endblock %}
55

6-
{% block og_title %}{{ object.headline|safe }}{% endblock %}
7-
{% block og_description %}{% blocktranslate trimmed with author=object.author pub_date=object.pub_date|date:"DATE_FORMAT" %}
8-
Posted by {{ author }} on {{ pub_date }}
9-
{% endblocktranslate %}{% endblock %}
6+
{% block og_tags %}
7+
{% for property, content in object.opengraph_tags.items %}
8+
<meta property="{{ property }}" content="{{ content }}" />
9+
{% endfor %}
10+
{% endblock og_tags %}
1011

1112
{% block content %}
1213
<h1>{{ object.headline|safe }}</h1>

0 commit comments

Comments
 (0)