From bf1447a3a939cb86156ac1938436637bf4e55e91 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sat, 17 Jul 2021 09:44:58 +0200 Subject: [PATCH] add dao --- run.py | 3 +-- stacosys/core/cron.py | 25 +++++++++++++------------ stacosys/db/__init__.py | 0 stacosys/db/dao.py | 18 ++++++++++++++++++ stacosys/{core => db}/database.py | 0 stacosys/model/comment.py | 10 +--------- 6 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 stacosys/db/__init__.py create mode 100644 stacosys/db/dao.py rename stacosys/{core => db}/database.py (100%) diff --git a/run.py b/run.py index 63c0f81..3496f48 100644 --- a/run.py +++ b/run.py @@ -7,10 +7,9 @@ import argparse import logging from stacosys.conf.config import Config, ConfigParameter -from stacosys.core import database +from stacosys.db import database from stacosys.core.rss import Rss from stacosys.core.mailer import Mailer -from stacosys.core.mailer import SSLSMTPHandler from stacosys.interface import app from stacosys.interface import api from stacosys.interface import form diff --git a/stacosys/core/cron.py b/stacosys/core/cron.py index ff498cc..0cd59e1 100644 --- a/stacosys/core/cron.py +++ b/stacosys/core/cron.py @@ -10,6 +10,7 @@ from stacosys.model.comment import Comment from stacosys.model.email import Email from stacosys.core.rss import Rss from stacosys.core.mailer import Mailer +from stacosys.db import dao logger = logging.getLogger(__name__) @@ -29,40 +30,40 @@ def _reply_comment_email(lang, mailer: Mailer, rss: Rss, email: Email, site_toke m = re.search(r"\[(\d+)\:(\w+)\]", email.subject) if not m: - logger.warn("ignore corrupted email. No token %s" % email.subject) + logger.warning("ignore corrupted email. No token %s" % email.subject) return comment_id = int(m.group(1)) token = m.group(2) if token != site_token: - logger.warn("ignore corrupted email. Unknown token %d" % comment_id) + logger.warning("ignore corrupted email. Unknown token %d" % comment_id) return # retrieve site and comment rows - comment = Comment.get_by_id(comment_id) + comment = Comment if not comment: - logger.warn("unknown comment %d" % comment_id) + logger.warning("unknown comment %d" % comment_id) return True if comment.published: - logger.warn("ignore already published email. token %d" % comment_id) + logger.warning("ignore already published email. token %d" % comment_id) return if not email.plain_text_content: - logger.warn("ignore empty email") + logger.warning("ignore empty email") return # safe logic: no answer or unknown answer is a go for publishing - if email.plain_text_content[:2].upper() in ("NO"): + if email.plain_text_content[:2].upper() == "NO": logger.info("discard comment: %d" % comment_id) comment.delete_instance() new_email_body = templater.get_template(lang, Template.DROP_COMMENT).render( original=email.plain_text_content ) if not mailer.send(email.from_addr, "Re: " + email.subject, new_email_body): - logger.warn("minor failure. cannot send rejection mail " + email.subject) + logger.warning("minor failure. cannot send rejection mail " + email.subject) else: # save publishing datetime - comment.publish() + dao.publish(comment) logger.info("commit comment: %d" % comment_id) # rebuild RSS @@ -73,7 +74,7 @@ def _reply_comment_email(lang, mailer: Mailer, rss: Rss, email: Email, site_toke original=email.plain_text_content ) if not mailer.send(email.from_addr, "Re: " + email.subject, new_email_body): - logger.warn("minor failure. cannot send approval email " + email.subject) + logger.warning("minor failure. cannot send approval email " + email.subject) return True @@ -100,6 +101,6 @@ def submit_new_comment(lang, site_name, site_token, site_admin_email, mailer): logger.debug("new comment processed ") # notify site admin and save notification datetime - comment.notify_site_admin() + dao.notify_site_admin(comment) else: - logger.warn("rescheduled. send mail failure " + subject) + logger.warning("rescheduled. send mail failure " + subject) diff --git a/stacosys/db/__init__.py b/stacosys/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stacosys/db/dao.py b/stacosys/db/dao.py new file mode 100644 index 0000000..ddf95cc --- /dev/null +++ b/stacosys/db/dao.py @@ -0,0 +1,18 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- +from datetime import datetime + +from stacosys.model.comment import Comment + + +def notify_site_admin(comment: Comment): + comment.notified = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + comment.save() + + +def publish(comment: Comment): + comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + comment.save() + + + diff --git a/stacosys/core/database.py b/stacosys/db/database.py similarity index 100% rename from stacosys/core/database.py rename to stacosys/db/database.py diff --git a/stacosys/model/comment.py b/stacosys/model/comment.py index e586b25..48b3e49 100644 --- a/stacosys/model/comment.py +++ b/stacosys/model/comment.py @@ -5,7 +5,7 @@ from peewee import CharField from peewee import TextField from peewee import DateTimeField from datetime import datetime -from stacosys.core.database import BaseModel +from stacosys.db.database import BaseModel class Comment(BaseModel): @@ -17,11 +17,3 @@ class Comment(BaseModel): author_site = CharField(default="") author_gravatar = CharField(default="") content = TextField() - - def notify_site_admin(self): - self.notified = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - self.save() - - def publish(self): - self.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - self.save()