"""Label details logic. Logic to get label details. """ from oto import response from prs.constants import error from prs.constants import tools_message from prs.constants import tools from prs.logic import tool_log_logic from prs.models import country from prs.models import collection_society from prs.models import label_details from prs.models import label_delivery_history from prs.utils import shared_utils from prs.utils import s3 def get_label_data(tool_param_data): """Get label details for passed label ids. Args: tool_param_data (dict): params to be used to get label details. Returns: Response: Message and code for response from model get_label_details. """ vendor_details_result = label_details.get_label_details(tool_param_data) if not vendor_details_result: return vendor_details_result column_labels = list(vendor_details_result.message[0].keys()) society_for_country = collection_society.get_society_id_by_country_id( tool_param_data['country_id']) society_id = tools.WORLDWIDE_SOCIETY_ID if society_for_country: society_id = society_for_country.message['society_id'] label_delivery_mapping = {} if vendor_details_result and society_id > 0 and \ society_id < tools.WORLDWIDE_SOCIETY_ID: # Label delivery_history converting from list of list to dict of label # id and it's last delivery date label_delivery_mapping = dict(get_label_delivery_history( vendor_details_result.message, society_id)) # Function to append to passed row with label_delivery_history mapping. label_rows = [] for row in vendor_details_result.message: label_row = [] for col, val in row.items(): if col == 'last_delivery_date': vendor_id = str(row['vendor_id']) if vendor_id in label_delivery_mapping: val = label_delivery_mapping.get(vendor_id) val = str(val) if val else '' label_row.append(val) label_rows.append(label_row) vendor_details_response = {'columns': column_labels, 'rows': label_rows} return response.Response(message=vendor_details_response) def fetch_label_details(tool_param_data): """Generate the label details CSV and log the progress. Args: tool_param_data (dict): params to be used to get label details. Returns: Response: Response containing the generated csv link with success or failure message. """ if tool_param_data.get('eligible_vendor_list'): if not tool_param_data['eligible_vendor_list'].replace( ',', '').isdigit(): return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, message=error.ERROR_MESSAGE_INVALID_CHARACTER, status=400) # filter will remove extraneous commas. # map will convert all the vendor ids to int. # set is used to remove duplicate vendor ids. tool_param_data['eligible_vendor_list'] = set(map(int, filter( None, tool_param_data['eligible_vendor_list'].split(',')))) default_tool_param_data = { 'country_id': 0, 'ignore_rome': 0, 'include_local_labels': 0, 'check_blacklist': 1, 'eligible_vendor_list': [] } for key, val in default_tool_param_data.items(): tool_param_data.setdefault(key, val) initialize_tool_response = tool_log_logic.initialize_tool( tools.LABEL_DETAILS, tool_param_data) if not initialize_tool_response: return initialize_tool_response log_values = initialize_tool_response.message # get label details for for all the labels label_data_response = get_label_data(tool_param_data) if not label_data_response: # Shutdown the tool log with failure progress message tool_log_logic.complete_tool( log_values, tools_message.FETCHING_LABEL_FAILURE_MESSAGE, error.TOOL_SHUTDOWN_FAILURE_STATUS) return label_data_response label_data = label_data_response.message # get country name by country id country_id = tool_param_data['country_id'] country_name = tools.WORLDWIDE_COUNTRY_NAME if country_id and country_id != tools.WORLDWIDE_COUNTRY_ID: country_name_response = country.get_country_name_by_id(country_id) if not country_name_response: # Shutdown the tool log with failure progress message tool_log_logic.complete_tool( log_values, tools_message.FETCHING_COUNTRY_FAILURE_MESSAGE, error.TOOL_SHUTDOWN_FAILURE_STATUS) return country_name_response country_name = country_name_response.message['name'] # Generate csv of label_data label_details_response = generate_csv_link( log_values, country_name, label_data) if not label_details_response: # Shutdown the tool log with failure progress message tool_log_logic.complete_tool( log_values, error.ERROR_CODE_CSV_FILE_WRITE, error.TOOL_SHUTDOWN_FAILURE_STATUS) return label_details_response # Shutdown the tool log with success progress message tool_log_logic.complete_tool( log_values, tools_message.TOOL_COMPLETED_MESSAGE, error.TOOL_SHUTDOWN_SUCCESS_STATUS) return response.Response(message=label_details_response) def generate_csv_link(log_values, country_name, label_data): """Generate a csv inside s3 for the label_data. Args: label_data (dict): The metadata for the respective label_details. log_values (dict): Contains the log data that needs to be updated in complete_tool_failure. country_name (String): Name of the country. Returns: Response: Dictionary containing the generated csv link with success message. """ file_code = shared_utils.get_file_code() file_base_name = shared_utils.generate_file_base_name( tools.LABEL_DETAILS, country_name, file_code) file_base_name_csv = '{}.csv'.format(file_base_name) write_csv_response = s3.write_csv_to_s3(file_base_name_csv, label_data) if not write_csv_response: return write_csv_response s3_url = s3.get_presigned_url(file_base_name_csv) message = tools_message.CSV_GENERATION_SUCCESS_MESSAGE # update the log values for complete tool success log_values['file_code'] = file_code log_values['output_filename'] = s3_url log_values['return_message'] = message # return_values for the handler. label_details_response = { 's3_url': s3_url, 'message': message } return label_details_response def get_label_delivery_history(label_rows, society_id): """Return label-wise last delivery dates for a society. Args: label_rows (list): Vendor list from the result. society_id (int): Society id for which label ids are required. Returns: label_delivery_history (List): Vendor-wise last delivery date. """ vendor_id_list = [row['vendor_id'] for row in label_rows] label_delivery_history_response = \ label_delivery_history.get_last_delivery_by_vendor_and_society_id( society_id, vendor_id_list) return label_delivery_history_response.message['label_delivery_history']