Beispiel #1
0
def check_timestamps():
    time_ = time.time()

    canceled_orders = Orders_Info.select().where(
        Orders_Info.status == 'CREATE').where(time_ - Orders_Info.time > 10)
    not_finished_orders = Orders_Info.select().where(
        Orders_Info.status == 'AWAIT').where(time_ - Orders_Info.time > 10)

    message_info = []
    for c_order in canceled_orders:
        items = Orders_Content.select().where(
            Orders_Content.order_id == c_order.id)
        for item in items:
            update_amount(item.item_id, item.amount)

        message_info.append([c_order.id, c_order.admin, c_order.chat_id])

    for nf_order in not_finished_orders:
        items = Orders_Content.select().where(
            Orders_Content.order_id == nf_order.id)
        for item in items:
            update_amount(item.item_id, item.amount)

        message_info.append([nf_order.id, -1, nf_order.chat_id])

    query = Orders_Info.update({
        Orders_Info.status: 'DEL'
    }).where((Orders_Info.status == 'AWAIT')
             | (Orders_Info.status == 'CREATE')).where(
                 time_ - Orders_Info.time > 10)
    query.execute()

    return message_info
Beispiel #2
0
def set_time_order(id_):
    query = Orders_Info.update({
        Orders_Info.status: 'AWAIT',
        Orders_Info.time: time.time()
    }).where(Orders_Info.id == id_)
    query.execute()
    order = Orders_Info.select().where(Orders_Info.id == id_)
    return order[0].chat_id
Beispiel #3
0
def check_order(order_id):
    items = Orders_Content.select().where(Orders_Content.order_id == order_id)
    order = Orders_Info.select().where((Orders_Info.id == order_id)
                                       & (Orders_Info.status != 'DEL'))
    if (not items.exists()):
        query = Orders_Info.update({
            Orders_Info.status: 'DEL'
        }).where(Orders_Info.id == order_id)
        query.execute()
        return -1
    if (not order.exists()):
        return -2
    return 0
Beispiel #4
0
def cancel_by_id(id_, chat_id):
    order = Orders_Info.select().where((Orders_Info.id == id_)
                                       & (Orders_Info.chat_id == chat_id)
                                       & (Orders_Info.status != 'CREATE'))
    if not order.exists():
        raise NameError('Не ваш заказ, ая-яй-яй!')
    query = Orders_Info.update({
        Orders_Info.status: 'DEL'
    }).where(Orders_Info.id == id_)
    query.execute()
    items = Orders_Content.select().where(Orders_Content.order_id == id_)
    for item in items:
        update_amount(item.item_id, item.amount)

    return order[0].admin, order[0].chat_id
Beispiel #5
0
def get_order_items(id_, chat_id):
    order = Orders_Info.select().where((Orders_Info.id == id_)
                                       & (Orders_Info.chat_id == chat_id))
    if not order.exists():
        raise NameError('Не ваш заказ, ая-яй-яй!')
    items = Orders_Content.select().where(
        Orders_Content.order_id == order[0].id)
    return [[get_full_name_by_id(item.item_id), item.item_id, item.amount]
            for item in items]
Beispiel #6
0
def clear_base():
    query = Goods.delete().where(Goods.id != 1)
    query.execute()
    query = Goods.update({Goods.amount: 0}).where(Goods.id == 0)
    query.execute()
    query = Orders_Info.delete().where(Orders_Info.id > 0)
    query.execute()
    query = Orders_Content.delete().where(Orders_Content.id > 0)
    query.execute()
Beispiel #7
0
def finish_order(order_id):
    query = Orders_Info.update({
        Orders_Info.time: time.time(),
        Orders_Info.status: 'PROCESS'
    }).where(Orders_Info.id == order_id)
    query.execute()

    order_content = Orders_Content.select().where(
        Orders_Content.order_id == order_id)

    order = '\n'.join([
        '{}::{}::{}'.format(get_full_name_by_id(inst.item_id), inst.amount,
                            inst.cost) for inst in order_content
    ])

    order_info = Orders_Info.select().where(Orders_Info.id == order_id)
    ovrl_cost = sum([
        item.cost for item in Orders_Content.select().where(
            Orders_Content.order_id == order_id)
    ])
    return order_info[0].admin, order, order_id, ovrl_cost
Beispiel #8
0
def erase_item_order_by_ids(id_, order_id):
    query = Orders_Info.update({
        Orders_Info.time: time.time()
    }).where(Orders_Info.id == order_id)
    query.execute()
    item = Orders_Content.select().where(Orders_Content.item_id == id_).where(
        Orders_Content.order_id == order_id)
    amount = item[0].amount
    update_amount(id_, amount)
    query = Orders_Content.delete().where(Orders_Content.item_id == id_).where(
        Orders_Content.order_id == order_id)
    query.execute()
    return amount
Beispiel #9
0
def buy_item(id_, amount_, order_id):
    item = Goods.select().where(Goods.id == id_)[0]
    if (item.amount < amount_):
        raise OverflowError(' '.join([
            'Этого товара осталось всего',
            str(item.amount), '\nВведите другое количество'
        ]))
    if (amount_ <= 0):
        raise OverflowError('\n'.join([
            'Число товаров должно быть натуральным',
            'Введите другое количество'
        ]))

    price = Goods.select().where(Goods.id == id_)[0].price
    same_item = Orders_Content.select().where((
        Orders_Content.order_id == order_id) & (Orders_Content.item_id == id_))
    if same_item.exists():
        query = Orders_Content.update({
            Orders_Content.amount:
            Orders_Content.amount + amount_,
            Orders_Content.cost:
            Orders_Content.cost + amount_ * price
        }).where((Orders_Content.order_id == order_id)
                 & (Orders_Content.item_id == id_))
        query.execute()
    else:
        Orders_Content.create(order_id=order_id,
                              item_id=id_,
                              amount=amount_,
                              cost=price * amount_)

    query = Orders_Info.update({
        Orders_Info.time: time.time()
    }).where(Orders_Info.id == order_id)
    query.execute()

    update_amount(id_, -amount_)
Beispiel #10
0
def complete_order(id_):
    query = Orders_Info.update({
        Orders_Info.status: 'COMPLETE'
    }).where(Orders_Info.id == id_)
    query.execute()
Beispiel #11
0
def add_new_order(chat_id, admin):
    item = Orders_Info.create(chat_id=chat_id,
                              admin=admin,
                              time=time.time(),
                              status='CREATE')
    return item.id