Product Error on *ngIf, new a dealbreaker, but not sure.
This commit is contained in:
@ -7,7 +7,10 @@ from sqlalchemy import (
|
||||
Numeric,
|
||||
Boolean,
|
||||
ForeignKey,
|
||||
Integer, case, JSON)
|
||||
Integer,
|
||||
case,
|
||||
JSON,
|
||||
)
|
||||
from sqlalchemy.ext.hybrid import hybrid_property
|
||||
from sqlalchemy.orm import relationship
|
||||
from .meta import Base
|
||||
@ -15,13 +18,13 @@ from barker.models.guidtype import GUID
|
||||
|
||||
|
||||
class Customer(Base):
|
||||
__tablename__ = 'customers'
|
||||
__tablename__ = "customers"
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
company = Column('company', Unicode(255), nullable=False)
|
||||
name = Column('name', Unicode(255), nullable=False)
|
||||
phone = Column('phone', Unicode(255), nullable=False, unique=True)
|
||||
address = Column('address', Unicode(255), nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
company = Column("company", Unicode(255), nullable=False)
|
||||
name = Column("name", Unicode(255), nullable=False)
|
||||
phone = Column("phone", Unicode(255), nullable=False, unique=True)
|
||||
address = Column("address", Unicode(255), nullable=False)
|
||||
|
||||
@property
|
||||
def __name__(self):
|
||||
@ -36,18 +39,18 @@ class Customer(Base):
|
||||
|
||||
@classmethod
|
||||
def cash(cls):
|
||||
return uuid.UUID('2c716f4b-0736-429a-ad51-610d7c47cb5e')
|
||||
return uuid.UUID("2c716f4b-0736-429a-ad51-610d7c47cb5e")
|
||||
|
||||
|
||||
class FoodTable(Base):
|
||||
__tablename__ = 'food_tables'
|
||||
__tablename__ = "food_tables"
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column('name', Unicode(255), nullable=False, unique=True)
|
||||
location = Column('location', Unicode(255), nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column("name", Unicode(255), nullable=False, unique=True)
|
||||
location = Column("location", Unicode(255), nullable=False)
|
||||
|
||||
is_active = Column('is_active', Boolean, nullable=False)
|
||||
sort_order = Column('sort_order', Numeric, nullable=False)
|
||||
is_active = Column("is_active", Boolean, nullable=False)
|
||||
sort_order = Column("sort_order", Numeric, nullable=False)
|
||||
|
||||
@property
|
||||
def __name__(self):
|
||||
@ -62,11 +65,11 @@ class FoodTable(Base):
|
||||
|
||||
|
||||
class Tax(Base):
|
||||
__tablename__ = 'taxes'
|
||||
__tablename__ = "taxes"
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column('name', Unicode(255), nullable=False, unique=True)
|
||||
rate = Column('rate', Numeric, nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column("name", Unicode(255), nullable=False, unique=True)
|
||||
rate = Column("rate", Numeric, nullable=False)
|
||||
is_fixture = Column("is_fixture", Boolean, nullable=False)
|
||||
|
||||
def __init__(self, name=None, rate=None, is_fixture=False, id=None):
|
||||
@ -77,19 +80,29 @@ class Tax(Base):
|
||||
|
||||
|
||||
class ProductGroup(Base):
|
||||
__tablename__ = 'product_groups'
|
||||
__tablename__ = "product_groups"
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column('name', Unicode(255), nullable=False, unique=True)
|
||||
discount_limit = Column('discount_limit', Numeric, nullable=False)
|
||||
is_modifier_compulsory = Column('is_modifier_compulsory', Boolean, nullable=False)
|
||||
group_type = Column('group_type', Unicode(255), nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column("name", Unicode(255), nullable=False, unique=True)
|
||||
discount_limit = Column("discount_limit", Numeric, nullable=False)
|
||||
is_modifier_compulsory = Column("is_modifier_compulsory", Boolean, nullable=False)
|
||||
group_type = Column("group_type", Unicode(255), nullable=False)
|
||||
|
||||
is_active = Column('is_active', Boolean, nullable=False)
|
||||
is_fixture = Column('is_fixture', Boolean, nullable=False)
|
||||
sort_order = Column('sort_order', Numeric, nullable=False)
|
||||
is_active = Column("is_active", Boolean, nullable=False)
|
||||
is_fixture = Column("is_fixture", Boolean, nullable=False)
|
||||
sort_order = Column("sort_order", Numeric, nullable=False)
|
||||
|
||||
def __init__(self, name, discount_limit, is_modifier_compulsory, group_type, is_active, sort_order, is_fixture=False, id=None):
|
||||
def __init__(
|
||||
self,
|
||||
name,
|
||||
discount_limit,
|
||||
is_modifier_compulsory,
|
||||
group_type,
|
||||
is_active,
|
||||
sort_order,
|
||||
is_fixture=False,
|
||||
id=None,
|
||||
):
|
||||
self.name = name
|
||||
self.discount_limit = discount_limit
|
||||
self.is_modifier_compulsory = is_modifier_compulsory
|
||||
@ -101,27 +114,41 @@ class ProductGroup(Base):
|
||||
|
||||
|
||||
class Product(Base):
|
||||
__tablename__ = 'products'
|
||||
__table_args__ = (UniqueConstraint('name', 'units'),)
|
||||
__tablename__ = "products"
|
||||
__table_args__ = (UniqueConstraint("name", "units"),)
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column('name', Unicode(255), nullable=False)
|
||||
units = Column('units', Unicode(255), nullable=False)
|
||||
product_group_id = Column('product_group_id', GUID(), ForeignKey('product_groups.id'), nullable=False)
|
||||
tax_id = Column('tax_id', GUID(), ForeignKey('taxes.id'), nullable=False)
|
||||
price = Column('price', Numeric, nullable=False)
|
||||
has_happy_hour = Column('has_happy_hour', Boolean, nullable=False)
|
||||
is_not_available = Column('is_not_available', Boolean, nullable=False)
|
||||
quantity = Column('quantity', Numeric, nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column("name", Unicode(255), nullable=False)
|
||||
units = Column("units", Unicode(255), nullable=False)
|
||||
product_group_id = Column(
|
||||
"product_group_id", GUID(), ForeignKey("product_groups.id"), nullable=False
|
||||
)
|
||||
tax_id = Column("tax_id", GUID(), ForeignKey("taxes.id"), nullable=False)
|
||||
price = Column("price", Numeric, nullable=False)
|
||||
has_happy_hour = Column("has_happy_hour", Boolean, nullable=False)
|
||||
is_not_available = Column("is_not_available", Boolean, nullable=False)
|
||||
quantity = Column("quantity", Numeric, nullable=False)
|
||||
|
||||
is_active = Column('is_active', Boolean, nullable=False)
|
||||
sort_order = Column('sort_order', Numeric, nullable=False)
|
||||
is_active = Column("is_active", Boolean, nullable=False)
|
||||
sort_order = Column("sort_order", Numeric, nullable=False)
|
||||
|
||||
product_group = relationship('ProductGroup', backref='products')
|
||||
tax = relationship('Tax', foreign_keys=tax_id)
|
||||
product_group = relationship("ProductGroup", backref="products")
|
||||
tax = relationship("Tax", foreign_keys=tax_id)
|
||||
|
||||
def __init__(self, name=None, units=None, product_group_id=None, tax_id=None, price=None,
|
||||
has_happy_hour=None, is_not_available=None, quantity=None, is_active=None, sort_order=None, id=None):
|
||||
def __init__(
|
||||
self,
|
||||
name=None,
|
||||
units=None,
|
||||
product_group_id=None,
|
||||
tax_id=None,
|
||||
price=None,
|
||||
has_happy_hour=None,
|
||||
is_not_available=None,
|
||||
quantity=None,
|
||||
is_active=None,
|
||||
sort_order=0,
|
||||
id=None,
|
||||
):
|
||||
self.name = name
|
||||
self.units = units
|
||||
self.product_group_id = product_group_id
|
||||
@ -140,25 +167,28 @@ class Product(Base):
|
||||
|
||||
@full_name.expression
|
||||
def full_name(cls):
|
||||
return cls.name + case([(cls.units != '', ' (' + cls.units + ')')], else_='')
|
||||
return cls.name + case([(cls.units != "", " (" + cls.units + ")")], else_="")
|
||||
|
||||
def can_delete(self, advanced_delete):
|
||||
if self.is_fixture:
|
||||
return False, "{0} is a fixture and cannot be edited or deleted.".format(self.name)
|
||||
return (
|
||||
False,
|
||||
"{0} is a fixture and cannot be edited or deleted.".format(self.name),
|
||||
)
|
||||
if self.is_active:
|
||||
return False, 'Product is active'
|
||||
return False, "Product is active"
|
||||
if len(self.inventories) > 0 and not advanced_delete:
|
||||
return False, 'Product has entries'
|
||||
return True, ''
|
||||
return False, "Product has entries"
|
||||
return True, ""
|
||||
|
||||
|
||||
class Modifier(Base):
|
||||
__tablename__ = 'modifiers'
|
||||
__tablename__ = "modifiers"
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column('name', Unicode(255), nullable=False, unique=True)
|
||||
show_in_bill = Column('show_in_bill', Boolean, nullable=False)
|
||||
price = Column('price', Numeric, nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column("name", Unicode(255), nullable=False, unique=True)
|
||||
show_in_bill = Column("show_in_bill", Boolean, nullable=False)
|
||||
price = Column("price", Numeric, nullable=False)
|
||||
|
||||
def __init__(self, name=None, show_in_bill=None, price=None, id=None):
|
||||
self.id = id
|
||||
@ -168,17 +198,23 @@ class Modifier(Base):
|
||||
|
||||
|
||||
class ProductGroupModifier(Base):
|
||||
__tablename__ = 'product_group_modifiers'
|
||||
__tablename__ = "product_group_modifiers"
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
product_group_id = Column('product_group_id', GUID(), ForeignKey('product_groups.id'))
|
||||
modifier_id = Column('modifier_id', GUID(), ForeignKey('modifiers.id'), nullable=False)
|
||||
show_automatically = Column('show_automatically', Boolean, nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
product_group_id = Column(
|
||||
"product_group_id", GUID(), ForeignKey("product_groups.id")
|
||||
)
|
||||
modifier_id = Column(
|
||||
"modifier_id", GUID(), ForeignKey("modifiers.id"), nullable=False
|
||||
)
|
||||
show_automatically = Column("show_automatically", Boolean, nullable=False)
|
||||
|
||||
product_group = relationship('ProductGroup', backref='modifiers')
|
||||
modifier = relationship('Modifier', backref='product_groups')
|
||||
product_group = relationship("ProductGroup", backref="modifiers")
|
||||
modifier = relationship("Modifier", backref="product_groups")
|
||||
|
||||
def __init__(self, product_group_id=None, modifier_id=None, show_automatically=None, id=None):
|
||||
def __init__(
|
||||
self, product_group_id=None, modifier_id=None, show_automatically=None, id=None
|
||||
):
|
||||
self.id = id
|
||||
self.product_group_id = product_group_id
|
||||
self.modifier_id = modifier_id
|
||||
@ -186,11 +222,11 @@ class ProductGroupModifier(Base):
|
||||
|
||||
|
||||
class DbSetting(Base):
|
||||
__tablename__ = 'settings'
|
||||
__tablename__ = "settings"
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column('name', Unicode(255), unique=True, nullable=False)
|
||||
data = Column('data', JSON)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column("name", Unicode(255), unique=True, nullable=False)
|
||||
data = Column("data", JSON)
|
||||
|
||||
def __init__(self, id=None, name=None, data=None):
|
||||
self.id = id
|
||||
@ -199,10 +235,10 @@ class DbSetting(Base):
|
||||
|
||||
|
||||
class Location(Base):
|
||||
__tablename__ = 'locations'
|
||||
__tablename__ = "locations"
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column('name', Unicode(255), unique=True, nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column("name", Unicode(255), unique=True, nullable=False)
|
||||
|
||||
def __init__(self, id=None, name=None):
|
||||
self.id = id
|
||||
@ -210,13 +246,15 @@ class Location(Base):
|
||||
|
||||
|
||||
class MachineLocation(Base):
|
||||
__tablename__ = 'machine_locations'
|
||||
__tablename__ = "machine_locations"
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
machine_name = Column('machine_name', Unicode(255), unique=True, nullable=False)
|
||||
location_id = Column('location_id', GUID(), ForeignKey('locations.id'), nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
machine_name = Column("machine_name", Unicode(255), unique=True, nullable=False)
|
||||
location_id = Column(
|
||||
"location_id", GUID(), ForeignKey("locations.id"), nullable=False
|
||||
)
|
||||
|
||||
location = relationship('Location', backref='machines')
|
||||
location = relationship("Location", backref="machines")
|
||||
|
||||
def __init__(self, machine_name=None, location_id=None, id=None):
|
||||
self.machine_name = machine_name
|
||||
@ -225,11 +263,11 @@ class MachineLocation(Base):
|
||||
|
||||
|
||||
class Printer(Base):
|
||||
__tablename__ = 'printers'
|
||||
__tablename__ = "printers"
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column('name', Unicode(255), unique=True, nullable=False)
|
||||
cut_code = Column('cut_code', Unicode(255), nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
name = Column("name", Unicode(255), unique=True, nullable=False)
|
||||
cut_code = Column("cut_code", Unicode(255), nullable=False)
|
||||
|
||||
def __init__(self, id=None, name=None, cut_code=None):
|
||||
self.id = id
|
||||
@ -238,18 +276,22 @@ class Printer(Base):
|
||||
|
||||
|
||||
class PrintLocation(Base):
|
||||
__tablename__ = 'print_locations'
|
||||
__table_args__ = (UniqueConstraint('product_group_id', 'location_id'),)
|
||||
__tablename__ = "print_locations"
|
||||
__table_args__ = (UniqueConstraint("product_group_id", "location_id"),)
|
||||
|
||||
id = Column('id', GUID(), primary_key=True, default=uuid.uuid4)
|
||||
product_group_id = Column('product_group_id', GUID(), ForeignKey('product_groups.id'))
|
||||
location_id = Column('location_id', GUID(), ForeignKey('locations.id'), nullable=False)
|
||||
printer_id = Column('printer_id', GUID(), ForeignKey('printers.id'), nullable=False)
|
||||
copies = Column('copies', Numeric, nullable=False)
|
||||
id = Column("id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||
product_group_id = Column(
|
||||
"product_group_id", GUID(), ForeignKey("product_groups.id")
|
||||
)
|
||||
location_id = Column(
|
||||
"location_id", GUID(), ForeignKey("locations.id"), nullable=False
|
||||
)
|
||||
printer_id = Column("printer_id", GUID(), ForeignKey("printers.id"), nullable=False)
|
||||
copies = Column("copies", Numeric, nullable=False)
|
||||
|
||||
product_group = relationship('ProductGroup', backref='print_locations')
|
||||
location = relationship('Location', backref='print_locations')
|
||||
printer = relationship('Printer', backref='print_locations')
|
||||
product_group = relationship("ProductGroup", backref="print_locations")
|
||||
location = relationship("Location", backref="print_locations")
|
||||
printer = relationship("Printer", backref="print_locations")
|
||||
|
||||
def __init__(self, product_group_id, location_id, printer_id, copies):
|
||||
self.product_group_id = product_group_id
|
||||
@ -259,13 +301,13 @@ class PrintLocation(Base):
|
||||
|
||||
|
||||
class SettleOption(Base):
|
||||
__tablename__ = 'settle_options'
|
||||
__tablename__ = "settle_options"
|
||||
|
||||
id = Column('id', Integer, primary_key=True)
|
||||
name = Column('name', Unicode(255), unique=True, nullable=False)
|
||||
show_in_choices = Column('show_in_choices', Boolean, nullable=False)
|
||||
group = Column('display_group', Integer, nullable=False)
|
||||
is_print = Column('is_print', Boolean, nullable=False)
|
||||
id = Column("id", Integer, primary_key=True)
|
||||
name = Column("name", Unicode(255), unique=True, nullable=False)
|
||||
show_in_choices = Column("show_in_choices", Boolean, nullable=False)
|
||||
group = Column("display_group", Integer, nullable=False)
|
||||
is_print = Column("is_print", Boolean, nullable=False)
|
||||
|
||||
def __init__(self, name, show_in_choices, group, is_print, id):
|
||||
self.id = id
|
||||
|
||||
@ -55,10 +55,6 @@ def includeme(config):
|
||||
config.add_route("v1_product_groups_list", "/v1/product-groups")
|
||||
config.add_route("v1_product_group_types_list", "/v1/product-group-types")
|
||||
|
||||
config.add_route('product_group', '/ProductGroup.json')
|
||||
config.add_route('product_group_list', '/ProductGroups.json')
|
||||
config.add_route('product_group_id', '/ProductGroup/{id}.json')
|
||||
|
||||
config.add_route('product_group_type_list', '/ProductGroupTypes.json')
|
||||
|
||||
config.add_route('quantity_sold', '/QuantitySold.json')
|
||||
|
||||
@ -9,7 +9,13 @@ from barker.models import Product
|
||||
from barker.models.validation_exception import ValidationError
|
||||
|
||||
|
||||
@view_config(request_method='PUT', route_name='v1_products_new', renderer='json', permission='Products', trans=True)
|
||||
@view_config(
|
||||
request_method="POST",
|
||||
route_name="v1_products_new",
|
||||
renderer="json",
|
||||
permission="Products",
|
||||
trans=True,
|
||||
)
|
||||
def save(request):
|
||||
json = request.json_body
|
||||
|
||||
@ -57,15 +63,20 @@ def save(request):
|
||||
has_happy_hour,
|
||||
is_not_available,
|
||||
quantity,
|
||||
is_active
|
||||
is_active,
|
||||
)
|
||||
request.dbsession.add(item)
|
||||
transaction.commit()
|
||||
item = request.dbsession.query(Product).filter(Product.id == item.id).first()
|
||||
return product_info(item)
|
||||
return product_info(item.id, request.dbsession)
|
||||
|
||||
|
||||
@view_config(request_method='POST', route_name='v1_products_id', renderer='json', permission='Products', trans=True)
|
||||
@view_config(
|
||||
request_method="PUT",
|
||||
route_name="v1_products_id",
|
||||
renderer="json",
|
||||
permission="Products",
|
||||
trans=True,
|
||||
)
|
||||
def update(request):
|
||||
item = (
|
||||
request.dbsession.query(Product)
|
||||
@ -76,30 +87,39 @@ def update(request):
|
||||
item.name = json["name"].strip()
|
||||
item.units = json["units"].strip()
|
||||
item.product_group_id = uuid.UUID(json["productGroup"]["id"])
|
||||
item.vat_id = uuid.UUID(json["tax"]["id"])
|
||||
item.tax_id = uuid.UUID(json["tax"]["id"])
|
||||
try:
|
||||
item.price = Decimal(json["price"])
|
||||
if item.price < 0:
|
||||
raise ValidationError("Price must be a decimal >= 0")
|
||||
except (ValueError, InvalidOperation):
|
||||
raise ValidationError("Price must be a decimal >= 0")
|
||||
item.has_happy_hour = json['hasHappyHour']
|
||||
item.is_not_available = json['isNotAvailable']
|
||||
item.has_happy_hour = json["hasHappyHour"]
|
||||
item.is_not_available = json["isNotAvailable"]
|
||||
try:
|
||||
item.quantity = Decimal(json["quantity"])
|
||||
if item.price < 0:
|
||||
raise ValidationError("Quantity must be a decimal >= 0")
|
||||
except (ValueError, InvalidOperation):
|
||||
raise ValidationError("Quantity must be a decimal >= 0")
|
||||
item.is_active = json['isActive']
|
||||
item.is_active = json["isActive"]
|
||||
transaction.commit()
|
||||
item = request.dbsession.query(Product).filter(Product.id == item.id).first()
|
||||
return product_info(item)
|
||||
return product_info(item.id, request.dbsession)
|
||||
|
||||
|
||||
@view_config(request_method='DELETE', route_name='v1_products_id', renderer='json', permission='Products', trans=True)
|
||||
@view_config(
|
||||
request_method="DELETE",
|
||||
route_name="v1_products_id",
|
||||
renderer="json",
|
||||
permission="Products",
|
||||
trans=True,
|
||||
)
|
||||
def delete(request):
|
||||
item = request.dbsession.query(Product).filter(Product.id == uuid.UUID(request.matchdict['id'])).first()
|
||||
item = (
|
||||
request.dbsession.query(Product)
|
||||
.filter(Product.id == uuid.UUID(request.matchdict["id"]))
|
||||
.first()
|
||||
)
|
||||
if item is None:
|
||||
response = Response("Product not Found")
|
||||
response.status_int = 500
|
||||
@ -110,56 +130,97 @@ def delete(request):
|
||||
return response
|
||||
|
||||
|
||||
@view_config(request_method='GET', route_name='v1_products_id', renderer='json', permission='Authenticated')
|
||||
@view_config(
|
||||
request_method="GET",
|
||||
route_name="v1_products_new",
|
||||
renderer="json",
|
||||
permission="Authenticated",
|
||||
)
|
||||
def show_blank(request):
|
||||
return product_info(None, request.dbsession)
|
||||
|
||||
|
||||
@view_config(
|
||||
request_method="GET",
|
||||
route_name="v1_products_id",
|
||||
renderer="json",
|
||||
permission="Authenticated",
|
||||
)
|
||||
def show_id(request):
|
||||
id_ = uuid.UUID(request.matchdict['id'])
|
||||
id_ = uuid.UUID(request.matchdict["id"])
|
||||
item = request.dbsession.query(Product).filter(Product.id == id_).first()
|
||||
return product_info(item)
|
||||
return product_info(item.id, request.dbsession)
|
||||
|
||||
|
||||
@view_config(request_method='GET', route_name='v1_products_list', renderer='json', permission='Authenticated')
|
||||
@view_config(
|
||||
request_method="GET",
|
||||
route_name="v1_products_list",
|
||||
renderer="json",
|
||||
permission="Authenticated",
|
||||
)
|
||||
def show_list(request):
|
||||
active = request.GET.get('a', None)
|
||||
active = request.GET.get("a", None)
|
||||
list_ = request.dbsession.query(Product)
|
||||
if active is not None:
|
||||
list_ = list_.filter(Product.is_active == active)
|
||||
list_ = list_.order_by(Product.product_group_id, Product.sort_order).order_by(Product.name).all()
|
||||
list_ = (
|
||||
list_.order_by(Product.product_group_id, Product.sort_order)
|
||||
.order_by(Product.name)
|
||||
.all()
|
||||
)
|
||||
products = []
|
||||
for item in list_:
|
||||
products.append(product_info(item))
|
||||
products.append(product_info(item, request.dbsession))
|
||||
return products
|
||||
|
||||
|
||||
@view_config(request_method='POST', route_name='v1_products_list', renderer='json', permission='Products', trans=True)
|
||||
@view_config(
|
||||
request_method="POST",
|
||||
route_name="v1_products_list",
|
||||
renderer="json",
|
||||
permission="Products",
|
||||
trans=True,
|
||||
)
|
||||
def sort_order(request):
|
||||
json = request.json_body
|
||||
index, last_group = 0, None
|
||||
indexes = {}
|
||||
for item in json:
|
||||
product_id = uuid.UUID(item['id'])
|
||||
product_group_id = uuid.UUID(item['ProductGroupID'])
|
||||
if last_group != product_group_id:
|
||||
index = 0
|
||||
last_group = product_group_id
|
||||
request.dbsession.query(
|
||||
Product
|
||||
).filter(
|
||||
Product.id == product_id
|
||||
).update(
|
||||
{Product.sort_order: index}
|
||||
product_id = uuid.UUID(item["id"])
|
||||
product_group_id = uuid.UUID(item["productGroup"]["id"])
|
||||
if product_group_id in indexes:
|
||||
indexes[product_group_id] += 1
|
||||
else:
|
||||
indexes[product_group_id] = 0
|
||||
request.dbsession.query(Product).filter(Product.id == product_id).update(
|
||||
{Product.sort_order: indexes[product_group_id]}
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def product_info(item):
|
||||
def product_info(item, dbsession):
|
||||
if item is None:
|
||||
return {
|
||||
"name": "",
|
||||
"units": "",
|
||||
"productGroup": {},
|
||||
"tax": {},
|
||||
"price": 0,
|
||||
"hasHappyHour": False,
|
||||
"isNotAvailable": False,
|
||||
"isActive": True,
|
||||
"sortOrder": 0,
|
||||
}
|
||||
if type(item) is uuid.UUID:
|
||||
item = dbsession.query(Product).filter(Product.id == item).first()
|
||||
return {
|
||||
'id': item.id,
|
||||
'name': item.name,
|
||||
'units': item.units,
|
||||
'productGroup': {'id': item.product_group_id, 'name': item.product_group.name},
|
||||
'tax': {'id': item.vat_id, 'name': item.vat.name, 'rate': item.vat.rate},
|
||||
'price': item.price,
|
||||
'hasHappyHour': item.has_happy_hour,
|
||||
'isNotAvailable': item.is_not_available,
|
||||
'isActive': item.is_active,
|
||||
'sortOrder': item.sort_order
|
||||
"id": item.id,
|
||||
"name": item.name,
|
||||
"units": item.units,
|
||||
"productGroup": {"id": item.product_group_id, "name": item.product_group.name},
|
||||
"tax": {"id": item.tax_id, "name": item.tax.name, "rate": item.tax.rate},
|
||||
"price": item.price,
|
||||
"hasHappyHour": item.has_happy_hour,
|
||||
"isNotAvailable": item.is_not_available,
|
||||
"isActive": item.is_active,
|
||||
"sortOrder": item.sort_order,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user