Пример #1
0
	def get(self):
		profile = Profile.get_or_create()
		assert profile is not None
		
		agencies = Agency.query(Agency.enabled == True)
		if agencies.count(limit=1) < 1:
			agencies = None

		context = dict(
			object_list=agencies,
			report_count=StopReport.query(StopReport.user == profile.key).count(limit=100),
		)
		self.render_to_response('agency_list.html', context)
Пример #2
0
	def post(self):
		profile = Profile.get_or_create()
		assert profile is not None

		agency_id = self.request.get('agency_id')
		vehicle = self.request.get('vehicle')
		card_id = self.request.get('card_id')
		known_stop = self.request.get('known_stop')
		stop_name = self.request.get('stop_name')
		comment = self.request.get('comment')

		# Start validation!
		# Check agency
		try:
			agency = Agency.get_by_id(int(agency_id))
			if agency is None or not agency.enabled:
				# Invalid agency
				return self.send_404()
		except:
			return self.send_404()

		# Check vehicle type
		if vehicle not in VEHICLE_TYPES or vehicle not in agency.vehicle_types:
			return self.send_404()

		# Check card id
		try:
			card_id = int(card_id)
		except:
			# invalid card id, not a number.
			return self._retry_form(agency, vehicle, card_id, known_stop, stop_name, comment, 'Card ID is not a number')

		# Check known_stop
		if known_stop != '':
			try:
				known_stop = Stop.get_by_id(int(known_stop))
				if known_stop.agency.id() != agency.key.id():
					# stop is not for agency
					known_stop = None
			except:
				
				known_stop = None
		
		if known_stop is None:
			# Invalid stop
			return self._retry_form(agency, vehicle, card_id, known_stop, stop_name, comment, 'Stop ID is not valid')

		if known_stop == '':
			known_stop = None

		# Check stop_name is present if known_stop is not
		if known_stop is None and stop_name == '':
			# Custom name not specified and no known stop selected.
			return self._retry_form(agency, vehicle, card_id, known_stop, stop_name, comment, 'No stop name was entered')

		# If the user is banned, then say we processed the report, but don't
		# actually store it anywhere.
		if not profile.banned:
			# Now get the extra metadata and make the report
			country = self.request.headers.get('x-appengine-country', 'XX')
			region = self.request.headers.get('x-appengine-region', '')
			city = self.request.headers.get('x-appengine-city', '')
			ip = self.request.remote_addr
		
			report = StopReport(
				agency=agency.key,
				stop=known_stop.key if known_stop else None,
				name=stop_name,
				stop_type=vehicle,
				card_id=str(card_id),
				comment=comment,
				gae_country=country,
				gae_region=region,
				gae_city=city,
				gae_ip=ip,
				user=profile.key
			)
			report.put()
		
		context = dict(
			agency_name=agency.name,
			agency_id=agency.key.id(),
		)
		self.render_to_response('report_sent.html', context)