<commit_before># -*- coding: utf-8 -*-
# © 2015 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.exceptions import ValidationError
from openerp.addons.connector.queue.job import job, related_action
from openerp.addons.connector.connector import ConnectorUnit
from openerp.addons.connector.unit.mapper import (mapping,
                                                  changed_by,
                                                  only_create,
                                                  )
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import CarepointImportMapper
from ..connector import get_environment
from ..backend import carepoint
from ..unit.import_synchronizer import (DelayedBatchImporter,
                                        CarepointImporter,
                                        )
from ..unit.export_synchronizer import (CarepointExporter)
from ..unit.delete_synchronizer import (CarepointDeleter)
from ..connector import add_checkpoint, get_environment
from ..related_action import unwrap_binding

_logger = logging.getLogger(__name__)


class CarepointMedicalPrescriptionOrderLine(models.Model):
    """ Binding Model for the Carepoint Prescription """
    _name = 'carepoint.medical.prescription.order.line'
    _inherit = 'carepoint.binding'
    _inherits = {'medical.prescription.order.line': 'odoo_id'}
    _description = 'Carepoint Prescription'
    _cp_lib = 'prescription'  # Name of model in Carepoint lib (snake_case)

    odoo_id = fields.Many2one(
        comodel_name='medical.prescription.order.line',
        string='Prescription Line',
        required=True,
        ondelete='cascade'
    )
    backend_id = fields.Many2one(
        comodel_name='carepoint.backend',
        string='Carepoint Backend',
        store=True,
        readonly=True,
        # override 'carepoint.binding', can't be INSERTed if True:
        required=False,
    )
    created_at = fields.Date('Created At (on Carepoint)')
    updated_at = fields.Date('Updated At (on Carepoint)')

    _sql_constraints = [
        ('odoo_uniq', 'unique(backend_id, odoo_id)',
         'A Carepoint binding for this prescription already exists.'),
    ]


class MedicalPrescriptionOrderLine(models.Model):
    """ Adds the ``one2many`` relation to the Carepoint bindings
    (``carepoint_bind_ids``)
    """
    _inherit = 'medical.prescription.order.line'

    carepoint_bind_ids = fields.One2many(
        comodel_name='carepoint.medical.prescription.order.line',
        inverse_name='odoo_id',
        string='Carepoint Bindings',
    )


@carepoint
class MedicalPrescriptionOrderLineAdapter(CarepointCRUDAdapter):
    """ Backend Adapter for the Carepoint Prescription """
    _model_name = 'carepoint.medical.prescription.order.line'


@carepoint
class MedicalPrescriptionOrderLineBatchImporter(DelayedBatchImporter):
    """ Import the Carepoint Prescriptions.
    For every prescription in the list, a delayed job is created.
    """
    _model_name = ['carepoint.medical.prescription.order.line']

    def run(self, filters=None):
        """ Run the synchronization """
        if filters is None:
            filters = {}
        record_ids = self.backend_adapter.search(**filters)
        for record_id in record_ids:
            self._import_record(record_id)


@carepoint
class MedicalPrescriptionOrderLineImportMapper(CarepointImportMapper):
    _model_name = 'carepoint.medical.prescription.order.line'

    direct = [
        # ('script_no', 'name'),
        ('script_no', 'name'),
        ('start_date', 'date_start_treatment'),
        ('expire_date', 'date_stop_treatment'),
        ('written_qty', 'qty'),
        ('refills_left', 'refill_qty_remain'),
        ('refills_orig', 'refill_qty_original'),
        ('freq_of_admin', 'frequency'),
        ('units_per_dose', 'quantity'),
    ]

    @mapping
    @only_create
    def duration(self, record):
        days_supply = record.get('days_supply', 0)
        refills = record.get('refills_orig', 1)
        duration = days_supply * refills
        return {'duration': duration}

    @mapping
    @only_create
    def medicament_and_meta(self, record):
        binder = self.binder_for('carepoint.fdb.ndc')<commit_msg>Fix cols to correspond with misnamed CP cols - carepoint_connector<commit_after># -*- coding: utf-8 -*-
