diff --git a/app/controllers/api.py b/app/controllers/api.py index 0c9a765..f84f72c 100644 --- a/app/controllers/api.py +++ b/app/controllers/api.py @@ -22,9 +22,9 @@ def query_comments(): logger.info('retrieve comments for token %s, url %s' % (token, url)) for comment in Comment.select(Comment).join(Site).where( - (Comment.url == url) & - (Comment.published.is_null(False)) & - (Site.token == token)).order_by(+Comment.published): + (Comment.url == url) & + (Comment.published.is_null(False)) & + (Site.token == token)).order_by(+Comment.published): d = {} d['author'] = comment.author_name d['content'] = comment.content @@ -50,9 +50,9 @@ def get_comments_count(): token = request.args.get('token', '') url = request.args.get('url', '') count = Comment.select(Comment).join(Site).where( - (Comment.url == url) & - (Comment.published.is_null(False)) & - (Site.token == token)).count() + (Comment.url == url) & + (Comment.published.is_null(False)) & + (Site.token == token)).count() r = jsonify({'count': count}) r.status_code = 200 except: diff --git a/app/controllers/mail.py b/app/controllers/mail.py index 51bbda5..81ec5d1 100644 --- a/app/controllers/mail.py +++ b/app/controllers/mail.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- import logging -from flask import request, jsonify, abort +from flask import request, abort from app import app from app.services import processor diff --git a/app/models/comment.py b/app/models/comment.py index 1b79a58..1492085 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -13,7 +13,7 @@ from app.services.database import get_db class Comment(Model): url = CharField() created = DateTimeField() - published = DateTimeField(null=True,default=None) + published = DateTimeField(null=True, default=None) author_name = CharField() author_email = CharField(default='') author_site = CharField(default='') diff --git a/app/run.py b/app/run.py index 21652ea..6823c3a 100644 --- a/app/run.py +++ b/app/run.py @@ -23,6 +23,7 @@ from app.controllers import api from app.controllers import mail from app import app + # configure logging def configure_logging(level): root_logger = logging.getLogger() @@ -30,7 +31,8 @@ def configure_logging(level): ch = logging.StreamHandler() ch.setLevel(level) # create formatter - formatter = logging.Formatter('[%(asctime)s] %(name)s %(levelname)s %(message)s') + formatter = logging.Formatter( + '[%(asctime)s] %(name)s %(levelname)s %(message)s') # add formatter to ch ch.setFormatter(formatter) # add ch to logger @@ -44,7 +46,11 @@ logger = logging.getLogger(__name__) # initialize database database.setup() -# start processor +# routes +logger.debug('imported: %s ' % api.__name__) +logger.debug('imported: %s ' % mail.__name__) + +# start processor template_path = os.path.abspath(os.path.join(current_path, 'templates')) processor.start(template_path) diff --git a/app/services/processor.py b/app/services/processor.py index 0176627..aeeb235 100644 --- a/app/services/processor.py +++ b/app/services/processor.py @@ -6,7 +6,6 @@ import re from datetime import datetime from threading import Thread from queue import Queue -import chardet from jinja2 import Environment, FileSystemLoader from app.models.site import Site from app.models.comment import Comment @@ -37,7 +36,7 @@ class Processor(Thread): new_comment(msg['data']) elif msg['request'] == 'new_mail': reply_comment_email(msg['data']) - #elif req['type'] == 'unsubscribe': + # elif req['type'] == 'unsubscribe': # unsubscribe_reader(req['email'], req['article']) else: logger.info("throw unknown request " + str(msg)) @@ -67,8 +66,8 @@ def new_comment(data): # 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) + author_site=author_site, author_email=author_email, + content=message, created=created, published=None) comment.save() # render email body template @@ -83,17 +82,18 @@ def new_comment(data): '' ) comment_text = '\n'.join(comment_list) - email_body = get_template('new_comment').render(url=url, comment=comment_text) + email_body = get_template('new_comment').render( + url=url, comment=comment_text) # send email - # TODO subject should embed a key subject = '%s: [%d:%s]' % (site.name, comment.id, token) 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 author_email: + # TODO support subscription + # subscribe_reader(email, article, url) + pass logger.debug("new comment processed ") @@ -118,8 +118,6 @@ def reply_comment_email(data): logger.warn('ignore corrupted email') return - # TODO validate chardet decoding is no more needed - #message = decode_best_effort(message) if not message: logger.warn('ignore empty email') return @@ -131,7 +129,7 @@ def reply_comment_email(data): email_body = get_template('drop_comment').render(original=message) mail(email_address, 'Re: ' + subject, email_body) else: - # update Comment row + # update Comment row comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S") comment.save() @@ -143,12 +141,12 @@ def reply_comment_email(data): # TODO manage subscriptions # notify reader once comment is published - #reader_email, article_url = get_email_metadata(message) - #if reader_email: + # reader_email, article_url = get_email_metadata(message) + # if reader_email: # notify_reader(reader_email, article_url) # notify subscribers every time a new comment is published - #notify_subscribers(article) + # notify_subscribers(article) def get_email_metadata(message): @@ -198,14 +196,6 @@ def notify_reader(email, url): mail(pecosys.get_config('subscription', 'from_email'), email, subject, email_body) -def decode_best_effort(string): - info = chardet.detect(string) - if info['confidence'] < 0.5: - return string.decode('utf8', errors='replace') - else: - return string.decode(info['encoding'], errors='replace') - - def mail(to_email, subject, message): headers = {'Content-Type': 'application/json; charset=utf-8'} diff --git a/config.py b/config.py index 174b316..cf0c768 100644 --- a/config.py +++ b/config.py @@ -4,7 +4,7 @@ DEBUG = True LANG = "fr" -#DB_URL = "mysql://stacosys_user:stacosys_password@localhost:3306/stacosys" +# DB_URL = "mysql://stacosys_user:stacosys_password@localhost:3306/stacosys" DB_URL = "sqlite:///db.sqlite" MAIL_URL = "http://localhost:8025/mbox"