<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import datetime <EOL> import models <EOL> import utils <EOL> import django . core . exceptions <EOL> def insertTrip ( trip , author , source , destination , modality , preferences ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> source . full_clean ( ) <EOL> destination . full_clean ( ) <EOL> modality . full_clean ( ) <EOL> preferences . full_clean ( ) <EOL> source . save ( ) <EOL> destination . save ( ) <EOL> modality . save ( ) <EOL> preferences . save ( ) <EOL> except django . core . exceptions . ValidationError , e : <EOL> resp = models . Response ( models . Response . BAD_REQUEST , <EOL> "<STR_LIT>" , e ) <EOL> return resp <EOL> except Exception , e : <EOL> resp = models . Response ( models . Response . BAD_REQUEST , <EOL> "<STR_LIT>" , e ) <EOL> return resp <EOL> trip . author = author <EOL> trip . modality = modality <EOL> trip . preferences = preferences <EOL> try : <EOL> trip . full_clean ( ) <EOL> trip . save ( ) <EOL> except django . core . exceptions . ValidationError , e : <EOL> resp = models . Response ( models . Response . BAD_REQUEST , <EOL> "<STR_LIT>" , e ) <EOL> return resp <EOL> except Exception , e : <EOL> resp = models . Response ( models . Response . BAD_REQUEST , <EOL> "<STR_LIT>" , e ) <EOL> return resp <EOL> trip . locations . add ( source ) <EOL> trip . locations . add ( destination ) <EOL> participation = models . Participation ( author = author , trip = trip , <EOL> role = '<STR_LIT>' ) <EOL> participation . save ( ) <EOL> resp = models . Response ( models . Response . CREATED , <EOL> "<STR_LIT>" , <EOL> trip ) <EOL> return resp <EOL> def startTrip ( trip , driver ) : <EOL> """<STR_LIT>""" <EOL> participation = models . Participation . objects . get ( trip = trip . id , role = '<STR_LIT>' ) <EOL> if participation . started : <EOL> resp = models . Response ( models . Response . DUPLICATE_ENTRY , <EOL> "<STR_LIT>" , models . Response . TRIP_ALREADY_STARTED ) <EOL> return resp <EOL> if trip . author != driver : <EOL> resp = models . Response ( models . Response . FORBIDDEN , <EOL> "<STR_LIT>" , models . Response . PERSON_NOT_AUTHORIZED ) <EOL> return resp <EOL> participation . started = True <EOL> participation . started_timestamp = datetime . datetime . now ( ) <EOL> try : <EOL> participation . started_position_id = driver . location_id <EOL> except models . Location . DoesNotExist : <EOL> participation . started_position = None <EOL> participation . save ( ) <EOL> trip . active = True <EOL> trip . save ( ) <EOL> resp = models . Response ( models . Response . ALL_OK , <EOL> "<STR_LIT>" , trip ) <EOL> return resp <EOL> def getRides ( trip , driver ) : <EOL> """<STR_LIT>""" <EOL> if trip . author != driver : <EOL> resp = models . Response ( models . Response . FORBIDDEN , <EOL> "<STR_LIT>" , models . Response . PERSON_NOT_AUTHORIZED ) <EOL> return resp <EOL> participations_for_trip = ( models . Participation . objects . filter ( trip = trip . id ) <EOL> . exclude ( author = driver ) <EOL> . filter ( started = False ) <EOL> . filter ( finished = False ) <EOL> . filter ( requested = True ) <EOL> . filter ( requested_deleted = False ) <EOL> ) . only ( "<STR_LIT>" ) <EOL> if not len ( participations_for_trip ) : <EOL> resp = models . Response ( models . Response . NOT_FOUND , <EOL> "<STR_LIT>" , models . Response . RIDE_REQUESTS_NOT_FOUND ) <EOL> return resp <EOL> else : <EOL> participations = [ participation . author <EOL> for participation in participations_for_trip ] <EOL> resp = models . Response ( models . Response . ALL_OK , <EOL> "<STR_LIT>" , participations ) <EOL> return resp <EOL> def acceptRide ( trip , driver , 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 . PERSON_NOT_FOUND ) <EOL> return resp <EOL> if passenger_participation . requested and not passenger_participation . accepted : <EOL> passenger_participation . accepted = True <EOL> passenger_participation . accepted_timestamp = datetime . datetime . now ( ) <EOL> try : <EOL> passenger_participation . accepted_position_id = passenger . location_id <EOL> except models . Location . DoesNotExist : <EOL> passenger_participation . accepted_position = None <EOL> passenger_participation . save ( ) <EOL> resp = models . Response ( models . Response . ALL_OK , <EOL> "<STR_LIT>" , passenger_participation ) <EOL> return resp <EOL> resp = models . Response ( models . Response . DUPLICATE_ENTRY , <EOL> "<STR_LIT>" , models . Response . RIDE_REQUEST_REFUSED ) <EOL> return resp <EOL> def refuseRide ( 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 . PERSON_NOT_FOUND ) <EOL> return resp <EOL> passenger_participation . refused = True <EOL> passenger_participation . refused_timestamp = datetime . datetime . now ( ) <EOL> try : <EOL> passenger_participation . refused_position_id = passenger . location_id <EOL> except models . Location . DoesNotExist : <EOL> passenger_participation . accepted_position = None <EOL> passenger_participation . save ( ) <EOL> resp = models . Response ( models . Response . DELETED , <EOL> "<STR_LIT>" , models . Response . RIDE_REQUEST_REFUSED ) <EOL> return resp <EOL> def finishTrip ( trip , driver ) : <EOL> """<STR_LIT>""" <EOL> if driver . is_participating ( ) : <EOL> participation = driver . get_active_participation ( ) <EOL> participation . finished = True <EOL> participation . finished_timestamp = utils . now ( ) <EOL> participation . save ( ) <EOL> trip . active = False <EOL> trip . save ( ) <EOL> resp = models . Response ( models . Response . DELETED , <EOL> "<STR_LIT>" , models . Response . TRIP_DELETED ) <EOL> return resp </s>
