summaryrefslogtreecommitdiff
path: root/addons/iap/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/iap/models
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/iap/models')
-rw-r--r--addons/iap/models/__init__.py5
-rw-r--r--addons/iap/models/iap_account.py120
-rw-r--r--addons/iap/models/res_config_settings.py15
3 files changed, 140 insertions, 0 deletions
diff --git a/addons/iap/models/__init__.py b/addons/iap/models/__init__.py
new file mode 100644
index 00000000..d1483778
--- /dev/null
+++ b/addons/iap/models/__init__.py
@@ -0,0 +1,5 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from . import iap_account
+from . import res_config_settings
diff --git a/addons/iap/models/iap_account.py b/addons/iap/models/iap_account.py
new file mode 100644
index 00000000..f4be36c7
--- /dev/null
+++ b/addons/iap/models/iap_account.py
@@ -0,0 +1,120 @@
+# -*- 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
+ 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')
+ 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.flush()
+ 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)
+ d = {'dbuuid': self.env['ir.config_parameter'].sudo().get_param('database.uuid')}
+
+ return '%s?%s' % (endpoint + route, werkzeug.urls.url_encode(d))
+
+ @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
diff --git a/addons/iap/models/res_config_settings.py b/addons/iap/models/res_config_settings.py
new file mode 100644
index 00000000..9c999eed
--- /dev/null
+++ b/addons/iap/models/res_config_settings.py
@@ -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(),
+ }