1
This commit is contained in:
		
						commit
						2c9129afd1
					
				
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
__pycache__/*.pyc
 | 
			
		||||
							
								
								
									
										145
									
								
								api.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										145
									
								
								api.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,145 @@
 | 
			
		||||
from operator import add
 | 
			
		||||
from flask import jsonify,request,session
 | 
			
		||||
from model import AAA, app,db,User
 | 
			
		||||
from flask_jwt_extended import create_access_token,JWTManager,jwt_required,get_jwt_claims, get_jwt_identity
 | 
			
		||||
import hashlib
 | 
			
		||||
 | 
			
		||||
app.config['JWT_SECRET_KEY'] = 'JWT'
 | 
			
		||||
jwt = JWTManager(app)
 | 
			
		||||
 | 
			
		||||
@app.route("/register",methods=["POST"])    #注册
 | 
			
		||||
def register():
 | 
			
		||||
    req_data=request.get_json()
 | 
			
		||||
    new_username=req_data.get("username")
 | 
			
		||||
    new_password=req_data.get("password")
 | 
			
		||||
    new_token = create_access_token(identity=new_username)
 | 
			
		||||
    new_user=User.query.filter(User.username==new_username).first()
 | 
			
		||||
 | 
			
		||||
    if not all([new_username, new_password]):
 | 
			
		||||
        return jsonify({"code":400,"msg":"请将账号密码填写完整"} )
 | 
			
		||||
 | 
			
		||||
    if new_user is not None :
 | 
			
		||||
        return jsonify({"code":400,"msg":"账号已存在"} )
 | 
			
		||||
 | 
			
		||||
    add_user = User(username=new_username,password=new_password,token=new_token)
 | 
			
		||||
    db.session.add(add_user)
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
    return jsonify({
 | 
			
		||||
        "code":200,
 | 
			
		||||
        "msg":"用户注册成功",
 | 
			
		||||
        "token":new_token
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route("/login",methods=["POST"])       #登录
 | 
			
		||||
def login():
 | 
			
		||||
    req_data=request.get_json()
 | 
			
		||||
    username=req_data.get("username")
 | 
			
		||||
    password=req_data.get("password")
 | 
			
		||||
    # password_md5=hashlib.md5(password.encode()).hexdigest()
 | 
			
		||||
    user=User.query.filter(User.username==username).first()
 | 
			
		||||
 | 
			
		||||
    if not all([username, password]):
 | 
			
		||||
        return jsonify({"code":400,"msg":"参数不完整"} )
 | 
			
		||||
 | 
			
		||||
    # if user is None or password_md5 != user.password:
 | 
			
		||||
    if user is None or password != user.password:
 | 
			
		||||
        return jsonify({"code":400,"msg":"账号或密码错误"} )
 | 
			
		||||
       
 | 
			
		||||
 | 
			
		||||
    token = create_access_token(identity=user.username)
 | 
			
		||||
    User.query.filter(User.username==username).update({"token":token})
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
  
 | 
			
		||||
    return jsonify({
 | 
			
		||||
        "code":200,
 | 
			
		||||
        "msg": '登录成功',
 | 
			
		||||
        "data":{
 | 
			
		||||
            "token":token
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
# str(AAA())
 | 
			
		||||
# data = [
 | 
			
		||||
#     {
 | 
			
		||||
#         "id":1,
 | 
			
		||||
#         "web":"xx"
 | 
			
		||||
#     }
 | 
			
		||||
# ]
 | 
			
		||||
 | 
			
		||||
# jsonify(data=data)
 | 
			
		||||
 | 
			
		||||
@app.route("/get",methods=["GET"])  #查
 | 
			
		||||
@jwt_required
 | 
			
		||||
def Get():
 | 
			
		||||
    get_website=AAA.query.all() 
 | 
			
		||||
    b=[]
 | 
			
		||||
    for i in get_website:
 | 
			
		||||
        a=dict() 
 | 
			
		||||
        a["id"]=i.id
 | 
			
		||||
        a["website"]=i.website
 | 
			
		||||
        b.append(a)
 | 
			
		||||
 | 
			
		||||
    return jsonify(b)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route("/add",methods=["POST"]) #增
 | 
			
		||||
@jwt_required
 | 
			
		||||
def Add():
 | 
			
		||||
    try:
 | 
			
		||||
        # claims = get_jwt_claims()
 | 
			
		||||
        # current_user = get_jwt_identity()
 | 
			
		||||
        add_website=request.get_json()
 | 
			
		||||
        new_website= add_website.get("website")
 | 
			
		||||
        add = AAA(website=new_website)
 | 
			
		||||
        db.session.add(add)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        return jsonify({"code":200,"msg":"数据添加成功"} )
 | 
			
		||||
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        return jsonify({"code":400,"msg":"格式不正确"} )
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
@app.route("/modify",methods=["POST"])  #改
 | 
			
		||||
@jwt_required
 | 
			
		||||
def Modify():
 | 
			
		||||
    try:
 | 
			
		||||
        get_date=request.get_json()
 | 
			
		||||
        modify_id= get_date.get("id")
 | 
			
		||||
        modify_website= get_date.get("website")
 | 
			
		||||
        id=AAA.query.filter(AAA.id==modify_id).first()
 | 
			
		||||
        if id is None :
 | 
			
		||||
            return jsonify({"code":400,"msg":"没有这项数据"} )
 | 
			
		||||
        AAA.query.filter(AAA.id==modify_id).update({"website":modify_website})
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
 | 
			
		||||
        return jsonify({"code":200,"msg":"数据修改成功"} )
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        return jsonify({"code":400,"msg":"格式不正确"} )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route("/delete",methods=["POST"])  #删
 | 
			
		||||
@jwt_required
 | 
			
		||||
def Delete():
 | 
			
		||||
    try:
 | 
			
		||||
        get_date=request.get_json()
 | 
			
		||||
        delete_id= get_date.get("id")
 | 
			
		||||
        id=AAA.query.filter(AAA.id==delete_id).first()
 | 
			
		||||
        if id is None :
 | 
			
		||||
            return jsonify({"code":400,"msg":"没有这项数据"} )
 | 
			
		||||
        AAA.query.filter(AAA.id==delete_id).delete()
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
 | 
			
		||||
        return jsonify({"code":200,"msg":"数据删除成功"} )
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        return jsonify({"code":400,"msg":"格式不正确"} )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__=="__main__":
 | 
			
		||||
    app.run(host="0.0.0.0")
 | 
			
		||||
# host="0.0.0.0"
 | 
			
		||||
							
								
								
									
										37
									
								
								model.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								model.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,37 @@
 | 
			
		||||
from enum import unique
 | 
			
		||||
from flask_sqlalchemy import SQLAlchemy
 | 
			
		||||
from flask import Flask
 | 
			
		||||
 | 
			
		||||
app = Flask(__name__)
 | 
			
		||||
 | 
			
		||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:/// "+ "DB.db"    #配置
 | 
			
		||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
 | 
			
		||||
app.config["SECRET_KEY"] = "jjjsks"
 | 
			
		||||
 | 
			
		||||
db = SQLAlchemy(app)   #app作为参数实例化一个sqlalchemy类的对象
 | 
			
		||||
 | 
			
		||||
class User (db.Model):
 | 
			
		||||
    __tablename__="users"  
 | 
			
		||||
    id=db.Column(db.Integer,primary_key=True)
 | 
			
		||||
    username=db.Column(db.String(32),nullable=False,unique=True)
 | 
			
		||||
    password=db.Column(db.String(64),nullable=False)
 | 
			
		||||
    token=db.Column(db.String(1024))
 | 
			
		||||
 | 
			
		||||
class AAA (db.Model):
 | 
			
		||||
    __tablename__="aaa"
 | 
			
		||||
    id=db.Column(db.Integer,primary_key=True)
 | 
			
		||||
    website=db.Column(db.String(128))
 | 
			
		||||
    
 | 
			
		||||
    def AAA_to_dict():
 | 
			
		||||
        get_db=AAA.query.all()
 | 
			
		||||
        AAA_dict={
 | 
			
		||||
            "id":AAA.id,
 | 
			
		||||
            "website":AAA.website
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return AAA_dict
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    db.create_all()
 | 
			
		||||
    # db.drop_all()
 | 
			
		||||
							
								
								
									
										62
									
								
								practice.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								practice.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,62 @@
 | 
			
		||||
from model import db, User,AAA
 | 
			
		||||
from flask_jwt_extended import create_access_token,jwt_required
 | 
			
		||||
import hashlib
 | 
			
		||||
# a = User(username="abc",password="123")
 | 
			
		||||
# db.session.add(a)
 | 
			
		||||
# db.session.commit()
 | 
			
		||||
 | 
			
		||||
# b = User(username="董月坤",password="1234")
 | 
			
		||||
# c = User(username="仇亚恒",password="1234")
 | 
			
		||||
# db.session.add_all([a,b,c])
 | 
			
		||||
# db.session.commit()
 | 
			
		||||
 | 
			
		||||
# d = AAA(website="www.baidu.com")
 | 
			
		||||
# e = AAA(website="www.123")
 | 
			
		||||
# f = AAA(website="www.456")
 | 
			
		||||
# db.session.add_all([d,e,f])
 | 
			
		||||
# db.session.commit()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
# u=User.query.get(1)    #一个
 | 
			
		||||
# print(u.username)
 | 
			
		||||
 | 
			
		||||
# u=User.query.all()      #全部
 | 
			
		||||
# for i in u:
 | 
			
		||||
#     print(i.username)
 | 
			
		||||
 | 
			
		||||
# username=my_json.get("username")
 | 
			
		||||
# user=User.query.filter_by(username="abc").first()
 | 
			
		||||
# print(user.username)
 | 
			
		||||
 | 
			
		||||
# user=User.query.filter(User.username=="abc").first()
 | 
			
		||||
# print(user.username)
 | 
			
		||||
 | 
			
		||||
# token = create_access_token(identity=user.username)
 | 
			
		||||
# print(token)
 | 
			
		||||
 | 
			
		||||
# md5=hashlib.md5("123".encode()).hexdigest()
 | 
			
		||||
# print(md5)
 | 
			
		||||
 | 
			
		||||
# User.query.filter(User.username=="abc").update({"password":"123"})
 | 
			
		||||
# db.session.commit()
 | 
			
		||||
 | 
			
		||||
# user = Student.query.filter(Student.name == "李依").delete()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
get_website=AAA.query.all()
 | 
			
		||||
 | 
			
		||||
b=[]
 | 
			
		||||
for i in get_website:      #字典中key是唯一值 “id”相同 会覆盖掉只剩最后一项
 | 
			
		||||
    a=dict() 
 | 
			
		||||
    a["id"]=i.id
 | 
			
		||||
    a["website"]=i.website
 | 
			
		||||
    b.append(a)
 | 
			
		||||
print(b)
 | 
			
		||||
# print(b)
 | 
			
		||||
# b.append(a)
 | 
			
		||||
# print(b)   
 | 
			
		||||
# a=dict() 
 | 
			
		||||
# a[AAA.id]=AAA.website
 | 
			
		||||
# a[12]=2222
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user