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 4, 2016
1 parent acc3750 commit 446d69e
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 53 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
48 changes: 34 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,35 @@ class CarepointAddress(models.Model):
string='Country',
comodel_name='res.country',
)
partner_id = fields.Many2one(
string='Partner',
comodel_name='res.partner',
)

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 rec_id.partner_id:
continue
rec_id.partner_id.write(
self._get_partner_sync_vals()
)

@api.multi
def _get_partner_sync_vals(self):
self.ensure_one()
vals = {}
for attr in self.PARTNER_ATTRS:
vals[attr] = getattr(self, attr, None)
return vals


@carepoint
class CarepointAddressAdapter(CarepointCRUDAdapter):
Expand All @@ -78,7 +104,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 +163,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
93 changes: 93 additions & 0 deletions connector_carepoint/models/address_abstract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# -*- 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
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


_logger = logging.getLogger(__name__)


class CarepointAddressAbstract(models.AbstractModel):
""" Adds the ``one2many`` relation to the Carepoint bindings
(``carepoint_bind_ids``)
"""
_inherits = {'carepoint.address': 'address_id',
'res.partner': 'partner_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,
ondelete='cascade',
)


@carepoint
class CarepointAddressAbstractImportMapper(CarepointImportMapper):

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 _has_empty_address(self, 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 _partner_or_parent_and_default(self, record, medical_entity):
""" It returns vals for partner associate/create for medical_entity
Params:
record: ``dict`` of carepoint record
medical_entity: Recordset with a ``partner_id`` column
Return:
``dict`` of values for mapping
"""
if not self._has_empty_address(medical_entity.commercial_partner_id):
return {'partner_id': medical_entity.commercial_partner_id.id}
else:
vals = self._get_partner_defaults(record)
vals.update({
'parent_id': medical_entity.commercial_partner_id.id,
})
return vals

@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):

def _import_dependencies(self):
""" Import depends for record """
self._import_dependency(self.carepoint_record['addr_id'],
'carepoint.carepoint.address')
58 changes: 19 additions & 39 deletions connector_carepoint/models/address_patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
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 .address_abstract import (CarepointAddressAbstractImportMapper,
CarepointAddressAbstractImporter,
)

_logger = logging.getLogger(__name__)

Expand All @@ -39,16 +41,10 @@ class CarepointAddressPatient(models.Model):
""" 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 +67,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_or_parent_and_default(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(CarepointAddressAbstractImportMapper, self)
return _sup.partner_or_parent_and_default(
record, patient_id,
)

@mapping
def carepoint_id(self, record):
Expand All @@ -110,16 +90,16 @@ 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')


Expand Down

0 comments on commit 446d69e

Please sign in to comment.