"""Application Handlers. Requests are redirected to handlers, which are responsible for getting information from the URL and passing it down to the logic layer. The way each layer talks to each other is through Response objects which defines the type status of the data and the data itself. Please note: the Orchard uses the term handlers over views as convention for clarity See: oto.response for more details. """ from flask import g from flask import request from oto import response from oto.adaptors.flask import flaskify from marketing import config from marketing.api import app from marketing.logic import entities from marketing.logic import health from marketing.logic import highlights from marketing.utils import headers @app.route(config.HEALTH_CHECK, methods=['GET']) def get_health(): """Check the health of the application.""" return flaskify(health.get_health_check()) @app.route('/highlights//', methods=['GET']) def get_highlights_by_entity(entity_type, entity_id): """Get a highlight by entity. Args: entity_type (str): the entity type. entity_id (str): the entity id. Returns: flask.Response: The marketing highlights. """ ownership = headers.verify_grass_ownership( entities.is_owner, entity_type, entity_id) if not ownership: return flaskify(ownership) return flaskify(highlights.get_highlights( entity_type, entity_id, client=request.args.get('client', None), offset=request.args.get('offset', None), limit=request.args.get('limit', None), mkt_program_id=request.args.get('mkt_program_id', None))) @app.route('/highlights//', methods=['DELETE']) def delete_highlights_by_entity(entity_type, entity_id): """Delete a highlight by entity. Args: entity_type (str): the entity type. entity_id (str): the entity id. Returns: flask.Response: The status of deletion. """ ownership = headers.verify_grass_ownership( entities.is_owner, entity_type, entity_id) if not ownership: return flaskify(ownership) return flaskify(highlights.delete_highlights_by_entity( entity_type, entity_id)) @app.route('/highlights', methods=['POST']) def create_highlight(): """Create a new highlight.""" data = request.get_json(silent=True, force=True) or {} ownership = headers.verify_grass_ownership( entities.is_owner, data.get('entity'), data.get('entity_id')) if not ownership: return flaskify(ownership) return flaskify(highlights.create_highlight(data)) @app.route('/highlights/', methods=['PUT']) def update_highlight(highlight_id): """Update an highlight by its id. Args: highlight_id (int): the highlight's id. Returns: flask.Response: the flask response of this operation. """ data = request.get_json(silent=True, force=True) or {} ownership = headers.verify_grass_ownership( highlights.is_owner, highlight_id) if not ownership: return flaskify(ownership) return flaskify(highlights.update_highlight(highlight_id, data)) @app.route('/highlights/', methods=['DELETE']) def delete_highlight(highlight_id): """Delete an highlight by its id. Args: highlight_id (int): the highlight's id. Returns: flask.Response: the flask response of this operation. """ ownership = headers.verify_grass_ownership( highlights.is_owner, highlight_id) if not ownership: return flaskify(ownership) return flaskify(highlights.delete_highlight(highlight_id)) @app.route( '/highlights/product//copy/', methods=['POST']) def copy_product_highlight(product_id, new_product_id): """Copy a product highlight. Args: product_id (int): the product id we are copying from. new_product_id (int): the destination product id. Returns: flask.Response: the flask response of this operation. """ copy_product_highlight_response = highlights.copy_product_highlights( product_id, new_product_id) return flaskify(copy_product_highlight_response) @app.errorhandler(500) def exception_handler(error): """Default handler when uncaught exception is raised. Note: Exception will also be sent to Sentry if config.SENTRY is set. Returns: flask.Response: A 500 response with JSON 'code' & 'message' payload. """ message = ( 'The server encountered an internal error ' 'and was unable to complete your request.') g.log.exception(error) return flaskify(response.create_fatal_response(message))