<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import datetime <EOL> import matching <EOL> import models <EOL> import django . db <EOL> def searchRide ( source , destination , passenger ) : <EOL> """<STR_LIT>""" <EOL> passenger_active_participation = passenger . get_active_participation ( ) <EOL> if passenger_active_participation : <EOL> resp = models . Response ( models . Response . FORBIDDEN , <EOL> "<STR_LIT>" , passenger_active_participation . trip ) <EOL> return resp <EOL> if passenger . location . georss_point != source . georss_point : <EOL> passenger . location = source <EOL> try : <EOL> passenger . location . full_clean ( ) <EOL> except django . core . exceptions . ValidationError , e : <EOL> resp = models . Response ( models . Response . BAD_REQUEST , <EOL> "<STR_LIT>" , e ) <EOL> return resp <EOL> passenger . location . save ( ) <EOL> trips = matching . search_ride ( destination , passenger ) <EOL> if not trips : <EOL> return models . Response ( models . Response . NOT_FOUND , <EOL> "<STR_LIT>" , [ ] ) <EOL> return models . Response ( models . Response . ALL_OK , <EOL> "<STR_LIT>" , trips ) <EOL> def requestRide ( trip , passenger ) : <EOL> """<STR_LIT>""" <EOL> passenger_active_participation = passenger . get_active_participation ( ) <EOL> if passenger_active_participation : <EOL> resp = models . Response ( models . Response . FORBIDDEN , <EOL> "<STR_LIT>" , passenger_active_participation . trip ) <EOL> return resp <EOL> participation = models . Participation ( trip_id = trip . id , <EOL> author_id = passenger . id , <EOL> role = '<STR_LIT>' , <EOL> requested = True , <EOL> requested_timestamp = <EOL> datetime . datetime . now ( ) ) <EOL> if passenger . location : <EOL> participation . requested_position_id = passenger . location_id <EOL> try : <EOL> participation . save ( ) <EOL> resp = models . Response ( models . Response . CREATED , <EOL> "<STR_LIT>" , participation ) <EOL> except django . db . IntegrityError , e : <EOL> resp = models . Response ( models . Response . BAD_REQUEST , <EOL> "<STR_LIT>" , e ) <EOL> return resp <EOL> def statusRide ( trip , passenger ) : <EOL> """<STR_LIT>""" <EOL> passenger_active_participation = passenger . get_active_participation ( ) <EOL> if passenger_active_participation : <EOL> resp = models . Response ( models . Response . FORBIDDEN , <EOL> "<STR_LIT>" , passenger_active_participation . trip ) <EOL> return resp <EOL> try : <EOL> passenger_participation = models . Participation . objects . get ( trip = trip . id , <EOL> author = passenger . id ) <EOL> except models . Participation . DoesNotExist : <EOL> resp = models . Response ( models . Response . NOT_FOUND , <EOL> "<STR_LIT>" , models . Response . PERSON_NOT_FOUND ) <EOL> if passenger_participation . accepted : <EOL> resp = models . Response ( models . Response . ALL_OK , <EOL> "<STR_LIT>" , trip . author ) <EOL> else : <EOL> resp = models . Response ( models . Response . NOT_FOUND , <EOL> "<STR_LIT>" , models . Response . RIDE_REQUEST_NOT_YET_ACCEPTED ) <EOL> return resp <EOL> def cancelRide ( trip , passenger ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> passenger_participation = models . Participation . objects . get ( trip = trip . id , <EOL> author = passenger . id ) <EOL> except models . Participation . DoesNotExist : <EOL> resp = models . Response ( models . Response . NOT_FOUND , <EOL> "<STR_LIT>" , models . Response . MUST_FIRST_REQUEST_RIDE ) <EOL> return resp <EOL> passenger_participation . requested_deleted = True <EOL> passenger_participation . requested_deleted_timestamp = datetime . datetime . now ( ) <EOL> if passenger . location : <EOL> passenger_participation . requested_deleted_position_id = passenger . location_id <EOL> try : <EOL> passenger_participation . save ( ) <EOL> resp = models . Response ( models . Response . DELETED , <EOL> "<STR_LIT>" , models . Response . PERSON_DELETED_REQUESTED_RIDE ) <EOL> except django . db . IntegrityError , e : <EOL> resp = models . Response ( models . Response . BAD_REQUEST , <EOL> "<STR_LIT>" , e ) <EOL> return resp <EOL> def startRide ( trip , passenger ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> is_already_participating = models . Participation . objects . filter ( trip = trip , author = passenger ) . exists ( ) <EOL> if not is_already_participating : <EOL> resp = models . Response ( models . Response . NOT_FOUND , <EOL> "<STR_LIT>" , models . Response . MUST_FIRST_REQUEST_RIDE ) <EOL> return resp <EOL> except ( KeyError , models . Participation . DoesNotExist ) : <EOL> resp = models . Response ( models . Response . NOT_FOUND , <EOL> "<STR_LIT>" , models . Response . TRIP_NOT_FOUND ) <EOL> return resp <EOL> try : <EOL> participation = trip . get_participations ( ) . only ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) . get ( author = passenger ) <EOL> participation . started = True <EOL> participation . started_timestamp = datetime . datetime . now ( ) <EOL> participation . started_position_id = passenger . location_id <EOL> participation . save ( ) <EOL> except Exception , e : <EOL> resp = models . Response ( models . Response . BAD_REQUEST , <EOL> "<STR_LIT>" , e ) <EOL> return resp <EOL> resp = models . Response ( models . Response . ALL_OK , <EOL> "<STR_LIT>" , participation ) <EOL> return resp <EOL> def finishRide ( trip , passenger ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> is_already_participating = models . Participation . objects . filter ( trip = trip , author = passenger ) . exists ( ) <EOL> if not is_already_participating : <EOL> resp = models . Response ( models . Response . NOT_FOUND , <EOL> models . Response . MUST_FIRST_REQUEST_RIDE , "<STR_LIT>" , False ) <EOL> return resp <EOL> except ( KeyError , models . Participation . DoesNotExist ) : <EOL> resp = models . Response ( models . Response . NOT_FOUND , <EOL> "<STR_LIT>" , models . Response . TRIP_NOT_FOUND ) <EOL> return resp <EOL> participation = trip . get_participations ( ) . only ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) . get ( author = passenger ) <EOL> participation . finished = True <EOL> participation . finished_timestamp = datetime . datetime . now ( ) <EOL> participation . finished_position_id = passenger . location_id <EOL> participation . save ( ) <EOL> resp = models . Response ( models . Response . ALL_OK , <EOL> "<STR_LIT>" , participation ) <EOL> return resp </s>
