Exemplo n.º 1
0
def index():
    if request.args.get('isbn') is not None:
        books = Book.objects(isbn=request.args.get('isbn'))
    elif request.args.get('title') is not None:
        books = Book.objects(title=request.args.get('title'))
    else:
        books = Book.objects.all()
    return books.to_json()
Exemplo n.º 2
0
def create_order(user_id, copies, book_id):
    book = Book.objects(id=book_id).first()
    customer = Customer.objects(id=user_id).first()
    books = []
    total = 0.0
    while copies > 0:
        books.append(book)
        total = round(total + Book.objects.with_id(book_id).price, 2)
        copies -= 1
    """checking to see if there's a order id already stored in session"""
    if session.get("order_id"):
        order = Order.objects(id=session.get("order_id")).first()
        order.total_price = round(order.total_price + total, 2)
        for book in books:
            order.books.append(book)
        order.save()
    else:
        order = Order(customer_name="{} {}".format(customer.first_name,
                                                   customer.last_name),
                      books=books,
                      shipping_address=customer.address,
                      total_price=total,
                      order_status="processing",
                      order_date=datetime.datetime.now).save()
        session["order_id"] = str(order.id)

    return order
Exemplo n.º 3
0
def test_valid_data_entry(app):
    book_input = Book(title="Test book title",
                      isbn="Test12345",
                      author="Test author",
                      price="123",
                      published=datetime.datetime.utcnow(),
                      publisher="Test company")
    book_input.save()

    # verify book is successfully saved and able to be retrieved and their values match
    book_saved = Book.objects(title=book_input.title).first()
    assert book_saved.title == book_input.title
    assert book_saved.isbn == book_input.isbn
    assert book_saved.author == book_input.author
    assert book_saved.price == book_input.price
    assert book_saved.published.strftime(
        "%m/%d/%Y, %H:%M:%S") == book_input.published.strftime(
            "%m/%d/%Y, %H:%M:%S")
    assert book_saved.publisher == book_input.publisher

    # verify book can be retrieved by isbn
    book_saved = Book.objects(isbn=book_input.isbn).first()
    assert book_saved.title == book_input.title
    assert book_saved.isbn == book_input.isbn
    assert book_saved.author == book_input.author
    assert book_saved.price == book_input.price
    assert book_saved.published.strftime(
        "%m/%d/%Y, %H:%M:%S") == book_input.published.strftime(
            "%m/%d/%Y, %H:%M:%S")
    assert book_saved.publisher == book_input.publisher

    # verify the book can be ordered by a customer
    sample_customer = Customer.objects.first()

    order = Order(customer_name="{} {}".format(sample_customer.first_name,
                                               sample_customer.last_name),
                  books=[book_saved.id],
                  shipping_address=sample_customer.address,
                  total_price=book_saved.price,
                  order_status="processing",
                  order_date=datetime.datetime.utcnow()).save()
    sample_customer.orders.append(order.id)
    sample_customer.save()

    # Look for customer by order. Customer ID should match
    order_customer = Customer.objects(orders=order.id).first()
    assert order_customer.id == sample_customer.id
Exemplo n.º 4
0
def create_order(copies, isbn):
    book = Book.objects(isbn=isbn).get()
    customer = Customer.objects(first_name="Kennth").get()