Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions om_connector_base/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
==============
Connector Base
==============

The common engine of the Odoo Mates connectors to shops and marketplaces (the Shopify Connector, and the
next ones). It has nothing to set up: the connectors that need it install it.

Features
========

* **Job queue**: every import, export and webhook of a connector is a job, run by the scheduled action
*Connectors: run the jobs*. Two workers never run the same job (row locking).
* **Retries**: a job that fails leaves no change and is tried again after 2, 4, 8 minutes and more, up to its
number of attempts; an error of the user (``UserError``) fails it at once. The connection gets told of the
jobs that fail for good.
* **No duplicates**: the jobs of a connection with the same key make one job, with the newest data.
* **Mappings**: an abstract model links an external ID to an Odoo record, once per connection.
* **Order workflows**: confirm an imported order, create and post its invoice, register one or several
payments, pay back its credit notes.
* **Secrets**: helpers to store access tokens and secrets encrypted (key: the ``om_connector_key`` option of
the Odoo configuration file, else the secret of the database).
* **Webhooks and HTTP**: HMAC-SHA256 signatures checked in constant time, and HTTP calls that wait and try
again on rate limits, server and network errors.

Jobs: Settings > Technical > Connectors > Jobs, and the menu of each connector. Done jobs are removed after
30 days (system parameter ``om_connector_base.job_keep_days``).

For developers
==============

* A connection inherits ``om.connector.backend.mixin`` and queues work with
``self._connector_enqueue('_connector_job_import_order', payload, dedupe_key='order:42')``; the method gets
the job and reads ``job.payload`` and ``job.backend``.
* A mapping inherits ``om.connector.binding.mixin`` (``_connector_find``, ``_connector_bind``).
* ``RetryableJobError(message, delay=60)`` tries a job again later.
2 changes: 2 additions & 0 deletions om_connector_base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import tools
22 changes: 22 additions & 0 deletions om_connector_base/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
'name': 'Connector Base',
'version': '1.0',
'author': 'Odoo Mates',
'category': 'Sales',
'summary': 'Job queue, retries, external ID mappings and webhook helpers for shop and marketplace connectors',
'description': 'The common engine of the Odoo Mates connectors (Shopify and others): a job queue with retries, '
'workflows for the imported orders (confirm, invoice, pay), mappings between external records and Odoo records, and webhook helpers.',
'maintainer': 'Odoo Mates',
'support': 'odoomates@gmail.com',
'license': 'LGPL-3',
'depends': ['mail', 'sale_management'],
'data': [
'security/om_connector_groups.xml',
'security/ir.access.csv',
'data/ir_cron.xml',
'views/om_connector_job_views.xml',
'views/om_connector_order_workflow_views.xml',
'views/menu.xml',
],
'images': ['static/description/banner.gif'],
}
11 changes: 11 additions & 0 deletions om_connector_base/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">
<record id="ir_cron_om_connector_run_jobs" model="ir.cron">
<field name="name">Connectors: run the jobs</field>
<field name="model_id" ref="model_om_connector_job"/>
<field name="state">code</field>
<field name="code">model._cron_run_jobs()</field>
<field name="interval_number">5</field>
<field name="interval_type">minutes</field>
</record>
</odoo>
4 changes: 4 additions & 0 deletions om_connector_base/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import om_connector_job
from . import om_connector_backend_mixin
from . import om_connector_binding_mixin
from . import om_connector_order_workflow
55 changes: 55 additions & 0 deletions om_connector_base/models/om_connector_backend_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from odoo import _, api, fields, models


class OmConnectorBackendMixin(models.AbstractModel):
""" A connection to an external system: a Shopify store, a WooCommerce site, a marketplace
account… Its jobs are queued with ``_connector_enqueue`` and shown from its form. """
_name = 'om.connector.backend.mixin'
_description = 'Connector Connection'

connector_job_todo_count = fields.Integer(string='Jobs To Do', compute='_compute_connector_job_counts')
connector_job_failed_count = fields.Integer(string='Failed Jobs', compute='_compute_connector_job_counts')

def _compute_connector_job_counts(self):
counts = {
(backend_id, state): count
for backend_id, state, count in self.env['om.connector.job'].sudo()._read_group(
[('backend_model', '=', self._name), ('backend_id', 'in', self.ids),
('state', 'in', ('pending', 'failed'))],
['backend_id', 'state'], ['__count'])
}
for backend in self:
backend.connector_job_todo_count = counts.get((backend.id, 'pending'), 0)
backend.connector_job_failed_count = counts.get((backend.id, 'failed'), 0)

def _connector_enqueue(self, method_name, payload=None, **kwargs):
""" Queue ``method_name`` (on this model, or on ``model_name``) for this connection.
See ``om.connector.job._enqueue`` for the options. """
self.ensure_one()
return self.env['om.connector.job']._enqueue(self, method_name, payload, **kwargs)

def _connector_enqueue_once(self, method_name, payload=None, **kwargs):
""" Queue ``method_name`` unless a job of this method is already to do for this connection:
for the scheduled work (fetch the new orders…), which must not pile up. """
self.ensure_one()
pending = self.env['om.connector.job'].sudo().search(
self._connector_job_domain() + [('method_name', '=', method_name), ('state', '=', 'pending')], limit=1)
return pending or self._connector_enqueue(method_name, payload, **kwargs)

def _connector_job_failed(self, job):
""" Called when ``job`` of this connection fails for good. """

def _connector_job_domain(self):
return [('backend_model', '=', self._name), ('backend_id', 'in', self.ids)]

def action_open_connector_jobs(self):
action = self.env['ir.actions.act_window']._for_xml_id('om_connector_base.om_connector_job_action')
action['domain'] = self._connector_job_domain()
action['context'] = {'search_default_filter_todo_failed': 1}
if len(self) == 1:
action['display_name'] = _("Jobs of %(connection)s", connection=self.display_name)
return action

def unlink(self):
self.env['om.connector.job'].sudo().search(self._connector_job_domain()).unlink()
return super().unlink()
36 changes: 36 additions & 0 deletions om_connector_base/models/om_connector_binding_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from odoo import api, fields, models


class OmConnectorBindingMixin(models.AbstractModel):
""" The link between a record of an external system and an Odoo record, for one connection:
a Shopify product and an Odoo product, a Shopify order and a sale order…

A model that inherits it has a Many2one to its connection, named by ``_connector_backend_field``,
and a Many2one to the Odoo record, named by ``_connector_record_field``. An external ID is linked
once per connection.
"""
_name = 'om.connector.binding.mixin'
_description = 'Connector Mapping'
_connector_backend_field = 'backend_id'
_connector_record_field = 'record_id'

external_id = fields.Char(string='External ID', required=True, index=True, readonly=True)
sync_date = fields.Datetime(string='Last Sync', readonly=True)

@api.model
def _connector_find(self, backend, external_ids):
""" The bindings of ``backend`` for one external ID or a list of them. """
if isinstance(external_ids, (str, int)):
external_ids = [external_ids]
return self.search([(self._connector_backend_field, '=', backend.id),
('external_id', 'in', [str(x) for x in external_ids])])

@api.model
def _connector_bind(self, backend, external_id, record, vals=None):
""" Link ``external_id`` to ``record`` for ``backend``, creating or updating the binding. """
binding = self._connector_find(backend, external_id)
values = {self._connector_record_field: record.id, 'sync_date': fields.Datetime.now(), **(vals or {})}
if binding:
binding.write(values)
return binding
return self.create({self._connector_backend_field: backend.id, 'external_id': str(external_id), **values})
Loading
Loading