Send comment by email via SRMail

pull/6/head
Yax 10 years ago
parent 692b90d7d9
commit 033e0821b2

@ -10,6 +10,7 @@ class Site(Model):
name = CharField(unique=True)
url = CharField()
token = CharField()
admin_email = CharField()
class Meta:
database = get_db()

@ -10,6 +10,9 @@ import chardet
from jinja2 import Environment, FileSystemLoader
from app.models.site import Site
from app.models.comment import Comment
import requests
import json
import config
logger = logging.getLogger(__name__)
@ -28,6 +31,7 @@ class Processor(Thread):
self.is_running = True
while self.is_running:
try:
msg = queue.get()
if msg['request'] == 'new_comment':
new_comment(msg['data'])
@ -36,12 +40,15 @@ class Processor(Thread):
#elif req['type'] == 'unsubscribe':
# unsubscribe_reader(req['email'], req['article'])
else:
logger.info("Dequeue unknown request " + str(msg))
logger.info("throw unknown request " + str(msg))
except:
logger.exception("processing failure")
def new_comment(data):
try:
logger.info('new comment received: %s' % data)
token = data.get('token', '')
url = data.get('url', '')
author_name = data.get('author', '')
@ -53,34 +60,42 @@ def new_comment(data):
# create a new comment row
site = Site.select().where(Site.token == token).get()
logger.info('new comment received: %s' % data)
if author_site and author_site[:4] != 'http':
author_site = 'http://' + author_site
created = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# add a row to Comment table
comment = Comment(site=site, url=url, author_name=author_name,
author_site=author_site, author_email=author_email,
content=message, created=created, published=None)
comment.save()
1 / 0
# Render email body template
email_body = get_template('new_comment').render(url=url, comment=comment)
# Send email
mail(pecosys.get_config('post', 'from_email'),
pecosys.get_config('post', 'to_email'),
'[' + branch_name + '-' + article + ']', email_body)
# render email body template
comment_list = (
'author: %s' % author_name,
'email: %s' % author_email,
'site: %s' % author_site,
'date: %s' % created,
'url: %s' % url,
'',
'%s' % message,
''
)
comment_text = '\n'.join(comment_list)
email_body = get_template('new_comment').render(url=url, comment=comment_text)
# send email
# TODO subject should embed a key
subject = '%s: %d' % (site.name, 1)
mail(site.admin_email, subject, email_body)
# TODO support subscription
# Reader subscribes to further comments
if subscribe and email:
subscribe_reader(email, article, url)
#if subscribe and email:
# subscribe_reader(email, article, url)
logger.debug("new comment processed ")
except:
logger.exception("new_comment failure")
def reply_comment_email(from_email, subject, message):
@ -184,31 +199,23 @@ def decode_best_effort(string):
return string.decode(info['encoding'], errors='replace')
def mail(from_email, to_email, subject, *messages):
def mail(to_email, subject, message):
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email
msg.preamble = subject
for message in messages:
part = MIMEText(message, 'plain')
msg.attach(part)
s = smtplib.SMTP(pecosys.get_config('smtp', 'host'),
pecosys.get_config('smtp', 'port'))
if(pecosys.get_config('smtp', 'starttls')):
s.starttls()
s.login(pecosys.get_config('smtp', 'login'),
pecosys.get_config('smtp', 'password'))
s.sendmail(from_email, to_email, msg.as_string())
s.quit()
headers = {'Content-Type': 'application/json; charset=utf-8'}
msg = {
'to': to_email,
'subject': subject,
'content': message
}
r = requests.post(config.MAIL_URL, data=json.dumps(msg), headers=headers)
if r.status_code in (200, 201):
logger.debug('Email for %s posted' % to_email)
else:
logger.warn('Cannot post email for %s' % to_email)
def get_template(name):
return env.get_template(pecosys.get_config('global', 'lang') + '/' + name + '.tpl')
return env.get_template(config.LANG + '/' + name + '.tpl')
def enqueue(something):

@ -2,12 +2,12 @@
DEBUG = True
LANG = "en"
LANG = "fr"
#DB_URL = "mysql://stacosys_user:stacosys_password@localhost:3306/stacosys"
DB_URL = "sqlite:///db.sqlite"
MAIL_URL = "http://localhost:8025"
MAIL_URL = "http://localhost:8025/mbox"
HTTP_ADDRESS = "0.0.0.0"
HTTP_PORT = 8000

@ -7,5 +7,6 @@ Jinja2==2.7.3
MarkupSafe==0.23
peewee==2.6.0
PyMySQL==0.6.6
requests==2.7.0
six==1.9.0
Werkzeug==0.10.4

@ -81,7 +81,7 @@ def convert_comment(db, site, root_url, filename):
@provide_db
def convert(db, site_name, url, comment_dir):
def convert(db, site_name, url, admin_email, comment_dir):
# create DB tables if needed
db.create_tables([Site, Comment], safe=True)
@ -93,7 +93,8 @@ def convert(db, site_name, url, comment_dir):
except Site.DoesNotExist:
pass
site = Site.create(name=site_name, url=url, token=salt(url))
site = Site.create(name=site_name, url=url, token=salt(url),
admin_email=admin_email)
for dirpath, dirs, files in os.walk(comment_dir):
for filename in files:
@ -105,8 +106,8 @@ def convert(db, site_name, url, comment_dir):
@clize
def pecosys2stacosys(site, url, comment_dir):
convert(site, url, comment_dir)
def pecosys2stacosys(site, url, admin_email, comment_dir):
convert(site, url, admin_email, comment_dir)
if __name__ == '__main__':

Loading…
Cancel
Save