"""Logic for WorksheetAdjustment.""" from typing import Type from marshmallow import ValidationError from owsresponse import response from abacus_worksheet.constants.constants import DEFAULT_PAGE_LIMIT, DEFAULT_PAGE_OFFSET from abacus_worksheet.models.worksheet_adjustment_detail import ( WorksheetAdjustmentDetail, ) from abacus_worksheet.schemas.worksheet_adjustment_detail import ( WorksheetAdjustmentDetailSchema, ) from abacus_worksheet.utils.format_error import validation_error from abacus_worksheet.utils.request import validate_pagination_params def get_by_worksheet_adjustment_id( worksheet_adjustment_id: int, request_params: dict ) -> response.Response: """Get a list of worksheet adjustment details by worksheet_adjustment_id. Args: worksheet_adjustment_id (int): id of the worksheet_adjustment request_params (dict)(Optional): dict of query string passed to the url - limit(int): pagination limit - offset(int): pagination offset """ try: limit = request_params.get('limit', DEFAULT_PAGE_LIMIT) offset = request_params.get('offset', DEFAULT_PAGE_OFFSET) pagination_params = validate_pagination_params(limit, offset) items, total_count = WorksheetAdjustmentDetail.get_by_worksheet_adjustment_id( worksheet_adjustment_id, **pagination_params ) message = { 'items': WorksheetAdjustmentDetailSchema(many=True).dump(items), 'total_count': total_count, } except ValidationError as e: return validation_error(str(e)) return response.Response(message=message, status=200) def create_worksheet_adjustment_details( adjustment_details: list, worksheet_adjustment_id: int ) -> Type[response.Response]: """Create one or more worksheet_adjustment_detail's. Args: adjustment_details (list): a list of adjustment details worksheet_adjustment_id (list): id of the related worksheet_adjustment Returns: A list of new worksheet_adjustment_detail records. """ try: worksheet_adjustment_details = [] for adjustment_detail in adjustment_details: adjustment_detail.update( {'worksheet_adjustment_id': worksheet_adjustment_id} ) worksheet_adjustment_detail = WorksheetAdjustmentDetail.build( **adjustment_detail ) worksheet_adjustment_details.append(worksheet_adjustment_detail) WorksheetAdjustmentDetail.commit_changes() except Exception as e: raise e return worksheet_adjustment_details