Flask接口设计和数据库设计

Flask接口设计和数据库设计

1、技术概述,描述这个技术是做什么?学习该技术的原因,技术的难点在哪里。

  使用Flask框架进行程序后端的接口设计和数据库,之所以学习是为了完成团队项目中后端笔记部分的接口,难点是之前为接触过Flask,学习起来有一定难度,对路由和ORM等不了解。

2、技术详述,描述你是如何实现和使用该技术的,要求配合代码和流程图详细描述。

  • 1、先建note.py文件到时在里面写路由。
import json
import os
from datetime import datetime

from flask import Blueprint,request,current_app,send_from_directory
from flask_login import current_user,login_user,logout_user
from ..models.note import Note,File,Compliments
from ..extensions import db
from ..models.user import User
from ..utils import random_filename


note_bp = Blueprint('note',__name__)

@note_bp.before_request   #!@#!@#
def login_project():
    route = ['avatar','file']
    method = request.method
    ext = request.path
    flag = False

    for i in route :
        if i in ext :
            flag = True

    if method == 'GET' and flag :
        pass

    else :
        result = {}
        if current_user.is_authenticated == False:
            result['code'] = -1
            result['msg'] = '您当前未登录!'
            return json.dumps(result)

        else :
            id =current_user.get_id()
            user = User.query.get(id)

            if user.is_verify == False and user.is_admin == False:
                result['code'] = -2
                result['msg'] = '请先实名制认证!'
                return json.dumps(result)

@note_bp.route('',methods=['POST'])
def release():
    results = {}
    data = {}
    title = request.form.get('title')
    tag = request.form.get('tag')
    content = request.form.get('content')

    uid = int(current_user.get_id())

    note = Note(
        title = title,
        tag = tag,
        content = content,
        note_date = datetime.today(),
        user_id = uid
    )

    db.session.add(note)
    db.session.commit()

    data['id'] = note.id
    results['code'] = 0
    results['msg'] = '发布成功'
    results['data'] = data

    return json.dumps(results)

@note_bp.route('/published',methods=['GET'])
def my_published():
    results = {}
    data = []
    id = int(current_user.get_id())

    notes = Note.query.filter_by(user_id=id).order_by(Note.note_date.desc()).all()

    if notes != None :
        for item in notes:
            flag = True
            cc = Compliments.query.filter_by(note_id=item.id, user_id=id).first()
            if cc is None:
                flag = False

            d = {
                "note_id" : item.id,
                "title" : item.title,
                "tag" : item.tag,
                "content" : item.content[0:32] + '...' ,
                "compliments" : item.compliments,
                "flag" : flag,
                "note_date" : item.note_date.strftime('%Y-%m-%d'),
            }
            data.append(d)

    results['code'] = 0
    results['msg'] = 'success'
    results['data'] = data

    return json.dumps(results)

@note_bp.route('/search',methods=['GET'])
def search():
    results = {}
    Data = []
    data = []
    word = str(request.args.get('word'))

    notes = Note.query.order_by(Note.note_date.desc()).all()

    if word != None:
        words = word.split(' ')
        for i in words:
            for note in notes:
                if i in note.tag:
                    if note not in Data:
                        flag = True
                        cc = Compliments.query.filter_by(note_id=note.id, user_id=current_user.id).first()
                        if cc is None :
                            flag = False
                        d = {
                            "publisher_id": note.user_id,
                            'publisher_nickname': note.user.nickname,
                            'note_id' : note.id,
                            'title': note.title,
                            'tag': note.tag,
                            'content': note.content,
                            "note_date": note.note_date.strftime('%Y-%m-%d'),
                            "compliments" : note.compliments,
                            "flag" : flag
                        }

                        Data.append(note)
                        data.append(d)

        results['code'] = 0
        results['msg'] = "查找成功"
        results['data'] = data

    return json.dumps(results)

@note_bp.route('/<int:id>',methods=['GET'])
def note(id):
    results = {}
    note = Note.query.get(id)

    flag = True#current_user.id
    cc = Compliments.query.filter_by(note_id=id, user_id=current_user.id).first()
    if cc is None:
        flag = False

    data = {
        "publisher_id" : note.user_id,
        "title" : note.title,
        "tag" : note.tag,
        "content" : note.content,
        "note_date" : note.note_date.strftime('%Y-%m-%d'),
        "compliments" : note.compliments,
        "flag" : flag
    }

    results['code'] = 0
    results['msg'] = '查看成功'
    results['data'] = data

    return json.dumps(results)

@note_bp.route('/upload',methods=['POST'])
def upload():
    results = {}
    f = request.files.get('file')

    filename = random_filename(f.filename)

    f.save(os.path.join(current_app.config['FILE_PATH'], filename))

    file = File(
        filename=filename
    )

    db.session.add(file)
    db.session.commit()

    data = {
        "file_id":file.id
    }

    results['code'] = 0
    results['msg'] = '上传成功'
    results['data'] = data

    return json.dumps(results)

@note_bp.route('/file/<int:id>',methods=['GET'])
def get_file(id):
    file = File.query.get(id)
    filename = file.filename

    return send_from_directory(current_app.config['FILE_PATH'], filename)

