<s> from __future__ import absolute_import , division , unicode_literals <EOL> from changes . api . base import APIView <EOL> from changes . api . auth import get_current_user <EOL> from changes . config import db <EOL> from changes . models import Author , Build , PhabricatorDiff <EOL> from collections import defaultdict <EOL> from changes . utils . phabricator_utils import PhabricatorClient <EOL> import requests <EOL> class AuthorPhabricatorDiffsAPIView ( APIView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , author_id ) : <EOL> authors = Author . find ( author_id , get_current_user ( ) ) <EOL> if not authors and author_id == '<STR_LIT>' : <EOL> return '''<STR_LIT>''' , <NUM_LIT> <EOL> elif not authors : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> try : <EOL> author_email = authors [ <NUM_LIT:0> ] . email <EOL> request = PhabricatorClient ( ) <EOL> request . connect ( ) <EOL> user_info = request . call ( '<STR_LIT>' , { '<STR_LIT>' : [ author_email ] } ) <EOL> if not user_info : <EOL> return '<STR_LIT>' % author_email , <NUM_LIT> <EOL> author_phid = user_info [ <NUM_LIT:0> ] [ "<STR_LIT>" ] <EOL> diff_info = request . call ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : [ author_phid ] , <EOL> '<STR_LIT:status>' : "<STR_LIT>" <EOL> } ) <EOL> diff_info . sort ( key = lambda k : - <NUM_LIT:1> * int ( k [ '<STR_LIT>' ] ) ) <EOL> except requests . exceptions . ConnectionError : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> if not diff_info : <EOL> return self . respond ( [ ] ) <EOL> rows = list ( db . session . query ( <EOL> PhabricatorDiff , Build <EOL> ) . join ( <EOL> Build , Build . source_id == PhabricatorDiff . source_id <EOL> ) . filter ( <EOL> PhabricatorDiff . revision_id . in_ ( [ d [ '<STR_LIT:id>' ] for d in diff_info ] ) <EOL> ) ) <EOL> serialized_builds = zip ( <EOL> self . serialize ( [ row . Build for row in rows ] ) , <EOL> [ row . PhabricatorDiff for row in rows ] <EOL> ) <EOL> builds_map = defaultdict ( list ) <EOL> for build , phabricator_diff in serialized_builds : <EOL> builds_map [ str ( phabricator_diff . revision_id ) ] . append ( build ) <EOL> for d in diff_info : <EOL> d [ '<STR_LIT>' ] = builds_map [ str ( d [ '<STR_LIT:id>' ] ) ] <EOL> return self . respond ( diff_info ) </s>
