FastAPI(六十六)实战开发《在线课程学习系统》接口开发--用户注册接口开发
在前面我们分析了接口的设计,那么我们现在做接口的开发。
我们先去设计下pydantic用户参数的校验
from pydantic import BaseModel
from typing import Optional class UserBase(BaseModel):
username: str class UserCreate(UserBase):
"""
请求模型验证:
username:
password:
"""
password: str
role: int
jobnum: Optional[int] = None
studentnum: Optional[int] = None
sex: str = "男"
age: int
接着,我们去设计对应的crud,操作对应的数据库。
from sqlalchemy.orm import Session
from models.models import *
from models.schemas import *
def get_user(db: Session, user_id: int):
return db.query(User).filter(User.id == user_id,User.status==False).first()
# 新建用户
def db_create_user(db: Session, user: UserCreate):
roles = db.query(Role).filter(Role.name == user.role).first()
db_user = User(**user.dict())
db_user.role=roles.id
db.add(db_user)
db.commit() # 提交保存到数据库中
db.refresh(db_user) # 刷新
return db_user def get_user_username(db: Session, username: str):
return db.query(User).filter(User.username == username,User.status==False).first()
接下来,我们看下注册接口的逻辑
1.校验参数是否合规
2.查询用户名是否存在
3.密码加密
4.保存到数据库
我们根据我们的逻辑去开发我们的接口。
from fastapi import APIRouter, Request
from fastapi import Depends, HTTPException, Header
from models.crud import *
from models.get_db import get_db
from jose import JWTError, jwt
from passlib.context import CryptContext
from config import *
from common.jsontools import *
from common.logs import logger
usersRouter = APIRouter()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password) def get_password_hash(password):
return pwd_context.hash(password)
# 新建用户
@usersRouter.post("/create", tags=["users"])
def create_user(user: UserCreate, db: Session = Depends(get_db)): logger.info("创建用户")
if len(user.username)<8 or len(user.username)>16:
return reponse(code=100106,message="用户名长度应该是8-16位",data="")
if user.age<18:
return reponse(code=100103, message="年纪大小不符合", data="")
if (user.role == "学生" and user.studentnum is None) or (user.role == "教师" and user.jobnum is None) or (
user.role not in ["教师", '学生']):
return reponse(code=100102, message="身份和对应号不匹配", data="")
db_crest = get_user_username(db, user.username)
if db_crest:
return reponse(code=100104, message="用户名重复", data="")
try:
user.password = get_password_hash(user.password)
except Exception as e:
logger.exception(e)
return reponse(code=100105, data="", message="密码加密失败")
try:
user=db_create_user(db=db, user=user)
logger.success("创建用户成功")
return reponse(code=200,data={'user':user.username},message="success")
except Exception as e:
logger.exception(e)
return reponse(code=100101, data="", message="注册失败")
在启动的时候,我们需要在main.py下注册对应的路由。
from fastapi import FastAPI
from routers.user import usersRouter
app = FastAPI()
app.include_router(usersRouter, prefix="/user", tags=['users'])
然后对应的启动的
这样我们就完成了注册的接口的开发。这里的知识点用到了jose,passlib,这里如果不太熟悉,可以查看FastAPI 学习之路(三十)使用(哈希)密码和 JWT Bearer 令牌的 OAuth2

