<s> from __future__ import absolute_import , division , unicode_literals <EOL> from flask_restful . reqparse import RequestParser <EOL> from sqlalchemy import or_ <EOL> from sqlalchemy . orm import contains_eager , joinedload <EOL> from changes . api . auth import get_current_user <EOL> from changes . api . base import APIView <EOL> from changes . constants import Cause , Result <EOL> from changes . models import Author , Project , Source , Build <EOL> from changes . utils . phabricator_utils import ( might_be_diffusion_iden , <EOL> get_hash_from_diffusion_iden ) <EOL> def validate_author ( author_id ) : <EOL> current_user = get_current_user ( ) <EOL> if author_id == '<STR_LIT>' and not current_user : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return Author . find ( author_id , current_user ) <EOL> class ProjectBuildIndexAPIView ( APIView ) : <EOL> get_parser = RequestParser ( ) <EOL> get_parser . add_argument ( '<STR_LIT>' , type = lambda x : bool ( int ( x ) ) , location = '<STR_LIT:args>' , <EOL> default = True ) <EOL> get_parser . add_argument ( '<STR_LIT>' , type = validate_author , location = '<STR_LIT:args>' , <EOL> dest = '<STR_LIT>' ) <EOL> get_parser . add_argument ( '<STR_LIT>' , type = unicode , location = '<STR_LIT:args>' ) <EOL> get_parser . add_argument ( '<STR_LIT:source>' , type = unicode , location = '<STR_LIT:args>' ) <EOL> get_parser . add_argument ( '<STR_LIT:result>' , type = unicode , location = '<STR_LIT:args>' , <EOL> choices = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> get_parser . add_argument ( '<STR_LIT>' , type = lambda x : bool ( int ( x ) ) , location = '<STR_LIT:args>' , <EOL> default = False ) <EOL> get_parser . add_argument ( '<STR_LIT>' , type = unicode , location = '<STR_LIT:args>' , <EOL> choices = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> get_parser . add_argument ( '<STR_LIT>' , type = unicode , default = '<STR_LIT>' ) <EOL> def get ( self , project_id ) : <EOL> project = Project . get ( project_id ) <EOL> if not project : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> args = self . get_parser . parse_args ( ) <EOL> filters = [ ] <EOL> if args . authors : <EOL> filters . append ( Build . author_id . in_ ( [ a . id for a in args . authors ] ) ) <EOL> elif args . authors is not None : <EOL> return [ ] <EOL> if args . source : <EOL> filters . append ( Build . target . startswith ( args . source ) ) <EOL> if args . query : <EOL> clauses = [ ] <EOL> clauses . append ( Build . label . contains ( args . query ) ) <EOL> clauses . append ( Build . target . startswith ( args . query ) ) <EOL> if len ( args . query ) > <NUM_LIT:12> : <EOL> clauses . append ( Build . target . startswith ( args . query [ <NUM_LIT:0> : <NUM_LIT:12> ] ) ) <EOL> if might_be_diffusion_iden ( args . query ) : <EOL> possible_hash = get_hash_from_diffusion_iden ( args . query ) <EOL> if possible_hash : <EOL> clauses . append ( <EOL> Build . target . startswith ( possible_hash [ <NUM_LIT:0> : <NUM_LIT:12> ] ) ) <EOL> filters . append ( or_ ( * clauses ) ) <EOL> if args . result : <EOL> filters . append ( Build . result == Result [ args . result ] ) <EOL> if args . cause : <EOL> filters . append ( Build . cause == Cause [ args . cause ] ) <EOL> if args . tag : <EOL> filters . append ( Build . tags . any ( args . tag ) ) <EOL> if args . patches_only : <EOL> filters . append ( Source . patch_id != None ) <EOL> elif not args . include_patches : <EOL> filters . append ( Source . patch_id == None ) <EOL> queryset = Build . query . options ( <EOL> joinedload ( '<STR_LIT>' , innerjoin = True ) , <EOL> joinedload ( '<STR_LIT>' ) , <EOL> contains_eager ( '<STR_LIT:source>' ) . joinedload ( '<STR_LIT>' ) , <EOL> ) . join ( <EOL> Source , Source . id == Build . source_id , <EOL> ) . filter ( <EOL> Build . project_id == project . id , <EOL> * filters <EOL> ) . order_by ( Build . date_created . desc ( ) ) <EOL> return self . paginate ( queryset ) </s>
