MODEL_NAME_FIELD = 'model_name' RECORD_ID_FIELD = 'record_id' TEMPLATE_ID_FIELD = 'template_id' PRODUCT_SKU_FIELD = 'product_sku' WRITE_FIELD_NAMES_FIELD = 'write_field_names' AMAZON_PRODUCT_SYNC_TABLE = 'amdeb.amazon.product.sync' SYNC_TYPE_FIELD = 'sync_type' SYNC_STATUS_FIELD = 'sync_status' SYNC_CHECK_STATUS_COUNT_FILED = 'sync_check_status_count' AMAZON_MESSAGE_CODE_FIELD = 'amazon_message_code' AMAZON_RESULT_DESCRIPTION_FIELD = 'amazon_result_description' AMAZON_REQUEST_TIMESTAMP_FIELD = 'amazon_request_timestamp' SYNC_DELETE = 'sync_delete' SYNC_STATUS_NEW = 'New' SYNC_STATUS_PENDING = 'Pending' SYNC_STATUS_ERROR = 'Error' AMAZON_STATUS_PROCESS_DONE = '_DONE_' SYNC_STATUS_SUCCESS = 'Success' SYNC_STATUS_WAITING = 'Waiting' def field_utcnow(*args):
    """ Return the current UTC day and time in the format expected by the ORM.
        This function may be used to compute default values.
    """
    return datetime.utcnow().strftime(DATETIME_FORMAT) class SyncHeadAccess(object):
    """
    The base class used to define common methods for
    sync head fields thus each sync_head can use its own access class.
    """
    @staticmethod
    def is_product_template(sync_head):
        return sync_head[MODEL_NAME_FIELD] == PRODUCT_TEMPLATE_TABLE

    @staticmethod
    def is_product_variant(sync_head):
        return sync_head[MODEL_NAME_FIELD] == PRODUCT_PRODUCT_TABLE

    @staticmethod
    def get_write_field_names(sync_head):
        field_names = sync_head[WRITE_FIELD_NAMES_FIELD]
        if field_names:
            data = set(field_names.split(FIELD_NAME_DELIMITER))
        else:
            data = set()
        return data

    @staticmethod
    def save_write_field_names(sync_head, write_fields):
        joined = FIELD_NAME_DELIMITER.join(write_fields)
        sync_head[WRITE_FIELD_NAMES_FIELD] = joined
import logging
from ..model_names.shared_names import (
    MODEL_NAME_FIELD, RECORD_ID_FIELD,
    TEMPLATE_ID_FIELD, PRODUCT_SKU_FIELD,
    WRITE_FIELD_NAMES_FIELD,
)
from ..model_names.product_sync import (
    AMAZON_PRODUCT_SYNC_TABLE,
    SYNC_TYPE_FIELD,
    SYNC_STATUS_FIELD,
    SYNC_CHECK_STATUS_COUNT_FILED,
    AMAZON_MESSAGE_CODE_FIELD,
    AMAZON_RESULT_DESCRIPTION_FIELD,
    AMAZON_REQUEST_TIMESTAMP_FIELD,
    SYNC_DELETE,
    SYNC_STATUS_NEW, SYNC_STATUS_PENDING,
    SYNC_STATUS_ERROR, AMAZON_STATUS_PROCESS_DONE,
    SYNC_STATUS_SUCCESS, SYNC_STATUS_WAITING,
)
from ..shared.utility import field_utcnow
from .sync_head_access import SyncHeadAccess
# -*- coding: utf-8 -*-



_CREATION_ERROR_CODE = "Amazon Product Is Not Created Or Missing SKU."
_REDUNDANT_SKIP_CODE = "Redundant Or Merged Operation."
_PRODUCT_NOT_FOUND_CODE = "Product Not Found Or Sync Disabled."

_logger = logging.getLogger(__name__)


class ProductSyncAccess(SyncHeadAccess):
    """
    Provide methods for accessing Amazon product sync table
    The sync_head is an object that defines model_name,
    record_id and template_id. It could be a product operation record
    or an Amazon sync record
    """
    def __init__(self, env):
        self._table = env[AMAZON_PRODUCT_SYNC_TABLE]

    @staticmethod
    def _log_values(values):
        log_template = "Create new sync record for Model: {0}, " \
                       "record id: {1}, sync type: {2}, " \
                       "write fields: {3}, product sku: {4}, " \
                       "waiting flag: {5}, error_flag: {6}."
        _logger.debug(log_template.format(
            values[MODEL_NAME_FIELD],

            values[RECORD_ID_FIELD],