<s> from __future__ import absolute_import , division , unicode_literals <EOL> from flask_restful . reqparse import RequestParser <EOL> from sqlalchemy . orm import joinedload <EOL> from uuid import UUID <EOL> from changes . api . base import APIView , error <EOL> from changes . models . build import Build <EOL> from changes . models . job import Job <EOL> from changes . models . source import Source <EOL> class SourceBuildIndexAPIView ( APIView ) : <EOL> """<STR_LIT>""" <EOL> get_parser = RequestParser ( ) <EOL> get_parser . add_argument ( '<STR_LIT>' , type = UUID , location = '<STR_LIT:args>' ) <EOL> get_parser . add_argument ( '<STR_LIT>' , location = '<STR_LIT:args>' ) <EOL> get_parser . add_argument ( '<STR_LIT>' , type = UUID , location = '<STR_LIT:args>' ) <EOL> def get ( self ) : <EOL> args = self . get_parser . parse_args ( ) <EOL> source_id = args . source_id <EOL> revision_sha = args . revision_sha <EOL> repo_id = args . repo_id <EOL> if source_id : <EOL> source = Source . query . filter ( <EOL> Source . id == source_id , <EOL> ) . first ( ) <EOL> elif revision_sha and repo_id : <EOL> source = Source . query . filter ( <EOL> Source . revision_sha == revision_sha , <EOL> Source . repository_id == repo_id , <EOL> Source . patch_id == None <EOL> ) . first ( ) <EOL> else : <EOL> return error ( '<STR_LIT>' ) <EOL> if source is None : <EOL> return error ( "<STR_LIT>" , http_code = <NUM_LIT> ) <EOL> builds = self . serialize ( list ( <EOL> Build . query . options ( <EOL> joinedload ( '<STR_LIT>' ) <EOL> ) . filter ( <EOL> Build . source_id == source . id , <EOL> ) . order_by ( Build . date_created . desc ( ) ) <EOL> ) ) <EOL> build_ids = [ build [ '<STR_LIT:id>' ] for build in builds ] <EOL> if len ( builds ) > <NUM_LIT:0> : <EOL> jobs = self . serialize ( list ( Job . query . filter ( <EOL> Job . build_id . in_ ( build_ids ) <EOL> ) ) ) <EOL> for b in builds : <EOL> b [ '<STR_LIT>' ] = [ j for j in jobs if j [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] == b [ '<STR_LIT:id>' ] ] <EOL> return self . paginate ( builds , serialize = False ) </s>
