def get(self): #get specific trip trip_key = self.request.get('trip_id') tripk = ndb.Key(urlsafe=trip_key) trip = tripk.get() #get tracks for trip track_query = Track.query(ancestor=tripk) tracks = track_query.fetch() for track in tracks: #delete .gpx files from blobstore bolob = track.blob_key blobstore.delete(bolob) #delete statistic for item in TrackStatistic.query(ancestor=track.key): item.key.delete() #delete track track.key.delete() #redirect to mytrips because showing all tips will only be consistent in scope of user # and only eventually consistent for whole datastore trip.key.delete() self.redirect('/tripmanager')
def make_track_tree(id,location,type,season): ''' Create tree (http://www.ztree.me/v3/main.php#_zTreeInfo) ''' #get all trips trips_query = Trip.query(ancestor=trip_key(id)) if location != []: trips_query = trips_query.filter(Trip.trip_tags.location.IN(location)) if type != []: trips_query = trips_query.filter(Trip.trip_tags.type.IN(type)) if season != []: trips_query = trips_query.filter(Trip.trip_tags.season.IN(season)) trips = trips_query.fetch() #create tree structure from trips tree = [] i = 1 of = 0 tree.append({'id':0, 'pId':0, 'name':'My Trips','isParent': 'true','open':'true'}) for trip in trips: tree.append({'id':i, 'pId':0, 'name':str(trip.trip_name),'isParent': 'true', 'click':"openTrip('"+ str(trip.key.urlsafe()) +"')"}) #get tracks from trip track_query = Track.query(ancestor=trip.key).order(-Track.creation_date) tracks = track_query.fetch(20) for track in tracks: tree.append({'id':i+10+of, 'pId':i, 'name':str(track.track_name),'click':"openTrack('"+ str(track.key.urlsafe()) +"')"}) of += 1 i += 1 return tree
def get(self): upload_url = blobstore.create_upload_url('/tripmanager/upload') user,_,url,url_linktext = self.get_user() #get specific trip trip_key = self.request.get('trip_id') tripk = ndb.Key(urlsafe=trip_key) trip = tripk.get() #get id of user for given trip trip_user = tripk.parent().id() #get tracks for trip track_query = Track.query(ancestor=tripk).order(-Track.creation_date) tracks = track_query.fetch(20) #get number of tracks num = len(tracks) #get global statistic for trip stat_query = TrackStatistic.query(ancestor=trip.key).fetch(1) #get blobInfo objects from blob_key bli = [] for track in tracks: bli.append(BlobInfo(track.blob_key)) #get trip cities cities = trip.cities #create list of lon,lat pares for every city cordinates = [] for city in cities: try: city = city.lower().replace(" ", "+") api_url = "http://api.geonames.org/searchJSON?formatted=true&name={0}&maxRows=1&lang=es&username=wsanjaw&style=short".format(city) result = urlfetch.fetch(api_url) cordinates.append(helper.procesCity(result.content)) except: cordinates.append([0,0]) #create template template_values = {'user': user, 'url': url, 'url_linktext': url_linktext,'trip':trip,'upload':upload_url, 'tracks':tracks,'blobs':bli,'num':num,'trip_user':trip_user,'stats':stat_query,'cordinates':cordinates} template = JINJA_ENVIRONMENT.get_template('templates/new/onetrip.html') #set cookie value to this page url self.response.set_cookie('redirect_url', self.request.url) self.response.write(template.render(template_values))
def calculate_trip_stat(trip_key): ''' Calculates statistic for whole trip ''' #get tracks for trip track_query = Track.query(ancestor=trip_key) tracks = track_query.fetch(20) stats = [] #get statistic from every track for track in tracks: track_stat_query= TrackStatistic.query(ancestor=track.key) track_stat = track_stat_query.fetch(1) if track_stat != []: stats.append(track_stat[0]) #get trip trip= trip_key.get() #calculate total distance dist = sum([s.total_distance for s in stats]) trip.trip_statistic.total_distance = dist #calculate total time time_sec = sum([time_to_sec(s.total_time) for s in stats]) time = sec_to_time(time_sec) trip.trip_statistic.total_time = time #calculate avr if time_sec > 0: trip.trip_statistic.avr_speed = dist/(time_sec * 0.000277778) else: trip.trip_statistic.avr_speed = 0 #calculate total climb climb = sum([s.total_climb for s in stats]) trip.trip_statistic.total_climb = climb #calculate max elev maxel = max([s.max_elev for s in stats] + [-100]) trip.trip_statistic.max_elev = maxel trip.put()
def get(self): upload_url = blobstore.create_upload_url('/tripmanager/upload') user,id,url,url_linktext = self.get_user() tree = self.create_trip_tree(id) #get specific trip trip_key = self.request.get('trip_id') tripk = ndb.Key(urlsafe=trip_key) trip = tripk.get() #get tracks for trip track_query = Track.query(ancestor=tripk).order(-Track.creation_date) tracks = track_query.fetch(20) #get number of tracks num = len(tracks) #get blobInfo objects from blob_key bli = [] for track in tracks: bli.append(BlobInfo(track.blob_key)) #get trip cities cities = trip.cities #create list of lon,lat pares for every city cordinates = [] cities_string = "" for city in cities: if city == cities[-1]: cities_string += str(city) else: cities_string += str(city)+"," city = city.lower().replace(" ", "+") try: api_url = "http://api.geonames.org/searchJSON?formatted=true&name={0}&maxRows=1&lang=es&username=wsanjaw&style=short".format(city) result = urlfetch.fetch(api_url) cordinates.append(helper.procesCity(result.content)) except: cordinates.append([0,0]) #get statistic track_stat_query= TrackStatistic.query(ancestor=tripk) stats = track_stat_query.fetch() #create data for total climb chart and bubble chart climb_data = [] bubble_data = [] for s in stats: climb_data.append([s.name,s.total_climb]) bubble_data.append([s.key.parent().id(),s.name[0:3].upper(),s.total_distance,s.total_climb,"tracks",helper.time_to_sec(s.total_time)]) tags = self.get_all_tags(id) #create template template_values = {'user': user, 'url': url, 'url_linktext': url_linktext,'tree':tree,'trip':trip, 'climb_data':map(json.dumps, climb_data),'bubble_data':map(json.dumps, bubble_data), 'tracks':tracks,'blobs':bli,'num':num,'stats':track_stat_query,'cordinates':cordinates, 'upload':upload_url,'cities':cities_string,'sug_location':tags[0],'sug_type':tags[1],'sug_season':tags[2], 'location':self.get_location_filters(),'type':self.get_type_filters(),'season':self.get_season_filters(), 'trip_loc':[str(x) for x in trip.trip_tags.location],'trip_type':[str(x) for x in trip.trip_tags.type], 'trip_ses':[str(x) for x in trip.trip_tags.season]} template = JINJA_ENVIRONMENT.get_template('templates/new/onetriptrackmanager.html') self.response.write(template.render(template_values))