Exemplo n.º 1
0
def addStop(r, bmark, re=None, agency=None, route=None, 
                                                    direction=None, stop=None):
    error = ''
    if (stop is not None):
        url = nextbus.timeURL % (agency, route, direction, stop)
        q = db.Query(Bmark)
        q.filter('name = ', bmark)
        b = q.get()
        q = db.Query(Stop)
        q.filter('bmark = ', b)
        q.filter('url = ', url)
        stopObj = q.get()
        logging.info(stopObj)
        if (stopObj):
            logging.info("Stop Already Exists")
            error = "This stop has already been added"
            #it'll fall through to picking a stop again
        else:
            stopObj = Stop()
            stopObj.bmark = b
            stopObj.url = url
            stopObj.system = "nextbus"
            stopObj.put()
            #TODO: redirect to an edit page that doesnt involve the scrape
            return HttpResponseRedirect('/catch/%s' % (bmark))
    if (direction is not None):
        data = nextbus.getStops(agency, route, direction)
        prefix = '/nb/addstop/%s/%s/%s/%s/%s'\
            % (bmark, re, agency, route, direction)
        instructions = 'pick a stop'
    elif (route is not None):
        data = nextbus.getDirections(agency, route)
        prefix = '/nb/addstop/%s/%s/%s/%s' % (bmark, re, agency, route)
        instructions = 'pick a direction'
    elif (agency is not None):
        data = nextbus.getRoutes(agency)
        prefix = '/nb/addstop/%s/%s/%s' % (bmark, re, agency)
        instructions = 'pick a route'
    items = []
    if not (data):
        #todo: throw 500
        return False
    for key, value in data:
        items.append({'url_piece': key, 'title': value})
    t = loader.get_template('listoflinks.html')
    c = Context({ 'items':items, 'prefix': prefix })
    items_t = t.render(c)
    params = { 'prefix': prefix,
               'instructions': instructions,
               'list': items_t,
               'error': error }
    return render_with_user('user/addstop.html', params)
