"""Project logic.""" import logging import const from models import ows_project_manager import util # noqa from utils import document as document_utils def prepare_doc(event): """Construct a Cloudsearch document for the projects corpus. Args: event (dict): event with project data from Maxwell's Returns: dict: cloudsearch document ready for upload """ json_doc = {} doc_type = 'add' event_type = event.get('type') if not event_type or event.get('_metadata'): logging.warning( 'An event with incompatible format received: %s', event) return json_doc if event_type == 'delete': doc_type = 'delete' if doc_type == 'add': json_doc = ows_project_manager.get_project_document( event['data']['project_id']) # Prevent subaccount_name from having a null value. if not json_doc.get('subaccount_name'): json_doc['subaccount_name'] = '' if event['data']['project_id'] in const.PROJECT_EXCLUSION_LIST: # Delete the cloudsearch record for exclusion list doc_type = 'delete' return document_utils.prepare_for_upload( util.parse_spec_file('projects'), { 'type': doc_type, 'id': 'proj{}'.format(event['data']['project_id']), 'fields': json_doc})