trytonpsk-sale_web_channel/web.py

132 lines
4.6 KiB
Python

from trytond.model import fields, ModelView
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval, And, Or
from datetime import datetime, timedelta
from . import endpoints_rappi
TYPES = [
('', ''),
('mercadolibre', 'Mercado Libre'),
('melhous', 'Melhous'),
('shopify', 'Shopify'),
('rappi', 'Rappi'),
]
TYPE_INVOICE = [
('', ''),
('C', 'Computador'),
('P', 'POS'),
('M', 'Manual'),
('1', 'Venta Electronica'),
('2', 'Exportacion'),
('3', 'Factura por Contingencia Facturador'),
('4', 'Factura por Contingencia DIAN'),
('91', 'Nota Crédito Eléctronica'),
('92', 'Nota Débito Eléctronica'),
]
class Shop(metaclass=PoolMeta):
__name__ = 'web.shop'
secret_key = fields.Char('Secret Key')
app_id = fields.Char('Application ID')
redirect_uri = fields.Char('Redirect URI', states={
'invisible': (Eval('type') != 'mercadolibre'),
})
authorization_code = fields.Char('Authorization Code', states={
'invisible': (Eval('type') != 'mercadolibre'),
})
access_token = fields.Char('Access Token', states={
'invisible': And(
Eval('type') != 'mercadolibre',
Eval('type') != 'rappi'),
})
token_create_date = fields.DateTime('Token Create Date', states={
'invisible': (Eval('type') != 'mercadolibre'),
})
status_token = fields.Function(fields.Selection([
('expired', 'Expired'),
('active', 'Active'),
], 'Status Token', readonly=True, states={
'invisible': (Eval('type') != 'mercadolibre'),
}), 'get_status_token')
refresh_token = fields.Char('Refresh Token', states={
'invisible': (Eval('type') != 'mercadolibre'),
})
# state = fields.Selection([
# ('draft', 'Draft'),
# ('active', 'Active'),
# ('finished', 'Finished'),
# ], 'State', select=True, readonly=True)
freight_product = fields.Many2One('product.product', 'Freight Product')
# states=STATES)
bonus_product = fields.Many2One('product.product', 'Bonus Product', states={
'invisible': (Eval('type') != 'mercadolibre'),
})
tip_product = fields.Many2One('product.product', 'Tip Product', states={
'invisible': (Eval('type') != 'shopify'),
})
generic_product = fields.Many2One('product.product', 'Generic Product')
# states={
# 'invisible': (Eval('type') != 'shopify'),
# })
report = fields.Many2One('ir.action.report', 'Report')
invoice_type = fields.Selection(TYPE_INVOICE, 'Type Invoice')
seller_id = fields.Char('Seller ID', states={
'invisible': ~Eval('type').in_(['mercadolibre', 'rappi']),
})
@classmethod
def __setup__(cls):
super(Shop, cls).__setup__()
cls.type.selection = TYPES
cls._buttons.update({
'generate_token_access': {
'invisible': ~Eval('type').in_(['mercadolibre', 'rappi']),
},
'synchronize_products': {
'invisible': ~Eval('type').in_(['mercadolibre', 'rappi']),
},
})
def get_status_token(self, name):
if self.token_create_date:
now = datetime.now()
res = (now - self.token_create_date).total_seconds()
if res >= 21600:
return 'expired'
else:
return 'active'
else:
return 'expired'
@classmethod
@ModelView.button
def generate_token_access(cls, records):
if records:
for record in records:
if record.type == 'mercadolibre':
MercadoLibre = Pool().get('sale.web_channel.mercado_libre')
channels = MercadoLibre.search([
('state', '=', 'active'),
('type', '=', 'mercadolibre'),
])
if record.type == 'rappi':
record.generate_token_access_rappi()
def generate_token_access_rappi(self):
api_rappi = endpoints_rappi.RappiAPI(self)
result = api_rappi.get_token_access(self.seller_id, self.secret_key)
self.access_token = result['access_token']
self.save()
def synchronize_products(self):
if self.type == 'rappi':
items = []
for category in self.admin_category.childs:
items.extend(self.get_products(category))
print(items)
api_rappi = endpoints_rappi.RappiAPI(self)
api_rappi.create_menu_store(items)