trytonpsk-sale_web_channel/rappi.py

116 lines
3.8 KiB
Python

# -*- coding: UTF-8 -*-
# This file is part electronic_mail_template module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from decimal import Decimal
from trytond.pool import Pool
from itertools import chain
from trytond.transaction import Transaction
from urllib.parse import urlencode
from datetime import datetime, date
from trytond.pyson import Eval
from .web import Shop
import json
import requests
from . import endpoints_rappi
URL_DEV = 'https://rests-integrations-dev.auth0.com/oauth/token'
URL_PRODUCTION = 'https://rests-integrations.auth0.com/oauth/token'
HEADERS = {
'Accept': 'application/json',
'Content-type': 'application/json'
}
class Rappi(Shop):
'Rappi'
__name__ = 'web.shop.rappi'
@classmethod
def __setup__(cls):
super(Rappi, cls).__setup__()
# cls._buttons.update({
# 'generate_token_access': {
# 'invisible': Eval('state') != 'active',
# },
# })
def _get_context(self):
print(self)
return {
'company': self.company.id,
'user': self.user.id,
'shops': [self.shop.id],
'shop': self.shop.id,
'language': 'es'
}
def generate_token_access(self):
token_request = {
"client_id": self.app_id,
"client_secret": self.secret_key,
"audience": 'https://int-public-api-v2/api',
"grant_type": "client_credentials",
}
result = requests.post(
URL_DEV,
json=token_request,
headers=HEADERS).json()
print(result)
self.access_token = result['access_token']
self.save()
def synchronize_menu_rappi(self):
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)
def get_products(self, category):
children_list = []
for child in category.childs:
children_list.append({
"category": {
"id": category.id,
"maxQty": 0,
"minQty": 0,
"name": category.name,
"sortingPosition": 0,
"children": []
},
})
children_list[0]['children'] = self.get_products(child)
if not category.childs:
for product in category.products:
children_list.append({
"name": product.name,
"description": product.description if product.description else 'not available',
"price": float(product.list_price),
"sku": product.code,
"sortingPosition": 0,
"type": "PRODUCT",
"category": {
"id": category.id,
"maxQty": 0,
"minQty": 0,
"name": category.name,
"sortingPosition": 0
}
})
return children_list
# rappi_api = endpoints_rappi.RappiAPI(self)
# rappi_api.create_menu_store(self.shop.store_id, items)
# menu_json = json.dumps(menu)
# URL = 'https://microservices.dev.rappi.com/api/v2/restaurants-integrations-public-api/menu'
# token = self.access_token
# headers = {
# 'Content-Type': 'application/json',
# 'x-authorization': 'bearer ' + token,
# }
# response = requests.request("POST", URL, headers=headers, data=menu_json)