# © 2015 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.exceptions import ValidationError
from openerp.addons.connector.queue.job import job, related_action
from openerp.addons.connector.connector import ConnectorUnit
from openerp.addons.connector.unit.mapper import (mapping,
                                                  changed_by,
                                                  only_create,
                                                  )
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import CarepointImportMapper
from ..connector import get_environment
from ..backend import carepoint
from ..unit.import_synchronizer import (DelayedBatchImporter,
                                        CarepointImporter,
                                        )
from ..unit.export_synchronizer import (CarepointExporter)
from ..unit.delete_synchronizer import (CarepointDeleter)
from ..connector import add_checkpoint, get_environment
from ..related_action import unwrap_binding

_logger = logging.getLogger(__name__)


class CarepointMedicalPrescriptionOrderLine(models.Model):
    """ Binding Model for the Carepoint Prescription """
    _name = 'carepoint.medical.prescription.order.line'
    _inherit = 'carepoint.binding'
    _inherits = {'medical.prescription.order.line': 'odoo_id'}
    _description = 'Carepoint Prescription'
    _cp_lib = 'prescription'  # Name of model in Carepoint lib (snake_case)

    odoo_id = fields.Many2one(
        comodel_name='medical.prescription.order.line',
        string='Prescription Line',
        required=True,
        ondelete='cascade'
    )
    backend_id = fields.Many2one(
        comodel_name='carepoint.backend',
        string='Carepoint Backend',
        store=True,
        readonly=True,
        # override 'carepoint.binding', can't be INSERTed if True:
        required=False,
    )
    created_at = fields.Date('Created At (on Carepoint)')
    updated_at = fields.Date('Updated At (on Carepoint)')

    _sql_constraints = [
        ('odoo_uniq', 'unique(backend_id, odoo_id)',
         'A Carepoint binding for this prescription already exists.'),
    ]


class MedicalPrescriptionOrderLine(models.Model):
    """ Adds the ``one2many`` relation to the Carepoint bindings
    (``carepoint_bind_ids``)
    """
    _inherit = 'medical.prescription.order.line'

    carepoint_bind_ids = fields.One2many(
        comodel_name='carepoint.medical.prescription.order.line',
        inverse_name='odoo_id',
        string='Carepoint Bindings',
    )


@carepoint
class MedicalPrescriptionOrderLineAdapter(CarepointCRUDAdapter):
    """ Backend Adapter for the Carepoint Prescription """
    _model_name = 'carepoint.medical.prescription.order.line'


@carepoint
class MedicalPrescriptionOrderLineBatchImporter(DelayedBatchImporter):
    """ Import the Carepoint Prescriptions.
    For every prescription in the list, a delayed job is created.
    """
    _model_name = ['carepoint.medical.prescription.order.line']

    def run(self, filters=None):
        """ Run the synchronization """
        if filters is None:
            filters = {}
        record_ids = self.backend_adapter.search(**filters)
        for record_id in record_ids:
            self._import_record(record_id)


@carepoint
class MedicalPrescriptionOrderLineImportMapper(CarepointImportMapper):
    _model_name = 'carepoint.medical.prescription.order.line'

    direct = [
        # ('script_no', 'name'),
        ('script_no', 'name'),
        ('start_date', 'date_start_treatment'),
        ('expire_date', 'date_stop_treatment'),
        ('written_qty', 'qty'),
        ('freq_of_admin', 'frequency'),
        ('units_per_dose', 'quantity'),
        # Note that the col naming seems to be reversed *shrug*
        ('refills_left', 'refill_qty_original'),
        ('refills_orig', 'refill_qty_remain'),
    ]

    @mapping
    @only_create
    def duration(self, record):
        days_supply = record.get('days_supply', 0)
        refills = record.get('refills_orig', 1)
        duration = days_supply * refills
        return {'duration': duration}

    @mapping
    @only_create
    def medicament_and_meta(self, record):
        binder = self.binder_for('carepoint.fdb.ndc')
{'fraction_non_alphanumeric': 0.10201985621362547, 'fraction_numerical': 0.002967020426794477, 'mean_word_length': 2.9265232974910393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6713902', 'n_tokens_mistral': 2546, 'n_tokens_neox': 2431, 'n_words': 549}