def post(self): # -------------------------------------------------------------------- # Retrive Session Info and GET Data # -------------------------------------------------------------------- # Session Values current_user = RoadMateUser.get_current_user() if current_user is None: self.redirect(users.create_login_url(self.request.url)) return # Request Values ride_id = self.get_request_parameter('id', converter=int, default=None) # Datastore Values ride = Ride.get_by_id(ride_id) # -------------------------------------------------------------------- # Validate Request # -------------------------------------------------------------------- # if the target ride does not exist in the datastore, then redirect # the user back to the home page. if ride is None: self.error(404) return # -------------------------------------------------------------------- # Generate and Store Template Values # -------------------------------------------------------------------- template_values = super(ViewRidePageHandler, self ).generate_template_values(self.request.url) template_values['ride'] = ride template_values['lat_lng_src'] = ride.source.get_lat_loc() template_values['lat_lng_des'] = ride.destination.get_lat_loc() template_values['googlemaps_key'] = GoogleMaps.get_key() template_values['google_calendar_key'] = GoogleCalendar.get_key() template_values['has_passengers'] = (ride.count_seats() - ride.count_emptyseats()) > 0 # has some passengers template_values['message_list'] = list(ride.ridemessages.order('created')) # the list of comments template_values['has_occurred'] = (ride.date < ride.date.today()) # is in the past template_values['is_full'] = (ride.count_emptyseats() == 0) # no empty seats template_values['enable_feedback_on_driver'] = template_values['has_occurred'] & ride.is_passenger(current_user) # ride is in the past and current user has been passenger template_values['enable_feedback_on_passengers'] = template_values['has_occurred'] & (current_user == ride.owner) # ride is in the past and current user was owner template_values['enable_edit_controls'] = (not template_values['has_occurred']) & (current_user == ride.owner) # ride is in the future and current user is owner template_values['enable_passenger_withdraw'] = (not template_values['has_occurred']) & ride.is_passenger(current_user) # ride is in the future and current user is passenger # -------------------------------------------------------------------- # Control the display of the form element # and handle the new request # -------------------------------------------------------------------- # if driver is cancelling their ride if current_user == ride.owner and self.request.POST.has_key('do_cancel_ride'): for seat in ride.seats: if seat.passenger: # notify the passengers mail.send_mail(sender="*****@*****.**", to=seat.passenger.user.email(), subject="RoadMate - Ride Cancelled", body=generate_cancelledride_email_body(ride)) ride.delete() self.redirect("/") # redirect to the main page return # if passenger is withdrawing if ride.is_passenger(current_user) and self.request.POST.has_key('do_withdraw'): passenger_seat = ride.seats.filter('passenger = ', user).get() # find the passenger's seat self.redirect("/ride?id=%s" % ride.key().id()) # redirect back to the view page # notify the driver mail.send_mail(sender="*****@*****.**", to=ride.owner.user.email(), subject="RoadMate - Passenger has withdrawn", body=generate_withdrawpassenger_email_body(ride)) #disassociate the seat from the user passenger_seat.passenger = None passenger_seat.accepted = None passenger_seat.save() return #if user has already placed a request on this ride ~~appears in get and post if ride.passengerrequests.filter('owner = ', current_user).get(): template_values['requestable'] = False #turn off the request button else: template_values['requestable'] = True #turn on the request button #if user is placing a passenger request if self.request.POST.has_key('do_request_ride') and self.request.POST['do_request_ride']: prq = PassengerRequest(owner=current_user, ride=ride) #create a new passenger request prq.put() template_values['requestable'] = False #turn off the request button #if user is posting a message #TODO make this more secure! clean the title and body text and validate max/min length! if self.request.POST.has_key('do_post_message') and self.request.POST['do_post_message']: message = RideMessage(author=current_user, ride=ride, title=self.request.POST['message_title'], text=self.request.POST['message_body']) message.put() # -------------------------------------------------------------------- # Render and Serve Template # -------------------------------------------------------------------- page_path = os.path.join(os.path.dirname(__file__), "ride_view.html") self.response.out.write(template.render(page_path, template_values))
def post(self): # -------------------------------------------------------------------- # Retrive Session Info and Request Data # -------------------------------------------------------------------- # Session Values current_user = RoadMateUser.get_current_user() # Request Values ride_id = self.get_request_parameter('ride', converter=int, default=None) target_user_id = self.get_request_parameter('on', converter=int, default=None) # Retrieve the instances ride = Ride.get_by_id(ride_id) target_user = RoadMateUser.get_by_id(target_user_id) feedback_form = FeedbackForm() target_user_role = '' # empty by default # -------------------------------------------------------------------- # Validate Session and Request # -------------------------------------------------------------------- # if the current users in not logged in, then we redirect them through # a login page. if current_user is None: self.redirect(users.create_login_url(self.request.url)) return # if the target user is not valid then error if target_user is None or ride is None: self.error(403) # forbidden return #if the current user has already placed feedback on the recipient for this ride if ride.feedbackmessages.filter('author =', current_user).filter('recipient =', target_user).count() >= 1: self.redirect("/feedback?id=%s" % target_user.key().id()) # redirect to the recipient's feedback page return # if the target user is not the driver, and the current user is not a passenger # or vice versa, then error if ride.is_passenger(current_user) and (target_user == ride.owner): target_user_role = 'passenger' elif ride.is_passenger(target_user) and (current_user == ride.owner): target_user_role = 'driver' else: # then the relationship is not valid self.error(403) # forbidden return ## not working ## # -------------------------------------------------------------------- ## # Retrive POST Data ## # and create a new instance of the form to validate the data ## # -------------------------------------------------------------------- ## feedback_data = { 'author': current_user, 'ride':ride, 'recipient':target_user, 'role': target_user_role} ## ## feedback_form = FeedbackForm( ## data=self.request.POST, ## initial=feedback_data ## ) # create the form to validate ## print(feedback_form['ride'].data) ## ## # -------------------------------------------------------------------- ## # Validate POST Data ## # -------------------------------------------------------------------- ## # if there are errors in the form, then re-serve the page with the ## # error values highlighted. ## if not feedback_form.is_valid(): ## # ---------------------------------------------------------------- ## # Generate Template Values ## # ---------------------------------------------------------------- ## template_values = BaseRequestHandler.generate_template_values(self, ## self.request.url) ## ## # because this page requires the user to be logged in, if they ## # logout we redirect them back to the home page. ## template_values['logout_url'] = users.create_logout_url("/") ## ## # -------------------------------------------------------------------- ## # Set up the page 'passenger' or 'driver' display ## # -------------------------------------------------------------------- ## target_user_role = 'driver' # by default ## if ride.is_passenger(target_user): ## target_user_role = 'passenger' ## ## # -------------------------------------------------------------------- ## # Generate Template Values ## # -------------------------------------------------------------------- ## template_values = super(CreateFeedbackPageHandler, self ## ).generate_template_values(self.request.url) ## ## # because this page requires the user to be logged in, if they logout ## # we redirect them back to the home page. ## template_values['logout_url'] = users.create_logout_url("/") ## template_values['owner'] = current_user ## template_values['target_user'] = target_user ## template_values['ride'] = ride ## template_values['feedback_form'] = feedback_form ## template_values['target_user_role'] = target_user_role ## ## # ---------------------------------------------------------------- ## # Render and Serve Template ## # ---------------------------------------------------------------- ## print(feedback_form.errors) ## page_path = os.path.join(os.path.dirname(__file__), "feedback_create.html") ## self.response.out.write(template.render(page_path, template_values)) ## return feedback_message = FeedbackMessage( ride=ride, author=current_user, recipient=target_user, role=target_user_role, value=int(self.request.POST['value']), text=escape(self.request.POST['text']) ) # not validated feedback_message.put() # save the new Message self.redirect("/feedback?id=%s" % target_user.key().id()) # redirect to the recipient's profile page or feedback page
def get(self): # -------------------------------------------------------------------- # Retrive Session Info and GET Data # -------------------------------------------------------------------- # Session Values current_user = RoadMateUser.get_current_user() # Request Values ride_id = self.get_request_parameter('id', converter=int, default=-1) # ride prq_id = self.get_request_parameter('prq_id', converter=int, default=None) # passenger request (accept) action = self.get_request_parameter('action', converter=str, default=None) seat_id = self.get_request_parameter('seat_id', converter=int, default=None) # seat (remove passenger) # Datastore Values ride = Ride.get_by_id(ride_id) # -------------------------------------------------------------------- # Validate Request # -------------------------------------------------------------------- # if the target ride does not exist in the datastore, then redirect # the user back to the home page. if ride is None: self.error(404) return # -------------------------------------------------------------------- # Handle approving and removing passengers and seats # -------------------------------------------------------------------- if current_user == ride.owner: # Approve a passenger request if prq_id and action == 'APRV': prq = PassengerRequest.get_by_id(prq_id) #only proceed if there is a valid passengerrequest if prq and (ride.count_emptyseats() > 0): empty_seat = ride.seats.filter('passenger = ', None).get() #retrieve the empty seats on this ride empty_seat.assign(prq) #assign the seat: this method of Seat handles setting the "assigned time" prq.delete() #delete the passenger request # notify the approved passenger mail.send_mail(sender="*****@*****.**", to=prq.owner.user.email(), subject="RoadMate - Your passenger request has been approved", body=generate_approvepassenger_email_body(ride)) # Remove a passenger from the seat if seat_id and action == 'RM': seat = Seat.get_by_id(seat_id) #only proceed if there is a valid seat if seat: # notify the removed passenger mail.send_mail(sender="*****@*****.**", to=seat.passenger.user.email(), subject="RoadMate - Removed from ride", body=generate_removedpassenger_email_body(ride)) #disassociate the seat from the user seat.passenger = None seat.accepted = None seat.save() # -------------------------------------------------------------------- # Generate and Store Template Values # -------------------------------------------------------------------- template_values = super(ViewRidePageHandler, self ).generate_template_values(self.request.url) template_values['ride'] = ride template_values['lat_lng_src'] = ride.source.get_lat_loc() template_values['lat_lng_des'] = ride.destination.get_lat_loc() template_values['googlemaps_key'] = GoogleMaps.get_key() template_values['google_calendar_key'] = GoogleCalendar.get_key() template_values['has_passengers'] = (ride.count_seats() - ride.count_emptyseats()) > 0 # has some passengers template_values['message_list'] = list(ride.ridemessages.order('created')) # the list of comments template_values['has_occurred'] = (ride.date < ride.date.today()) # is in the past template_values['is_full'] = (ride.count_emptyseats() == 0) # no empty seats template_values['enable_feedback_on_driver'] = template_values['has_occurred'] & ride.is_passenger(current_user) # ride is in the past and current user has been passenger template_values['enable_feedback_on_passengers'] = template_values['has_occurred'] & (current_user == ride.owner) # ride is in the past and current user was owner template_values['enable_edit_controls'] = (not template_values['has_occurred']) & (current_user == ride.owner) # ride is in the future and current user is owner template_values['enable_passenger_withdraw'] = (not template_values['has_occurred']) & ride.is_passenger(current_user) # ride is in the future and current user is passenger # -------------------------------------------------------------------- # Control the display of the form element # -------------------------------------------------------------------- #if user has already placed a request on this ride ~~appears in get and post if ride.passengerrequests.filter('owner = ', current_user).get(): template_values['requestable'] = False #turn off the request button else: template_values['requestable'] = True #turn on the request button # -------------------------------------------------------------------- # Render and Serve Template # -------------------------------------------------------------------- page_path = os.path.join(os.path.dirname(__file__), "ride_view.html") self.response.out.write(template.render(page_path, template_values))
def get(self): # -------------------------------------------------------------------- # Retrive Session Info and Request Data # -------------------------------------------------------------------- # Session Values current_user = RoadMateUser.get_current_user() # Request Values ride_id = self.get_request_parameter('ride', converter=int, default=None) target_user_id = self.get_request_parameter('on', converter=int, default=None) # Retrieve the instances ride = Ride.get_by_id(ride_id) target_user = RoadMateUser.get_by_id(target_user_id) feedback_form = FeedbackForm() target_user_role = '' # empty by default # -------------------------------------------------------------------- # Validate Session and Request # -------------------------------------------------------------------- # if the current users in not logged in, then we redirect them through # a login page. if current_user is None: self.redirect(users.create_login_url(self.request.url)) return # if the target user is not valid then error if target_user is None or ride is None: self.error(403) # forbidden return # if the target user is not the driver, and the current user is not a passenger # or vice versa, then error if ride.is_passenger(current_user) and (target_user == ride.owner): target_user_role = 'driver' elif ride.is_passenger(target_user) and (current_user == ride.owner): target_user_role = 'passenger' else: # then the relationship is not valid self.error(403) # forbidden return # -------------------------------------------------------------------- # Generate Template Values # -------------------------------------------------------------------- template_values = super(CreateFeedbackPageHandler, self ).generate_template_values(self.request.url) # because this page requires the user to be logged in, if they logout # we redirect them back to the home page. template_values['logout_url'] = users.create_logout_url("/") template_values['owner'] = current_user template_values['target_user'] = target_user template_values['ride'] = ride template_values['feedback_form'] = feedback_form template_values['target_user_role'] = target_user_role # -------------------------------------------------------------------- # Render and Serve Template # -------------------------------------------------------------------- page_path = os.path.join(os.path.dirname(__file__), "feedback_create.html") self.response.out.write(template.render(page_path, template_values))