mirror of https://github.com/kianby/stacosys
Replace DB layer Peewee by PyDal
parent
bafc0af92c
commit
1522f2826d
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=False)
|
||||
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,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