From 34f10baae22ce96f50c70d97cb97e9425db6de19 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sun, 3 Dec 2017 13:40:24 +0100 Subject: [PATCH] add private mode --- app/controllers/api.py | 4 +--- app/models/comment.py | 1 + app/run.py | 24 ++++++++++++++++-------- app/services/processor.py | 29 +++++++++++++++++++---------- config.py | 3 ++- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/app/controllers/api.py b/app/controllers/api.py index 0facc42..e03591d 100644 --- a/app/controllers/api.py +++ b/app/controllers/api.py @@ -7,7 +7,6 @@ from flask import request, jsonify, abort from app import app from app.models.site import Site from app.models.comment import Comment -from app.helpers.hashing import md5 from app.services import processor logger = logging.getLogger(__name__) @@ -31,8 +30,7 @@ def query_comments(): d['content'] = comment.content if comment.author_site: d['site'] = comment.author_site - if comment.author_email: - d['avatar'] = md5(comment.author_email.strip().lower()) + d['avatar'] = comment.author_gravatar d['date'] = comment.published.strftime("%Y-%m-%d %H:%M:%S") logger.debug(d) comments.append(d) diff --git a/app/models/comment.py b/app/models/comment.py index 1492085..9ebdfb7 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -17,6 +17,7 @@ class Comment(Model): author_name = CharField() author_email = CharField(default='') author_site = CharField(default='') + author_gravatar = CharField(default='') content = TextField() site = ForeignKeyField(Site, related_name='site') diff --git a/app/run.py b/app/run.py index 073cea2..1494fc9 100644 --- a/app/run.py +++ b/app/run.py @@ -4,7 +4,6 @@ import os import sys import logging -from werkzeug.contrib.fixers import ProxyFix from flask.ext.cors import CORS # add current path and parent path to syspath @@ -23,7 +22,6 @@ from app.controllers import api from app.controllers import form from app.controllers import report from app.controllers import mail -from app.controllers import reader from app import app @@ -49,26 +47,36 @@ logger = logging.getLogger(__name__) # initialize database database.setup() +#from app.helpers.hashing import md5 +#from app.models.comment import Comment +#for comment in Comment.select(): +# email = comment.author_email.strip().lower() +# if email: +# comment.author_gravatar = md5(email) +# comment.author_email = '' +# comment.save() + # routes logger.debug('imported: %s ' % api.__name__) logger.debug('imported: %s ' % mail.__name__) -logger.debug('imported: %s ' % reader.__name__) # start processor template_path = os.path.abspath(os.path.join(current_path, 'templates')) processor.start(template_path) -logger.info("Start Stacosys application") - -# enable CORS -cors = CORS(app, resources={r"/comments/*": {"origins": "*"}}) +# less feature in private mode +if not config.PRIVATE: + # enable CORS + cors = CORS(app, resources={r"/comments/*": {"origins": "*"}}) + from app.controllers import reader + logger.debug('imported: %s ' % reader.__name__) # tune logging level if not config.DEBUG: logging.getLogger('app.cors').level = logging.WARNING logging.getLogger('werkzeug').level = logging.WARNING -app.wsgi_app = ProxyFix(app.wsgi_app) +logger.info("Start Stacosys application") if __name__ == '__main__': diff --git a/app/services/processor.py b/app/services/processor.py index 2c18536..c2af6a8 100644 --- a/app/services/processor.py +++ b/app/services/processor.py @@ -10,9 +10,9 @@ from queue import Queue from jinja2 import Environment from jinja2 import FileSystemLoader from app.models.site import Site -from app.models.comment import Comment from app.models.reader import Reader from app.models.report import Report +from app.helpers.hashing import md5 import requests import json import config @@ -68,6 +68,13 @@ def new_comment(data): message = data.get('message', '') subscribe = data.get('subscribe', '') + # private mode: email contains gravar md5 hash + if config.PRIVATE: + author_gravatar = author_email + author_email = '' + else: + author_gravatar = md5(author_email.lower()) + # create a new comment row site = Site.select().where(Site.token == token).get() @@ -79,6 +86,7 @@ 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, + author_gravatar=author_gravatar, content=message, created=created, published=None) comment.save() @@ -104,7 +112,7 @@ def new_comment(data): mail(site.admin_email, subject, email_body) # Reader subscribes to further comments - if subscribe and author_email: + if not config.PRIVATE and subscribe and author_email: subscribe_reader(author_email, token, url) logger.debug("new comment processed ") @@ -163,14 +171,15 @@ def reply_comment_email(data): mail(from_email, 'Re: ' + subject, email_body) # notify reader once comment is published - reader_email = get_email_metadata(message) - if reader_email: - notify_reader(from_email, reader_email, comment.site.token, - comment.site.url, comment.url) - - # notify subscribers every time a new comment is published - notify_subscribed_readers( - comment.site.token, comment.site.url, comment.url) + if not config.PRIVATE: + reader_email = get_email_metadata(message) + if reader_email: + notify_reader(from_email, reader_email, comment.site.token, + comment.site.url, comment.url) + + # notify subscribers every time a new comment is published + notify_subscribed_readers( + comment.site.token, comment.site.url, comment.url) def late_reject_comment(id): diff --git a/config.py b/config.py index 04ac873..219e3e7 100644 --- a/config.py +++ b/config.py @@ -12,7 +12,6 @@ MAIL_URL = "http://localhost:8025/mbox" HTTP_ADDRESS = "127.0.0.1" HTTP_PORT = 8100 HTTP_WORKERS = 1 -CORS_ORIGIN = "*" SALT = "BRRJRqXgGpXWrgTidBPcixIThHpDuKc0" @@ -22,3 +21,5 @@ ROOT_URL = 'http://localhost:8100' RSS_URL_PROTO = 'http' RSS_FILE = 'comments.xml' + +PRIVATE = True \ No newline at end of file