"""Manual Adjustment Flask handlers.""" from flask import g from flask import request from manualadjustment import config from manualadjustment import constants from manualadjustment import presentation from manualadjustment.app import application from manualadjustment.logic import manual_adjustment from manualadjustment.logic.manual_adjustment import Result from manualadjustment.ma_timing import timed_fn from manualadjustment.models import manual_adj_persister as persister @application.route('/manual_adjustment', methods=['GET']) @timed_fn def get_manual_adjustment(): """Get manual adjustment. Gets manual adjustment(s) from art_relations from optional args Args: parent_id (int): parent id parent_type (int): parent type category_id (int): category id apply_to_period_id (int): period id manual adjustment was applied page_offset: db record to start at (begins at 0) page_limit: number of records to retrieve from db Returns: json response of manual adjustment(s) """ # get request params parent_id = request.args.get('parent_id') parent_type = request.args.get('parent_type') category_id = request.args.get('category_id') apply_to_period_id = request.args.get('apply_to_period_id') page_offset = request.args.get( 'page_offset', default=constants.PAGE_OFFSET_DEFAULT, type=int) page_limit = request.args.get( 'page_limit', default=constants.PAGE_LIMIT_DEFAULT, type=int) # get manual_adjustment list and page_count result = manual_adjustment.get( parent_id, parent_type, category_id, apply_to_period_id, page_offset, page_limit) g.log.debug('retrieved manual_adjustments and page_count from db') # construct json response and return with status code if not result.success: return presentation.error_response(result) else: return presentation.get_manual_adj_response(result) @application.route('/manual_adjustment', methods=['POST']) @timed_fn def post_manual_adjustment(): """Post manual adjustment. Inserts new manual adjustment into art_relations with optional attachment Args: amount (decimal): amount of adjustment category_id (int): id for perf royalties, sync licensing, etc comment (string): comment regarding the adjustment adjust_for_period_id (int): period id manual adjustment is for apply_to_period_id (int): period id manual adjustment is applied to parent_id (int): parent id parent_type (string): type of parent id (vendor, etc) created_by (int): OA user ID to associate with the adjustment attachment (file): optional attached file with statement details Returns: json response of manual adjustment that was inserted """ request_data = request.get_json() if not request_data: result = Result( error='invalid mimetype', error_detail='please set the mimetype to application/json', status=400) return presentation.error_response(result) else: attachment = request.files.get('attachment', None) result = manual_adjustment.create(attachment, request_data) if not result.success: return presentation.error_response(result) else: return presentation.manual_adj_response(result) @application.route('/manual_adjustment/', methods=['PUT']) @timed_fn def put_manual_adjustment(adjustment_id): """Put manual adjustment data. Updates manual adjustment into art_relations with optional attachment. Args: adjustment_id (int): manual adjustment id. Returns: Response: manual adjustment that was updated. """ # @todo: maybe extract the common mimetime stuff into a decorator request_data = request.get_json() if request_data: attachment = request.files.get('attachment', None) result = manual_adjustment.update( adjustment_id, request_data, attachment) return presentation.manual_adj_response(result) else: result = Result( error='invalid mimetype', error_detail='please set the mimetype to application/json', status=400) return presentation.error_response(result) @application.route(config.HEALTH_CHECK, methods=['GET']) def health(): """Application health check. This simply returns an 200 OK response, if this endpoint is not responding it is assumed the system/app is unhealthy. Returns: json response indicating system health """ persister.check_db_connectivity() return presentation.health_response('OK') @application.route('/service_info', methods=['GET']) def service_info(): """Show information about the service and environment.""" return presentation.service_info()