260 lines
7.4 KiB
Python
260 lines
7.4 KiB
Python
import uuid
|
|
|
|
import transaction
|
|
|
|
from pyramid.view import view_config
|
|
from sqlalchemy import or_
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
from barker.models import ModifierCategory, Product
|
|
from barker.models.validation_exception import ValidationError
|
|
|
|
|
|
@view_config(
|
|
request_method="POST",
|
|
route_name="v1_modifier_categories_new",
|
|
renderer="json",
|
|
permission="Modifiers",
|
|
trans=True,
|
|
)
|
|
def save(request):
|
|
json = request.json_body
|
|
name = json.get("name", "").strip()
|
|
if name == "":
|
|
raise ValidationError("Name cannot be blank")
|
|
try:
|
|
minimum = int(json.get("minimum", 0))
|
|
if minimum < 0:
|
|
raise ValidationError("Minimum must be an integer >= 0")
|
|
except ValueError:
|
|
raise ValidationError("Minimum must be an integer >= 0")
|
|
try:
|
|
maximum = json.get("maximum", None)
|
|
if maximum is not None:
|
|
maximum = int(maximum)
|
|
if maximum < 0:
|
|
raise ValidationError(
|
|
"Maximum must be an integer and cannot be less than zero"
|
|
)
|
|
elif maximum == 0:
|
|
maximum = None
|
|
except ValueError:
|
|
raise ValidationError("Maximum must be an integer and cannot be less than zero")
|
|
item = ModifierCategory(name, minimum, maximum)
|
|
request.dbsession.add(item)
|
|
add_products(item, json["menuCategories"], request.dbsession)
|
|
transaction.commit()
|
|
return modifier_category_info(item.id, request.dbsession)
|
|
|
|
|
|
@view_config(
|
|
request_method="PUT",
|
|
route_name="v1_modifier_categories_id",
|
|
renderer="json",
|
|
permission="Modifiers",
|
|
trans=True,
|
|
)
|
|
def update(request):
|
|
item = (
|
|
request.dbsession.query(ModifierCategory)
|
|
.filter(ModifierCategory.id == uuid.UUID(request.matchdict["id"]))
|
|
.first()
|
|
)
|
|
json = request.json_body
|
|
item.name = json["name"].strip()
|
|
if item.name == "":
|
|
raise ValidationError("Name cannot be blank")
|
|
try:
|
|
item.minimum = int(json.get("minimum", 0))
|
|
if item.minimum < 0:
|
|
raise ValidationError("Minimum must be an integer >= 0")
|
|
except ValueError:
|
|
raise ValidationError("Minimum must be an integer >= 0")
|
|
try:
|
|
item.maximum = json.get("maximum", None)
|
|
if item.maximum is not None:
|
|
item.maximum = int(item.maximum)
|
|
if item.maximum < 0:
|
|
raise ValidationError(
|
|
"Maximum must be an integer and cannot be less than zero"
|
|
)
|
|
elif item.maximum == 0:
|
|
item.maximum = None
|
|
except ValueError:
|
|
raise ValidationError("Maximum must be an integer and cannot be less than zero")
|
|
add_products(item, json["menuCategories"], request.dbsession)
|
|
transaction.commit()
|
|
return modifier_category_info(item.id, request.dbsession)
|
|
|
|
|
|
@view_config(
|
|
request_method="DELETE",
|
|
route_name="v1_modifier_categories_id",
|
|
renderer="json",
|
|
permission="Modifiers",
|
|
trans=True,
|
|
)
|
|
def delete(request):
|
|
item = (
|
|
request.dbsession.query(ModifierCategory)
|
|
.filter(ModifierCategory.id == uuid.UUID(request.matchdict["id"]))
|
|
.first()
|
|
)
|
|
request.dbsession.delete(item)
|
|
transaction.commit()
|
|
return {None, request.dbsession}
|
|
|
|
|
|
@view_config(
|
|
request_method="GET",
|
|
route_name="v1_modifier_categories_id",
|
|
renderer="json",
|
|
permission="Authenticated",
|
|
)
|
|
def show_id(request):
|
|
id_ = uuid.UUID(request.matchdict["id"])
|
|
return modifier_category_info(id_, request.dbsession)
|
|
|
|
|
|
@view_config(
|
|
request_method="GET",
|
|
route_name="v1_modifier_categories_new",
|
|
renderer="json",
|
|
permission="Authenticated",
|
|
)
|
|
def show_blank(request):
|
|
return modifier_category_info(None, request.dbsession)
|
|
|
|
|
|
@view_config(
|
|
request_method="GET",
|
|
route_name="v1_modifier_categories_list",
|
|
renderer="json",
|
|
permission="Authenticated",
|
|
)
|
|
def show_list(request):
|
|
list_ = (
|
|
request.dbsession.query(ModifierCategory)
|
|
.order_by(ModifierCategory.sort_order)
|
|
.order_by(ModifierCategory.name)
|
|
.all()
|
|
)
|
|
modifier_categories = []
|
|
for item in list_:
|
|
modifier_categories.append(
|
|
{
|
|
"id": item.id,
|
|
"name": item.name,
|
|
"minimum": item.minimum,
|
|
"maximum": item.maximum,
|
|
"products": [p.name for p in item.products],
|
|
}
|
|
)
|
|
return modifier_categories
|
|
|
|
|
|
@view_config(
|
|
request_method="GET",
|
|
route_name="v1_modifier_categories_list",
|
|
renderer="json",
|
|
request_param="pg",
|
|
permission="Authenticated",
|
|
)
|
|
def show_for_pg(request):
|
|
menu_category_id = uuid.UUID(request.GET["pg"])
|
|
pgm_list = (
|
|
request.dbsession.query(MenuCategoryModifier)
|
|
.filter(
|
|
or_(
|
|
MenuCategoryModifier.menu_category == None,
|
|
MenuCategoryModifier.menu_category_id == menu_category_id,
|
|
)
|
|
)
|
|
.all()
|
|
)
|
|
|
|
pg_modifiers = []
|
|
for item in pgm_list:
|
|
pg_modifiers.append(
|
|
{
|
|
"ModifierID": item.modifier_id,
|
|
"Name": item.modifier.name,
|
|
"ShowInBill": item.modifier.show_in_bill,
|
|
"Price": item.modifier.price,
|
|
}
|
|
)
|
|
return pg_modifiers
|
|
|
|
|
|
def modifier_category_info(item, dbsession):
|
|
from barker.models import MenuCategory
|
|
|
|
menu_categories = (
|
|
dbsession.query(MenuCategory)
|
|
.join(MenuCategory.products)
|
|
.filter(Product.is_active == True)
|
|
.order_by(MenuCategory.sort_order, Product.sort_order, Product.name)
|
|
.all()
|
|
)
|
|
if item is None:
|
|
return {
|
|
"name": "",
|
|
"minimum": 0,
|
|
"maximum": 0,
|
|
"menuCategories": [
|
|
{
|
|
"id": mc.id,
|
|
"name": mc.name,
|
|
"enabled": False,
|
|
"products": [
|
|
{"id": p.id, "name": p.name, "enabled": False}
|
|
for p in mc.products
|
|
],
|
|
}
|
|
for mc in menu_categories
|
|
],
|
|
}
|
|
if type(item) is uuid.UUID:
|
|
item = (
|
|
dbsession.query(ModifierCategory)
|
|
.filter(ModifierCategory.id == item)
|
|
.first()
|
|
)
|
|
return {
|
|
"id": item.id,
|
|
"name": item.name,
|
|
"minimum": item.minimum,
|
|
"maximum": item.maximum,
|
|
"menuCategories": [
|
|
{
|
|
"id": mc.id,
|
|
"name": mc.name,
|
|
"enabled": False,
|
|
"products": [
|
|
{
|
|
"id": p.id,
|
|
"name": p.name,
|
|
"enabled": True if p in item.products else False,
|
|
}
|
|
for p in mc.products
|
|
],
|
|
}
|
|
for mc in menu_categories
|
|
],
|
|
}
|
|
|
|
|
|
def add_products(modifier_category, menu_categories, dbsession):
|
|
for mc in menu_categories:
|
|
for p in mc["products"]:
|
|
id_ = uuid.UUID(p["id"])
|
|
old = [p for p in modifier_category.products if p.id == id_]
|
|
old = None if len(old) == 0 else old[0]
|
|
if p["enabled"] and old is None:
|
|
product_object = (
|
|
dbsession.query(Product).filter(Product.id == id_).one()
|
|
)
|
|
modifier_category.products.append(product_object)
|
|
elif not p["enabled"] and old:
|
|
modifier_category.products.remove(old)
|