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
|
#!/usr/bin/python
|
||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: UTF-8 -*-
|
||||||
|
# pylint: disable=singleton-comparison
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from stacosys.db import db
|
||||||
from stacosys.model.comment import Comment
|
from stacosys.model.comment import Comment
|
||||||
|
|
||||||
TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
|
|
||||||
|
|
||||||
|
|
||||||
def find_comment_by_id(comment_id):
|
def find_comment_by_id(comment_id):
|
||||||
return Comment.get_by_id(comment_id)
|
return db().comment(comment_id)
|
||||||
|
|
||||||
|
|
||||||
def notify_comment(comment: Comment):
|
def notify_comment(comment: Comment):
|
||||||
comment.notified = datetime.now().strftime(TIME_FORMAT)
|
db()(db().comment.id == comment.id).update(notified=datetime.now())
|
||||||
comment.save()
|
db().commit()
|
||||||
|
|
||||||
|
|
||||||
def publish_comment(comment: Comment):
|
def publish_comment(comment: Comment):
|
||||||
comment.published = datetime.now().strftime(TIME_FORMAT)
|
db()(db().comment.id == comment.id).update(published=datetime.now())
|
||||||
comment.save()
|
db().commit()
|
||||||
|
|
||||||
|
|
||||||
def delete_comment(comment: Comment):
|
def delete_comment(comment: Comment):
|
||||||
comment.delete_instance()
|
db()(db().comment.id == comment.id).delete()
|
||||||
|
db().commit()
|
||||||
|
|
||||||
|
|
||||||
def find_not_notified_comments():
|
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():
|
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):
|
def find_published_comments_by_url(url):
|
||||||
return (
|
return db()((db().comment.url == url) & (db().comment.published != None)).select(
|
||||||
Comment.select(Comment)
|
orderby=db().comment.published
|
||||||
.where((Comment.url == url) & (Comment.published.is_null(False)))
|
|
||||||
.order_by(+Comment.published)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def count_published_comments(url):
|
def count_published_comments(url):
|
||||||
return (
|
return (
|
||||||
Comment.select(Comment)
|
db()((db().comment.url == url) & (db().comment.published != None)).count()
|
||||||
.where((Comment.url == url) & (Comment.published.is_null(False)))
|
|
||||||
.count()
|
|
||||||
if url
|
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):
|
def create_comment(url, author_name, author_site, author_gravatar, message):
|
||||||
created = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
row = db().comment.insert(
|
||||||
comment = Comment(
|
|
||||||
url=url,
|
url=url,
|
||||||
author_name=author_name,
|
author_name=author_name,
|
||||||
author_site=author_site,
|
author_site=author_site,
|
||||||
author_gravatar=author_gravatar,
|
author_gravatar=author_gravatar,
|
||||||
content=message,
|
content=message,
|
||||||
created=created,
|
created=datetime.now(),
|
||||||
notified=None,
|
notified=None,
|
||||||
published=None,
|
published=None,
|
||||||
)
|
)
|
||||||
comment.save()
|
db().commit()
|
||||||
return comment
|
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
|
#!/usr/bin/python
|
||||||
# -*- coding: UTF-8 -*-
|
# -*- 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
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class Comment(BaseModel):
|
class Comment:
|
||||||
url = CharField()
|
id: int = 0
|
||||||
created = DateTimeField()
|
url: str = ""
|
||||||
notified = DateTimeField(null=True, default=None)
|
created: Optional[datetime] = None
|
||||||
published = DateTimeField(null=True, default=None)
|
notified: Optional[datetime] = None
|
||||||
author_name = CharField()
|
published: Optional[datetime] = None
|
||||||
author_site = CharField(default="")
|
author_name: str = ""
|
||||||
author_gravatar = CharField(default="")
|
author_site: str = ""
|
||||||
content = TextField()
|
author_gravatar: str = ""
|
||||||
|
content: str = ""
|
||||||
|
Loading…
Reference in New Issue