Replace database methods with peewee ORM #39

Merged
osilkin98 merged 26 commits from orm-rewrite into master 2020-04-03 23:40:43 +02:00
Showing only changes of commit aee12eba54 - Show all commits

View file

@ -12,24 +12,15 @@ from src.server.validation import is_valid_base_comment
from src.misc import clean
def get_database_connection():
# for now it's an sqlite database
db = SqliteDatabase()
return db
def get_database_connection(dbms, db_name, **params):
if dbms == 'mysql':
return MySQLDatabase(db_name, **params)
else:
# return SqliteDatabase('/home/oleg/PycharmProjects/comment-server/database/default_pw.db')
return SqliteDatabase(db_name)
database_proxy = DatabaseProxy()
database = get_database_connection()
database_proxy.initialize(database)
class BaseModel(Model):
class Meta:
database = database_proxy
class Channel(BaseModel):
class Channel(Model):
claim_id = TextField(column_name='ClaimId', primary_key=True)
name = TextField(column_name='Name')
@ -37,7 +28,7 @@ class Channel(BaseModel):
table_name = 'CHANNEL'
class Comment(BaseModel):
class Comment(Model):
comment = TextField(column_name='Body')
channel = ForeignKeyField(
backref='comments',
@ -47,7 +38,7 @@ class Comment(BaseModel):
null=True
)
comment_id = TextField(column_name='CommentId', primary_key=True)
is_hidden = BooleanField(column_name='IsHidden', constraints=[SQL("DEFAULT FALSE")])
is_hidden = BooleanField(column_name='IsHidden', constraints=[SQL("DEFAULT 0")])
claim_id = TextField(column_name='LbryClaimId')
parent = ForeignKeyField(
column_name='ParentId',
@ -63,7 +54,7 @@ class Comment(BaseModel):
class Meta:
table_name = 'COMMENT'
indexes = (
(('author', 'comment_id'), False),
(('channel', 'comment_id'), False),
(('claim_id', 'comment_id'), False),
)
@ -203,7 +194,6 @@ def create_comment(comment: str = None, claim_id: str = None,
timestamp = int(time.time())
comment_id = create_comment_id(comment, channel_id, timestamp)
with database_proxy.atomic():
new_comment = Comment.create(
claim_id=claim_id,
comment_id=comment_id,
@ -223,7 +213,6 @@ def delete_comment(comment_id: str) -> bool:
except DoesNotExist as e:
raise ValueError from e
else:
with database_proxy.atomic():
return 0 < comment.delete_instance(True, delete_nullable=True)
@ -233,7 +222,6 @@ def edit_comment(comment_id: str, new_comment: str, new_sig: str, new_ts: str) -
except DoesNotExist as e:
raise ValueError from e
else:
with database_proxy.atomic():
comment.comment = new_comment
comment.signature = new_sig
comment.signing_ts = new_ts
@ -245,7 +233,6 @@ def edit_comment(comment_id: str, new_comment: str, new_sig: str, new_ts: str) -
def set_hidden_flag(comment_ids: typing.List[str], hidden=True) -> bool:
# sets `is_hidden` flag for all `comment_ids` to the `hidden` param
with database_proxy.atomic():
update = (Comment
.update(is_hidden=hidden)
.where(Comment.comment_id.in_(comment_ids)))