summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorMagnus Hagander2020-10-21 09:38:33 +0000
committerMagnus Hagander2020-10-21 09:38:33 +0000
commit4b4ce3d5da9363068517486dd089f9f5185d29ad (patch)
tree6d340827c9812de5b7c1560cabeb04fdd0441067 /django
parentf91d2ba43fcd2c62e94a82bef8c675ac4b318770 (diff)
Add opengraph metatags to messages
This makes for nicer "previews" on for example twitter when posting links. Suggested by Andres Freund
Diffstat (limited to 'django')
-rw-r--r--django/archives/mailarchives/models.py8
-rw-r--r--django/archives/mailarchives/templates/base.html10
-rw-r--r--django/archives/mailarchives/views.py14
3 files changed, 31 insertions, 1 deletions
diff --git a/django/archives/mailarchives/models.py b/django/archives/mailarchives/models.py
index fd9a017..afd0c09 100644
--- a/django/archives/mailarchives/models.py
+++ b/django/archives/mailarchives/models.py
@@ -1,6 +1,8 @@
from django.db import models
from django.contrib.auth.models import User
+from email.utils import parseaddr
+
# Reason a message was hidden.
# We're intentionally putting the prefix text in the array here, since
# we might need that flexibility in the future.
@@ -39,6 +41,12 @@ class Message(models.Model):
def shortdate(self):
return self.date.strftime("%Y%m%d%H%M")
+ def from_name_only(self):
+ try:
+ return parseaddr(self.mailfrom)[0]
+ except Exception:
+ return ''
+
# We explicitly cache the attachments here, so we can use them
# multiple times from templates without generating multiple queries
# to the database.
diff --git a/django/archives/mailarchives/templates/base.html b/django/archives/mailarchives/templates/base.html
index 43f05e5..6946d3d 100644
--- a/django/archives/mailarchives/templates/base.html
+++ b/django/archives/mailarchives/templates/base.html
@@ -3,7 +3,15 @@
<head>
<title>PostgreSQL: {%block title%}{%endblock%}</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
- <meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8" />
+ <meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8" />{%if og%}
+ <meta property="og:url" content="https://www.postgresql.org/{{og.url}}" />
+ <meta property="og:type" content="article" />
+ <meta property="og:article:author" content="{{og.author}}" />
+ <meta property="og:published_time" content="{{og.time|date:"c"}}" />
+ <meta property="og:image" content="https://www.postgresql.org/media-archives/img/about/press/elephant.png" />
+ <meta property="og:title" content="{{og.title}}" />
+ <meta property="og:description" content="{{og.description|truncatewords:"20"}}" />
+ <meta property="og:site_name" content="PostgreSQL Mailing List Archives" />{%endif%}
{%block meta%}{%endblock%} {# used for custom meta tags such as description which we don't want for every page #}
<meta name="theme-color" content="#336791"/>
<meta name="copyright" content="The PostgreSQL Global Development Group" />
diff --git a/django/archives/mailarchives/views.py b/django/archives/mailarchives/views.py
index aba82f5..d32ccce 100644
--- a/django/archives/mailarchives/views.py
+++ b/django/archives/mailarchives/views.py
@@ -19,6 +19,7 @@ import calendar
import email.parser
import email.policy
from io import BytesIO
+from urllib.parse import quote
import json
@@ -490,6 +491,13 @@ def message(request, msgid):
'parent': parent,
'lists': lists,
'nextprev': nextprev,
+ 'og': {
+ 'url': 'message-id/{}'.format(quote(m.messageid)),
+ 'author': m.from_name_only(),
+ 'time': m.date,
+ 'title': m.subject,
+ 'description': m.bodytxt,
+ },
})
if settings.PUBLIC_ARCHIVES:
r['xkey'] = 'pgat_{0}'.format(m.threadid)
@@ -521,6 +529,12 @@ def message_flat(request, msgid):
'allmsg': allmsg,
'lists': lists,
'isfirst': isfirst,
+ 'og': {
+ 'url': 'message-id/flat/{}'.format(quote(msg.messageid)),
+ 'author': msg.from_name_only(),
+ 'time': msg.date,
+ 'title': msg.subject,
+ },
})
if settings.PUBLIC_ARCHIVES:
r['xkey'] = 'pgat_{0}'.format(msg.threadid)