Feature:
SectionPrinter is now working Migration: Section, Printers, SectionPrinters working
This commit is contained in:
@ -81,6 +81,7 @@ def includeme(config):
|
||||
config.add_route("v1_sections_list", "/v1/sections")
|
||||
|
||||
config.add_route("section_printers", "/section-printers/{section_id}")
|
||||
config.add_route("v1_section_printers_noid", "/v1/section-printers")
|
||||
config.add_route("v1_section_printers", "/v1/section-printers/{section_id}")
|
||||
|
||||
config.add_route("devices_new", "/devices/new")
|
||||
|
||||
@ -1,21 +1,30 @@
|
||||
import transaction
|
||||
from pyramid.response import Response
|
||||
from sqlalchemy.exc import OperationalError, IntegrityError, DBAPIError
|
||||
import transaction
|
||||
from barker.models.validation_exception import ValidationError
|
||||
|
||||
|
||||
def transactional_view(view, info):
|
||||
if info.options.get('trans'):
|
||||
if info.options.get("trans"):
|
||||
|
||||
def wrapper_view(context, request):
|
||||
try:
|
||||
response = view(context, request)
|
||||
except (
|
||||
ValueError, KeyError, AttributeError, TypeError, OperationalError, IntegrityError, DBAPIError
|
||||
ValidationError,
|
||||
ValueError,
|
||||
KeyError,
|
||||
AttributeError,
|
||||
TypeError,
|
||||
OperationalError,
|
||||
IntegrityError,
|
||||
DBAPIError,
|
||||
) as ex:
|
||||
transaction.abort()
|
||||
response = Response()
|
||||
response.status_int = 500
|
||||
response.content_type = 'application/json'
|
||||
response.json = {'Error': "Failed validation: {0!s}".format(ex)}
|
||||
response.content_type = "application/json"
|
||||
response.json = {"Error": "Failed validation: {0!s}".format(ex)}
|
||||
return response
|
||||
|
||||
return wrapper_view
|
||||
@ -26,4 +35,4 @@ def transactional_view(view, info):
|
||||
# return view
|
||||
|
||||
|
||||
transactional_view.options = ('trans',)
|
||||
transactional_view.options = ("trans",)
|
||||
|
||||
@ -21,15 +21,26 @@ def save(request):
|
||||
json = request.json_body
|
||||
section_id = uuid.UUID(request.matchdict["section_id"])
|
||||
current = []
|
||||
for mcs in json:
|
||||
for mcs in json["menuCategories"]:
|
||||
menu_category = mcs.get("menuCategory", None)
|
||||
menu_category_id = (
|
||||
uuid.UUID(menu_category["id"]) if menu_category is not None else None
|
||||
uuid.UUID(menu_category["id"])
|
||||
if menu_category is not None and menu_category["id"] != ""
|
||||
else None
|
||||
)
|
||||
printer = mcs.get("printer", None)
|
||||
printer_id = uuid.UUID(printer["id"]) if printer is not None else None
|
||||
printer_id = (
|
||||
uuid.UUID(printer["id"])
|
||||
if printer is not None
|
||||
and "id" in printer
|
||||
and printer["id"] is not None
|
||||
and printer["id"] != ""
|
||||
else None
|
||||
)
|
||||
if menu_category_id is None and printer_id is None:
|
||||
raise ValidationError("Please choose a default printer")
|
||||
if printer_id is None:
|
||||
continue
|
||||
try:
|
||||
copies = int(mcs.get("copies", 0))
|
||||
if copies < 1:
|
||||
@ -46,8 +57,8 @@ def save(request):
|
||||
copies=copies,
|
||||
)
|
||||
.on_conflict_do_update(
|
||||
constraint=["menu_category_id", "section_id"],
|
||||
set_=dict(printerid=printer_id, copies=copies),
|
||||
index_elements=["menu_category_id", "section_id"],
|
||||
set_=dict(printer_id=printer_id, copies=copies),
|
||||
)
|
||||
)
|
||||
request.dbsession.execute(stmt)
|
||||
@ -61,7 +72,7 @@ def save(request):
|
||||
mark_changed(request.dbsession)
|
||||
transaction.commit()
|
||||
|
||||
return report(section_id, request.dbsession)
|
||||
return {"id": section_id, "menuCategories": report(section_id, request.dbsession)}
|
||||
|
||||
|
||||
@view_config(
|
||||
@ -76,7 +87,7 @@ def delete(request):
|
||||
SectionPrinter.__table__.delete(SectionPrinter.section_id == section_id)
|
||||
mark_changed(request.dbsession)
|
||||
transaction.commit()
|
||||
return report(section_id, request.dbsession)
|
||||
return {"id": "", "menuCategories": report(section_id, request.dbsession)}
|
||||
|
||||
|
||||
@view_config(
|
||||
@ -85,20 +96,28 @@ def delete(request):
|
||||
renderer="json",
|
||||
permission="Authenticated",
|
||||
)
|
||||
@view_config(
|
||||
request_method="GET",
|
||||
route_name="v1_section_printers_noid",
|
||||
renderer="json",
|
||||
permission="Authenticated",
|
||||
)
|
||||
def show_id(request):
|
||||
section_id = uuid.UUID(request.matchdict["section_id"])
|
||||
return report(section_id, request.dbsession)
|
||||
section_id = request.matchdict.get("section_id", None)
|
||||
if section_id is not None:
|
||||
section_id = uuid.UUID(section_id)
|
||||
return {"id": section_id, "menuCategories": report(section_id, request.dbsession)}
|
||||
|
||||
|
||||
@view_config(
|
||||
request_method="GET",
|
||||
route_name="v1_section_printers",
|
||||
renderer="json",
|
||||
request_param="s",
|
||||
request_param="m",
|
||||
permission="Authenticated",
|
||||
)
|
||||
def show_details(request):
|
||||
section_id = uuid.UUID(request.GET["s"])
|
||||
section_id = uuid.UUID(request.matchdict["section_id"])
|
||||
menu_category_id = request.GET.get("m", None)
|
||||
if menu_category_id is not None:
|
||||
menu_category_id = uuid.UUID(menu_category_id)
|
||||
@ -115,28 +134,31 @@ def show_details(request):
|
||||
|
||||
def report(section_id, dbsession):
|
||||
menu_categories = (
|
||||
dbsession.query(MenuCategory.id)
|
||||
dbsession.query(MenuCategory.id, MenuCategory.name)
|
||||
.filter(MenuCategory.is_active == True)
|
||||
.order_by(MenuCategory.sort_order)
|
||||
.all()
|
||||
)
|
||||
list_ = []
|
||||
for item in [None] + menu_categories:
|
||||
for item in [(None,)] + menu_categories:
|
||||
mc = (
|
||||
dbsession.query(SectionPrinter)
|
||||
.filter(
|
||||
SectionPrinter.section_id == section_id,
|
||||
SectionPrinter.menu_category_id == id,
|
||||
SectionPrinter.menu_category_id == item[0],
|
||||
)
|
||||
.first()
|
||||
)
|
||||
list_.append(
|
||||
{
|
||||
"menuCategory": {"id": item},
|
||||
"menuCategory": {"id": "", "name": "Default"}
|
||||
if item[0] is None
|
||||
else {"id": item[0], "name": item[1]},
|
||||
"printer": {} if mc is None else {"id": mc.printer_id},
|
||||
"copies": 0 if mc is None else mc.copies,
|
||||
}
|
||||
)
|
||||
return list_
|
||||
|
||||
|
||||
def section_printer_info(item, dbsession):
|
||||
|
||||
Reference in New Issue
Block a user