mirror of
https://github.com/bringout/oca-ocb-technical.git
synced 2026-04-19 19:52:00 +02:00
Initial commit: Technical packages
This commit is contained in:
commit
3473fa71a0
873 changed files with 297766 additions and 0 deletions
6
odoo-bringout-oca-ocb-iap/iap/models/__init__.py
Normal file
6
odoo-bringout-oca-ocb-iap/iap/models/__init__.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from . import iap_account
|
||||
from . import res_config_settings
|
||||
from . import iap_enrich_api
|
||||
159
odoo-bringout-oca-ocb-iap/iap/models/iap_account.py
Normal file
159
odoo-bringout-oca-ocb-iap/iap/models/iap_account.py
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
import werkzeug.urls
|
||||
|
||||
from odoo import api, fields, models
|
||||
from odoo.addons.iap.tools import iap_tools
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_ENDPOINT = 'https://iap.odoo.com'
|
||||
|
||||
|
||||
class IapAccount(models.Model):
|
||||
_name = 'iap.account'
|
||||
_rec_name = 'service_name'
|
||||
_description = 'IAP Account'
|
||||
|
||||
service_name = fields.Char()
|
||||
account_token = fields.Char(default=lambda s: uuid.uuid4().hex)
|
||||
company_ids = fields.Many2many('res.company')
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
accounts = super().create(vals_list)
|
||||
if self.env['ir.config_parameter'].sudo().get_param('database.is_neutralized'):
|
||||
# Disable new accounts on a neutralized database
|
||||
for account in accounts:
|
||||
account.account_token = f"{account.account_token.split('+')[0]}+disabled"
|
||||
return accounts
|
||||
|
||||
@api.model
|
||||
def get(self, service_name, force_create=True):
|
||||
domain = [
|
||||
('service_name', '=', service_name),
|
||||
'|',
|
||||
('company_ids', 'in', self.env.companies.ids),
|
||||
('company_ids', '=', False)
|
||||
]
|
||||
accounts = self.search(domain, order='id desc')
|
||||
accounts_without_token = accounts.filtered(lambda acc: not acc.account_token)
|
||||
if accounts_without_token:
|
||||
with self.pool.cursor() as cr:
|
||||
# In case of a further error that will rollback the database, we should
|
||||
# use a different SQL cursor to avoid undo the accounts deletion.
|
||||
|
||||
# Flush the pending operations to avoid a deadlock.
|
||||
self.env.flush_all()
|
||||
IapAccount = self.with_env(self.env(cr=cr))
|
||||
# Need to use sudo because regular users do not have delete right
|
||||
IapAccount.search(domain + [('account_token', '=', False)]).sudo().unlink()
|
||||
accounts = accounts - accounts_without_token
|
||||
if not accounts:
|
||||
with self.pool.cursor() as cr:
|
||||
# Since the account did not exist yet, we will encounter a NoCreditError,
|
||||
# which is going to rollback the database and undo the account creation,
|
||||
# preventing the process to continue any further.
|
||||
|
||||
# Flush the pending operations to avoid a deadlock.
|
||||
self.env.flush_all()
|
||||
IapAccount = self.with_env(self.env(cr=cr))
|
||||
account = IapAccount.search(domain, order='id desc', limit=1)
|
||||
if not account:
|
||||
if not force_create:
|
||||
return account
|
||||
account = IapAccount.create({'service_name': service_name})
|
||||
# fetch 'account_token' into cache with this cursor,
|
||||
# as self's cursor cannot see this account
|
||||
account_token = account.account_token
|
||||
account = self.browse(account.id)
|
||||
self.env.cache.set(account, IapAccount._fields['account_token'], account_token)
|
||||
return account
|
||||
accounts_with_company = accounts.filtered(lambda acc: acc.company_ids)
|
||||
if accounts_with_company:
|
||||
return accounts_with_company[0]
|
||||
return accounts[0]
|
||||
|
||||
@api.model
|
||||
def get_credits_url(self, service_name, base_url='', credit=0, trial=False):
|
||||
""" Called notably by ajax crash manager, buy more widget, partner_autocomplete, sanilmail. """
|
||||
dbuuid = self.env['ir.config_parameter'].sudo().get_param('database.uuid')
|
||||
if not base_url:
|
||||
endpoint = iap_tools.iap_get_endpoint(self.env)
|
||||
route = '/iap/1/credit'
|
||||
base_url = endpoint + route
|
||||
account_token = self.get(service_name).account_token
|
||||
d = {
|
||||
'dbuuid': dbuuid,
|
||||
'service_name': service_name,
|
||||
'account_token': account_token,
|
||||
'credit': credit,
|
||||
}
|
||||
if trial:
|
||||
d.update({'trial': trial})
|
||||
return '%s?%s' % (base_url, werkzeug.urls.url_encode(d))
|
||||
|
||||
@api.model
|
||||
def get_account_url(self):
|
||||
""" Called only by res settings """
|
||||
route = '/iap/services'
|
||||
endpoint = iap_tools.iap_get_endpoint(self.env)
|
||||
all_accounts = self.search([
|
||||
'|',
|
||||
('company_ids', '=', self.env.company.id),
|
||||
('company_ids', '=', False),
|
||||
])
|
||||
|
||||
global_account_per_service = {
|
||||
account.service_name: account.account_token
|
||||
for account in all_accounts.filtered(lambda acc: not acc.company_ids)
|
||||
}
|
||||
company_account_per_service = {
|
||||
account.service_name: account.account_token
|
||||
for account in all_accounts.filtered(lambda acc: acc.company_ids)
|
||||
}
|
||||
|
||||
# Prioritize company specific accounts over global accounts
|
||||
account_per_service = {**global_account_per_service, **company_account_per_service}
|
||||
|
||||
parameters = {'tokens': list(account_per_service.values())}
|
||||
|
||||
return '%s?%s' % (endpoint + route, werkzeug.urls.url_encode(parameters))
|
||||
|
||||
@api.model
|
||||
def get_config_account_url(self):
|
||||
""" Called notably by ajax partner_autocomplete. """
|
||||
account = self.env['iap.account'].get('partner_autocomplete')
|
||||
action = self.env.ref('iap.iap_account_action')
|
||||
menu = self.env.ref('iap.iap_account_menu')
|
||||
no_one = self.user_has_groups('base.group_no_one')
|
||||
if account:
|
||||
url = "/web#id=%s&action=%s&model=iap.account&view_type=form&menu_id=%s" % (account.id, action.id, menu.id)
|
||||
else:
|
||||
url = "/web#action=%s&model=iap.account&view_type=form&menu_id=%s" % (action.id, menu.id)
|
||||
return no_one and url
|
||||
|
||||
@api.model
|
||||
def get_credits(self, service_name):
|
||||
account = self.get(service_name, force_create=False)
|
||||
credit = 0
|
||||
|
||||
if account:
|
||||
route = '/iap/1/balance'
|
||||
endpoint = iap_tools.iap_get_endpoint(self.env)
|
||||
url = endpoint + route
|
||||
params = {
|
||||
'dbuuid': self.env['ir.config_parameter'].sudo().get_param('database.uuid'),
|
||||
'account_token': account.account_token,
|
||||
'service_name': service_name,
|
||||
}
|
||||
try:
|
||||
credit = iap_tools.iap_jsonrpc(url=url, params=params)
|
||||
except Exception as e:
|
||||
_logger.info('Get credit error : %s', str(e))
|
||||
credit = -1
|
||||
|
||||
return credit
|
||||
39
odoo-bringout-oca-ocb-iap/iap/models/iap_enrich_api.py
Normal file
39
odoo-bringout-oca-ocb-iap/iap/models/iap_enrich_api.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import models, api
|
||||
from odoo.addons.iap.tools import iap_tools
|
||||
|
||||
|
||||
class IapEnrichAPI(models.AbstractModel):
|
||||
_name = 'iap.enrich.api'
|
||||
_description = 'IAP Lead Enrichment API'
|
||||
_DEFAULT_ENDPOINT = 'https://iap-services.odoo.com'
|
||||
|
||||
@api.model
|
||||
def _contact_iap(self, local_endpoint, params):
|
||||
account = self.env['iap.account'].get('reveal')
|
||||
dbuuid = self.env['ir.config_parameter'].sudo().get_param('database.uuid')
|
||||
params['account_token'] = account.account_token
|
||||
params['dbuuid'] = dbuuid
|
||||
base_url = self.env['ir.config_parameter'].sudo().get_param('enrich.endpoint', self._DEFAULT_ENDPOINT)
|
||||
return iap_tools.iap_jsonrpc(base_url + local_endpoint, params=params, timeout=300)
|
||||
|
||||
@api.model
|
||||
def _request_enrich(self, lead_emails):
|
||||
""" Contact endpoint to get enrichment data.
|
||||
|
||||
:param lead_emails: dict{lead_id: email}
|
||||
:return: dict{lead_id: company data or False}
|
||||
:raise: several errors, notably
|
||||
* InsufficientCreditError: {
|
||||
"credit": 4.0,
|
||||
"service_name": "reveal",
|
||||
"base_url": "https://iap.odoo.com/iap/1/credit",
|
||||
"message": "You don't have enough credits on your account to use this service."
|
||||
}
|
||||
"""
|
||||
params = {
|
||||
'domains': lead_emails,
|
||||
}
|
||||
return self._contact_iap('/iap/clearbit/1/lead_enrichment_email', params=params)
|
||||
15
odoo-bringout-oca-ocb-iap/iap/models/res_config_settings.py
Normal file
15
odoo-bringout-oca-ocb-iap/iap/models/res_config_settings.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, models
|
||||
|
||||
|
||||
class ResConfigSettings(models.TransientModel):
|
||||
_inherit = 'res.config.settings'
|
||||
|
||||
@api.model
|
||||
def _redirect_to_iap_account(self):
|
||||
return {
|
||||
'type': 'ir.actions.act_url',
|
||||
'url': self.env['iap.account'].get_account_url(),
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue