Skip to content

Commit

Permalink
[IMP] connector_carepoint: Upgrade address handling
Browse files Browse the repository at this point in the history
* Add Abstract importer logic
* Add partner sync logic
* Implement new logic in address_patient
  • Loading branch information
lasley committed Sep 6, 2016
1 parent b9e4868 commit 5617a7d
Show file tree
Hide file tree
Showing 9 changed files with 522 additions and 64 deletions.
1 change: 1 addition & 0 deletions connector_carepoint/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

# Address / Relations
from . import address
from . import address_abstract
from . import address_patient
from . import address_pharmacy
from . import address_physician
Expand Down
57 changes: 43 additions & 14 deletions connector_carepoint/models/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging
from openerp import models, fields
from openerp import models, fields, api

from openerp.addons.connector.connector import ConnectorUnit
from openerp.addons.connector.unit.mapper import (mapping,
ExportMapper
Expand Down Expand Up @@ -45,12 +46,15 @@ class CarepointAddress(models.Model):
_name = 'carepoint.address'
_description = 'Carepoint Address'

partner_id = fields.Many2one(
string='Partner',
comodel_name='res.partner',
required=True,
ondelete='cascade',
)
PARTNER_ATTRS = [
'street',
'street2',
'zip',
'city',
'state_id',
'country_id',
]

street = fields.Char()
street2 = fields.Char()
zip = fields.Char()
Expand All @@ -63,13 +67,44 @@ class CarepointAddress(models.Model):
string='Country',
comodel_name='res.country',
)
partner_id = fields.Many2one(
string='Partner',
comodel_name='res.partner',
readonly=True,
)

carepoint_bind_ids = fields.One2many(
comodel_name='carepoint.carepoint.address',
inverse_name='odoo_id',
string='Carepoint Bindings',
)

@api.multi
@api.depends('partner_id', *PARTNER_ATTRS)
def _sync_partner(self):
for rec_id in self:
if not len(rec_id.partner_id):
continue
rec_id.partner_id.write(
self._get_partner_sync_vals(self)
)

@api.model
def _get_partner_sync_vals(self, partner):
""" It extracts sync values from the partner or partner like record
Params:
parner: Recordset of partner or address
Returns:
``dict`` of values for create or write
"""
vals = {}
for attr in self.PARTNER_ATTRS:
val = getattr(partner, attr, None)
if getattr(val, 'id', None):
val = val.id
vals[attr] = val
return vals


@carepoint
class CarepointAddressAdapter(CarepointCRUDAdapter):
Expand All @@ -78,7 +113,7 @@ class CarepointAddressAdapter(CarepointCRUDAdapter):


@carepoint
class AddressUnit(ConnectorUnit):
class CarepointAddressUnit(ConnectorUnit):
_model_name = 'carepoint.carepoint.address'

def _import_by_filter(self, **filters):
Expand Down Expand Up @@ -137,12 +172,6 @@ class CarepointAddressImporter(CarepointImporter):
_model_name = ['carepoint.carepoint.address']
_base_mapper = CarepointAddressImportMapper

#
# def _after_import(self, partner_binding):
# """ Import the addresses """
# book = self.unit_for(PartnerAddressBook, model='carepoint.address')
# book.import_addresses(self.carepoint_id, partner_binding.id)


@carepoint
class CarepointAddressExportMapper(ExportMapper):
Expand Down
141 changes: 141 additions & 0 deletions connector_carepoint/models/address_abstract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging
from openerp import models, fields, api
from openerp.addons.connector.unit.mapper import (mapping,
only_create,
)
from ..unit.mapper import CarepointImportMapper
from ..backend import carepoint
from ..unit.import_synchronizer import CarepointImporter

from .address import CarepointAddress

from ..connector import add_checkpoint


_logger = logging.getLogger(__name__)


class CarepointAddressAbstract(models.AbstractModel):
""" Adds the ``one2many`` relation to the Carepoint bindings
(``carepoint_bind_ids``)
"""
_inherits = {'carepoint.address': 'address_id'}
_name = 'carepoint.address.abstract'
_description = 'Carepoint Address Abstract'

address_id = fields.Many2one(
string='Address',
comodel_name='carepoint.address',
required=True,
ondelete='cascade',
)
partner_id = fields.Many2one(
string='Partner',
comodel_name='res.partner',
required=True,
compute='_compute_partner_id',
inverse='_set_partner_id',
)

@api.multi
def _compute_partner_id(self):
""" It sets the partner_id from the address_id """
for rec_id in self:
rec_id.partner_id = rec_id.address_id.partner_id.id

@api.multi
@api.depends(
'partner_id',
*['partner_id.%s' % a for a in CarepointAddress.PARTNER_ATTRS]
)
def _set_partner_id(self):
""" It sets the partner_id and attrs on the address_id """
for rec_id in self:
need_sync = True
if rec_id.partner_id != rec_id.address_id.partner_id:
rec_id.address_id.write({'partner_id': rec_id.partner_id.id})
if not CarepointAddressAbstractImportMapper._has_empty_address(
rec_id
):
vals = self.env['carepoint.address']._get_partner_sync_vals(
rec_id.partner_id,
)
if any(vals.values()):
need_sync = False
if need_sync:
rec_id.address_id._sync_partner()
else:
rec_id.address_id.write(vals)


@carepoint
class CarepointAddressAbstractImportMapper(CarepointImportMapper):

@staticmethod
def _has_empty_address(partner):
""" It determines if the provided partner has an empty address.
Currently only looks at ``street`` and ``street2``.
"""
return not any([partner.street, partner.street2])

