PEP8 compliance
This commit is contained in:
parent
58b499f9fe
commit
224ece6be1
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))
|
||||
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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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='')
|
||||
|
|
10
app/run.py
10
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)
|
||||
|
||||
|
|
|
@ -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'}
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Add table
Reference in a new issue