From 0d37b9374a9b6ca067fc6d63a40984776d8831bc Mon Sep 17 00:00:00 2001 From: Elvis Date: Thu, 17 Aug 2023 14:52:47 -0500 Subject: [PATCH] create token and menu rappi --- __init__.py | 3 + rappi.py | 239 +++++++++++++++++++++++++++++++++ view/sale_web_channel_form.xml | 2 + web_channel.py | 57 +++++++- web_channel.xml | 10 ++ 5 files changed, 306 insertions(+), 5 deletions(-) create mode 100644 rappi.py diff --git a/__init__.py b/__init__.py index 6c84645..8e247c7 100644 --- a/__init__.py +++ b/__init__.py @@ -6,6 +6,7 @@ from . import party from . import web_channel from . import mercado_libre from . import shopify +from . import rappi from . import api_log from . import ir from . import routes @@ -19,6 +20,7 @@ def register(): web_channel.SaleWebChannel, mercado_libre.MercadoLibre, shopify.Shopify, + rappi.Rappi, sale.Sale, sale.SaleForChannelStart, web_channel.SynchronizeChannelOrdersStart, @@ -33,6 +35,7 @@ def register(): sale.SaleForChannel, web_channel.SynchronizeChannelOrders, web_channel.FinishInvoices, + web_channel.SynchronizeMenuWizard, module='sale_web_channel', type_='wizard') Pool.register( sale.SaleForChannelReport, diff --git a/rappi.py b/rappi.py new file mode 100644 index 0000000..98463cc --- /dev/null +++ b/rappi.py @@ -0,0 +1,239 @@ +# -*- 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_channel import SaleWebChannel +from urllib.parse import urlencode +import json +import requests +import base64 +import hmac +import hashlib +from pprint import pprint + + +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(SaleWebChannel): + 'Rappi' + __name__ = 'sale.web_channel.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): + print('holis') + 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): + menu = { + # 'storeId': self.store_id, + 'storeId': '900152684', + 'items': [] + } + menu_append = menu['items'].append + pool = Pool() + Category = pool.get('product.template-product.category') + # price_list = self.shop.price_list.lines + # for index, line in enumerate(price_list): + # if line.category and not line.product: + # self.create_menu_by_global_category(line, Category) + # elif not line.category and line.product: + # # elif line.category and line.product: + # # pass + # # else: + # # raise UserWarning('BAD CONFIGURATION IN LINE ID ', line.id) + + # # menu_append({ + # # 'category': { + # # 'id': line.product.categories[0].id, + # # 'maxQty': 0, + # # 'minQty': 0, + # # 'name': line.product.categories[0].name, + # # }, + # # 'children': [], + # # 'name': line.product.categories[0].name, + # # 'description': line.product.description if line.product.description else 'no esta disponible', + # # 'imageUrl': line.product.images[0].image_url if line.product.images else '', + # # 'price': float(line.price_w_tax_computed), + # # 'sku': line.product.code, + # # 'sortingPosition': 0, + # # "type": "PRODUCT", + # # # 'maxLimit': '', + # # }) + # # if hasattr(line.product, 'products_mix') and line.product.products_mix: + # # for mix in line.product.products_mix: + # # menu['items'][0]['children'].append({ + # # "category": { + # # "id": mix.categories[0].id, + # # "maxQty": 1, + # # "minQty": 0, + # # "name": mix.categories[0].name, + # # "sortingPosition": 0 + # # }, + # # "name": mix.name, + # # "description": mix.description, + # # "price": float(mix.list_price), + # # "sku": mix.id, + # # "maxLimit": 1, + # # "sortingPosition": 1, + # # "type": "PRODUCT" + # # }) + + menu = { + "storeId": "900152684", + "items": [ + { + "name": "Grilled Chicken Burger", + "description": "Grilled chicken burger description", + "price": 14000, + "sku": "10", + "sortingPosition": 0, + "type": "PRODUCT", + "category": { + "id": "2090019638", + "maxQty": 0, + "minQty": 0, + "name": "Burgers", + "sortingPosition": 0 + }, + "children": [ + { + "category": { + "id": "211", + "maxQty": 1, + "minQty": 0, + "name": "Do you want to add?", + "sortingPosition": 0 + }, + "name": "French Fries", + "description": "crunchy french fries", + "price": 5000, + "sku": "1", + "maxLimit": 1, + "sortingPosition": 1, + "type": "TOPPING" + }, + { + "category": { + "id": "211", + "maxQty": 1, + "minQty": 0, + "name": "Do you want to add?", + "sortingPosition": 0 + }, + "name": "Potato Wedges", + "price": 7000, + "sku": "2", + "maxLimit": 1, + "sortingPosition": 1, + "type": "TOPPING" + } + ] + }, + { + "name": "Hawaiian Pizza", + "description": "hawaiian pizza description", + "price": 18000, + "sku": "11", + "sortingPosition": 1, + "type": "PRODUCT", + "category": { + "id": "2090019639", + "maxQty": 0, + "minQty": 0, + "name": "Pizzas", + "sortingPosition": 1 + }, + "children": [] + } + ] + } + 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, + } + pprint(menu_json) + response = requests.request("POST", URL, headers=headers, data=menu_json) + pprint(response.text.encode('utf8')) + + def create_menu_by_global_category(self, line, Category): + pass + # category = Category.search_read([('categories', 'in', '')]) + # dict_menu = { + # 'category': { + # 'id': line.product.category.id, + # 'maxQty': 0, + # 'minQty': 0, + # 'name': line.product.category.name, + # }, + + # dict_menu['children'].append(dict_child) + # 'children': [], + # 'name': line.product.categories[0].name, + # 'description': line.product.description if line.product.description else 'no esta disponible', + # 'imageUrl': line.product.images[0].image_url if line.product.images else '', + # 'price': float(line.price_w_tax_computed), + # 'sku': line.product.code, + # 'sortingPosition': 0, + # "type": "PRODUCT", + # # 'maxLimit': '', + # }) + + def create_menu_by_local_category(self, line): + if line.product.categories[0]: + category = { + 'id': line.product.categories[0].id, + 'maxQty': 0, + 'minQty': 0, + 'name': line.product.categories[0].name, + 'children': [] + } + category['children'].append({ + + }) diff --git a/view/sale_web_channel_form.xml b/view/sale_web_channel_form.xml index c13dd2f..effa2d2 100644 --- a/view/sale_web_channel_form.xml +++ b/view/sale_web_channel_form.xml @@ -66,6 +66,8 @@ The COPYRIGHT file at the top level of this repository contains the full copyrig icon="tryton-forward"/>