<s> from sqlalchemy . orm import joinedload <EOL> from datetime import datetime <EOL> from changes . api . base import APIView <EOL> from changes . api . build_index import execute_build <EOL> from changes . config import db <EOL> from changes . constants import Result , Status <EOL> from changes . models import Build , Job , JobStep , ItemStat <EOL> class BuildRestartAPIView ( APIView ) : <EOL> def post ( self , build_id ) : <EOL> build = Build . query . options ( <EOL> joinedload ( '<STR_LIT>' , innerjoin = True ) , <EOL> joinedload ( '<STR_LIT>' ) , <EOL> joinedload ( '<STR_LIT:source>' ) . joinedload ( '<STR_LIT>' ) , <EOL> ) . get ( build_id ) <EOL> if build is None : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> if build . status != Status . finished : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> stat_ids = [ build . id ] <EOL> job_ids = [ <EOL> j [ <NUM_LIT:0> ] for j in <EOL> db . session . query ( Job . id ) . filter ( Job . build_id == build . id ) <EOL> ] <EOL> if job_ids : <EOL> step_ids = [ <EOL> s [ <NUM_LIT:0> ] for s in <EOL> db . session . query ( JobStep . id ) . filter ( JobStep . job_id . in_ ( job_ids ) ) <EOL> ] <EOL> stat_ids . extend ( job_ids ) <EOL> stat_ids . extend ( step_ids ) <EOL> if stat_ids : <EOL> ItemStat . query . filter ( <EOL> ItemStat . item_id . in_ ( stat_ids ) , <EOL> ) . delete ( synchronize_session = False ) <EOL> Job . query . filter ( <EOL> Job . build_id == build . id <EOL> ) . delete ( synchronize_session = False ) <EOL> build . date_started = datetime . utcnow ( ) <EOL> build . date_modified = build . date_started <EOL> build . date_finished = None <EOL> build . duration = None <EOL> build . status = Status . queued <EOL> build . result = Result . unknown <EOL> db . session . add ( build ) <EOL> execute_build ( build = build ) <EOL> return self . respond ( build ) </s>
