Exemple #1
0
def api_error_handler(error):
    if error is None:
        return ApiResponse(500, 'Application internal error')
    message = ''
    if hasattr(error, 'description') and error.description:
        message = error.description
    errors = None
    if hasattr(error, 'response') and error.response is not None:
        errors = error.response
    return ApiResponse(error.code, message, errors=errors)
Exemple #2
0
def index():
    request_query = request.args
    if request_query is None:
        raise BadRequest()

    sales_date = format_date(request_query.get('sales_date', type=str))

    start_time = None
    if request_query.get('time_from', type=str):
        start_time = format_time(request_query.get('time_from', type=str))

    end_time = None
    if request_query.get('time_to', type=str):
        end_time = format_time(request_query.get('time_to', type=str))

    option = DailySalesSearchOption(sales_date=sales_date,
                                    start_time=start_time,
                                    end_time=end_time)
    mapper = SalesMapper()

    daily_sales = mapper.find_daily_sales(option)
    if len(daily_sales) > 0:
        sales_ids = [s['id'] for s in daily_sales]
        item_mapper = SalesItemMapper()
        sales_items = item_mapper.find_daily_sales_items(sales_ids)
        for s in daily_sales:
            items = []
            for item in sales_items:
                if item['sales_id'] == s['id']:
                    items.append(item)
            s['items'] = items

    return ApiResponse(200, data={'daily_sales': daily_sales})
Exemple #3
0
def delete(id):
    mapper = CategoryMapper()
    deleted = mapper.delete(id)
    if not deleted:
        raise NotFound(description='リクエストデータはすでに削除されたか、または存在しません。')

    return ApiResponse(204, '削除しました')
Exemple #4
0
def add():
    request_data = request.get_json()
    if request_data is None:
        raise BadRequest()
    if request_data['items'] is None or len(request_data['items']) < 1:
        raise BadRequest(description='売上明細データがセットされていません')

    items = []
    for i, item in enumerate(request_data['items'], 1):
        items.append(SalesItem(item_no=i, **item))
    del request.json['items']
    now_date = datetime.now().date()
    now_time = datetime.now().time()
    sales = Sales(None,
                  sales_date=now_date,
                  sales_time=now_time,
                  items=items,
                  **request.json)

    if not sales.is_valid():
        raise BadRequest(description='保存エラー。エラー内容を確認してください。',
                         response=sales.validation_errors)

    mapper = SalesMapper()
    saved = mapper.add(sales)
    if not saved:
        raise Conflict()

    return ApiResponse(201, message='保存しました')
Exemple #5
0
def index(year, month):
    monthly_sales = __get_monthly_sales(year, month)
    items = __get_popular_items(year, month)
    return ApiResponse(
        200,
        data={'monthly_sales': monthly_sales, 'popular_items': items}
    )
Exemple #6
0
def index():
    categories = _get_categories()
    items = _get_items(categories)
    for category in categories:
        data = [i for i in items if category['id'] == i['category_id']]
        category['items'] = data

    return ApiResponse(200, data={'categories': categories})
Exemple #7
0
def user_loader_error_handler(identity):
    """
        Flask-JWT-extended
        user_loader_error_loader callback function
    """
    message = 'Unauthorization. Please reflesh token.'
    data = {'refleshing': True}
    return ApiResponse(401, message=message, data=data)
Exemple #8
0
def index():
    option = ItemSearchOption()
    if request.args is not None:
        option.category_id = request.args.get('category_id', type=int)
        option.q = request.args.get('q', type=str)

    mapper = ItemMapper()
    items = mapper.find(option)

    return ApiResponse(200, data={'items': items})
Exemple #9
0
def index():
    option = CategorySearchOption()
    if request.args is not None:
        option = CategorySearchOption(
            q=request.args.get('q', type=str)
        )

    mapper = CategoryMapper()
    categories = mapper.find(option)

    return ApiResponse(200, data={'categories': categories})
Exemple #10
0
def cancel():
    request_data = request.get_json()
    if request_data is None:
        raise BadRequest(description='売上IDをセットしてください')

    # todo: 売上データ存在チェック
    # if sales_data is None:
    #     raise NotFound(description='取消対象の売上データが見つかりませんでした')

    mapper = SalesMapper()
    canceled = mapper.cancel(request_data['params']['sales_id'])
    if not canceled:
        raise Conflict()

    return ApiResponse(201, message='売上キャンセルしました')
Exemple #11
0
def edit(id):
    request_data = request.get_json()
    if request_data is None:
        raise BadRequest()

    category = Category(**request.json)
    if not category.is_valid():
        raise BadRequest(
            description='保存エラー。エラー内容を確認してください。',
            response=category.validation_errors
        )

    mapper = CategoryMapper()
    saved = mapper.save(category)
    if not saved:
        raise Conflict()

    return ApiResponse(200, message='更新しました')
Exemple #12
0
def edit(id):
    request_data = request.get_json()
    if request_data is None:
        raise BadRequest()

    item = Item(**request.json)
    if not item.is_valid():
        raise BadRequest(
            description='保存エラー。エラー内容を確認してください。',
            response=item.validation_errors
        )

    mapper = ItemMapper()
    saved = mapper.save(item)
    if not saved:
        raise Conflict()

    return ApiResponse(200, message='更新しました')
Exemple #13
0
def add():
    request_data = request.get_json()
    if request_data is None:
        raise BadRequest()

    category = Category(**request.json)
    if not category.is_valid():
        raise BadRequest(
            description='保存エラー。エラー内容を確認してください。',
            response=category.validation_errors
        )

    mapper = CategoryMapper()
    if mapper.is_upper_limit():
        raise BadRequest(description='商品カテゴリの登録数は10件までです')

    saved = mapper.save(category)
    if not saved:
        raise Conflict()

    return ApiResponse(201, message='保存しました')