PEP8 compliance
This commit is contained in:
parent
c9307935db
commit
6db8fac605
6 changed files with 30 additions and 34 deletions
|
@ -22,9 +22,9 @@ def query_comments():
|
||||||
|
|
||||||
logger.info('retrieve comments for token %s, url %s' % (token, url))
|
logger.info('retrieve comments for token %s, url %s' % (token, url))
|
||||||
for comment in Comment.select(Comment).join(Site).where(
|
for comment in Comment.select(Comment).join(Site).where(
|
||||||
(Comment.url == url) &
|
(Comment.url == url) &
|
||||||
(Comment.published.is_null(False)) &
|
(Comment.published.is_null(False)) &
|
||||||
(Site.token == token)).order_by(+Comment.published):
|
(Site.token == token)).order_by(+Comment.published):
|
||||||
d = {}
|
d = {}
|
||||||
d['author'] = comment.author_name
|
d['author'] = comment.author_name
|
||||||
d['content'] = comment.content
|
d['content'] = comment.content
|
||||||
|
@ -50,9 +50,9 @@ def get_comments_count():
|
||||||
token = request.args.get('token', '')
|
token = request.args.get('token', '')
|
||||||
url = request.args.get('url', '')
|
url = request.args.get('url', '')
|
||||||
count = Comment.select(Comment).join(Site).where(
|
count = Comment.select(Comment).join(Site).where(
|
||||||
(Comment.url == url) &
|
(Comment.url == url) &
|
||||||
(Comment.published.is_null(False)) &
|
(Comment.published.is_null(False)) &
|
||||||
(Site.token == token)).count()
|
(Site.token == token)).count()
|
||||||
r = jsonify({'count': count})
|
r = jsonify({'count': count})
|
||||||
r.status_code = 200
|
r.status_code = 200
|
||||||
except:
|
except:
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from flask import request, jsonify, abort
|
from flask import request, abort
|
||||||
from app import app
|
from app import app
|
||||||
from app.services import processor
|
from app.services import processor
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ from app.services.database import get_db
|
||||||
class Comment(Model):
|
class Comment(Model):
|
||||||
url = CharField()
|
url = CharField()
|
||||||
created = DateTimeField()
|
created = DateTimeField()
|
||||||
published = DateTimeField(null=True,default=None)
|
published = DateTimeField(null=True, default=None)
|
||||||
author_name = CharField()
|
author_name = CharField()
|
||||||
author_email = CharField(default='')
|
author_email = CharField(default='')
|
||||||
author_site = CharField(default='')
|
author_site = CharField(default='')
|
||||||
|
|
|
@ -23,6 +23,7 @@ from app.controllers import api
|
||||||
from app.controllers import mail
|
from app.controllers import mail
|
||||||
from app import app
|
from app import app
|
||||||
|
|
||||||
|
|
||||||
# configure logging
|
# configure logging
|
||||||
def configure_logging(level):
|
def configure_logging(level):
|
||||||
root_logger = logging.getLogger()
|
root_logger = logging.getLogger()
|
||||||
|
@ -30,7 +31,8 @@ def configure_logging(level):
|
||||||
ch = logging.StreamHandler()
|
ch = logging.StreamHandler()
|
||||||
ch.setLevel(level)
|
ch.setLevel(level)
|
||||||
# create formatter
|
# 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
|
# add formatter to ch
|
||||||
ch.setFormatter(formatter)
|
ch.setFormatter(formatter)
|
||||||
# add ch to logger
|
# add ch to logger
|
||||||
|
@ -44,6 +46,10 @@ logger = logging.getLogger(__name__)
|
||||||
# initialize database
|
# initialize database
|
||||||
database.setup()
|
database.setup()
|
||||||
|
|
||||||
|
# routes
|
||||||
|
logger.debug('imported: %s ' % api.__name__)
|
||||||
|
logger.debug('imported: %s ' % mail.__name__)
|
||||||
|
|
||||||
# start processor
|
# start processor
|
||||||
template_path = os.path.abspath(os.path.join(current_path, 'templates'))
|
template_path = os.path.abspath(os.path.join(current_path, 'templates'))
|
||||||
processor.start(template_path)
|
processor.start(template_path)
|
||||||
|
|
|
@ -6,7 +6,6 @@ import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
import chardet
|
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
from app.models.site import Site
|
from app.models.site import Site
|
||||||
from app.models.comment import Comment
|
from app.models.comment import Comment
|
||||||
|
@ -37,7 +36,7 @@ class Processor(Thread):
|
||||||
new_comment(msg['data'])
|
new_comment(msg['data'])
|
||||||
elif msg['request'] == 'new_mail':
|
elif msg['request'] == 'new_mail':
|
||||||
reply_comment_email(msg['data'])
|
reply_comment_email(msg['data'])
|
||||||
#elif req['type'] == 'unsubscribe':
|
# elif req['type'] == 'unsubscribe':
|
||||||
# unsubscribe_reader(req['email'], req['article'])
|
# unsubscribe_reader(req['email'], req['article'])
|
||||||
else:
|
else:
|
||||||
logger.info("throw unknown request " + str(msg))
|
logger.info("throw unknown request " + str(msg))
|
||||||
|
@ -67,8 +66,8 @@ def new_comment(data):
|
||||||
|
|
||||||
# add a row to Comment table
|
# add a row to Comment table
|
||||||
comment = Comment(site=site, url=url, author_name=author_name,
|
comment = Comment(site=site, url=url, author_name=author_name,
|
||||||
author_site=author_site, author_email=author_email,
|
author_site=author_site, author_email=author_email,
|
||||||
content=message, created=created, published=None)
|
content=message, created=created, published=None)
|
||||||
comment.save()
|
comment.save()
|
||||||
|
|
||||||
# render email body template
|
# render email body template
|
||||||
|
@ -83,17 +82,18 @@ def new_comment(data):
|
||||||
''
|
''
|
||||||
)
|
)
|
||||||
comment_text = '\n'.join(comment_list)
|
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
|
# send email
|
||||||
# TODO subject should embed a key
|
|
||||||
subject = '%s: [%d:%s]' % (site.name, comment.id, token)
|
subject = '%s: [%d:%s]' % (site.name, comment.id, token)
|
||||||
mail(site.admin_email, subject, email_body)
|
mail(site.admin_email, subject, email_body)
|
||||||
|
|
||||||
# TODO support subscription
|
|
||||||
# Reader subscribes to further comments
|
# Reader subscribes to further comments
|
||||||
#if subscribe and email:
|
if subscribe and author_email:
|
||||||
# subscribe_reader(email, article, url)
|
# TODO support subscription
|
||||||
|
# subscribe_reader(email, article, url)
|
||||||
|
pass
|
||||||
|
|
||||||
logger.debug("new comment processed ")
|
logger.debug("new comment processed ")
|
||||||
|
|
||||||
|
@ -118,8 +118,6 @@ def reply_comment_email(data):
|
||||||
logger.warn('ignore corrupted email')
|
logger.warn('ignore corrupted email')
|
||||||
return
|
return
|
||||||
|
|
||||||
# TODO validate chardet decoding is no more needed
|
|
||||||
#message = decode_best_effort(message)
|
|
||||||
if not message:
|
if not message:
|
||||||
logger.warn('ignore empty email')
|
logger.warn('ignore empty email')
|
||||||
return
|
return
|
||||||
|
@ -143,12 +141,12 @@ def reply_comment_email(data):
|
||||||
|
|
||||||
# TODO manage subscriptions
|
# TODO manage subscriptions
|
||||||
# notify reader once comment is published
|
# notify reader once comment is published
|
||||||
#reader_email, article_url = get_email_metadata(message)
|
# reader_email, article_url = get_email_metadata(message)
|
||||||
#if reader_email:
|
# if reader_email:
|
||||||
# notify_reader(reader_email, article_url)
|
# notify_reader(reader_email, article_url)
|
||||||
|
|
||||||
# notify subscribers every time a new comment is published
|
# notify subscribers every time a new comment is published
|
||||||
#notify_subscribers(article)
|
# notify_subscribers(article)
|
||||||
|
|
||||||
|
|
||||||
def get_email_metadata(message):
|
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)
|
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):
|
def mail(to_email, subject, message):
|
||||||
|
|
||||||
headers = {'Content-Type': 'application/json; charset=utf-8'}
|
headers = {'Content-Type': 'application/json; charset=utf-8'}
|
||||||
|
|
|
@ -4,7 +4,7 @@ DEBUG = True
|
||||||
|
|
||||||
LANG = "fr"
|
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"
|
DB_URL = "sqlite:///db.sqlite"
|
||||||
|
|
||||||
MAIL_URL = "http://localhost:8025/mbox"
|
MAIL_URL = "http://localhost:8025/mbox"
|
||||||
|
|
Loading…
Add table
Reference in a new issue