<s> from openprocurement . api . models import get_now <EOL> from openprocurement . api . utils import ( <EOL> apply_patch , <EOL> save_tender , <EOL> check_tender_status , <EOL> opresource , <EOL> json_view , <EOL> context_unpack , <EOL> APIResource , <EOL> ) <EOL> from openprocurement . api . validation import ( <EOL> validate_contract_data , <EOL> validate_patch_contract_data , <EOL> ) <EOL> @ opresource ( name = '<STR_LIT>' , <EOL> collection_path = '<STR_LIT>' , <EOL> path = '<STR_LIT>' , <EOL> procurementMethodType = '<STR_LIT>' , <EOL> description = "<STR_LIT>" ) <EOL> class TenderAwardContractResource ( APIResource ) : <EOL> @ json_view ( content_type = "<STR_LIT:application/json>" , permission = '<STR_LIT>' , validators = ( validate_contract_data , ) ) <EOL> def collection_post ( self ) : <EOL> """<STR_LIT>""" <EOL> tender = self . request . validated [ '<STR_LIT>' ] <EOL> if tender . status not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , '<STR_LIT>' . format ( tender . status ) ) <EOL> self . request . errors . status = <NUM_LIT> <EOL> return <EOL> contract = self . request . validated [ '<STR_LIT>' ] <EOL> tender . contracts . append ( contract ) <EOL> if save_tender ( self . request ) : <EOL> self . LOGGER . info ( '<STR_LIT>' . format ( contract . id ) , <EOL> extra = context_unpack ( self . request , { '<STR_LIT>' : '<STR_LIT>' } , { '<STR_LIT>' : contract . id } ) ) <EOL> self . request . response . status = <NUM_LIT> <EOL> self . request . response . headers [ '<STR_LIT>' ] = self . request . route_url ( '<STR_LIT>' , tender_id = tender . id , contract_id = contract [ '<STR_LIT:id>' ] ) <EOL> return { '<STR_LIT:data>' : contract . serialize ( ) } <EOL> @ json_view ( permission = '<STR_LIT>' ) <EOL> def collection_get ( self ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT:data>' : [ i . serialize ( ) for i in self . request . context . contracts ] } <EOL> @ json_view ( permission = '<STR_LIT>' ) <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT:data>' : self . request . validated [ '<STR_LIT>' ] . serialize ( ) } <EOL> @ json_view ( content_type = "<STR_LIT:application/json>" , permission = '<STR_LIT>' , validators = ( validate_patch_contract_data , ) ) <EOL> def patch ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . request . validated [ '<STR_LIT>' ] not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , '<STR_LIT>' . format ( self . request . validated [ '<STR_LIT>' ] ) ) <EOL> self . request . errors . status = <NUM_LIT> <EOL> return <EOL> tender = self . request . validated [ '<STR_LIT>' ] <EOL> if any ( [ i . status != '<STR_LIT>' for i in tender . lots if i . id in [ a . lotID for a in tender . awards if a . id == self . request . context . awardID ] ] ) : <EOL> self . request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> self . request . errors . status = <NUM_LIT> <EOL> return <EOL> data = self . request . validated [ '<STR_LIT:data>' ] <EOL> if data [ '<STR_LIT:value>' ] : <EOL> for ro_attr in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if data [ '<STR_LIT:value>' ] [ ro_attr ] != getattr ( self . context . value , ro_attr ) : <EOL> self . request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , '<STR_LIT>' . format ( ro_attr ) ) <EOL> self . request . errors . status = <NUM_LIT> <EOL> return <EOL> award = [ a for a in tender . awards if a . id == self . request . context . awardID ] [ <NUM_LIT:0> ] <EOL> if data [ '<STR_LIT:value>' ] [ '<STR_LIT>' ] > award . value . amount : <EOL> self . request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , '<STR_LIT>' . format ( award . value . amount ) ) <EOL> self . request . errors . status = <NUM_LIT> <EOL> return <EOL> if self . request . context . status != '<STR_LIT>' and '<STR_LIT:status>' in data and data [ '<STR_LIT:status>' ] == '<STR_LIT>' : <EOL> award = [ a for a in tender . awards if a . id == self . request . context . awardID ] [ <NUM_LIT:0> ] <EOL> stand_still_end = award . complaintPeriod . endDate <EOL> if stand_still_end > get_now ( ) : <EOL> self . request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , '<STR_LIT>' . format ( stand_still_end . isoformat ( ) ) ) <EOL> self . request . errors . status = <NUM_LIT> <EOL> return <EOL> pending_complaints = [ <EOL> i <EOL> for i in tender . complaints <EOL> if i . status in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] and i . relatedLot in [ None , award . lotID ] <EOL> ] <EOL> pending_awards_complaints = [ <EOL> i <EOL> for a in tender . awards <EOL> for i in a . complaints <EOL> if i . status in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] and a . lotID == award . lotID <EOL> ] <EOL> if pending_complaints or pending_awards_complaints : <EOL> self . request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> self . request . errors . status = <NUM_LIT> <EOL> return <EOL> contract_status = self . request . context . status <EOL> apply_patch ( self . request , save = False , src = self . request . context . serialize ( ) ) <EOL> if contract_status != self . request . context . status and ( contract_status != '<STR_LIT>' or self . request . context . status != '<STR_LIT>' ) : <EOL> self . request . errors . add ( '<STR_LIT:body>' , '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> self . request . errors . status = <NUM_LIT> <EOL> return <EOL> if self . request . context . status == '<STR_LIT>' and not self . request . context . dateSigned : <EOL> self . request . context . dateSigned = get_now ( ) <EOL> check_tender_status ( self . request ) <EOL> if save_tender ( self . request ) : <EOL> self . LOGGER . info ( '<STR_LIT>' . format ( self . request . context . id ) , <EOL> extra = context_unpack ( self . request , { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> return { '<STR_LIT:data>' : self . request . context . serialize ( ) } </s>
