def post(self): #time.sleep(30) #get track key = self.request.get('key') track_key = ndb.Key(urlsafe=key) track = track_key.get() #to ensure immutability track_stat_query= TrackStatistic.query(ancestor=track_key) stat = track_stat_query.fetch(1) if stat == []: #read data from uploaded file blob_reader = blobstore.BlobReader(track.blob_key) data = blob_reader.read() try: g = GPXReader() a = g.parse_gpx(data) for item in a: gc = GPXCalculation(item) ts = TrackStatistic(parent=track.key,name=item.name) total_distance = gc.calculate_distance() ts.total_distance = total_distance #get track time total_time = gc.total_time().total_seconds() #convert time from seconds to string to match db format str_time = helper.sec_to_time(total_time) ts.total_time = str_time avr_speed = gc.avr_speed() ts.avr_speed = avr_speed total_climb = gc.total_climb() ts.total_climb = total_climb max_elev = gc.max_elev() ts.max_elev = max_elev ts.put() #change status to indicate that calculation is done track.status = "" track.put() except GpxDateFormatException: #if error occurs set status so appropriate message is shown track.status = "Date in wrong format" track.put() except GpxFormatException: #if error occurs set status so appropriate message is shown track.status = "Unable to parse gpx" track.put() #if track is added to db calculate statistic for whole trip helper.calculate_trip_stat(track.key.parent())
def get(self): user,id,url,url_linktext = self.get_user() tree = self.create_trip_tree(id) #get specific track track_key = self.request.get('track_id') trackk = ndb.Key(urlsafe=track_key) track = trackk.get() #get statistic track_stat_query= TrackStatistic.query(ancestor=trackk) stat = track_stat_query.fetch() tags = self.get_all_tags(id) #create template template_values = {'user': user, 'url': url, 'url_linktext': url_linktext,'tree':tree,'track':track, 'stat':stat,'trip': trackk.parent().get(),"blob":BlobInfo(track.blob_key),'status':track.status, 'location':self.get_location_filters(),'type':self.get_type_filters(),'season':self.get_season_filters(), 'sug_location':tags[0],'sug_type':tags[1],'sug_season':tags[2]} template = JINJA_ENVIRONMENT.get_template('templates/new/onetrack.html') self.response.write(template.render(template_values))
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 get(self): 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 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)]) #create template template_values = {'user': user, 'url': url, 'url_linktext': url_linktext, 'climb_data':map(json.dumps, climb_data),'bubble_data':map(json.dumps, bubble_data)} template = JINJA_ENVIRONMENT.get_template('templates/charts.html') self.response.write(template.render(template_values))
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 post(self): trips_query = Trip.query(Trip.visibility==True).order(-Trip.creation_date) #get cursor to fetch only new trips curs = Cursor(urlsafe=self.request.get('cursor')) trips, next_curs, more = trips_query.fetch_page(PAGE_SIZE, start_cursor=curs) tripsstats = {} #get all statistics for trip in trips: stat_query = TrackStatistic.query(ancestor=trip.key).fetch(1) tripsstats[trip.key]=stat_query data = self.create_trip_html(trips,tripsstats) # Create an array array = {'trips': data,'next_curs':next_curs.urlsafe(),'more':more} # Output the JSON self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps(array))
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): user,_,url,url_linktext = self.get_user() #get specific track track_key = self.request.get('track_id') trackk = ndb.Key(urlsafe=track_key) track = trackk.get() #get statistic track_stat_query= TrackStatistic.query(ancestor=trackk) stat = track_stat_query.fetch() #create template template_values = {'user': user, 'url': url, 'url_linktext': url_linktext,'stat': stat,'status':track.status, 'back':self.request.referer,'trip': trackk.parent().get(),"track":track, "blob":BlobInfo(track.blob_key)} template = JINJA_ENVIRONMENT.get_template('templates/new/stat.html') self.response.write(template.render(template_values))
def get(self): #get specific track track_key = self.request.get('track_id') trakk = ndb.Key(urlsafe=track_key) track = trakk.get() #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() #calculate trip statistic helper.calculate_trip_stat(track.key.parent()) self.redirect("/tripmanager/onetrip?trip_id="+track.key.parent().urlsafe())
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))