Example #1
0
	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))
Example #2
0
	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))