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