Generate RSS for comments
This commit is contained in:
parent
c88d20b710
commit
5c68eff22b
5 changed files with 45 additions and 8 deletions
|
@ -15,6 +15,8 @@ from app.models.report import Report
|
|||
import requests
|
||||
import json
|
||||
import config
|
||||
import PyRSS2Gen
|
||||
import markdown
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
queue = Queue()
|
||||
|
@ -146,9 +148,11 @@ def reply_comment_email(data):
|
|||
# update Comment row
|
||||
comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
comment.save()
|
||||
|
||||
logger.info('commit comment: %d' % comment_id)
|
||||
|
||||
# rebuild RSS
|
||||
rss(token)
|
||||
|
||||
# send approval confirmation email to admin
|
||||
email_body = get_template('approve_comment').render(original=message)
|
||||
mail(from_email, 'Re: ' + subject, email_body)
|
||||
|
@ -297,27 +301,27 @@ def report(token):
|
|||
|
||||
published = []
|
||||
for row in Report.select().join(Site).where(
|
||||
Site.token == token, Report.published):
|
||||
Site.token == token, Report.published):
|
||||
published.append({'url': "http://" + site.url + row.url,
|
||||
'name': row.name, 'email': row.email})
|
||||
'name': row.name, 'email': row.email})
|
||||
|
||||
rejected = []
|
||||
for row in Report.select().join(Site).where(
|
||||
Site.token == token, Report.rejected):
|
||||
Site.token == token, Report.rejected):
|
||||
rejected.append({'url': "http://" + site.url + row.url,
|
||||
'name': row.name, 'email': row.email})
|
||||
|
||||
subscribed = []
|
||||
for row in Report.select().join(Site).where(
|
||||
Site.token == token, Report.subscribed):
|
||||
Site.token == token, Report.subscribed):
|
||||
subscribed.append({'url': "http://" + site.url + row.url,
|
||||
'name': row.name, 'email': row.email})
|
||||
'name': row.name, 'email': row.email})
|
||||
|
||||
unsubscribed = []
|
||||
for row in Report.select().join(Site).where(
|
||||
Site.token == token, Report.subscribed):
|
||||
Site.token == token, Report.subscribed):
|
||||
unsubscribed.append({'url': "http://" + site.url + row.url,
|
||||
'name': row.name, 'email': row.email})
|
||||
'name': row.name, 'email': row.email})
|
||||
|
||||
email_body = get_template('report').render(secret=config.SECRET,
|
||||
root_url=config.ROOT_URL,
|
||||
|
@ -334,6 +338,31 @@ def report(token):
|
|||
Report.delete().execute()
|
||||
|
||||
|
||||
def rss(token):
|
||||
site = Site.select().where(Site.token == token).get()
|
||||
rss_title = get_template('rss_title_message').render(site=site.name)
|
||||
md = markdown.Markdown()
|
||||
|
||||
items = []
|
||||
for row in Comment.select().join(Site).where(
|
||||
Site.token == token, Comment.published).order_by(-Comment.published).limit(10):
|
||||
items.append(PyRSS2Gen.RSSItem(
|
||||
title='%s - http://%s%s' % (row.author_name, site.url, row.url),
|
||||
link="http://%s%s" % (site.url, row.url),
|
||||
description=md.convert(row.content),
|
||||
guid=PyRSS2Gen.Guid('%s%d' % (token, row.id)),
|
||||
pubDate=row.published
|
||||
))
|
||||
|
||||
rss = PyRSS2Gen.RSS2(
|
||||
title=rss_title,
|
||||
link="http://" + site.url,
|
||||
description="Commentaires du site '%s'" % site.name,
|
||||
lastBuildDate=datetime.now(),
|
||||
items=items)
|
||||
rss.write_xml(open(config.RSS_FILE, "w"), encoding="utf-8")
|
||||
|
||||
|
||||
def mail(to_email, subject, message):
|
||||
|
||||
headers = {'Content-Type': 'application/json; charset=utf-8'}
|
||||
|
@ -368,6 +397,10 @@ def start(template_dir):
|
|||
logger.info("load templates from directory %s" % template_dir)
|
||||
env = Environment(loader=FileSystemLoader(template_dir))
|
||||
|
||||
# generate RSS for all sites
|
||||
for site in Site.select():
|
||||
rss(site.token)
|
||||
|
||||
# start processor thread
|
||||
proc = Processor()
|
||||
proc.start()
|
||||
|
|
1
app/templates/en/rss_title_message.tpl
Normal file
1
app/templates/en/rss_title_message.tpl
Normal file
|
@ -0,0 +1 @@
|
|||
Comments : {{ site }}
|
1
app/templates/fr/rss_title_message.tpl
Normal file
1
app/templates/fr/rss_title_message.tpl
Normal file
|
@ -0,0 +1 @@
|
|||
Commentaires du site : {{ site }}
|
|
@ -18,3 +18,4 @@ SECRET = "Uqca5Kc8xuU6THz9"
|
|||
|
||||
ROOT_URL = 'http://localhost:8000'
|
||||
|
||||
RSS_FILE = 'comments.xml'
|
|
@ -3,6 +3,7 @@ Flask==0.10.1
|
|||
Flask-Cors==2.0.1
|
||||
itsdangerous==0.24
|
||||
Jinja2==2.7.3
|
||||
Markdown==2.6.2
|
||||
MarkupSafe==0.23
|
||||
peewee==2.6.0
|
||||
PyMySQL==0.6.6
|
||||
|
|
Loading…
Add table
Reference in a new issue