コード例 #1
0
def order_create(storeId, customerID):
    order_fields = order_schema.load(request.json)

    new_order = Order()
    new_order.order_placed = order_fields["order_placed"]
    cart = order_fields["cart"]
    for item in cart:
        item_query = Product.query.filter_by(id=item).first()
        new_order.orders_products.append(item_query)
        db.session.commit()
    new_order.customer_id = customerID

    customer = Customer.query.filter_by(id=customerID).first()
    if not customer:
        return abort(400, description="Incorrect customer")

    customer.order.append(new_order)
    db.session.commit()

    return jsonify(order_schema.dump(new_order))
コード例 #2
0
def seed_db():
    from models.User import User
    from models.Store import Store
    from models.Product import Product
    from models.Customer import Customer
    from models.Order import Order
    from main import bcrypt
    from faker import Faker

    faker = Faker()
    users = []
    stores = []
    products = []
    customers = []

    for i in range(5):
        user = User()
        user.email = f"test{i+1}@test.com"
        user.password = bcrypt.generate_password_hash("123456").decode("utf-8")
        user.isAdmin = False
        db.session.add(user)
        users.append(user)

        db.session.commit()

        store = Store()
        store.storename = faker.bs()
        store.firstname = faker.first_name()
        store.lastname = faker.last_name()
        store.user_id = users[i].id
        db.session.add(store)
        stores.append(store)

        db.session.commit()

        for j in range(5):
            product = Product()
            product.title = faker.numerify(text="Duck ###")
            product.price = faker.random_int(min=5, max=200, step=5)
            product.store_id = stores[i].id
            db.session.add(product)
            products.append(product)

        db.session.commit()

        for j in range(5):
            customer = Customer()
            customer.firstname = faker.first_name()
            customer.lastname = faker.last_name()
            customer.email = faker.ascii_email()
            customer.phone = faker.phone_number()
            customer.store_id = stores[i].id
            db.session.add(customer)
            customers.append(customer)

            db.session.commit()

            for k in range(5):
                order = Order()
                order.order_placed = choice([True, False])
                order.customer_id = choice(customers).id
                db.session.add(order)

                db.session.commit()

                for m in range(3):
                    order.orders_products.append(choice(products))

                    db.session.commit()

        customers = []

    db.session.commit()

    print("Tables seeded")