@note_bp.route('/categories',methods=['GET'])
def categories():
    results = {}
    data = []
    notes = Note.query.all()
    for note in notes :
        if note.tag not in data:
            data.append(note.tag)

    results['code'] = 0
    results['msg'] = 'success'
    results['data'] = data

    return json.dumps(results)

@note_bp.route('/index',methods=['GET'])
def index():
    results={}
    data = []

    tag = request.args.get('tag')
    page = int(request.args.get('page'))
    each_page = int(request.args.get('each_page'))

    length = Note.query.filter_by(tag=tag).count()
    pagination = Note.query.filter_by(tag=tag).order_by(Note.note_date.desc()).paginate(page,per_page=each_page)
    notes = pagination.items

    for note in notes :
        flag = True
        cc = Compliments.query.filter_by(note_id=note.id, user_id=current_user.id).first()
        if cc is None:
            flag = False

        d = {}
        d["publisher_id"] = note.user_id
        d["publisher_nickname"] = note.user.nickname
        d["note_id"] = note.id
        d["title"] = note.title
        d["content"] = note.content[0:32] +'...'
        d["note_date"] = note.note_date.strftime('%Y-%m-%d')
        d["compliments"] = note.compliments
        d['flag'] = flag

        data.append(d)

    results['code'] = 0
    results['msg'] = '返回成功'
    results['data'] = data
    results['length'] = length

    return json.dumps(results)

@note_bp.route('/edit',methods=['POST'])
def edit():
    results = {}
    id = request.form.get('note_id')
    content = request.form.get('content')

    note = Note.query.get(id)
    note.content = content
    note.note_date = datetime.today()

    db.session.commit()

    results['code'] = 0
    results['msg'] = '修改成功'

    return json.dumps(results)

@note_bp.route('/compliments',methods=['POST'])
def compliments():
    results = {}

    id = request.form.get('note_id')
    uid = current_user.id

    cc = Compliments.query.filter_by(note_id=id, user_id=uid).first()
    if cc != None :
        results['code'] = 1
        results['msg'] = '你已经点过赞了'

        return json.dumps(results)

    c = Compliments(
        note_id = id,
        user_id = uid
    )

    note = Note.query.get(id)
    note.compliments += 1

    db.session.add(c)
    db.session.commit()

    results['code'] = 0
    results['msg'] = '点赞成功'

    return json.dumps(results)

@note_bp.route('/recomp',methods=['POST'])
def recomp():
    results = {}

    id = request.form.get('note_id')
    uid = current_user.id

    c = Compliments.query.filter_by(note_id = id,user_id = uid).first()

    note = Note.query.get(id)
    note.compliments -= 1

    db.session.delete(c)
    db.session.commit()

    results['code'] = 0
    results['msg'] = '取消点赞成功'

    return json.dumps(results)
  
  • 2、在modules里建notes.py。
from ..extensions import db
from ..models.user import User


class Note(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(32), nullable=False)  # 标题
    compliments = db.Column(db.Integer, default=0)  # 点赞数
    note_date = db.Column(db.TIMESTAMP, nullable=False)  # 笔记最近一次更新时间
    tag = db.Column(db.String(32),nullable=False)
    content = db.Column(db.Text)

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))  #谁发布的

    user = db.relationship('User', back_populates='notes')  # 用户
    comps = db.relationship('Compliments', back_populates='note')


class File(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    filename = db.Column(db.String(128),nullable=False,unique=True)

class Compliments(db.Model):
    id = db.Column(db.Integer, primary_key=True)

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # 谁点赞的
    note_id = db.Column(db.Integer, db.ForeignKey('note.id'))  # 谁点赞的

    note = db.relationship('Note', back_populates='comps')  # 用户
    user = db.relationship('User', back_populates='comps')  # 用户

  
  • 3、根据数据库设计用ORM实现即可。

3、技术使用中遇到的问题和解决过程。

  刚刚开始的时候对路由的概念不了解,所以先进行了学习,使用的书是《Flask Web开发:Python基于Web应用开发实战 第 2 版 》关于路由的定义

@app.route('/user/<name>') 
def user(name):     
return '<h1>Hello, {}!</h1>'.format(name)

了解之后直接就可以上手了,比较简单,数据库使用时碰到的问题是在使用db.create_all() 时出现了一些问题,原因是一些表已经存在了,但是使用它时不会重新创建或者更新相应的表,于是我先删除旧表再重新创建,然后就一切顺利

>>> db.drop_all() 
>>> db.create_all()

4、进行总结

  初学的时候感觉压力很大,因为时间赶,加上我觉得它难度比较大,后面逼着自己学习才发现这个技术还是比较简单的,中间碰到的问题通过百度和电子书查看实例都能够解决,所以说不要去害怕学习新的知识,只要用心打的去学习了,就会有收获。

5、列出参考文献、参考博客(标题、作者、链接)。

《Flask Web开发:Python基于Web应用开发实战 第 2 版 》
Flask中路由使用解析

猜你喜欢

转载自www.cnblogs.com/shenmw/p/13190554.html