Skip to content

Commit

Permalink
2024年12月30日 整理SQLAlchemy和pydantic 大版本升级问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ss1917 committed Dec 30, 2024
1 parent d9d7e55 commit b6ce756
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 723 deletions.
45 changes: 0 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,51 +39,6 @@

- 系统日志 (从API网关获取日志,当然也可以自行从基类获取)

### 结构

```shell
├── doc
│   ├── data.sql
│   ├── deployment.md
│   ├── nginx_ops.conf
│   ├── requirements.txt
│   └── supervisor_ops.conf
├── docker-compose.yml
├── Dockerfile
├── __init__.py
├── libs
│   ├── base_handler.py
│   ├── __init__.py
│   ├── my_verify.py
│   └── utils.py
├── mg
│   ├── applications.py
│   ├── handlers
│   │   ├── app_mg_handler.py
│   │   ├── app_settings_handler.py
│   │   ├── components_v4_handler.py
│   │   ├── configs_init.py
│   │   ├── functions_v4_handler.py
│   │   ├── __init__.py
│   │   ├── login_handler.py
│   │   ├── menus_v4_handler.py
│   │   ├── notifications_handler.py
│   │   ├── roles_handler.py
│   │   ├── users_v4_handler.py
│   │   └── verify_handler.py
│   ├── __init__.py
│   └── subscribe.py
├── models
│   ├── admin.py
│   ├── app_config.py
│   ├── __init__.py
├── README.md
├── settings.py
└── startup.py
```

### 展示

### 用户管理

> 这部分文档主要用来介绍用户管理,它可以很精细的管理你的用户权限
Expand Down
16 changes: 10 additions & 6 deletions db_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
from websdk2.consts import const
from settings import settings as app_settings
from models.paas_model import Base as AppsBase
Expand All @@ -17,13 +18,16 @@

default_configs = app_settings[const.DB_CONFIG_ITEM][const.DEFAULT_DB_KEY]

engine = create_engine(
f'mysql+pymysql://{default_configs.get(const.DBUSER_KEY)}:'
f'{default_configs.get(const.DBPWD_KEY)}@{default_configs.get(const.DBHOST_KEY)}:'
f'{default_configs.get(const.DBPORT_KEY)}/{default_configs.get(const.DBNAME_KEY)}'
f'?charset=utf8mb4',
echo=True
url_object = URL.create(
drivername='mysql+pymysql',
username=default_configs.get(const.DBUSER_KEY),
password=default_configs.get(const.DBPWD_KEY),
host=default_configs.get(const.DBHOST_KEY),
port=int(default_configs.get(const.DBPORT_KEY)),
database=default_configs.get(const.DBNAME_KEY),
query={'charset': 'utf8mb4'}
)
engine = create_engine(url_object, echo=True)


def create():
Expand Down
2 changes: 1 addition & 1 deletion get_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import requests
from websdk2.db_context import DBContextV2 as DBContext
from websdk2.model_utils import insert_or_update
from libs.feature_model_utils import insert_or_update

from models.authority import Users
from settings import settings
Expand Down
29 changes: 3 additions & 26 deletions libs/feature_model_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env python
# -*-coding:utf-8-*-
"""
Author : shenshuo
Date : 2019年12月11日
Contact : [email protected]
Author : shenshuo
Date : 2024/12/30 20:28
Desc : models类
"""

Expand Down Expand Up @@ -35,30 +36,6 @@ def queryset_to_list(queryset, **kwargs) -> list:
return [model_to_dict(q) for q in queryset]


def GetInsertOrUpdateObj(cls: Type, str_filter: str, **kw) -> classmethod:
"""
cls: Model 类名
str_filter: filter的参数.eg:"name='name-14'" 必须设置唯一 支持 and or
**kw: 【属性、值】字典,用于构建新实例,或修改存在的记录
session.add(GetInsertOrUpdateObj(TableTest, "name='name-114'", age=33114, height=123.14, name='name-114'))
"""
with DBContext('r') as session:
existing = session.query(cls).filter(text(str_filter)).first()
if not existing:
res = cls()
for k, v in kw.items():
if hasattr(res, k):
setattr(res, k, v)
return res
else:
res = existing
for k, v in kw.items():
if hasattr(res, k):
setattr(res, k, v)

return res


def insert_or_update(cls: Type[DeclarativeMeta], str_filter: str, **kw) -> Union[None, DeclarativeMeta]:
"""
cls: Model 类名
Expand Down
22 changes: 9 additions & 13 deletions libs/feature_pydantic_utils.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Version : 0.0.8
Version : 0.0.9
Contact : [email protected]
Author : shenshuo
Date : 2021/1/26 20:28
Desc : https://github.com/tiangolo/pydantic-sqlalchemy
Date : 2024/12/30 20:28
Desc : https://github.com/tiangolo/pydantic-sqlalchemy
"""

####
from typing import Container, Optional, Type
from pydantic import BaseModel, create_model, ValidationError, ConfigDict


# from sqlalchemy.inspection import inspect
# from sqlalchemy.orm.properties import ColumnProperty


# 删除的时候一般只有id
class PydanticDel(BaseModel):
id: int
Expand All @@ -26,7 +22,7 @@ class PydanticDelList(BaseModel):
id_list: list[int]


def sqlalchemy_to_pydantic(db_model: Type, *, exclude: Container[str] = []) -> Type[BaseModel]:
def sqlalchemy_to_pydantic(db_model: Type, *, exclude: Container[str] = ()) -> Type[BaseModel]:
table = db_model.metadata.tables[db_model.__tablename__]
fields = {}
config = ConfigDict(from_attributes=True)
Expand All @@ -41,9 +37,9 @@ def sqlalchemy_to_pydantic(db_model: Type, *, exclude: Container[str] = []) -> T
elif hasattr(column.type, "python_type"):
python_type = column.type.python_type
assert python_type, f"Could not infer python_type for {column}"
default = None
if column.default is None and not column.nullable:
default = ...

default = ... if column.default is None and not column.nullable else None

fields[name] = (python_type, default)
pydantic_model = create_model(db_model.__name__, __config__=config, **fields)
return pydantic_model

return create_model(db_model.__name__, __config__=config, **fields)
146 changes: 0 additions & 146 deletions libs/sync_notice_user.py

This file was deleted.

2 changes: 1 addition & 1 deletion libs/sync_user_verift_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from websdk2.consts import const
from websdk2.db_context import DBContextV2 as DBContext
from websdk2.jwt_token import gen_md5
from websdk2.model_utils import insert_or_update
from libs.feature_model_utils import insert_or_update
from websdk2.tools import RedisLock, now_timestamp, convert

from libs.etcd import Etcd3Client
Expand Down
32 changes: 0 additions & 32 deletions mg/handlers/configs_init.py

This file was deleted.

Loading

0 comments on commit b6ce756

Please sign in to comment.