"""Module that process data for Sales Sheets SQS Queue and Daemon. This module works on processing provided data and putting messages with processed data into SQS Queue to trigger pdf generating in Sales Sheets Daemon. """ from oto import response from salessheets import features from salessheets.constants import features as feature_constants from salessheets.constants import field_const from salessheets.constants import models from salessheets.constants import ows_services from salessheets.constants import salessheets from salessheets.models import ows_account from salessheets.models import ows_product from salessheets.models import release from salessheets.models import salessheets_daemon from salessheets.models import vendor_default_template from salessheets.models.history import create_job from salessheets.utils import logic_utils def trigger_salessheets_generation_job_duplicate_upc( context, context_type, generation_method, user_id, correlation_id, grass_account_type=None, grass_account_id=None, template_type=None, localized_template_type_id=None): """Get release ids, put message with ids to queue and return result. Args: context (list): list of strings (up to 30 values) context_type (string): context_type (str): possible values- 'upc', 'display_upc' or 'release_id'. generation_method (str): how generating output should looks like. Could be "zip" or "single". user_id (str): user_id from request header. correlation_id (str): correlation_id from request header. grass_account_type (str): Type of user account from workstation. allowed values: "vendor", "subaccount"; not required for OA users. grass_account_id (str): Id of user account from workstation; not required for OA users. template_type (str): one of the values 'orchard' or 'redessential', the type of template to be generated. localized_template_type_id (int): id of localized template type, corresponds to the id in salessheets.template_detail table. Returns: Response: response with errors or with job id. """ if context_type == models.RELEASE_ID: product_ids = tuple(context) else: release_ids = release.get_multiple_release_ids(context, context_type) if not release_ids: return release_ids product_ids = release_ids.message # Validate release ownership if both grass headers were provided. if grass_account_type and grass_account_id: for product_id in product_ids: validated_product = ows_product.get_product_ownership_by_account( account_type=grass_account_type, account_id=grass_account_id, product_id=str(product_id)) if not validated_product: return validated_product if (features.is_feature_enabled( feature_constants.LOCALIZED_SALES_SHEETS) and logic_utils.is_workstation_user(user_id)): localized_template_type_resp = ( get_localized_template_type_id_for_vendor( grass_account_type, grass_account_id)) if not localized_template_type_resp: return localized_template_type_resp localized_template_type_id = localized_template_type_resp.message[ salessheets.LOCALIZED_TEMPLATE_TYPE_ID] created_job = create_job( user_id=user_id, context=context, context_type=context_type, output_format=generation_method) if not created_job: return created_job job_id = str(created_job.message) return salessheets_daemon.put_job_to_sqs( job_id=job_id, product_ids=product_ids, generation_method=generation_method, correlation_id=correlation_id, user_id=user_id, template_type=template_type, localized_template_type_id=localized_template_type_id) def get_localized_template_type_id_for_vendor(account_type, account_id): """Get localized_template_type_id for vendor. Args: account_type (str): Type of user account from workstation. possible values: "vendor", "subaccount". account_id (int): Id of user account from workstation. Returns: Response: response containing localized_template_type_id in case of success, error response in case of failure. """ vendor_id = None if account_type == ows_services.ACCOUNT_TYPE_VENDOR: vendor_id = account_id elif account_type == ows_services.ACOUNT_TYPE_SUBACCOUNT: subaccount_response = ows_account.get_subaccount_by_id(account_id) if not subaccount_response: return subaccount_response vendor_id = subaccount_response.message[ows_services.VENDOR_ID] vendor_default_template_response = ( vendor_default_template.get_vendor_default_template(vendor_id)) if not vendor_default_template_response: return vendor_default_template_response localized_template_type_id = ( vendor_default_template_response.message[field_const.TEMPLATE_ID]) return response.Response( {salessheets.LOCALIZED_TEMPLATE_TYPE_ID: localized_template_type_id})