forked from CircleCI-Public/sample-python-cfd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart_controller.py
More file actions
65 lines (48 loc) · 1.89 KB
/
cart_controller.py
File metadata and controls
65 lines (48 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import connexion
import six
from sqlalchemy import exc
from sqlalchemy.sql.functions import mode
from openapi_server.models.error import Error # noqa: E501
from openapi_server.models.menu_item import MenuItem # noqa: E501
from openapi_server import util
from openapi_server.database import models
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
def add_cart_item(): # noqa: E501
"""Add a menu item a cart
Creates a new item in the cart. Duplicates are allowed # noqa: E501
:param menu_item: Item to add to the cart
:type menu_item: dict | bytes
:rtype: None
"""
if connexion.request.is_json:
menu_item = MenuItem.from_dict(connexion.request.get_json()) # noqa: E501
try:
models.Cart.add_item(connexion.request.remote_addr, menu_item)
# models.Cart.add_item(connexion.request.host.split(':')[0], menu_item)
except (SQLAlchemyError, TypeError, AttributeError):
models.db.session.rollback()
return Error(400), 400
def delete_cart_item(item_id): # noqa: E501
"""Remove item from cart
The item must be in the cart. If multiple of same item, call this twice # noqa: E501
:param item_id: The menu item to delete from cart
:type item_id: int
:rtype: None
"""
# this is by no means efficient
try:
models.Cart.delete_item_by_id(connexion.request.remote_addr, item_id)
except (SQLAlchemyError, TypeError):
models.db.session.rollback()
return Error(403), 403 # cart is already devoid of this item
def list_cart(limit=None): # noqa: E501
"""List all cart items
# noqa: E501
:param limit: How many items to return at one time (max 100)
:type limit: int
:rtype: List[MenuItem]
"""
if cart := models.Cart.query_by_host(connexion.request.remote_addr):
return [_.serialize() for _ in cart.items]
else:
return []