mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-02-10 22:16:01 +08:00
feat(db): update model to support JSON
This commit is contained in:
@@ -160,8 +160,6 @@ class Base:
|
||||
def update(self, db: Session, payload: dict):
|
||||
payload = {k: v for k, v in payload.items() if v is not None}
|
||||
for key, value in payload.items():
|
||||
if ObjectUtils.is_obj(value):
|
||||
value = json.dumps(value)
|
||||
setattr(self, key, value)
|
||||
if inspect(self).detached:
|
||||
db.add(self)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from sqlalchemy import Column, Integer, String, Sequence
|
||||
from sqlalchemy import Column, Integer, String, Sequence, JSON
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import db_query, db_update, Base
|
||||
@@ -11,7 +11,7 @@ class PluginData(Base):
|
||||
id = Column(Integer, Sequence('id'), primary_key=True, index=True)
|
||||
plugin_id = Column(String, nullable=False, index=True)
|
||||
key = Column(String, index=True, nullable=False)
|
||||
value = Column(String)
|
||||
value = Column(JSON)
|
||||
|
||||
@staticmethod
|
||||
@db_query
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Column, Integer, String, Sequence
|
||||
from sqlalchemy import Column, Integer, String, Sequence, JSON
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import db_query, db_update, Base
|
||||
@@ -24,7 +24,7 @@ class SiteStatistic(Base):
|
||||
# 最后访问时间
|
||||
lst_mod_date = Column(String, default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
||||
# 耗时记录 Json
|
||||
note = Column(String)
|
||||
note = Column(JSON)
|
||||
|
||||
@staticmethod
|
||||
@db_query
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Column, Integer, String, Sequence, Float
|
||||
from sqlalchemy import Column, Integer, String, Sequence, Float, JSON
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import db_query, Base
|
||||
@@ -38,11 +38,11 @@ class SiteUserData(Base):
|
||||
# 下载体积
|
||||
leeching_size = Column(Float, default=0)
|
||||
# 做种人数, 种子大小 JSON
|
||||
seeding_info = Column(String)
|
||||
seeding_info = Column(JSON)
|
||||
# 未读消息
|
||||
message_unread = Column(Integer, default=0)
|
||||
# 未读消息内容 JSON
|
||||
message_unread_contents = Column(String)
|
||||
message_unread_contents = Column(JSON)
|
||||
# 错误信息
|
||||
err_msg = Column(String)
|
||||
# 更新日期
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from sqlalchemy import Column, Integer, String, Sequence
|
||||
from sqlalchemy import Column, Integer, String, Sequence, JSON
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import db_query, db_update, Base
|
||||
@@ -12,7 +12,7 @@ class SystemConfig(Base):
|
||||
# 主键
|
||||
key = Column(String, index=True)
|
||||
# 值
|
||||
value = Column(String, nullable=True)
|
||||
value = Column(JSON, nullable=True)
|
||||
|
||||
@staticmethod
|
||||
@db_query
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import Tuple, Any
|
||||
|
||||
from sqlalchemy import Boolean, Column, Integer, String, Sequence
|
||||
from sqlalchemy import Boolean, Column, Integer, String, Sequence, JSON
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.security import verify_password
|
||||
@@ -31,9 +31,9 @@ class User(Base):
|
||||
# otp秘钥
|
||||
otp_secret = Column(String, default=None)
|
||||
# 用户权限 json
|
||||
permissions = Column(String, default='')
|
||||
permissions = Column(JSON, default='')
|
||||
# 用户个性化设置 json
|
||||
settings = Column(String, default='')
|
||||
settings = Column(JSON, default='')
|
||||
|
||||
@staticmethod
|
||||
@db_query
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from sqlalchemy import Column, Integer, String, Sequence, UniqueConstraint, Index
|
||||
from sqlalchemy import Column, Integer, String, Sequence, UniqueConstraint, Index, JSON
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import db_query, db_update, Base
|
||||
@@ -14,7 +14,7 @@ class UserConfig(Base):
|
||||
# 配置键
|
||||
key = Column(String)
|
||||
# 值
|
||||
value = Column(String, nullable=True)
|
||||
value = Column(JSON, nullable=True)
|
||||
|
||||
__table_args__ = (
|
||||
# 用户名和配置键联合唯一
|
||||
|
||||
@@ -36,8 +36,6 @@ class PluginDataOper(DbOper):
|
||||
data = PluginData.get_plugin_data_by_key(self._db, plugin_id, key)
|
||||
if not data:
|
||||
return None
|
||||
if ObjectUtils.is_objstr(data.value):
|
||||
return json.loads(data.value)
|
||||
return data.value
|
||||
else:
|
||||
return PluginData.get_plugin_data(self._db, plugin_id)
|
||||
|
||||
@@ -7,7 +7,6 @@ from app.db.models import SiteIcon
|
||||
from app.db.models.site import Site
|
||||
from app.db.models.sitestatistic import SiteStatistic
|
||||
from app.db.models.siteuserdata import SiteUserData
|
||||
from app.utils.object import ObjectUtils
|
||||
|
||||
|
||||
class SiteOper(DbOper):
|
||||
@@ -125,9 +124,7 @@ class SiteOper(DbOper):
|
||||
else:
|
||||
# 不存在则插入
|
||||
for key, value in payload.items():
|
||||
if ObjectUtils.is_obj(value):
|
||||
payload[key] = json.dumps(value)
|
||||
SiteUserData(**payload).create(self._db)
|
||||
SiteUserData(**payload).create(self._db)
|
||||
return True, "更新站点用户数据成功"
|
||||
|
||||
def get_userdata_by_domain(self, domain: str, workdate: str = None) -> List[SiteUserData]:
|
||||
@@ -153,7 +150,7 @@ class SiteOper(DbOper):
|
||||
更新站点图标
|
||||
"""
|
||||
icon_base64 = f"data:image/ico;base64,{icon_base64}" if icon_base64 else ""
|
||||
siteicon = self.get_by_domain(domain)
|
||||
siteicon = self.get_icon_by_domain(domain)
|
||||
if not siteicon:
|
||||
SiteIcon(name=name, domain=domain, url=icon_url, base64=icon_base64).create(self._db)
|
||||
elif icon_base64:
|
||||
@@ -172,7 +169,7 @@ class SiteOper(DbOper):
|
||||
if sta:
|
||||
avg_seconds, note = None, {}
|
||||
if seconds is not None:
|
||||
note: dict = json.loads(sta.note or "{}")
|
||||
note: dict = sta.note or {}
|
||||
note[lst_date] = seconds or 1
|
||||
avg_times = len(note.keys())
|
||||
if avg_times > 10:
|
||||
|
||||
@@ -18,10 +18,7 @@ class SystemConfigOper(DbOper, metaclass=Singleton):
|
||||
"""
|
||||
super().__init__()
|
||||
for item in SystemConfig.list(self._db):
|
||||
if ObjectUtils.is_objstr(item.value):
|
||||
self.__SYSTEMCONF[item.key] = json.loads(item.value)
|
||||
else:
|
||||
self.__SYSTEMCONF[item.key] = item.value
|
||||
self.__SYSTEMCONF[item.key] = item.value
|
||||
|
||||
def set(self, key: Union[str, SystemConfigKey], value: Any):
|
||||
"""
|
||||
@@ -38,10 +35,6 @@ class SystemConfigOper(DbOper, metaclass=Singleton):
|
||||
else:
|
||||
conf.delete(self._db, conf.id)
|
||||
else:
|
||||
if ObjectUtils.is_obj(value):
|
||||
value = json.dumps(value)
|
||||
elif value is None:
|
||||
value = ''
|
||||
conf = SystemConfig(key=key, value=value)
|
||||
conf.create(self._db)
|
||||
|
||||
|
||||
@@ -18,8 +18,7 @@ class UserConfigOper(DbOper, metaclass=Singleton):
|
||||
"""
|
||||
super().__init__()
|
||||
for item in UserConfig.list(self._db):
|
||||
value = json.loads(item.value) if ObjectUtils.is_objstr(item.value) else item.value
|
||||
self.__set_config_cache(username=item.username, key=item.key, value=value)
|
||||
self.__set_config_cache(username=item.username, key=item.key, value=item.value)
|
||||
|
||||
def set(self, username: str, key: Union[str, UserConfigKey], value: Any):
|
||||
"""
|
||||
@@ -30,10 +29,6 @@ class UserConfigOper(DbOper, metaclass=Singleton):
|
||||
# 更新内存
|
||||
self.__set_config_cache(username=username, key=key, value=value)
|
||||
# 写入数据库
|
||||
if ObjectUtils.is_obj(value):
|
||||
value = json.dumps(value)
|
||||
elif value is None:
|
||||
value = ''
|
||||
conf = UserConfig.get_by_key(db=self._db, username=username, key=key)
|
||||
if conf:
|
||||
if value:
|
||||
|
||||
@@ -63,7 +63,7 @@ class SiteStatistic(BaseModel):
|
||||
# 最后修改时间
|
||||
lst_mod_date: Optional[str]
|
||||
# 备注
|
||||
note: Optional[str] = None
|
||||
note: Optional[dict] = None
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
Reference in New Issue
Block a user