<s> from openprocurement . api . models import get_now <EOL> from schematics . exceptions import ModelValidationError , ModelConversionError <EOL> from openprocurement . api . utils import apply_data_patch , update_logging_context <EOL> def validate_json_data ( request ) : <EOL> try : <EOL> json = request . json_body <EOL> except ValueError , e : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , e . message ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> if not isinstance ( json , dict ) or '<STR_LIT:data>' not in json or not isinstance ( json . get ( '<STR_LIT:data>' ) , dict ) : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , "<STR_LIT>" ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> return json [ '<STR_LIT:data>' ] <EOL> def validate_data ( request , model , partial = False , data = None ) : <EOL> if data is None : <EOL> data = validate_json_data ( request ) <EOL> if data is None : <EOL> return <EOL> try : <EOL> if partial and isinstance ( request . context , model ) : <EOL> initial_data = request . context . serialize ( ) <EOL> m = model ( initial_data ) <EOL> new_patch = apply_data_patch ( initial_data , data ) <EOL> if new_patch : <EOL> m . import_data ( new_patch , partial = True , strict = True ) <EOL> m . __parent__ = request . context . __parent__ <EOL> m . validate ( ) <EOL> role = request . context . get_role ( ) <EOL> method = m . to_patch <EOL> else : <EOL> m = model ( data ) <EOL> m . __parent__ = request . context <EOL> m . validate ( ) <EOL> method = m . serialize <EOL> role = '<STR_LIT>' <EOL> except ( ModelValidationError , ModelConversionError ) , e : <EOL> for i in e . message : <EOL> request . errors . add ( '<STR_LIT:body>' , i , e . message [ i ] ) <EOL> request . errors . status = <NUM_LIT> <EOL> data = None <EOL> except ValueError , e : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , e . message ) <EOL> request . errors . status = <NUM_LIT> <EOL> data = None <EOL> else : <EOL> if hasattr ( type ( m ) , '<STR_LIT>' ) and role not in type ( m ) . _options . roles : <EOL> request . errors . add ( '<STR_LIT:url>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> request . errors . status = <NUM_LIT> <EOL> data = None <EOL> else : <EOL> data = method ( role ) <EOL> request . validated [ '<STR_LIT:data>' ] = data <EOL> if not partial : <EOL> m = model ( data ) <EOL> m . __parent__ = request . context <EOL> request . validated [ model . __name__ . lower ( ) ] = m <EOL> return data <EOL> def validate_tender_data ( request ) : <EOL> update_logging_context ( request , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> data = validate_json_data ( request ) <EOL> if data is None : <EOL> return <EOL> model = request . tender_from_data ( data , create = False ) <EOL> if not request . check_accreditation ( model . create_accreditation ) : <EOL> request . errors . add ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> data = validate_data ( request , model , data = data ) <EOL> if data and data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , '<STR_LIT>' ) not in model . procuring_entity_kinds : <EOL> request . errors . add ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( kind = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , '<STR_LIT>' ) , kinds = '<STR_LIT:U+002CU+0020>' . join ( model . procuring_entity_kinds ) ) ) <EOL> request . errors . status = <NUM_LIT> <EOL> def validate_patch_tender_data ( request ) : <EOL> return validate_data ( request , type ( request . tender ) , True ) <EOL> def validate_tender_auction_data ( request ) : <EOL> data = validate_patch_tender_data ( request ) <EOL> tender = request . validated [ '<STR_LIT>' ] <EOL> if tender . status != '<STR_LIT>' : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , '<STR_LIT>' . format ( '<STR_LIT>' if request . method == '<STR_LIT:POST>' else '<STR_LIT>' , tender . status ) ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> lot_id = request . matchdict . get ( '<STR_LIT>' ) <EOL> if tender . lots and any ( [ i . status != '<STR_LIT>' for i in tender . lots if i . id == lot_id ] ) : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , '<STR_LIT>' . format ( '<STR_LIT>' if request . method == '<STR_LIT:POST>' else '<STR_LIT>' ) ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> if data is not None : <EOL> bids = data . get ( '<STR_LIT>' , [ ] ) <EOL> tender_bids_ids = [ i . id for i in tender . bids ] <EOL> if len ( bids ) != len ( tender . bids ) : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT>' , "<STR_LIT>" ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> if set ( [ i [ '<STR_LIT:id>' ] for i in bids ] ) != set ( tender_bids_ids ) : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT>' , "<STR_LIT>" ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> data [ '<STR_LIT>' ] = [ x for ( y , x ) in sorted ( zip ( [ tender_bids_ids . index ( i [ '<STR_LIT:id>' ] ) for i in bids ] , bids ) ) ] <EOL> if data . get ( '<STR_LIT>' ) : <EOL> tender_lots_ids = [ i . id for i in tender . lots ] <EOL> if len ( data . get ( '<STR_LIT>' , [ ] ) ) != len ( tender . lots ) : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT>' , "<STR_LIT>" ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> if set ( [ i [ '<STR_LIT:id>' ] for i in data . get ( '<STR_LIT>' , [ ] ) ] ) != set ( [ i . id for i in tender . lots ] ) : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT>' , "<STR_LIT>" ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> data [ '<STR_LIT>' ] = [ <EOL> x if x [ '<STR_LIT:id>' ] == lot_id else { } <EOL> for ( y , x ) in sorted ( zip ( [ tender_lots_ids . index ( i [ '<STR_LIT:id>' ] ) for i in data . get ( '<STR_LIT>' , [ ] ) ] , data . get ( '<STR_LIT>' , [ ] ) ) ) <EOL> ] <EOL> if tender . lots : <EOL> for index , bid in enumerate ( bids ) : <EOL> if ( getattr ( tender . bids [ index ] , '<STR_LIT:status>' , '<STR_LIT>' ) or '<STR_LIT>' ) == '<STR_LIT>' : <EOL> if len ( bid . get ( '<STR_LIT>' , [ ] ) ) != len ( tender . bids [ index ] . lotValues ) : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT>' , [ { u'<STR_LIT>' : [ u'<STR_LIT>' ] } ] ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> for lot_index , lotValue in enumerate ( tender . bids [ index ] . lotValues ) : <EOL> if lotValue . relatedLot != bid . get ( '<STR_LIT>' , [ ] ) [ lot_index ] . get ( '<STR_LIT>' , None ) : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT>' , [ { u'<STR_LIT>' : [ { u'<STR_LIT>' : [ '<STR_LIT>' ] } ] } ] ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> for bid_index , bid in enumerate ( data [ '<STR_LIT>' ] ) : <EOL> if '<STR_LIT>' in bid : <EOL> bid [ '<STR_LIT>' ] = [ <EOL> x if x [ '<STR_LIT>' ] == lot_id and ( getattr ( tender . bids [ bid_index ] . lotValues [ lotValue_index ] , '<STR_LIT:status>' , '<STR_LIT>' ) or '<STR_LIT>' ) == '<STR_LIT>' else { } <EOL> for lotValue_index , x in enumerate ( bid [ '<STR_LIT>' ] ) <EOL> ] <EOL> else : <EOL> data = { } <EOL> if request . method == '<STR_LIT:POST>' : <EOL> now = get_now ( ) . isoformat ( ) <EOL> if tender . lots : <EOL> data [ '<STR_LIT>' ] = [ { '<STR_LIT>' : { '<STR_LIT>' : now } } if i . id == lot_id else { } for i in tender . lots ] <EOL> else : <EOL> data [ '<STR_LIT>' ] = { '<STR_LIT>' : now } <EOL> request . validated [ '<STR_LIT:data>' ] = data <EOL> def validate_bid_data ( request ) : <EOL> if not request . check_accreditation ( request . tender . edit_accreditation ) : <EOL> request . errors . add ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> update_logging_context ( request , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> model = type ( request . tender ) . bids . model_class <EOL> return validate_data ( request , model ) <EOL> def validate_patch_bid_data ( request ) : <EOL> model = type ( request . tender ) . bids . model_class <EOL> return validate_data ( request , model , True ) <EOL> def validate_award_data ( request ) : <EOL> update_logging_context ( request , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> model = type ( request . tender ) . awards . model_class <EOL> return validate_data ( request , model ) <EOL> def validate_patch_award_data ( request ) : <EOL> model = type ( request . tender ) . awards . model_class <EOL> return validate_data ( request , model , True ) <EOL> def validate_patch_document_data ( request ) : <EOL> model = type ( request . context ) <EOL> return validate_data ( request , model , True ) <EOL> def validate_question_data ( request ) : <EOL> if not request . check_accreditation ( request . tender . edit_accreditation ) : <EOL> request . errors . add ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> update_logging_context ( request , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> model = type ( request . tender ) . questions . model_class <EOL> return validate_data ( request , model ) <EOL> def validate_patch_question_data ( request ) : <EOL> model = type ( request . tender ) . questions . model_class <EOL> return validate_data ( request , model , True ) <EOL> def validate_complaint_data ( request ) : <EOL> if not request . check_accreditation ( request . tender . edit_accreditation ) : <EOL> request . errors . add ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> request . errors . status = <NUM_LIT> <EOL> return <EOL> update_logging_context ( request , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> model = type ( request . tender ) . complaints . model_class <EOL> return validate_data ( request , model ) <EOL> def validate_patch_complaint_data ( request ) : <EOL> model = type ( request . tender ) . complaints . model_class <EOL> return validate_data ( request , model , True ) <EOL> def validate_cancellation_data ( request ) : <EOL> update_logging_context ( request , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> model = type ( request . tender ) . cancellations . model_class <EOL> return validate_data ( request , model ) <EOL> def validate_patch_cancellation_data ( request ) : <EOL> model = type ( request . tender ) . cancellations . model_class <EOL> return validate_data ( request , model , True ) <EOL> def validate_contract_data ( request ) : <EOL> update_logging_context ( request , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> model = type ( request . tender ) . contracts . model_class <EOL> return validate_data ( request , model ) <EOL> def validate_patch_contract_data ( request ) : <EOL> model = type ( request . tender ) . contracts . model_class <EOL> return validate_data ( request , model , True ) <EOL> def validate_lot_data ( request ) : <EOL> update_logging_context ( request , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> model = type ( request . tender ) . lots . model_class <EOL> return validate_data ( request , model ) <EOL> def validate_patch_lot_data ( request ) : <EOL> model = type ( request . tender ) . lots . model_class <EOL> return validate_data ( request , model , True ) <EOL> def validate_file_upload ( request ) : <EOL> update_logging_context ( request , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> if '<STR_LIT:file>' not in request . POST or not hasattr ( request . POST [ '<STR_LIT:file>' ] , '<STR_LIT:filename>' ) : <EOL> request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:file>' , '<STR_LIT>' ) <EOL> request . errors . status = <NUM_LIT> <EOL> else : <EOL> request . validated [ '<STR_LIT:file>' ] = request . POST [ '<STR_LIT:file>' ] <EOL> def validate_file_update ( request ) : <EOL> if request . content_type == '<STR_LIT>' : <EOL> validate_file_upload ( request ) </s>