Exemplo n.º 2
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)
Exemplo n.º 3
0
    def post(self):
        if self.request.get('edit-route-entity-key'):
            route_key = ndb.Key(
                urlsafe=str(self.request.get('edit-route-entity-key')))
            new_route = route_key.get()
            routeStops = Stop.query(ancestor=route_key).order(
                Stop.order_number).fetch()

            dictionary = {}
            Stop1 = self.request.get('stop1')
            if self.request.get('stop1-checkbox') == 'on':
                Checkbox1 = True
            else:
                Checkbox1 = False
            dictionary["Stop1"] = Stop1
            dictionary["Checkbox-Stop1"] = Checkbox1
            lastStop = self.request.get('stop2')
            Stop2 = lastStop
            if self.request.get('stop2-checkbox') == 'on':
                Checkbox2 = True
            else:
                Checkbox2 = False
            dictionary["Stop2"] = Stop2
            dictionary["Checkbox-Stop2"] = Checkbox2
            Stop3 = ""
            Stop4 = ""
            Stop5 = ""

            if self.request.get('stop3'):
                lastStop = self.request.get('stop3')
                Stop3 = lastStop
                if self.request.get('stop3-checkbox') == 'on':
                    Checkbox3 = True
                else:
                    Checkbox3 = False
                dictionary["Stop3"] = Stop3
                dictionary["Checkbox-Stop3"] = Checkbox3

            if self.request.get('stop4'):
                lastStop = self.request.get('stop4')
                Stop4 = lastStop
                if self.request.get('stop4-checkbox') == 'on':
                    Checkbox4 = True
                else:
                    Checkbox4 = False
                dictionary["Stop4"] = Stop4
                dictionary["Checkbox-Stop4"] = Checkbox4

            if self.request.get('stop5'):
                lastStop = self.request.get('stop5')
                Stop5 = lastStop
                if self.request.get('stop5-checkbox') == 'on':
                    Checkbox5 = True
                else:
                    Checkbox5 = False
                dictionary["Stop5"] = Stop5
                dictionary["Checkbox-Stop5"] = Checkbox5

            new_route.name = Stop1 + " to " + lastStop

            for i in range(1, (len(dictionary.keys()) / 2) + 1):
                if i < len(routeStops) + 1:
                    routeStops[i - 1].ordered = dictionary["Checkbox-Stop" +
                                                           str(i)]
                    routeStops[i - 1].stop_name = dictionary["Stop" + str(i)]
                    routeStops[i - 1].put()
                else:
                    new_stop = Stop(parent=new_route.key,
                                    route_key=new_route.key,
                                    order_number=i,
                                    stop_name=dictionary["Stop" + str(i)],
                                    ordered=dictionary["Checkbox-Stop" +
                                                       str(i)])
                    new_stop.put()

            new_route.put()
        else:
            firstStop = self.request.get('stop1')
            if self.request.get('stop1-checkbox') == 'on':
                firstCheckbox = True
            else:
                firstCheckbox = False
            lastStop = self.request.get('stop2')
            secondStop = lastStop
            if self.request.get('stop2-checkbox') == 'on':
                secondCheckbox = True
            else:
                secondCheckbox = False
            thirdStop = ""
            fourthStop = ""
            fifthStop = ""

            if self.request.get('stop3'):
                lastStop = self.request.get('stop3')
                thirdStop = lastStop
                if self.request.get('stop3-checkbox') == 'on':
                    thirdCheckbox = True
                else:
                    thirdCheckbox = False
            if self.request.get('stop4'):
                lastStop = self.request.get('stop4')
                fourthStop = lastStop
                if self.request.get('stop4-checkbox') == 'on':
                    fourthCheckbox = True
                else:
                    fourthCheckbox = False

            if self.request.get('stop5'):
                lastStop = self.request.get('stop5')
                fifthStop = lastStop
                if self.request.get('stop5-checkbox') == 'on':
                    fifthCheckbox = True
                else:
                    fifthCheckbox = False

            user = users.get_current_user()
            email = user.email().lower()
            # TODO: Name will change when modal has dynamic number of routes
            # NOTE: Created routes start with type = 0 (not saved)
            # NOTE: Created routes start with daily = 0 (non-recurring)
            new_route = Route(parent=utils.get_parent_key_for_email(email),
                              created_by=email,
                              name=firstStop + " to " + lastStop,
                              type=0,
                              daily=0,
                              start_time=datetime.datetime.now())
            new_route.put()

            new_stop1 = Stop(parent=new_route.key,
                             route_key=new_route.key,
                             order_number=1,
                             stop_name=self.request.get('stop1'),
                             ordered=firstCheckbox)
            new_stop1.put()
            new_stop2 = Stop(parent=new_route.key,
                             route_key=new_route.key,
                             order_number=2,
                             stop_name=self.request.get('stop2'),
                             ordered=secondCheckbox)
            new_stop2.put()
            if thirdStop != "":
                new_stop3 = Stop(parent=new_route.key,
                                 route_key=new_route.key,
                                 order_number=3,
                                 stop_name=self.request.get('stop3'),
                                 ordered=thirdCheckbox)
                new_stop3.put()
            if fourthStop != "":
                new_stop4 = Stop(parent=new_route.key,
                                 route_key=new_route.key,
                                 order_number=4,
                                 stop_name=self.request.get('stop4'),
                                 ordered=fourthCheckbox)
                new_stop4.put()
            if fifthStop != "":
                new_stop5 = Stop(parent=new_route.key,
                                 route_key=new_route.key,
                                 order_number=5,
                                 stop_name=self.request.get('stop5'),
                                 ordered=fifthCheckbox)
                new_stop5.put()

        self.redirect('/'.join(self.request.referer.split("/")[:3]) +
                      "?route=" + str(new_route.key.urlsafe()))