removes inherited base database in favor of binding api

This commit is contained in:
Oleg Silkin 2020-03-30 21:58:07 -04:00
parent a22b4a9162
commit aee12eba54

View file

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