add dao
This commit is contained in:
parent
5ad049f618
commit
92e6a8965d
6 changed files with 33 additions and 23 deletions
3
run.py
3
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
|
||||
|
|
|
@ -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)
|
||||
|
|
0
stacosys/db/__init__.py
Normal file
0
stacosys/db/__init__.py
Normal file
18
stacosys/db/dao.py
Normal file
18
stacosys/db/dao.py
Normal file
|
@ -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()
|
||||
|
||||
|
||||
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Reference in a new issue