FastAPI(六十六)实战开发《在线课程学习系统》接口开发--用户注册接口开发的更多相关文章
- FastAPI(六十二)实战开发《在线课程学习系统》需求分析
前言 基础的分享我们已经分享了六十篇,那么我们这次分享开始将用一系列的文章分享实战课程.我们分享的系统是在线学习系统.我们会分成不同的模块进行分享.我们的目的是带着大家去用fastapi去实战一次,开 ...
- FastAPI(六十九)实战开发《在线课程学习系统》接口开发--修改密码
之前我们分享了FastAPI(六十八)实战开发<在线课程学习系统>接口开发--用户 个人信息接口开发.这次我们去分享实战开发<在线课程学习系统>接口开发--修改密码 我们梳理一 ...
- FastAPI(六十八)实战开发《在线课程学习系统》接口开发--用户 个人信息接口开发
在之前的文章:FastAPI(六十七)实战开发<在线课程学习系统>接口开发--用户登陆接口开发,今天实战:用户 个人信息接口开发. 在开发个人信息接口的时候,我们要注意了,因为我们不一样的 ...
- FastAPI(六十三)实战开发《在线课程学习系统》梳理系统需要接口
针对上一篇FastAPI(六十二)实战开发<在线课程学习系统>需求分析需求的功能,我们对需要的接口进行梳理,大概的规划出来现有的接口,作为我们第一版的接口的设计出版,然后我们根据设计的接口 ...
- FastAPI(六十七)实战开发《在线课程学习系统》接口开发--用户登陆接口开发
接上一篇文章FastAPI(六十六)实战开发<在线课程学习系统>接口开发--用户注册接口开发.这次我们分享实际开发--用户登陆接口开发. 我们先来梳理下逻辑 1.查询用户是否存在2.校验密 ...
- FastAPI(七十)实战开发《在线课程学习系统》接口开发--留言功能开发
在之前的文章:FastAPI(六十九)实战开发<在线课程学习系统>接口开发--修改密码,这次分享留言功能开发 我们能梳理下对应的逻辑 1.校验用户是否登录 2.校验留言的用户是否存在 3. ...
- FastAPI(七十四)实战开发《在线课程学习系统》接口开发-- 删除留言
之前文章FastAPI(七十三)实战开发<在线课程学习系统>接口开发-- 回复留言,那么我们这次分享删除留言接口的开发 可以对留言进行删除,这里的删除,我们使用的是逻辑的删除,不是物理删除 ...
- FastAPI(七十二)实战开发《在线课程学习系统》接口开发-- 留言列表开发
之前我们分享了FastAPI(七十一)实战开发<在线课程学习系统>接口开发-- 查看留言,这次我们分享留言列表开发. 列表获取,也需要登录,根据登录用户来获取对应的留言.逻辑梳理如下. 1 ...
- FastAPI(七十三)实战开发《在线课程学习系统》接口开发-- 回复留言
之前文章分享FastAPI(七十二)实战开发<在线课程学习系统>接口开发-- 留言列表开发,这次我们分享如何回复留言 按照惯例,我们还是去分析这里面的逻辑. 1.判断用户是否登录 2.用户 ...
- FastAPI(七十一)实战开发《在线课程学习系统》接口开发-- 查看留言
之前FastAPI(七十)实战开发<在线课程学习系统>接口开发--留言功能开发分享了留言开发,这次我们分享查看留言 梳理这里的逻辑,这个接口要依赖登录. 1.判断用户是否登录 2.判断对应 ...
随机推荐
- Wireshark DTN解析器拒绝服务漏洞
受影响系统:Wireshark Wireshark 2.2.0 - 2.2.1Wireshark Wireshark 2.0.0 - 2.0.7描述:CVE(CAN) ID: CVE-2016-937 ...
- springcloud学习00-开发工具相关准备
用maven构建springcloud项目,目录结构(图片来源:https://blog.csdn.net/qq_36688143/article/details/82755492) 1.maven ...
- sql高级手工注入
非常重要:首先在网站找到管理入口,否则,呵呵就算有用户名和密码,找不到入口,也是白玩.. 注入时,注意通过改变大小写.编码.转换等方式躲过系统检查,顺利执行语句!!! (一)数字型注入 正常步骤: 1 ...
- ElasticSearch7.3 学习之定制动态映射(dynamic mapping)
1.dynamic mapping ElasticSearch中有一个非常重要的特性--动态映射,即索引文档前不需要创建索引.类型等信息,在索引的同时会自动完成索引.类型.映射的创建. 当ES在文档中 ...
- SpringAOP--动态数据源
前言 通过注解和AOP,实现主从数据源的切换. 示例 首先项目布局: 1:实体类,与数据库表的映射 @Data @Builder public class UserBean { private Lon ...
- 作为 务注册中心,Eureka比Zookeeper好在哪里?
(1)Eureka保证的是可用性和分区容错性,Zookeeper 保证的是一致性和分区容错性 . (2)Eureka还有一种自我保护机制,如果在15分钟内超过85%的节点都没有正常的心跳,那么Eure ...
- Oracle入门基础(八)一一数据处理
SQL> SQL的类型 SQL> 1.DML(Data Manipulation Language 数据操作语言): select insert update delete SQL> ...
- 简述 synchronized 和 java.util.concurrent.locks.Lock 的异同?
Lock 是 Java 5 以后引入的新的 API,和关键字 synchronized 相比主要相同点: Lock 能完成 synchronized 所实现的所有功能:主要不同点:Lock 有比 sy ...
- java中的异常体系?throw和throws的区别?
一.java中的异常体系 Thorwable类(表示可抛出)是所有异常和错误的超类,两个直接子类为Error和Exception,分别表示错误和异常.其中异常类Exception又分为运行时异常(Ru ...
- sleep 方法和 wait 方法有什么区别?
这个问题常问,sleep 方法和 wait 方法都可以用来放弃 CPU 一定的时间,不同点 在于如果线程持有某个对象的监视器,sleep 方法不会放弃这个对象的监视器, wait 方法会放弃这个对象的 ...