diff --git a/app/models/site.py b/app/models/site.py index 413dd82..9c34cb2 100644 --- a/app/models/site.py +++ b/app/models/site.py @@ -10,6 +10,7 @@ class Site(Model): name = CharField(unique=True) url = CharField() token = CharField() + admin_email = CharField() class Meta: database = get_db() diff --git a/app/services/processor.py b/app/services/processor.py index a46bb78..a5f083c 100644 --- a/app/services/processor.py +++ b/app/services/processor.py @@ -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,59 +31,71 @@ class Processor(Thread): self.is_running = True while self.is_running: - msg = queue.get() - if msg['request'] == 'new_comment': - new_comment(msg['data']) - #elif msg['type'] == 'reply_comment_email': - # reply_comment_email(req['From'], req['Subject'], req['Body']) - #elif req['type'] == 'unsubscribe': - # unsubscribe_reader(req['email'], req['article']) - else: - logger.info("Dequeue unknown request " + str(msg)) + try: + msg = queue.get() + if msg['request'] == 'new_comment': + new_comment(msg['data']) + #elif msg['type'] == 'reply_comment_email': + # reply_comment_email(req['From'], req['Subject'], req['Body']) + #elif req['type'] == 'unsubscribe': + # unsubscribe_reader(req['email'], req['article']) + else: + logger.info("throw unknown request " + str(msg)) + except: + logger.exception("processing failure") def new_comment(data): - try: - token = data.get('token', '') - url = data.get('url', '') - author_name = data.get('author', '') - author_email = data.get('email', '') - author_site = data.get('site', '') - message = data.get('message', '') - subscribe = data.get('subscribe', '') - - # 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") - - 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) - - # Reader subscribes to further comments - if subscribe and email: - subscribe_reader(email, article, url) - - logger.debug("new comment processed ") - except: - logger.exception("new_comment failure") + logger.info('new comment received: %s' % data) + + token = data.get('token', '') + url = data.get('url', '') + author_name = data.get('author', '') + author_email = data.get('email', '') + author_site = data.get('site', '') + message = data.get('message', '') + subscribe = data.get('subscribe', '') + + # create a new comment row + site = Site.select().where(Site.token == token).get() + + 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() + + # 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) + + logger.debug("new comment processed ") 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): diff --git a/config.py b/config.py index 0aab685..174b316 100644 --- a/config.py +++ b/config.py @@ -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 diff --git a/requirements.txt b/requirements.txt index cd3fc9b..c43096d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/tools/pecosys2stacosys.py b/tools/pecosys2stacosys.py index 408db43..bfb1b28 100644 --- a/tools/pecosys2stacosys.py +++ b/tools/pecosys2stacosys.py @@ -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__':