"""Validation for a Product.""" from switchboard_consumer.constants.validation import ( REJECT_RESPONSE, UNKNOWN_TYPE_OF_VIDEO, VALIDATION_CODE, VIDEO_CLINE_NOT_VALID, VIDEO_PLINE_NOT_VALID, VIDEO_TITLE_IS_EMPTY, WARNING_RESPONSE) from switchboard_consumer.constants.video_single_type import VIDEO_TYPE_MAPPING from switchboard_consumer.formatters.product import format_cline, \ format_pline, format_title_fields from switchboard_consumer.logic.validation.track.rules import valid_pline def validate_video_single_product(product_data): """Validate product.""" error_messages = [] try: sides = product_data['components'][0]['sides'] track = sides[0]['productTracks'][0]['track'] invalid_type_of_video = _validate_type_of_video(track) if invalid_type_of_video: error_messages.append(invalid_type_of_video) invalid_video_title = _validate_video_title(product_data) if invalid_video_title: error_messages.append(invalid_video_title) invalid_pline = _validate_pline(product_data) if invalid_pline: error_messages.append(invalid_pline) invalid_cline = _validate_cline(product_data) if invalid_cline: error_messages.append(invalid_cline) except KeyError: pass return error_messages def _validate_type_of_video(track_data): """Validate video type.""" type_of_video = track_data['videoType'] if type_of_video not in VIDEO_TYPE_MAPPING: return { 'code': VALIDATION_CODE, 'message': UNKNOWN_TYPE_OF_VIDEO.format(type_of_video), 'response': WARNING_RESPONSE } def _validate_video_title(product_data): """Validate video title is not empty.""" formal_title_text, formal_title_subtext = format_title_fields( product_data) if not formal_title_text: return { 'code': VALIDATION_CODE, 'message': VIDEO_TITLE_IS_EMPTY, 'response': WARNING_RESPONSE } def _validate_pline(product_data): """Validate pline.""" pline = format_pline(product_data) if not valid_pline(pline): return { 'code': VALIDATION_CODE, 'message': VIDEO_PLINE_NOT_VALID.format(pline=pline), 'response': REJECT_RESPONSE } def _validate_cline(product_data): """Validate cline.""" cline = format_cline(product_data) if not valid_pline(cline): return { 'code': VALIDATION_CODE, 'message': VIDEO_CLINE_NOT_VALID.format(cline=cline), 'response': REJECT_RESPONSE }