Files
postgres-web/pgweb/news/models.py
Magnus Hagander ba9138f36b Add support for sending out news as HTML email
When a news article is approved, it gets delivered as an email to the
pgsql-announce mailinglist. It will render the markdown of the news
article into a HTML part of the email, and include the markdown raw as
the text part (for those unable or unwilling to read html mail).

For each organisation, a mail template can be specified. Initially only
two templates are supported, one "default" and one "pgproject" which is
for official project news. The intention is *not* to provide generic
templates, but we may want to extend this to certain related projects in
the future *maybe* (such as regional NPOs).

These templates are stored in templates/news/mail/*.html, and for each
template *all* images found in templates/news/mail/img.<template>/ will
be attached to the email. "Conditional image inclusion" currently not
supported.

To do CSS inlining on top of the markdown output, module pynliner is now
required (available in the python3-pynliner package on Debian).

A testing script is added as news_send_email.py in order to easier test
out templates. This is *not* intended for production sending, so it will
for example send unmoderated news. By sending, it adds it to the
outgoing mailqueue in the system, so unless the cronjob is set up to
send, nothing will happen until that is run manually.

Support is included for tagged delivery using pglister, by directly
mapping NewsTags to pglister tags.
2020-07-13 14:58:08 +02:00

80 lines
3.0 KiB
Python

from django.db import models
from datetime import date
from pgweb.core.models import Organisation
from pgweb.util.moderation import TristateModerateModel, ModerationState
from .util import send_news_email
class NewsTag(models.Model):
urlname = models.CharField(max_length=20, null=False, blank=False, unique=True)
name = models.CharField(max_length=32, null=False, blank=False)
description = models.CharField(max_length=200, null=False, blank=False)
allowed_orgs = models.ManyToManyField(Organisation, blank=True,
help_text="Organisations allowed to use this tag")
sortkey = models.IntegerField(null=False, blank=False, default=100)
def __str__(self):
return self.name
class Meta:
ordering = ('sortkey', 'urlname', )
class NewsArticle(TristateModerateModel):
org = models.ForeignKey(Organisation, null=False, blank=False, verbose_name="Organisation", help_text="If no organisations are listed, please check the <a href=\"/account/orglist/\">organisation list</a> and contact the organisation manager or <a href=\"mailto:webmaster@postgresql.org\">webmaster@postgresql.org</a> if none are listed.", on_delete=models.CASCADE)
date = models.DateField(null=False, blank=False, default=date.today)
title = models.CharField(max_length=200, null=False, blank=False)
content = models.TextField(null=False, blank=False)
tweeted = models.BooleanField(null=False, blank=False, default=False)
tags = models.ManyToManyField(NewsTag, blank=False, help_text="Select the tags appropriate for this post")
account_edit_suburl = 'news'
markdown_fields = ('content',)
moderation_fields = ('org', 'date', 'title', 'content', 'taglist')
preview_fields = ('title', 'content', 'taglist')
extramodnotice = "In particular, note that news articles will be sent by email to subscribers, and therefor cannot be recalled in any way once sent."
def purge_urls(self):
yield '/about/news/%s/' % self.pk
yield '/about/newsarchive/'
yield '/news.rss'
yield '/news/.*.rss'
# FIXME: when to expire the front page?
yield '/$'
def __str__(self):
return "%s: %s" % (self.date, self.title)
def verify_submitter(self, user):
return (len(self.org.managers.filter(pk=user.pk)) == 1)
def is_migrated(self):
if self.org.pk == 0:
return True
return False
@property
def taglist(self):
return ", ".join([t.name for t in self.tags.all()])
@property
def displaydate(self):
return self.date.strftime("%Y-%m-%d")
class Meta:
ordering = ('-date',)
@classmethod
def get_formclass(self):
from pgweb.news.forms import NewsArticleForm
return NewsArticleForm
@property
def block_edit(self):
# Don't allow editing of news articles that have been published
return self.modstate in (ModerationState.PENDING, ModerationState.APPROVED)
def on_approval(self, request):
send_news_email(self)