def __init__(self, fallback_string, user, date=None): self.user = user # the date of this route, so we know which identifiers to look for self.date = date if not fallback_string: #return empty route self.route = Route() self.route.save() return None route = Route(fallback_string=fallback_string, p2p=False) route.save() is_p2p, routebases = self.make_routebases_from_fallback_string(route) route.p2p = is_p2p for routebase in routebases: routebase.route = route routebase.save() route.easy_render() self.route = route
def save(entity): email = users.get_current_user().email() if email is not None: entity.created_by = email if entity.key is None: entity = Route.save(entity) else: current = Route.get(entity.key.urlsafe()) if current is not None: current.company_key = entity.company_key current.date = entity.date current.driver_key = entity.driver_key current.total_distance = entity.total_distance current.total_time = entity.total_time current.num_of_stops = entity.num_of_stops current.status = entity.status current.notes = entity.notes current.active = entity.active entity = Route.save(entity) else: raise ValueError("Route does not exists") return entity
def account(request, email): user = None if email: user = get_object_or_404(User, username=email) if request.method == 'POST': email = request.POST['email'] mobile_num = to_e164(request.POST['mobile_num']) password = request.POST['password'] if not user: if password == request.POST['confirm']: user = User.objects.create(username=email, email=email) user.set_password(password) user.save() UserProfile.objects.create(user=user).save() send_confirmation_text(user, mobile_num) user = auth.authenticate(username=user.username, password=password) auth.login(request, user) try: route_json = request.session['route_json'] except IndexError: pass else: r = Route(user=request.user, json=route_json, start_time=request.POST['start_time']) r.save() profile_data = {'mobile_num': mobile_num} UserProfile.objects.filter(user=user).update(**profile_data) return redirect('myroutes') profile = user.get_profile() if user else None return render(request, 'account.html', {'user': user, 'profile': profile})
def delete(id): entity = Route.get(id) if (entity is None): raise ValueError("Route does not exists") else: entity.active = False Route.save(entity)
def insert_route(route_id, route_short_name, route_long_name): """ insert a new route :param route_id: str, route id :param route_short_name: str, short name for route :param route_long_name: str, long name for route :return: """ d = Route(route_id=route_id, route_short_name=route_short_name, route_long_name=route_long_name) d.save()
def bind(self, uuid): """Bind ``uuid`` resource to a service. """ services = Service.objects.annotate( num_routes=Count('routes')).order_by('num_routes') if not services: raise NoServicesAvailable service = services[0] route = Route(uuid=uuid, service=service) route.save() return service
def route(request, id): if request.method == 'POST': if request.user.is_authenticated(): # store the route in the database for the user r = Route(user=request.user, json=request.POST['route_json']) r.save() return redirect('myroutes') else: # store the route in session # will store route in database for the user after # we create the user request.session['route_json'] = request.POST['route_json'] return redirect('account') else: return redirect('home')
def getRoutesAndSave(o, d): data = json.load(open(data_prefix+'/data/%s_%s.json' % (o, d))) for route_index, route in enumerate(data['routes']): gpolyline = route['overview_polyline']['points'] linestring = google_lines.decode_line(gpolyline) linestring.set_srid(canonical_projection) linestring_dist = linestring.clone() linestring_dist.transform(google_projection) route_object = Route(geom=linestring, geom_dist=linestring_dist, \ summary=route['summary'], origin_taz=taz_lookup[o], \ destination_taz=taz_lookup[d], \ travel_time=compute_route_time(route), \ od_route_index=route_index, \ json_contents=json.dumps(route)) route_object.save()
def create_route(): result = '' error = '' try: route = Route( request.json['routeId'], request.json['busId'], request.json['stopId'] ) route.save() print('Added route: ', route) result = 'Added route: {0}'.format(route.serialize()) except Exception as err: print('Unexpected error:', err) error = 'Error: {0}'.format(err) return jsonify({'result': result, 'error': error})
def save_route(code, sign, direction, main_to_sec, sec_to_main, type_route): try: route = Route.objects.get(code=code) route.sign = sign route.direction = direction route.main_to_sec = main_to_sec route.sec_to_main = sec_to_main route.type_route = type_route except Route.DoesNotExist: route = Route(code=code, sign=sign, direction=direction, main_to_sec=main_to_sec, sec_to_main=sec_to_main, type_route=type_route) route.save() return route
def clean_route(self): '''Translate number from autocomplete to object. If not number, just create a new route with the text given as name ''' data = self.cleaned_data['route'] try: data = Route.objects.get(pk=data) except ValueError: # not int, means name if data: # Check that string i set, if not, leave it to exercise.save() to create autoroute r = Route() r.name = data r.single_serving = True r.save() data = r else: return None return data
def get_stored_historic_score(origin,destination,startTime,endTime,sinceDate,beforeDate,alpha): score = None scores = None calculatedScore = -1 today = datetime.now() today = datetime(2015,05,07,15,00) # If the route already exists, get from database, else save new instance. route = Route.get_or_none(origin=origin,destination=destination) if route is None: route = Route(origin=origin,destination=destination) route.save() # If both extremes of the date range are not none, then the scores of # the tweets from that time range are retrived. if sinceDate is not None and beforeDate is not None: scores = HistoricScore.get_scores_between_dates(route_id=route.id, lower_timestamp=sinceDate,higher_timestamp=beforeDate) else: if sinceDate is None: scores = HistoricScore.get_scores_until_date(route_id=route.id,timestamp=beforeDate) if beforeDate is None: scores = HistoricScore.get_scores_from_date(route_id=route.id,timestamp=sinceDate) #If previous scores don't exist, then a score is calculated and saved to the database. if scores is None or not scores: # Verifies if a score was already calculated for the route with today's date. score = HistoricScore.get_or_none(route_id=route.id,timestamp=today) if score is None: calculatedScore = get_score(related_tweets_time(origin, destination, startTime, endTime, sinceDate, beforeDate)) score = HistoricScore(route_id=route.id,timestamp=today,score=calculatedScore) score.save() else: calculatedScore = score.score else: calculatedScore = exponential_smoothing(scores,alpha) return calculatedScore
def change_status(route_key, new_status): route = Route.get(route_key) if (route is None): raise ValueError("Route does not exists") if new_status == int(RouteStatus.InProgress): driver_key = route.driver_key routes = Route.get_all_in_progress_by_driver_key(driver_key) if len(routes) > 0: route_in_progress = routes[0] raise ValueError( "Driver already has a route in progress. Current route in pogress " + route.key.urlsafe()) if new_status == int(RouteStatus.Failed): route_incidents = RouteIncident.get_by_route(route.key) if len(route_incidents) > 0: for route_incident in route_incidents: route_incident.status = RouteIncidentStatus.Failed RouteIncident.save(route_incident) route.status = RouteStatus(int(new_status)) route = Route.save(route) return route
class MakeRoute(object): """ creates a route object from a string. The constructor takes a user instance because it needs to know which "namespace" to use for looking up custom places. """ def __init__(self, fallback_string, user, date=None): self.user = user # the date of this route, so we know which identifiers to look for self.date = date if not fallback_string: #return empty route self.route = Route() self.route.save() return None route = Route(fallback_string=fallback_string, p2p=False) route.save() is_p2p, routebases = self.make_routebases_from_fallback_string(route) route.p2p = is_p2p for routebase in routebases: routebase.route = route routebase.save() route.easy_render() self.route = route def get_route(self): return self.route ########################################################### ########################################################### def normalize(self, string): """ removes all cruf away from the route string, returns only the alpha numeric characters with clean seperators """ import re string = string.upper() string = string.replace("LOCAL", " ") string = string.replace(" TO ", " ") return re.sub(r'[^A-Z0-9!@]+', ' ', string).strip() ########################################################### ########################################################### def find_navaid(self, ident, i, last_rb=None): """ Searches the database for the navaid object according to ident. if it finds a match, creates and returns a routebase object """ if last_rb: navaid = Location.objects.filter(loc_class=2, identifier=ident) #if more than 1 navaids come up, if navaid.count() > 1: #run another query to find the nearest last_point = last_rb.location navaid = navaid.distance(last_point.location)\ .order_by('distance')[0] elif navaid.count() == 0: navaid = None else: navaid = navaid[0] else: # no previous routebases, # dont other with the extra queries trying to find the nearest # based on the last try: navaid = Location.objects.filter(loc_class=2, identifier=ident)[0] except IndexError: navaid = None if navaid: return RouteBase(location=navaid, sequence=i) else: # wasn't a navaid, maybe it was an airport that they flew over? return self.find_airport(ident, i) return None ########################################################################### def find_custom(self, ident, i, force=False): """ Tries to find the custom point, if it can't find one, and force = True, it adds it to the user's custom list. """ ident = ident[:8] if force: cu,cr = Location.objects.get_or_create(user=self.user, loc_class=3, identifier=ident) else: try: cu = Location.objects.filter(loc_class=3, user=self.user, identifier=ident)[0] except IndexError: cu = None if cu: return RouteBase(location=cu, sequence=i) else: return None ########################################################################### def find_airport(self, ident, i): """ i = sequence of the airport in the route ident = identifier, always uppercase Uses the search_airport method to find the airport, then returns it as a routebase """ def swap(ident): "Swaps zero's and o's" new = ident.replace('O', '&').replace('0', '$') return new.replace('&', '0').replace('$', 'O') if ident == '': return None numeric = re.search(r'[0-9]', ident) date = self.date retry = False airport = self.search_airport(ident, date) if not airport and len(ident) == 3 and not numeric: # if the ident is 3 letters and no hit, try again with an added 'K' retry = True ident = "K" + ident elif not airport and len(ident) == 4 and ident.startswith('K') and \ numeric: # if the ident is 4 letters and starts with a 'K it's # possible that the user has erroneously put it there, remove it retry = True ident = ident[1:] if not airport and retry: #try again with fixed identifier airport = self.search_airport(ident, date) if not airport and ('O' in ident or '0' in ident) and not ident.startswith('K'): ident = swap(ident) airport = self.search_airport(ident, date) if airport: return RouteBase(location=airport, sequence=i) def search_airport(self, ident, date): hi = HistoricalIdent.objects.filter(identifier=ident) ex = Location.goon(loc_class=1, identifier=ident) valid_hi = None invalid_hi = None airport_ident = None airport = None if hi.count() > 0: try: valid_hi = hi.get(start__lte=date, end__gte=date) invalid_hi = None except HistoricalIdent.DoesNotExist: valid_hi = None invalid_hi = hi.latest('end') elif ex: return ex ############## if invalid_hi and not valid_hi and not ex: #we dont have anything but an expired HI, just use it return invalid_hi.current_location elif invalid_hi and not valid_hi and ex: # an ex trumps an invalid HI return ex elif valid_hi: # we have a valid HI, use it no matter what! return valid_hi.current_location elif not valid_hi and not invalid_hi and not ex: #we have nothing :( return None else: assert False, "Some weird corner case" def make_routebases_from_fallback_string(self, route): """ Returns a list of RouteBase objects according to the fallback_string, basically hard_render() """ fbs = self.normalize(route.fallback_string) points = fbs.split() # 'MER / VGA - mer' -> ['MER', 'VGA', 'MER'] unknown = False p2p = [] routebases = [] for i, ident in enumerate(points): if "@" in ident: # "@" means we didn't land land = False else: land = True if "!" in ident: # "!" means it's a custom place custom = True else: custom = False #replace all the control characters now that we know their purpose ident = ident.replace('!','').replace('@','') if not land and not custom: # must be a navaid # is this the first routebase? if so don't try to guess which # navaid is closest to the previous point first_rb = len(routebases) == 0 if not first_rb and not routebases[i-1].unknown: routebase = self.find_navaid(ident, i, last_rb=routebases[i-1]) else: routebase = self.find_navaid(ident, i) elif custom: # force=True means if it can't find the 'custom', then make it routebase = self.find_custom(ident, i, force=True) else: #must be an airport routebase = self.find_airport(ident, i) if not routebase: # if the airport can't be found, see if theres a 'custom' # by the same identifier routebase = self.find_custom(ident, i, force=False) ####################################################################### # no routebase? must be unknown if not routebase: routebase = RouteBase(unknown=ident, sequence=i) routebase.land = land routebases.append(routebase) if land: loc = routebase.location or ident p2p.append(loc) return len(set(p2p)) > 1, routebases
class MakeRoute(object): """ creates a route object from a string. The constructor takes a user instance because it needs to know which "namespace" to use for looking up custom places. """ def __init__(self, fallback_string, user, date=None): self.user = user # the date of this route, so we know which identifiers to look for self.date = date if not fallback_string: #return empty route self.route = Route() self.route.save() return None route = Route(fallback_string=fallback_string, p2p=False) route.save() is_p2p, routebases = self.make_routebases_from_fallback_string(route) route.p2p = is_p2p for routebase in routebases: routebase.route = route routebase.save() route.easy_render() self.route = route def get_route(self): return self.route ########################################################### ########################################################### def normalize(self, string): """ removes all cruf away from the route string, returns only the alpha numeric characters with clean seperators """ import re string = string.upper() string = string.replace("LOCAL", " ") string = string.replace(" TO ", " ") return re.sub(r'[^A-Z0-9!@]+', ' ', string).strip() ########################################################### ########################################################### def find_navaid(self, ident, i, last_rb=None): """ Searches the database for the navaid object according to ident. if it finds a match, creates and returns a routebase object """ if last_rb: navaid = Location.objects.filter(loc_class=2, identifier=ident) #if more than 1 navaids come up, if navaid.count() > 1: #run another query to find the nearest last_point = last_rb.location navaid = navaid.distance(last_point.location)\ .order_by('distance')[0] elif navaid.count() == 0: navaid = None else: navaid = navaid[0] else: # no previous routebases, # dont other with the extra queries trying to find the nearest # based on the last try: navaid = Location.objects.filter(loc_class=2, identifier=ident)[0] except IndexError: navaid = None if navaid: return RouteBase(location=navaid, sequence=i) else: # wasn't a navaid, maybe it was an airport that they flew over? return self.find_airport(ident, i) return None ########################################################################### def find_custom(self, ident, i, force=False): """ Tries to find the custom point, if it can't find one, and force = True, it adds it to the user's custom list. """ ident = ident[:8] if force: cu, cr = Location.objects.get_or_create(user=self.user, loc_class=3, identifier=ident) else: try: cu = Location.objects.filter(loc_class=3, user=self.user, identifier=ident)[0] except IndexError: cu = None if cu: return RouteBase(location=cu, sequence=i) else: return None ########################################################################### def find_airport(self, ident, i): """ i = sequence of the airport in the route ident = identifier, always uppercase Uses the search_airport method to find the airport, then returns it as a routebase """ def swap(ident): "Swaps zero's and o's" new = ident.replace('O', '&').replace('0', '$') return new.replace('&', '0').replace('$', 'O') if ident == '': return None numeric = re.search(r'[0-9]', ident) date = self.date retry = False airport = self.search_airport(ident, date) if not airport and len(ident) == 3 and not numeric: # if the ident is 3 letters and no hit, try again with an added 'K' retry = True ident = "K" + ident elif not airport and len(ident) == 4 and ident.startswith('K') and \ numeric: # if the ident is 4 letters and starts with a 'K it's # possible that the user has erroneously put it there, remove it retry = True ident = ident[1:] if not airport and retry: #try again with fixed identifier airport = self.search_airport(ident, date) if not airport and ('O' in ident or '0' in ident) and not ident.startswith('K'): ident = swap(ident) airport = self.search_airport(ident, date) if airport: return RouteBase(location=airport, sequence=i) def search_airport(self, ident, date): hi = HistoricalIdent.objects.filter(identifier=ident) ex = Location.goon(loc_class=1, identifier=ident) valid_hi = None invalid_hi = None airport_ident = None airport = None if hi.count() > 0: try: valid_hi = hi.get(start__lte=date, end__gte=date) invalid_hi = None except HistoricalIdent.DoesNotExist: valid_hi = None invalid_hi = hi.latest('end') elif ex: return ex ############## if invalid_hi and not valid_hi and not ex: #we dont have anything but an expired HI, just use it return invalid_hi.current_location elif invalid_hi and not valid_hi and ex: # an ex trumps an invalid HI return ex elif valid_hi: # we have a valid HI, use it no matter what! return valid_hi.current_location elif not valid_hi and not invalid_hi and not ex: #we have nothing :( return None else: assert False, "Some weird corner case" def make_routebases_from_fallback_string(self, route): """ Returns a list of RouteBase objects according to the fallback_string, basically hard_render() """ fbs = self.normalize(route.fallback_string) points = fbs.split() # 'MER / VGA - mer' -> ['MER', 'VGA', 'MER'] unknown = False p2p = [] routebases = [] for i, ident in enumerate(points): if "@" in ident: # "@" means we didn't land land = False else: land = True if "!" in ident: # "!" means it's a custom place custom = True else: custom = False #replace all the control characters now that we know their purpose ident = ident.replace('!', '').replace('@', '') if not land and not custom: # must be a navaid # is this the first routebase? if so don't try to guess which # navaid is closest to the previous point first_rb = len(routebases) == 0 if not first_rb and not routebases[i - 1].unknown: routebase = self.find_navaid(ident, i, last_rb=routebases[i - 1]) else: routebase = self.find_navaid(ident, i) elif custom: # force=True means if it can't find the 'custom', then make it routebase = self.find_custom(ident, i, force=True) else: #must be an airport routebase = self.find_airport(ident, i) if not routebase: # if the airport can't be found, see if theres a 'custom' # by the same identifier routebase = self.find_custom(ident, i, force=False) ####################################################################### # no routebase? must be unknown if not routebase: routebase = RouteBase(unknown=ident, sequence=i) routebase.land = land routebases.append(routebase) if land: loc = routebase.location or ident p2p.append(loc) return len(set(p2p)) > 1, routebases
def addRoute(name): route = Route(name=name) route.save() return route.id
def tbas(request): if request.method == 'GET': data = serializers.serialize("json", Tba.objects.all()) return HttpResponse(data, content_type='application/json') if request.method == 'POST': req = json.loads(request.body) print req for item in req: tba = item['tba'] link = item['link'] status = item['status'] city = item['city'] zipCode = item['zipCode'] route = item['route'] associate = item['associate'] sortZone = item['sortZone'] address = item['address'] if route != "" or route != None: try: Route.objects.get(route=route) except ObjectDoesNotExist: cluster = filter(lambda x: x.isalpha(), route) print cluster print route aRoute = Route( route=route, cluster = cluster, tbaCount = 0 ) aRoute.save() try: Tba.objects.get(tba=tba) except ObjectDoesNotExist: routeToUpdate = Route.objects.get(route=route) tba = Tba( tba=tba, link=link, status=status, city=city, route=routeToUpdate, zipCode=zipCode, sortZone=sortZone, address=address ) tba.save() else: tba = Tba( tba=tba, link=link, status=status, city=city, zipCode=zipCode, sortZone=sortZone, address=address ) tba.save() return HttpResponse("Kevin is the greatest of all time --tbas")