mirror of https://github.com/kianby/stacosys
commit
bedf8b7160
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from pydal import DAL, Field
|
||||
|
||||
|
||||
class Database:
|
||||
|
||||
db_dal = DAL()
|
||||
|
||||
def configure(self, db_uri):
|
||||
self.db_dal = DAL(db_uri, migrate=db_uri.startswith("sqlite:memory"))
|
||||
self.db_dal.define_table(
|
||||
"comment",
|
||||
Field("url"),
|
||||
Field("created", type="datetime"),
|
||||
Field("notified", type="datetime"),
|
||||
Field("published", type="datetime"),
|
||||
Field("author_name"),
|
||||
Field("author_site"),
|
||||
Field("author_gravatar"),
|
||||
Field("content", type="text"),
|
||||
)
|
||||
|
||||
def get(self):
|
||||
return self.db_dal
|
||||
|
||||
|
||||
database = Database()
|
||||
db = database.get
|
@ -1,67 +1,80 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
# pylint: disable=singleton-comparison
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from stacosys.db import db
|
||||
from stacosys.model.comment import Comment
|
||||
|
||||
TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
|
||||
|
||||
|
||||
def find_comment_by_id(comment_id):
|
||||
return Comment.get_by_id(comment_id)
|
||||
return db().comment(comment_id)
|
||||
|
||||
|
||||
def notify_comment(comment: Comment):
|
||||
comment.notified = datetime.now().strftime(TIME_FORMAT)
|
||||
comment.save()
|
||||
db()(db().comment.id == comment.id).update(notified=datetime.now())
|
||||
db().commit()
|
||||
|
||||
|
||||
def publish_comment(comment: Comment):
|
||||
comment.published = datetime.now().strftime(TIME_FORMAT)
|
||||
comment.save()
|
||||
db()(db().comment.id == comment.id).update(published=datetime.now())
|
||||
db().commit()
|
||||
|
||||
|
||||
def delete_comment(comment: Comment):
|
||||
comment.delete_instance()
|
||||
db()(db().comment.id == comment.id).delete()
|
||||
db().commit()
|
||||
|
||||
|
||||
def find_not_notified_comments():
|
||||
return Comment.select().where(Comment.notified.is_null())
|
||||
return db()(db().comment.notified == None).select()
|
||||
|
||||
|
||||
def find_not_published_comments():
|
||||
return Comment.select().where(Comment.published.is_null())
|
||||
return db()(db().comment.published == None).select()
|
||||
|
||||
|
||||
def find_published_comments_by_url(url):
|
||||
return (
|
||||
Comment.select(Comment)
|
||||
.where((Comment.url == url) & (Comment.published.is_null(False)))
|
||||
.order_by(+Comment.published)
|
||||
return db()((db().comment.url == url) & (db().comment.published != None)).select(
|
||||
orderby=db().comment.published
|
||||
)
|
||||
|
||||
|
||||
def count_published_comments(url):
|
||||
return (
|
||||
Comment.select(Comment)
|
||||
.where((Comment.url == url) & (Comment.published.is_null(False)))
|
||||
.count()
|
||||
db()((db().comment.url == url) & (db().comment.published != None)).count()
|
||||
if url
|
||||
else Comment.select(Comment).where(Comment.published.is_null(False)).count()
|
||||
else db()(db().comment.published != None).count()
|
||||
)
|
||||
|
||||
|
||||
def find_recent_published_comments():
|
||||
return db()(db().comment.published != None).select(
|
||||
orderby=~db().comment.published, limitby=(0, 10)
|
||||
)
|
||||
|
||||
|
||||
def create_comment(url, author_name, author_site, author_gravatar, message):
|
||||
created = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
comment = Comment(
|
||||
row = db().comment.insert(
|
||||
url=url,
|
||||
author_name=author_name,
|
||||
author_site=author_site,
|
||||
author_gravatar=author_gravatar,
|
||||
content=message,
|
||||
created=created,
|
||||
created=datetime.now(),
|
||||
notified=None,
|
||||
published=None,
|
||||
)
|
||||
comment.save()
|
||||
return comment
|
||||
db().commit()
|
||||
return Comment(
|
||||
id=row.id,
|
||||
url=row.url,
|
||||
author_name=row.author_name,
|
||||
author_site=row.author_site,
|
||||
author_gravatar=row.author_gravatar,
|
||||
content=row.content,
|
||||
created=row.created,
|
||||
notified=row.notified,
|
||||
published=row.published,
|
||||
)
|
||||
|
@ -1,26 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
# pylint: disable=import-outside-toplevel
|
||||
|
||||
from peewee import Model
|
||||
from playhouse.db_url import SqliteDatabase
|
||||
|
||||
db = SqliteDatabase(None)
|
||||
|
||||
|
||||
class BaseModel(Model):
|
||||
class Meta:
|
||||
database = db
|
||||
|
||||
|
||||
def setup(db_url):
|
||||
db.init(db_url)
|
||||
db.connect()
|
||||
|
||||
from stacosys.model.comment import Comment
|
||||
|
||||
db.create_tables([Comment], safe=True)
|
||||
|
||||
|
||||
def get_db():
|
||||
return db
|
@ -1,9 +1,43 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import logging
|
||||
|
||||
import background
|
||||
from flask import Flask
|
||||
|
||||
from stacosys.db import dao
|
||||
from stacosys.service import config, mailer
|
||||
from stacosys.service.configuration import ConfigParameter
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Set the secret key to some random bytes. Keep this really secret!
|
||||
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@background.task
|
||||
def submit_new_comment(comment):
|
||||
site_url = config.get(ConfigParameter.SITE_URL)
|
||||
comment_list = (
|
||||
f"Web admin interface: {site_url}/web/admin",
|
||||
"",
|
||||
f"author: {comment.author_name}",
|
||||
f"site: {comment.author_site}",
|
||||
f"date: {comment.created}",
|
||||
f"url: {comment.url}",
|
||||
"",
|
||||
comment.content,
|
||||
"",
|
||||
)
|
||||
email_body = "\n".join(comment_list)
|
||||
|
||||
# send email to notify admin
|
||||
site_name = config.get(ConfigParameter.SITE_NAME)
|
||||
subject = f"STACOSYS {site_name}"
|
||||
if mailer.send(subject, email_body):
|
||||
logger.debug("new comment processed")
|
||||
# save notification datetime
|
||||
dao.notify_comment(comment)
|
||||
|
@ -1,17 +1,19 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
from peewee import CharField, DateTimeField, TextField
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from stacosys.db.database import BaseModel
|
||||
|
||||
|
||||
class Comment(BaseModel):
|
||||
url = CharField()
|
||||
created = DateTimeField()
|
||||
notified = DateTimeField(null=True, default=None)
|
||||
published = DateTimeField(null=True, default=None)
|
||||
author_name = CharField()
|
||||
author_site = CharField(default="")
|
||||
author_gravatar = CharField(default="")
|
||||
content = TextField()
|
||||
@dataclass
|
||||
class Comment:
|
||||
id: int = 0
|
||||
url: str = ""
|
||||
created: Optional[datetime] = None
|
||||
notified: Optional[datetime] = None
|
||||
published: Optional[datetime] = None
|
||||
author_name: str = ""
|
||||
author_site: str = ""
|
||||
author_gravatar: str = ""
|
||||
content: str = ""
|
||||
|
Loading…
Reference in New Issue