"""Interface for the ows-marketing microservice.""" from oto import response from owsrequest import request from project_manager.constant import marketing_const from project_manager.constant.services import OWS_MARKETING from project_manager.util.response_handler_util import safe_decode_json def create_project_highlights(project_id, highlight_description): """Create a project highlights for a project. POST request. Args: project_id (int): primary key for a project. highlight_description (str): The copy for the project highlights. Returns: response.Response: the result of creating a project highlights. """ params = { 'entity': 'project', 'entity_id': project_id, 'subject': 'Project Highlights', 'description': highlight_description, 'client': 'alw', 'mkt_program_id': marketing_const.PROGRAM_MARKETING_HIGHLIGHTS, 'attachment': 'N', 'scope': 'public'} highlight_response = request.post( OWS_MARKETING, '/highlights', json=params) content = safe_decode_json( highlight_response, OWS_MARKETING) if highlight_response.status_code != 201: return response.create_error_response( status=highlight_response.status_code, code=content.get('code'), message=content.get('message')) return response.Response( status=highlight_response.status_code, message=content) def get_project_highlights_by_project_id(project_id): """Get a single project highlights entry for a project. Args: project_id (int): primary key for a project. """ # this will not be necessary when we start using owsrequest params = { 'client': 'alw', 'limit': 1 } highlight_response = request.get( OWS_MARKETING, '/highlights/project/{PROJECT_ID}?mkt_program_id={PROGRAM_ID}'.format( PROJECT_ID=project_id, PROGRAM_ID=marketing_const.PROGRAM_MARKETING_HIGHLIGHTS), params=params) content = safe_decode_json( highlight_response, OWS_MARKETING) if highlight_response.status_code != 200: return response.create_error_response( status=highlight_response.status_code, code=content.get('code'), message=content.get('message')) return response.Response( status=highlight_response.status_code, message=content.get('items')[0]) def update_project_highlights(highlight_id, highlight_description): """Update a project highlights entry. PUT request. Args: highlight_id (int): primary key for the highlight to update. highlight_description (str): The copy for the project highlights. Returns: response.Response: the result of updating project highlights. """ params = {'description': highlight_description} highlight_response = request.put( OWS_MARKETING, '/highlights/{HIGHLIGHT_ID}'.format(HIGHLIGHT_ID=highlight_id), json=params) content = safe_decode_json( highlight_response, OWS_MARKETING) if highlight_response.status_code != 200: return response.create_error_response( status=highlight_response.status_code, code=content.get('code'), message=content.get('message')) return response.Response( status=highlight_response.status_code, message=content) def delete_project_highlight_with_project_deletion(highlight_id): """Delete a project highlight. This is intended to fail silently if there is an error when deleting a project higlight. This method is only called if the project has been successfully deleted. Args: highlight_id (int): primary key for the highlight to delete. Returns: response.Response: A successful response message. """ request.delete( OWS_MARKETING, '/highlights/{highlight_id}'.format(highlight_id=highlight_id)) return response.Response()