Пример #1
0
	def post(self, agency_id):
		user = users.get_current_user()
		if not user or not users.is_current_user_admin():
			return

		try:
			agency = Agency.get_by_id(int(agency_id))
			if agency is None:
				return self.send_404()
		except:
			return self.send_404()

		vehicle_types = [x.strip().lower() for x in self.request.get('vehicle_types').split(',')]
		vehicle_types = [x for x in vehicle_types if x in VEHICLE_TYPES.keys()]
		agency.vehicle_types = vehicle_types
		agency.put()

		context = dict(
			agency_name=agency.name,
			agency=agency,
			vehicle_types=VEHICLE_TYPES.keys(),
		)
		
		self.render_to_response('agency_edit.html', context)
Пример #2
0
	def get(self, agency_id):
		user = users.get_current_user()
		if not user or not users.is_current_user_admin():
			return

		try:
			agency = Agency.get_by_id(int(agency_id))
			if agency is None:
				return self.send_404()
		except:
			return self.send_404()

		context = dict(
			agency_name=agency.name,
			vehicle_types=VEHICLE_TYPES.keys(),
		)
		
		self.render_to_response('agency_stops_import.html', context)
Пример #3
0
	def post(self, agency_id):
		user = users.get_current_user()
		if not user or not users.is_current_user_admin():
			return

		try:
			agency = Agency.get_by_id(int(agency_id))
			if agency is None:
				return self.send_404()
		except:
			return self.send_404()

		error_msg = None
		ok_msg = None

		# Process the upload
		upload = self.request.POST['upload']
		if upload.file:
			upload_csv = csv.reader(upload.file)
			header = upload_csv.next()
			
			name_f = header.index('name')
			stop_type_f = header.index('stop_type')
			try:
				stop_id_f = header.index('stop_id')
			except:
				stop_id_f = None
			try:
				stop_code_f = header.index('stop_code')
			except:
				stop_code_f = None
			try:
				y_f = header.index('y')
				x_f = header.index('x')
			except:
				y_f = x_f = None
			
			# Start reading rows
			stop_count = 0
			for row in upload_csv:
				name = row[name_f].strip()
				stop_type = row[stop_type_f].strip()
				if stop_type not in VEHICLE_TYPES.keys():
					error_msg = 'stop type invalid (%r)' % (stop_type,)
					break
				try:
					stop_code = row[stop_code_f].strip()
				except:
					stop_code = None
				try:
					stop_id = row[stop_id_f].strip()
				except:
					stop_id = None
				try:
					x = float(row[x_f].strip())
					y = float(row[y_f].strip())
				except:
					x_f = y_f = None
				if x_f is not None:
					point = ndb.GeoPt(y, x)
				else:
					point = None
				
				stop = Stop(
					name=name,
					stop_type=stop_type,
					agency=agency.key,
					gtfs_stop_id=stop_id,
					gtfs_stop_code=stop_code,
					gtfs_point=point,
				)
				stop.put()
				stop_count += 1
			ok_msg = 'imported %d stop(s)' % stop_count
		else:
			error_msg = 'cannot open uploaded file'

		context = dict(
			agency_name=agency.name,
			vehicle_types=VEHICLE_TYPES.keys(),
			error_msg=error_msg,
			ok_msg=ok_msg,
		)
		
		self.render_to_response('agency_stops_import.html', context)