<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import piston . handler <EOL> import piston . utils <EOL> import server . models <EOL> import server . utils <EOL> import server . common <EOL> import rest . utils <EOL> class ParticipationHandler ( piston . handler . BaseHandler ) : <EOL> allowed_methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> model = server . models . Participation <EOL> fields = ( '<STR_LIT>' , ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT:username>' , '<STR_LIT>' , ( '<STR_LIT:location>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) ) ) , '<STR_LIT:status>' ) <EOL> def read ( self , request , trip_id = None , username = None ) : <EOL> if username : <EOL> try : <EOL> trip = server . models . Trip . objects . get ( id = trip_id , active = True ) <EOL> participation = trip . get_participations ( ) . filter ( author__username = username ) . get ( ) <EOL> setattr ( participation , '<STR_LIT:status>' , participation . get_status_name ( ) ) <EOL> if participation . status == '<STR_LIT>' or participation . status == '<STR_LIT>' : <EOL> return piston . utils . rc . NOT_FOUND <EOL> return participation <EOL> except server . models . Trip . DoesNotExist : <EOL> return piston . utils . rc . NOT_FOUND <EOL> except server . models . Participation . DoesNotExist : <EOL> return piston . utils . rc . NOT_FOUND <EOL> try : <EOL> trip = server . models . Trip . objects . get ( id = trip_id , active = True ) <EOL> participations = trip . get_participations ( ) <EOL> for participation in participations : <EOL> setattr ( participation , '<STR_LIT:status>' , participation . get_status_name ( ) ) <EOL> if participation . status == '<STR_LIT>' or participation . status == '<STR_LIT>' : <EOL> participations . exclude ( id = participation . id ) <EOL> return participations <EOL> except server . models . Trip . DoesNotExist : <EOL> return piston . utils . rc . NOT_FOUND <EOL> def create ( self , request , trip_id ) : <EOL> data = request . data <EOL> current_user = rest . utils . get_rest_user ( request ) <EOL> try : <EOL> trip = server . models . Trip . objects . get ( id = trip_id ) <EOL> status = data [ '<STR_LIT:status>' ] <EOL> person_participation = trip . get_participations ( ) . filter ( author__username = current_user . username ) <EOL> if len ( person_participation ) > <NUM_LIT:0> : <EOL> return piston . utils . rc . DUPLICATE_ENTRY <EOL> if status == server . models . Participation . _status [ '<STR_LIT>' ] [ <NUM_LIT:0> ] : <EOL> result = server . passenger . requestRide ( trip , current_user ) <EOL> result . value . href = rest . utils . get_href ( request , '<STR_LIT>' , [ trip . id , result . value . author . username ] ) <EOL> result . value . save ( ) <EOL> else : <EOL> result = server . models . Response ( <EOL> server . models . Response . BAD_REQUEST , <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT:status>' : [ u'<STR_LIT>' ] } ) <EOL> if result . code < <NUM_LIT> : <EOL> result . value . status = result . value . get_status_name ( ) <EOL> return result . value <EOL> return rest . utils . extract_result_from_response ( result ) <EOL> except server . models . Trip . DoesNotExist : <EOL> return piston . utils . rc . NOT_FOUND <EOL> except KeyError , e : <EOL> result = server . models . Response ( <EOL> server . models . Response . BAD_REQUEST , <EOL> '<STR_LIT>' , <EOL> e ) <EOL> return rest . utils . extract_result_from_response ( result ) <EOL> def update ( self , request , trip_id = None , username = None ) : <EOL> data = request . data <EOL> current_user = rest . utils . get_rest_user ( request ) <EOL> try : <EOL> trip = server . models . Trip . objects . get ( id = trip_id ) <EOL> status = data [ '<STR_LIT:status>' ] <EOL> person_participation = trip . get_participations ( ) . filter ( author__username = username ) . get ( ) <EOL> person = person_participation . author <EOL> if current_user != person and current_user != trip . author : <EOL> return piston . utils . rc . FORBIDDEN <EOL> status_code = person_participation . get_status_code ( status ) <EOL> if status_code < person_participation . get_status_code ( ) : <EOL> result = server . models . Response ( <EOL> server . models . Response . FORBIDDEN , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' + str ( data [ '<STR_LIT:status>' ] ) + '<STR_LIT>' + str ( status ) + '<STR_LIT>' ) <EOL> return rest . utils . extract_result_from_response ( result ) <EOL> status_set = server . models . Participation . _status <EOL> if status == status_set [ '<STR_LIT>' ] [ <NUM_LIT:0> ] : <EOL> if not current_user == trip . author : <EOL> return piston . utils . rc . FORBIDDEN <EOL> result = server . driver . acceptRide ( trip , current_user , person_participation . author ) <EOL> elif status == status_set [ '<STR_LIT:start>' ] [ <NUM_LIT:0> ] : <EOL> result = server . passenger . startRide ( trip , person_participation . author ) <EOL> elif status == status_set [ '<STR_LIT>' ] [ <NUM_LIT:0> ] : <EOL> result = server . passenger . finishRide ( trip , person_participation . author ) <EOL> else : <EOL> result = server . models . Response ( <EOL> server . models . Response . FORBIDDEN , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' + str ( data [ '<STR_LIT:status>' ] ) + '<STR_LIT>' + str ( status ) + '<STR_LIT>' ) <EOL> return rest . utils . extract_result_from_response ( result ) <EOL> except server . models . Trip . DoesNotExist : <EOL> return piston . utils . rc . NOT_FOUND <EOL> except server . models . Participation . DoesNotExist : <EOL> return piston . utils . rc . NOT_FOUND <EOL> except KeyError , e : <EOL> result = server . models . Response ( <EOL> server . models . Response . BAD_REQUEST , <EOL> '<STR_LIT>' , <EOL> e ) <EOL> return rest . utils . extract_result_from_response ( result ) <EOL> def delete ( self , request , trip_id = None , username = None ) : <EOL> current_user = rest . utils . get_rest_user ( request ) <EOL> try : <EOL> trip = server . models . Trip . objects . get ( id = trip_id ) <EOL> person_participation = trip . get_participations ( ) . filter ( author__username = username ) . get ( ) <EOL> person = person_participation . author <EOL> if current_user != person and current_user != trip . author : <EOL> return piston . utils . rc . FORBIDDEN <EOL> if current_user == person : <EOL> response = server . passenger . cancelRide ( trip , current_user ) <EOL> return rest . utils . extract_result_from_response ( response ) <EOL> elif current_user == trip . author : <EOL> response = server . driver . refuseRide ( trip , person ) <EOL> rest . utils . extract_result_from_response ( response ) <EOL> else : <EOL> return piston . utils . rc . FORBIDDEN <EOL> except server . models . Trip . DoesNotExist , server . models . Participation . DoesNotExist : <EOL> return piston . utils . rc . NOT_FOUND <EOL> except Exception , e : <EOL> result = server . models . Response ( <EOL> server . models . Response . BAD_REQUEST , <EOL> '<STR_LIT>' , <EOL> e ) <EOL> return rest . utils . extract_result_from_response ( result ) </s>
