2022/1/23
This commit is contained in:
+42
-2
@@ -1,3 +1,43 @@
|
|||||||
from flask_sqlalchemy import SQLAlchemy
|
import datetime
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
db: SQLAlchemy = SQLAlchemy()
|
from flask import request
|
||||||
|
from flask_sqlalchemy import SQLAlchemy, BaseQuery
|
||||||
|
|
||||||
|
|
||||||
|
class Query(BaseQuery):
|
||||||
|
|
||||||
|
def all_json(self) -> list:
|
||||||
|
print(self)
|
||||||
|
return [dict(i) for i in self.all()]
|
||||||
|
|
||||||
|
def soft_delete(self):
|
||||||
|
return self.update({"delete_at": datetime.datetime.now()})
|
||||||
|
|
||||||
|
def logic_all(self):
|
||||||
|
return self.filter_by(delete_at=None).all()
|
||||||
|
|
||||||
|
def layui_paginate(self):
|
||||||
|
"""
|
||||||
|
layui表格分页
|
||||||
|
page
|
||||||
|
limit
|
||||||
|
"""
|
||||||
|
return self.paginate(page=request.args.get('page', type=int),
|
||||||
|
per_page=request.args.get('limit', type=int),
|
||||||
|
error_out=False)
|
||||||
|
|
||||||
|
def layui_paginate_json(self) -> Tuple[list, int]:
|
||||||
|
"""
|
||||||
|
layui表格分页
|
||||||
|
page
|
||||||
|
limit
|
||||||
|
"""
|
||||||
|
p = self.paginate(page=request.args.get('page', type=int),
|
||||||
|
per_page=request.args.get('limit', type=int),
|
||||||
|
error_out=False)
|
||||||
|
|
||||||
|
return [dict(i) for i in p.items], p.total
|
||||||
|
|
||||||
|
|
||||||
|
db = SQLAlchemy(query_class=Query)
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: d51040631f21
|
||||||
|
Revises: 74133ba99af7
|
||||||
|
Create Date: 2022-01-22 23:30:04.372043
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'd51040631f21'
|
||||||
|
down_revision = '74133ba99af7'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('admin_test',
|
||||||
|
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False, comment='ID'),
|
||||||
|
sa.Column('name', sa.String(length=20), nullable=True, comment='名'),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_table('admin_test')
|
||||||
|
# ### end Alembic commands ###
|
||||||
@@ -8,8 +8,15 @@ class User(BaseModel):
|
|||||||
username = Column(String(20), comment='用户名')
|
username = Column(String(20), comment='用户名')
|
||||||
|
|
||||||
|
|
||||||
|
class Test(BaseModel):
|
||||||
|
__tablename__ = 'admin_test'
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True, comment='ID')
|
||||||
|
name = Column(String(20), comment='名')
|
||||||
|
|
||||||
|
|
||||||
class Userschema(PModel):
|
class Userschema(PModel):
|
||||||
id: int
|
id: int
|
||||||
username: str
|
username: str
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
orm_mode = True
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from common.view import Blueprint,View
|
from common.view import Blueprint, View
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
|
|
||||||
from pydantic import BaseModel, ValidationError, constr, parse_obj_as
|
from pydantic import BaseModel, ValidationError, constr, parse_obj_as
|
||||||
|
|
||||||
from modules.sys.models.user import User, Userschema
|
from entrance.extend.orm import db
|
||||||
|
from modules.sys.models.user import User, Userschema, Test
|
||||||
|
|
||||||
user = Blueprint('sys', __name__, url_prefix='/user')
|
user = Blueprint('sys', __name__, url_prefix='/user')
|
||||||
|
|
||||||
@@ -13,7 +14,6 @@ user = Blueprint('sys', __name__, url_prefix='/user')
|
|||||||
@user.route('/')
|
@user.route('/')
|
||||||
class UserView(View):
|
class UserView(View):
|
||||||
def get(self):
|
def get(self):
|
||||||
u = User.query.all()
|
data, total = db.session.query(User.id, User.username).layui_paginate_json()
|
||||||
# m = parse_obj_as(List[User], u)
|
print(data)
|
||||||
|
return jsonify(data)
|
||||||
return '1'
|
|
||||||
|
|||||||
Reference in New Issue
Block a user