""" Logic for Label. Methods for getting labels associated with either a vendor or subaccount. """ import copy from sony_metadata import response from sony_metadata.models import field_manager def get_subaccount_labels(subaccount_id): """Get sony label fields associated with a subaccount. Args: subaccount_id (int): the id of the subaccount. Returns: response.Response: If subaccount_id found, Response contains the sony labels associated with the subaccount. If no subaccount_id is found, a Response with a not found error code and a 404 status. """ result_response = field_manager.get_subaccount_labels(subaccount_id) # 200 response indicates found fields if result_response: result_response.message = _create_label_fields_element( result_response.message) return result_response def get_vendor_labels(vendor_id): """Get sony label fields associated with a vendor. Args: vendor_id (int): the id of the vendor. Returns: response.Response: If vendor_id found, Response contains the sony labels associated with the vendor. If no vendor_id is found, a Response with a not found error code and a http code of 404. """ result_response = field_manager.get_vendor_labels(vendor_id) # 200 response indicates found fields if result_response: result_response.message = _create_label_fields_element( result_response.message) return result_response def _create_label_fields_element(flat_field_labels): """Move 'label_fields' keys of dict into nested label_fields element. Transform a flat dictionary with label_fields into a nested dictionary. Any label_fields keys that have a value of None/False are pruned. ex. given a flat_field_labels of { 'vendor_id': 2, 'financial_label_code': 'q', 'financial_label_name': 't', 'rep_owner_name': None } the following dict is returned { 'vendor_id': 2, label_fields: { 'financial_label_code': 'q', 'financial_label_name': 't' } } Args: flat_field_labels (dict): dictionary with label fields to nest. Returns: dict: dictionary with label fields nested. """ label_fields = ( 'financial_label_code', 'financial_label_name', 'rep_owner_code', 'rep_owner_name', 'gras_imprint_code', 'gras_imprint_name', 'sap_profit_center_code', 'sap_profit_center_name') nested_label_fields = copy.deepcopy(flat_field_labels) nested_label_fields['label_fields'] = dict() for label_field in label_fields: value = nested_label_fields.pop(label_field, None) if value: nested_label_fields['label_fields'][label_field] = value return nested_label_fields def add_update_vendor_meta(vendor_id, metadata, subaccount_id=None): """Add or update or delete sony metadata fields. Args: vendor_id (int): the id of the label metadata (dict): dictionary w/ label fields to add or update. subaccount_id (int): the id of the subaccount Returns: response.Response: a response whose message has a dict w/ label fields """ if ( 'financial_label_code' not in metadata.keys() or 'financial_label_name' not in metadata.keys() or 'rep_owner_code' not in metadata.keys() or 'rep_owner_name' not in metadata.keys() or 'gras_imprint_code' not in metadata.keys() or 'gras_imprint_name' not in metadata.keys() ): return response.create_fatal_response('missing key in metadata') # if all fields empty then its a deletion if is_empty_metadata(metadata): return field_manager.delete_vendor_metadata(vendor_id, subaccount_id) else: return field_manager.add_update_vendor_metadata( vendor_id, metadata, subaccount_id) def is_empty_metadata(metadata): """Were the fields posted all blank. Args: metadata (dict): dictionary w/ label fields to add or update Returns: (bool): True if all fields are empty. False if at least one filled """ # check if the fields are available if ( 'financial_label_code' not in metadata.keys() or 'financial_label_name' not in metadata.keys() or 'rep_owner_code' not in metadata.keys() or 'rep_owner_name' not in metadata.keys() or 'gras_imprint_code' not in metadata.keys() or 'gras_imprint_name' not in metadata.keys() or 'sap_profit_center_code' not in metadata.keys() or 'sap_profit_center_name' not in metadata.keys() ): return False if ( not metadata['financial_label_code'] and not metadata['financial_label_name'] and not metadata['rep_owner_code'] and not metadata['rep_owner_name'] and not metadata['gras_imprint_code'] and not metadata['gras_imprint_name'] and not metadata['sap_profit_center_code'] and not metadata['sap_profit_center_name'] ): return True else: return False