Skip to content

Commit

Permalink
[IMP] connector_carepoint: Add FDB Image import
Browse files Browse the repository at this point in the history
* Update importers for proper sync and inheritance logic
* Improve test coverage
  • Loading branch information
lasley committed Sep 9, 2016
1 parent 5c4da4b commit 9064c90
Show file tree
Hide file tree
Showing 12 changed files with 706 additions and 23 deletions.
29 changes: 17 additions & 12 deletions connector_carepoint/models/fdb_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from openerp.addons.connector.unit.mapper import (mapping,
)
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import CarepointImportMapper
from ..unit.mapper import CarepointImportMapper, trim
from ..backend import carepoint
from ..unit.import_synchronizer import (DelayedBatchImporter,
CarepointImporter,
Expand All @@ -21,7 +21,7 @@ class CarepointFdbImg(models.Model):
_inherit = 'carepoint.binding'
_inherits = {'fdb.img': 'odoo_id'}
_description = 'Carepoint FdbImg'
_cp_lib = 'fdb_img' # Name of model in Carepoint lib (snake_case)
_cp_lib = 'fdb_img'

odoo_id = fields.Many2one(
string='FdbImg',
Expand Down Expand Up @@ -59,13 +59,20 @@ class FdbImgBatchImporter(DelayedBatchImporter):
class FdbImgImportMapper(CarepointImportMapper):
_model_name = 'carepoint.fdb.img'
direct = [
('IMGFILENM', 'file_name'),
('data', 'data'),
(trim('IMGFILENM'), 'name'),
]

@mapping
def data(self, record):
return {'data': record['data'].decode('base64')}
def datas(self, record):
return {'datas': record['data']}

@mapping
def mimetype(self, record):
return {'mimetype': 'image/jpeg'}

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

@mapping
def carepoint_id(self, record):
Expand All @@ -75,14 +82,12 @@ def carepoint_id(self, record):
@carepoint
class FdbImgImporter(CarepointImporter):
_model_name = ['carepoint.fdb.img']

_base_mapper = FdbImgImportMapper

def _get_carepoint_data(self):
""" Return the raw Carepoint data for ``self.carepoint_id`` """
_logger.debug('Getting CP data for %s', self.carepoint_id)
record = self.backend_adapter.read(self.carepoint_id, [
'IMGFILENM', 'IMAGE_PATH', 'IMGID'
])
record['data'] = self.backend_adapter.read_image(record['IMAGE_PATH'])
record = super(FdbImgImporter, self)._get_carepoint_data()
record.data = self.backend_adapter.read_image(
record['IMAGE_PATH'],
)
return record
39 changes: 36 additions & 3 deletions connector_carepoint/models/fdb_img_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging
from openerp import models, fields
from openerp.addons.connector.connector import ConnectorUnit
from openerp.addons.connector.unit.mapper import (mapping,
)
from ..unit.backend_adapter import CarepointCRUDAdapter
Expand All @@ -21,7 +22,7 @@ class CarepointFdbImgDate(models.Model):
_inherit = 'carepoint.binding'
_inherits = {'fdb.img.date': 'odoo_id'}
_description = 'Carepoint FdbImgDate'
_cp_lib = 'fdb_img_date' # Name of model in Carepoint lib (snake_case)
_cp_lib = 'fdb_img_date'

odoo_id = fields.Many2one(
string='FdbImgDate',
Expand All @@ -46,6 +47,18 @@ class FdbImgDateAdapter(CarepointCRUDAdapter):
_model_name = 'carepoint.fdb.img.date'


@carepoint
class FdbImgDateUnit(ConnectorUnit):
_model_name = 'carepoint.fdb.img.date'

def _import_by_unique_id(self, unique_id):
""" It imports by CP col ``IMGUNIQID`` """
adapter = self.unit_for(FdbImgDateAdapter)
importer = self.unit_for(FdbImgDateImporter)
for record in adapter.search(IMGUNIQID=unique_id):
importer.run(record)


@carepoint
class FdbImgDateBatchImporter(DelayedBatchImporter):
""" Import the Carepoint FdbImgDates.
Expand All @@ -61,15 +74,35 @@ class FdbImgDateImportMapper(CarepointImportMapper):
direct = [
('IMGSTRTDT', 'start_date'),
('IMGSTOPDT', 'stop_date'),
('IMGID', 'img_id'),
]

@mapping
def relation_id(self, record):
binder = self.binder_for('carepoint.fdb.img.id')
relation_id = binder.to_odoo(record['IMGUNIQID'])
return {'relation_id': relation_id}

@mapping
def image_id(self, record):
binder = self.binder_for('carepoint.fdb.img')
img_id = binder.to_odoo(record['IMGID'])
return {'image_id': img_id}

@mapping
def carepoint_id(self, record):
return {'carepoint_id': record['IMGUNIQID']}
return {'carepoint_id': '%s,%s' % (record['IMGUNIQID'],
record['IMGSTRTDT'],
)}


@carepoint
class FdbImgDateImporter(CarepointImporter):
_model_name = ['carepoint.fdb.img.date']
_base_mapper = FdbImgDateImportMapper

def _import_dependencies(self):
""" It imports depends for record """
self._import_dependency(self.carepoint_record['IMGUNIQID'],
'carepoint.fdb.img.id')
self._import_dependency(self.carepoint_record['IMGID'],
'carepoint.fdb.img')
56 changes: 53 additions & 3 deletions connector_carepoint/models/fdb_img_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import logging
from openerp import models, fields
from openerp.addons.connector.connector import ConnectorUnit
from openerp.addons.connector.unit.mapper import (mapping,
only_create,
)
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import CarepointImportMapper
Expand All @@ -13,6 +15,9 @@
CarepointImporter,
)

from .fdb_img_date import FdbImgDateUnit


_logger = logging.getLogger(__name__)


Expand All @@ -21,7 +26,7 @@ class CarepointFdbImgId(models.Model):
_inherit = 'carepoint.binding'
_inherits = {'fdb.img.id': 'odoo_id'}
_description = 'Carepoint FdbImgId'
_cp_lib = 'fdb_img_id' # Name of model in Carepoint lib (snake_case)
_cp_lib = 'fdb_img_id'

odoo_id = fields.Many2one(
string='FdbImgId',
Expand All @@ -46,6 +51,21 @@ class FdbImgIdAdapter(CarepointCRUDAdapter):
_model_name = 'carepoint.fdb.img.id'


@carepoint
class FdbImgIdUnit(ConnectorUnit):
_model_name = 'carepoint.fdb.img.id'

def _import_by_ndc(self, ndc):
""" It should search for records w/ ndc and import
Params:
ndc: :type:str NDC to seartch for
"""
adapter = self.unit_for(FdbImgIdAdapter)
importer = self.unit_for(FdbImgIdImporter)
for record in adapter.search(IMGNDC=ndc):
importer.run(record)


@carepoint
class FdbImgIdBatchImporter(DelayedBatchImporter):
""" Import the Carepoint FdbImgIds.
Expand All @@ -58,12 +78,25 @@ class FdbImgIdBatchImporter(DelayedBatchImporter):
@carepoint
class FdbImgIdImportMapper(CarepointImportMapper):
_model_name = 'carepoint.fdb.img.id'

direct = [
('IMGDFID', 'df_id'),
('IMGNDC', 'ndc'),
('IMGMFGID', 'mfg_id'),
]

@mapping
@only_create
def ndc_id(self, record):
binder = self.binder_for('carepoint.fdb.ndc')
ndc_id = binder.to_odoo(record['IMGNDC'].strip())
return {'ndc_id': ndc_id}

@mapping
@only_create
def manufacturer_id(self, record):
binder = self.binder_for('carepoint.fdb.img.mfg')
mfg_id = binder.to_odoo(record['IMGMFGID'])
return {'manufacturer_id': mfg_id}

@mapping
def carepoint_id(self, record):
return {'carepoint_id': record['IMGUNIQID']}
Expand All @@ -73,3 +106,20 @@ def carepoint_id(self, record):
class FdbImgIdImporter(CarepointImporter):
_model_name = ['carepoint.fdb.img.id']
_base_mapper = FdbImgIdImportMapper

def _import_dependencies(self):
""" Import depends for record """
record = self.carepoint_record
self._import_dependency(record['IMGNDC'].strip(),
'carepoint.fdb.ndc')
self._import_dependency(record['IMGMFGID'],
'carepoint.fdb.img.mfg')

def _after_import(self, binding):
img_unit = self.unit_for(
FdbImgDateUnit,
model='carepoint.fdb.img.date',
)
img_unit._import_by_unique_id(
self.carepoint_record['IMGUNIQID'],
)
19 changes: 15 additions & 4 deletions connector_carepoint/models/fdb_img_mfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import logging
from openerp import models, fields
from openerp.addons.connector.unit.mapper import (mapping,
only_create,
)
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import PartnerImportMapper
from ..unit.mapper import trim_and_titleize
from ..unit.mapper import PartnerImportMapper, trim
from ..backend import carepoint
from ..unit.import_synchronizer import (DelayedBatchImporter,
CarepointImporter,
Expand All @@ -22,7 +22,7 @@ class CarepointFdbImgMfg(models.Model):
_inherit = 'carepoint.binding'
_inherits = {'fdb.img.mfg': 'odoo_id'}
_description = 'Carepoint FdbImgMfg'
_cp_lib = 'fdb_img_mfg' # Name of model in Carepoint lib (snake_case)
_cp_lib = 'fdb_img_mfg'

odoo_id = fields.Many2one(
string='FdbImgMfg',
Expand Down Expand Up @@ -60,9 +60,20 @@ class FdbImgMfgBatchImporter(DelayedBatchImporter):
class FdbImgMfgImportMapper(PartnerImportMapper):
_model_name = 'carepoint.fdb.img.mfg'
direct = [
(trim_and_titleize('IMGMFGNAME'), 'name'),
(trim('IMGMFGNAME'), 'name'),
]

@mapping
@only_create
def manufacturer_id(self, record):
""" It finds Manufacturer of same name and binds on it """
manufacturer = self.env['medical.manufacturer'].search(
[('name', 'ilike', record['IMGMFGNAME'].strip())],
limit=1,
)
if len(manufacturer):
return {'manufacturer_id': manufacturer[0].id}

@mapping
def carepoint_id(self, record):
return {'carepoint_id': record['IMGMFGID']}
Expand Down
18 changes: 18 additions & 0 deletions connector_carepoint/models/fdb_ndc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from psycopg2 import IntegrityError
from openerp.exceptions import ValidationError

from .fdb_img_id import FdbImgIdUnit


_logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -289,6 +292,12 @@ def medicament_id(self, record):

return {'medicament_id': medicament_id[0].id}

@mapping
def lbl_mfg_id(self, record):
binder = self.binder_for('carepoint.fdb.lbl.rid')
lbl_rid = binder.to_odoo(record['lblrid'].strip())
return {'lbl_mfg_id': lbl_rid}

@mapping
def carepoint_id(self, record):
return {'carepoint_id': record['ndc'].strip()}
Expand All @@ -310,3 +319,12 @@ def _import_dependencies(self):
pass
self._import_dependency(record['gcn_seqno'],
'carepoint.fdb.gcn')

def _after_import(self, binding):
img_unit = self.unit_for(
FdbImgIdUnit,
model='carepoint.fdb.img.id',
)
img_unit._import_by_ndc(
self.carepoint_record['ndc'].strip(),
)
4 changes: 4 additions & 0 deletions connector_carepoint/tests/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@
from . import test_address_patient
from . import test_address_store
from . import test_address_organization
from . import test_fdb_img
from . import test_fdb_img_date
from . import test_fdb_img_mfg
from . import test_fdb_img_id
Loading

0 comments on commit 9064c90

Please sign in to comment.