create token and menu rappi

This commit is contained in:
Elvis 2023-08-17 14:52:47 -05:00
parent a474e55de2
commit 0d37b9374a
5 changed files with 306 additions and 5 deletions

View File

@ -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,

239
rappi.py Normal file
View File

@ -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({
})

View File

@ -66,6 +66,8 @@ The COPYRIGHT file at the top level of this repository contains the full copyrig
icon="tryton-forward"/>
<button name="refresh_token_b" string="Refresh Token"
icon="tryton-ok"/>
<button name="generate_token_access" string="generate_token_access"
icon="tryton-ok"/>
</group>
</group>

View File

@ -3,7 +3,7 @@
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from trytond.model import Workflow, ModelView, ModelSQL, fields
from trytond.pyson import Eval
from trytond.pyson import Eval, And
from datetime import datetime, timedelta
from trytond.pool import Pool
from trytond.wizard import (Wizard, StateTransition, StateView, Button)
@ -70,7 +70,9 @@ class SaleWebChannel(Workflow, ModelSQL, ModelView):
'invisible': (Eval('channel_name') != 'mercadolibre'),
})
access_token = fields.Char('Access Token', states={
'invisible': (Eval('channel_name') != 'mercadolibre'),
'invisible': And(
Eval('channel_name') != 'mercadolibre',
Eval('channel_name') != 'rappi'),
})
creation_time = fields.DateTime('Creation Time', states={
'invisible': (Eval('channel_name') != 'mercadolibre'),
@ -120,7 +122,9 @@ class SaleWebChannel(Workflow, ModelSQL, ModelView):
'invisible': (Eval('channel_name') != 'shopify'),
})
host_name = fields.Char('Host Name', states={
'invisible': (Eval('channel_name') != 'shopify'),
'invisible': And(
Eval('channel_name') != 'shopify',
Eval('channel_name') != 'rappi'),
})
party = fields.Many2One('party.party', 'party', required=False)
payment_term = fields.Many2One('account.invoice.payment_term',
@ -146,7 +150,10 @@ class SaleWebChannel(Workflow, ModelSQL, ModelView):
'invisible': Eval('state') != 'active',
},
'refresh_token_b': {
'invisible': Eval('channel_name') != 'mercadolibre',
'invisible': And(Eval('channel_name') != 'mercadolibre', Eval('channel_name') != 'rappi'),
},
'synchronize_menu': {
'invisible': And(Eval('channel_name') != 'rappi', Eval('state') == 'active'),
},
})
@ -180,6 +187,13 @@ class SaleWebChannel(Workflow, ModelSQL, ModelView):
def finished(cls, records):
pass
@classmethod
@ModelView.button
def synchronize_menu(cls, records):
for record in records:
if record.channel_name == 'rappi':
record.synchronize_menu_rappi()
@classmethod
@ModelView.button
def refresh_token_b(cls, records):
@ -192,6 +206,14 @@ class SaleWebChannel(Workflow, ModelSQL, ModelView):
('channel_name', '=', 'mercadolibre'),
])
res = channels[0]._validate_token()
if record.channel_name == 'rappi':
Rappi = Pool().get('sale.web_channel.rappi')
channels = Rappi.search([
('state', '=', 'active'),
('channel_name', '=', 'rappi'),
])
res = channels[0].generate_token_access()
print(res, 'fuck yeah!')
def send_mail_notification(self, message):
Template = Pool().get('email.template')
@ -220,7 +242,7 @@ class SaleWebChannel(Workflow, ModelSQL, ModelView):
report = Report.execute([record.id], {'id': record.id})
ext, data, filename, file_name = report
return data
class SynchronizeChannelOrdersStart(ModelView):
'Synchronize Channel orders Start'
@ -453,3 +475,28 @@ class SynchronizeChannelOrdersDone(ModelView):
@staticmethod
def default_result():
return 'successful synchronization'
class SynchronizeMenuWizard(Wizard):
'Synchronize Menu'
__name__ = 'sale_web_channel.synchronize_menu'
start_state = 'synchronize_menu'
synchronize_menu = StateTransition()
@classmethod
def transition_synchronize_menu(self):
pool = Pool()
active_id = Transaction().context.get('active_id')
Chanel = pool.get('sale.web_channel')
chanel = Chanel(active_id)
print(chanel)
if chanel.channel_name == 'rappi':
print('ya paso')
Rappi = Pool().get('sale.web_channel.rappi')
channels = Rappi.search([
('state', '=', 'active'),
('channel_name', '=', 'rappi'),
])
res = channels[0].synchronize_menu_rappi()
print(res, 'fuck yeah')
return 'end'

View File

@ -62,5 +62,15 @@ The COPYRIGHT file at the top level of this repository contains the full copyrig
<menuitem action="finish_invoices_wizard" parent="sale.menu_sale"
id="menu_finish_invoices" sequence="110"/>
<record model="ir.action.wizard" id="wizard_synchronize_menu">
<field name="name">Synchronize Menu</field>
<field name="wiz_name">sale_web_channel.synchronize_menu</field>
</record>
<record model="ir.action.keyword" id="action_synchronize_menu_keyword">
<field name="keyword">form_action</field>
<field name="model">sale.web_channel,-1</field>
<field name="action" ref="wizard_synchronize_menu"/>
</record>
</data>
</tryton>