def _get_partner_defaults(self, record):
""" It provides a method for partner defaults
This could be handy to provide custom defaults via modules
Params:
record: ``dict`` of carepoint record
"""
return {
'type': 'delivery',
'customer': True,
}

def partner_id(self, record, medical_entity):
""" It returns either the existing partner or a new one
Params:
record: ``dict`` of carepoint record
medical_entity: Recordset with a ``partner_id`` column
Return:
``dict`` of values for mapping
"""
if CarepointAddressAbstractImportMapper._has_empty_address(
medical_entity.commercial_partner_id
):
_logger.info('Empty address, sending %s',
medical_entity.commercial_partner_id)
partner = medical_entity.commercial_partner_id
else:
_logger.info('Full address, sending defaults')
vals = self._get_partner_defaults(record)
vals.update({
'parent_id': medical_entity.commercial_partner_id.id,
})
partner = self.env['res.partner'].create(vals)
return {'partner_id': partner.id}

@mapping
@only_create
def address_id(self, record):
binder = self.binder_for('carepoint.carepoint.address')
address_id = binder.to_odoo(record['addr_id'])
return {'address_id': address_id}


@carepoint
class CarepointAddressAbstractImporter(CarepointImporter):
_model_name = 'carepoint.address.abstract'

def _import_dependencies(self):
""" Import depends for record """
self._import_dependency(self.carepoint_record['addr_id'],
'carepoint.carepoint.address')

def _create(self, data):
binding = super(CarepointAddressAbstractImporter, self)._create(data)
add_checkpoint(
self.session, binding._name, binding.id, binding.backend_id.id
)
return binding
66 changes: 22 additions & 44 deletions connector_carepoint/models/address_patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
only_create,
)
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import CarepointImportMapper
from ..backend import carepoint
from ..unit.import_synchronizer import (DelayedBatchImporter,
CarepointImporter,
)
from ..unit.import_synchronizer import DelayedBatchImporter

from .address_abstract import (CarepointAddressAbstractImportMapper,
CarepointAddressAbstractImporter,
)

_logger = logging.getLogger(__name__)

Expand All @@ -36,19 +36,13 @@ class CarepointCarepointAddressPatient(models.Model):


class CarepointAddressPatient(models.Model):
""" Adds the ``one2many`` relation to the Carepoint bindings
""" Adds the ``One2many`` relation to the Carepoint bindings
(``carepoint_bind_ids``)
"""
_inherits = {'carepoint.address': 'address_id'}
_name = 'carepoint.address.patient'
_inherit = 'carepoint.address.abstract'
_description = 'Carepoint Address Patient'

address_id = fields.Many2one(
string='Address',
comodel_name='carepoint.address',
required=True,
ondelete='cascade',
)
carepoint_bind_ids = fields.One2many(
comodel_name='carepoint.carepoint.address.patient',
inverse_name='odoo_id',
Expand All @@ -71,37 +65,21 @@ class CarepointAddressPatientBatchImporter(DelayedBatchImporter):


@carepoint
class CarepointAddressPatientImportMapper(CarepointImportMapper):
class CarepointAddressPatientImportMapper(
CarepointAddressAbstractImportMapper,
):
_model_name = 'carepoint.carepoint.address.patient'

@mapping
@only_create
def parent_id(self, record):
def partner_id(self, record):
""" It returns either the commercial partner or parent & defaults """
binder = self.binder_for('carepoint.medical.patient')
patient_id = binder.to_odoo(record['pat_id'])
self.env['medical.patient'].browse(patient_id).partner_id
# if not partner_id.street:
#
# return {
# 'parent_id': partner_id.id,
# }

@mapping
@only_create
def partner_and_address_id(self, record):
binder = self.binder_for('carepoint.carepoint.address')
address_id = binder.to_odoo(record['addr_id'])
vals = {'address_id': address_id}
# address_id = self.env['carepoint.address'].browse(address_id)
return vals

@mapping
def type(self, record):
return {'type': 'delivery'}

@mapping
def customer(self, record):
return {'customer': True}
patient_id = binder.to_odoo(record['pat_id'], browse=True)
_sup = super(CarepointAddressPatientImportMapper, self)
return _sup.partner_id(
record, patient_id,
)

@mapping
def carepoint_id(self, record):
Expand All @@ -110,24 +88,24 @@ def carepoint_id(self, record):


@carepoint
class CarepointAddressPatientImporter(CarepointImporter):
class CarepointAddressPatientImporter(
CarepointAddressAbstractImporter,
):
_model_name = ['carepoint.carepoint.address.patient']
_base_mapper = CarepointAddressPatientImportMapper

def _import_dependencies(self):
""" Import depends for record """
record = self.carepoint_record
self._import_dependency(record['addr_id'],
'carepoint.carepoint.address')
self._import_dependency(record['pat_id'],
super(CarepointAddressPatientImporter, self)._import_dependencies()
self._import_dependency(self.carepoint_record['pat_id'],
'carepoint.medical.patient')


@carepoint
class CarepointAddressPatientUnit(ConnectorUnit):
_model_name = 'carepoint.carepoint.address.patient'

def _import_addresses(self, patient_id, binding_id):
def _import_addresses(self, patient_id, partner_binding):
adapter = self.unit_for(CarepointCRUDAdapter)
importer = self.unit_for(CarepointAddressPatientImporter)
address_ids = adapter.search(pat_id=patient_id)
Expand Down
Loading

0 comments on commit 5617a7d

Please sign in to comment.