__author__ = 'tanshu'

import uuid

from pyramid.view import view_config

from picard.views.sales.voucher import get_voucher
from picard.models import DBSession
from picard.models.master import FoodTable
from picard.models.validation_exception import TryCatchFunction


@view_config(request_method='GET', route_name='api_table_list', renderer='json')
@TryCatchFunction
def table_list(request):
    list = DBSession.query(FoodTable).order_by(FoodTable.name).all()
    food_tables = []
    for item in list:
        item = {'Name': item.name, 'Location': item.location,
                'Status': 'Empty' if item.info is None else item.info.status.status,
                'Url': request.route_url('voucher', _query={'t': item.name.replace(' ', '-')})}
        food_tables.append(item)
    return food_tables


@view_config(request_method='GET', route_name='api_table_id', renderer='json')
@TryCatchFunction
def get_table_id(request):
    item = FoodTable.by_id(uuid.UUID(request.matchdict['id']))
    return get_voucher(food_table=item)