提交 18d86321 编辑于 作者: Libcat's avatar Libcat 👷
浏览文件

新增数据库初始化脚本

上级 956e1906
from typing import List, Optional
from fastapi import APIRouter
from fastapi import APIRouter, Response
import apischemas as api
from database import *
......@@ -35,7 +35,7 @@ async def create_one(item: api.ProductTypeCreate):
async def delete_one(id: int):
dbitem: Product = Product.get_or_none(Product.id == id)
if dbitem:
dbitem.delete_instance()
return api.Response(succ=True, msg="删除成功")
dbitem.delete_instance(recursive=True)
return Response("删除成功", 200)
else:
return api.Response(succ=False, msg="该产品类型不存在")
\ No newline at end of file
return Response("该项目不存在", 404)
\ No newline at end of file
......@@ -13,4 +13,5 @@
from .models import *
from .database import *
from .crud import *
\ No newline at end of file
from .crud import *
from . import config
\ No newline at end of file
host = "localhost"
user = "root"
password = "root"
port = 3306
database = "wenyuanmall"
\ No newline at end of file
......@@ -12,14 +12,15 @@
from playhouse.pool import PooledMySQLDatabase
from .models import *
from . import config
__all__=('init_database', 'reset_database', 'connect', 'close', 'init_data', 'dbconnection')
dbconnection = PooledMySQLDatabase('wenyuanmall',
dbconnection = PooledMySQLDatabase(config.database,
max_connections=10,
stale_timeout=300,
user='root',
password='root')
user=config.user,
password=config.password)
registed_models = (
ProductType, Product, User, UserAddress, Order, OrderItems, CarItems,
......@@ -54,41 +55,4 @@ def connect():
def close():
dbconnection.close()
def init_data():
"""生成初始数据"""
__check_database()
init_database()
with dbconnection.atomic():
User.create(username='admin',
password='admin',
is_admin=True,
email='admin@wenyuan.com')
User.create(username='user',
password='user',
is_admin=False,
email='user@wenyuan.com')
type1 = ProductType.create(name='关于爱情')
type2 = ProductType.create(name='关于友情')
ProductType.create(name='关于亲情')
ProductType.create(name='多肉绿植')
ProductType.create(name='花束自选')
Product.create(
name="红玫瑰",
price=9.9,
inventory=17,
description=
"红色玫瑰的花语是我爱你,代表着热恋、希望和你泛起激情的爱。 它因花语和寓意非常美好且花朵也十分娇艳美丽,所以成为每年情人节最受欢迎的鲜花,是情侣间表达爱意最好的花束。",
product_type=type1,
image="redrose.png")
Product.create(name="白玫瑰",
price=8.8,
inventory=8,
description="白玫瑰花语:纯洁,浪漫,求爱之花,以及我足以与你相配。",
product_type=type2,
image="whiterose.png")
dbconnection.close()
\ No newline at end of file
......@@ -21,6 +21,7 @@ __all__ = ('ProductType', 'Product', 'User', 'UserAddress', 'Order',
class ProductType(Model):
"""商品类型"""
name = CharField()
flexi = BooleanField(default=False, verbose_name="是否是自选花束", help_text="是否是自选花束")
# 商品
......
from database import *
import pymysql
import sys
# 建立数据库
conn = pymysql.connect(host=config.host,user=config.user, password=config.password, charset='utf8')
cursor= conn.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS wenyuanmall DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;")
conn.close()
# 创建一些数据
dbconnection.connect(True)
if dbconnection.table_exists("product"):
print("Abort: 数据库已经存在,请不要重复初始化!")
sys.exit(0)
init_database()
with dbconnection.atomic():
User.create(username='admin',
password='admin',
is_admin=True,
email='admin@wenyuan.com')
User.create(username='user',
password='user',
is_admin=False,
email='user@wenyuan.com')
type1 = ProductType.create(name='关于爱情')
type2 = ProductType.create(name='关于友情')
ProductType.create(name='关于亲情')
ProductType.create(name='多肉绿植')
ProductType.create(name='花束自选')
Product.create(
name="红玫瑰",
price=9.9,
inventory=17,
description=
"红色玫瑰的花语是我爱你,代表着热恋、希望和你泛起激情的爱。 它因花语和寓意非常美好且花朵也十分娇艳美丽,所以成为每年情人节最受欢迎的鲜花,是情侣间表达爱意最好的花束。",
product_type=type1,
image="redrose.png")
Product.create(name="白玫瑰",
price=8.8,
inventory=8,
description="白玫瑰花语:纯洁,浪漫,求爱之花,以及我足以与你相配。",
product_type=type2,
image="whiterose.png")
dbconnection.close()
print("OK")
\ No newline at end of file
支持 Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册