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.判断对应 ...
随机推荐
- tp5 商城商品模型删除
1:控制器代码 public function delete($id) { //验证id是否正确 $id if (!intval($id)) { return getJsonData(10010, ' ...
- KDT入门小讲
KDT入门小讲 为了搞讲课两天搞出来的PPT,质量不高,随便看看 附:讲课用PPT 链接: https://pan.baidu.com/s/1qHea0fEhscAsQh8-Yu_j_A 提取码: 4 ...
- CSAPP CH7链接的应用:静动态库制作与神奇的库打桩机制
目录 创建静态库 创建动态库 库打桩机制 编译时打桩: 链接时打桩 运行时打桩 运行时打桩的printf与malloc循环调用debug 使用LD_PRELOAD对任意可执行程序调用运行时打桩 总结 ...
- LGP5363题解
感觉博弈题都是高大上神秘结论... 感谢@KaiSuoShuTong 开锁疏通愿意教我这题的博弈部分/qq 考虑每次移动棋子,实际上是有一车 \(a_i\),每次操作相当于令 \(a_i-c,a_{i ...
- python 矩阵顺时针旋转90度
# 4*4矩阵旋转90度 def matrix_transposition(data): for index,row in enumerate(data): for col in range(inde ...
- Linux服务器上搭建Centos7.0+Apache+php+Mysql网站
一.安装Linux系统 1.1虚拟机搭建Linux Centos7.0版本,搭建过程省略. 二. 安装apache.php.mysql.php-gd等组件. 2.1安装Apache服务程序(apach ...
- weblogic重要漏洞记录
(PS:之前在freebuf发过,这里直接复制过来的,所以有些图片会有水印) 前言 T3协议存在多个反序列化漏洞CVE-2015-4852/CVE-2016-0638/CVE-2016-3510/CV ...
- python3 爬虫--Chrome以及 Chromedriver安装配置
1终端 将下载源加入到列表 sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/ ...
- Linux 性能调优都有哪几种方法?
1.Disabling daemons (关闭 daemons). 2.Shutting down the GUI (关闭 GUI). 3.Changing kernel paramete ...
- Xshell 连接虚拟机OS Linux 设置静态ip ,网络配置中无VmWare8 的解决办法
前序:最近开始研究Hadoop平台的搭建,故在本机上安装了VMware workstation pro,并创建了Linux虚拟机(centos系统),为了方便本机和虚拟机间的切换,准备使用Xshell ...