WIP
This commit is contained in:
parent
637b00261a
commit
3c4a25e5ad
17 changed files with 197 additions and 272 deletions
|
|
@ -1,84 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
from flask import Flask
|
||||
from conf import config
|
||||
from jsonschema import validate
|
||||
from flask_apscheduler import APScheduler
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# add current path and parent path to syspath
|
||||
current_path = os.path.dirname(__file__)
|
||||
parent_path = os.path.abspath(os.path.join(current_path, os.path.pardir))
|
||||
paths = [current_path, parent_path]
|
||||
for path in paths:
|
||||
if path not in sys.path:
|
||||
sys.path.insert(0, path)
|
||||
|
||||
# more imports
|
||||
import database
|
||||
import processor
|
||||
from interface import api
|
||||
from interface import form
|
||||
|
||||
# configure logging
|
||||
def configure_logging(level):
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(level)
|
||||
ch = logging.StreamHandler()
|
||||
ch.setLevel(level)
|
||||
# create formatter
|
||||
formatter = logging.Formatter(
|
||||
'[%(asctime)s] %(name)s %(levelname)s %(message)s')
|
||||
# add formatter to ch
|
||||
ch.setFormatter(formatter)
|
||||
# add ch to logger
|
||||
root_logger.addHandler(ch)
|
||||
|
||||
logging_level = (20, 10)[config.general['debug']]
|
||||
configure_logging(logging_level)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class Config(object):
|
||||
JOBS = [
|
||||
{
|
||||
'id': 'fetch_mail',
|
||||
'func': 'core.cron:fetch_mail_answers',
|
||||
'trigger': 'interval',
|
||||
'seconds': 120
|
||||
},
|
||||
{
|
||||
'id': 'submit_new_comment',
|
||||
'func': 'core.cron:submit_new_comment',
|
||||
'trigger': 'interval',
|
||||
'seconds': 60
|
||||
},
|
||||
]
|
||||
|
||||
# initialize database
|
||||
database.setup()
|
||||
|
||||
# start processor
|
||||
template_path = os.path.abspath(os.path.join(current_path, '../templates'))
|
||||
processor.start(template_path)
|
||||
|
||||
# cron
|
||||
app.config.from_object(Config())
|
||||
scheduler = APScheduler()
|
||||
scheduler.init_app(app)
|
||||
scheduler.start()
|
||||
|
||||
# tune logging level
|
||||
if not config.general['debug']:
|
||||
logging.getLogger('werkzeug').level = logging.WARNING
|
||||
|
||||
logger.info("Start Stacosys application")
|
||||
|
||||
app.run(host=config.http['host'],
|
||||
port=config.http['port'],
|
||||
debug=config.general['debug'], use_reloader=False)
|
||||
|
|
@ -3,26 +3,29 @@
|
|||
|
||||
import logging
|
||||
import time
|
||||
from core import app
|
||||
from core import processor
|
||||
from models.comment import Comment
|
||||
from core import mailer
|
||||
from core import templater
|
||||
from model.comment import Comment
|
||||
from model.comment import Site
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def fetch_mail_answers():
|
||||
|
||||
logger.info('DEBUT POP MAIL')
|
||||
logger.info("DEBUT POP MAIL")
|
||||
time.sleep(80)
|
||||
logger.info('FIN POP MAIL')
|
||||
#data = request.get_json()
|
||||
#logger.debug(data)
|
||||
logger.info("FIN POP MAIL")
|
||||
# data = request.get_json()
|
||||
# logger.debug(data)
|
||||
|
||||
# processor.enqueue({'request': 'new_mail', 'data': data})
|
||||
|
||||
#processor.enqueue({'request': 'new_mail', 'data': data})
|
||||
|
||||
def submit_new_comment():
|
||||
|
||||
for comment in Comment.select().where(Comment.notified.is_null()):
|
||||
# render email body template
|
||||
|
||||
comment_list = (
|
||||
"author: %s" % comment.author_name,
|
||||
"site: %s" % comment.author_site,
|
||||
|
|
@ -33,15 +36,10 @@ def submit_new_comment():
|
|||
"",
|
||||
)
|
||||
comment_text = "\n".join(comment_list)
|
||||
email_body = get_template("new_comment").render(url=url, comment=comment_text)
|
||||
email_body = templater.get_template("new_comment").render(url=comment.url, comment=comment_text)
|
||||
|
||||
if clientip:
|
||||
client_ips[comment.id] = clientip
|
||||
|
||||
# send email
|
||||
subject = "STACOSYS %s: [%d:%s]" % (site.name, comment.id, token)
|
||||
mailer.send_mail(site.admin_email, subject, email_body)
|
||||
logger.debug("new comment processed ")
|
||||
|
||||
def get_template(name):
|
||||
return env.get_template(config.general["lang"] + "/" + name + ".tpl")
|
||||
site = Site.select().where(Site.id == Comment.site).get()
|
||||
# send email
|
||||
subject = "STACOSYS %s: [%d:%s]" % (site.name, comment.id, site.token)
|
||||
mailer.send_mail(site.admin_email, subject, email_body)
|
||||
logger.debug("new comment processed ")
|
||||
|
|
|
|||
|
|
@ -2,26 +2,15 @@
|
|||
# -*- coding: UTF-8 -*-
|
||||
|
||||
from conf import config
|
||||
import functools
|
||||
from playhouse.db_url import connect
|
||||
|
||||
|
||||
def get_db():
|
||||
return connect(config.general['db_url'])
|
||||
return connect(config.get(config.DB_URL))
|
||||
|
||||
|
||||
def provide_db(func):
|
||||
def setup():
|
||||
from model.site import Site
|
||||
from model.comment import Comment
|
||||
|
||||
@functools.wraps(func)
|
||||
def new_function(*args, **kwargs):
|
||||
return func(get_db(), *args, **kwargs)
|
||||
|
||||
return new_function
|
||||
|
||||
|
||||
@provide_db
|
||||
def setup(db):
|
||||
from models.site import Site
|
||||
from models.comment import Comment
|
||||
|
||||
db.create_tables([Site, Comment], safe=True)
|
||||
get_db().create_tables([Site, Comment], safe=True)
|
||||
|
|
|
|||
|
|
@ -10,10 +10,8 @@ import json
|
|||
from datetime import datetime
|
||||
from threading import Thread
|
||||
from queue import Queue
|
||||
from jinja2 import Environment
|
||||
from jinja2 import FileSystemLoader
|
||||
from models.site import Site
|
||||
from models.comment import Comment
|
||||
from model.site import Site
|
||||
from model.comment import Comment
|
||||
from helpers.hashing import md5
|
||||
from conf import config
|
||||
from core import mailer
|
||||
|
|
|
|||
16
app/core/templater.py
Normal file
16
app/core/templater.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
from jinja2 import Environment
|
||||
from jinja2 import FileSystemLoader
|
||||
from conf import config
|
||||
|
||||
current_path = os.path.dirname(__file__)
|
||||
template_path = os.path.abspath(os.path.join(current_path, "../templates"))
|
||||
env = Environment(loader=FileSystemLoader(template_path))
|
||||
|
||||
|
||||
def get_template(name):
|
||||
return env.get_template(config.general["lang"] + "/" + name + ".tpl")
|
||||
Loading…
Add table
Add a link
Reference in a new issue