def pads(): #checks to see if any pad was updated via http form pad_one = request.query.get('item1') pad_two = request.query.get('item2') pad_three = request.query.get('item3') pad_four = request.query.get('item4') key_list = [pad_one, pad_two, pad_three, pad_four] #if any of the pads were updated if pad_one or pad_two or pad_three or pad_four: for index, pad in enumerate(key_list): #checks which pad was the one updated if pad: current_item = pad #the new item is saved in a variable number_pad = str(index+1) #the number of the pad is saved key = db.Key.from_path('Pad', number_pad) new = db.get(key) #fetches the database entity for the pad which was updated new.item = current_item #updates the item new.put() mail_send(number_pad, current_item) #sends email to the Pi if current_item not in [history.item for history in db.Query(History, projection=['item'])]: #checks if the item was never used before history_entry = History(item=current_item) history_entry.put() #add the item to history sleep(.25) #puts in a delay to give the website time to update the pads redirect('/pads') #redirects back to the base pads url to show changes history_list = [] #fetches all of the history entries for previous in db.Query(History, projection=['item']): history_list.append(previous.item) pad_dic = {} #fetches all of the pads data for pad in db.Query(Pad, projection=['item', 'number']): pad_dic[pad.number] = pad.item #displays the pad template with the history and pad data as input output = template('templates/pads', data=pad_dic, history_data=history_list) return output
def validateemail(): #TODO: id = request.query.id if (id == "123456789"): mail.tempSendSolutionMail() return template("validate.tpl") else: redirect("/")
def weather(): if request.query.lat != "" and request.query.lon != "": # Got what we need, make the template now. return template("templates/weather.tmpl", lat=request.query.lat, lon=request.query.lon) elif request.query.zipcode == None or request.query.zipcode == "": # Need zipcode return redirect('static/zip_form.html') else: # Need to get Lat Lon result = get_lat_lon(request.query.zipcode) redirect("/weather?lat="+result['lat']+"&lon="+result['lon'])
def do_login(): """ Captura los datos de la vista para iniciar session """ _auth = Auth() usr = request.forms.get('username') pwd = request.forms.get('password') if _auth.login(usr, pwd): redirect('/') else: return template('auth/login.html', {"login_error": True})
def grocery_list(): to_delete = request.query.get('delete') #if the user removed something from the grocery list if to_delete: #delete the database entity and redirect back to the base url key = db.Key.from_path('Grocery', to_delete) db.delete(key) sleep(.25) redirect('/grocery_list') #requests all of the grocery list database entities groceries_list = [] q = Grocery.all() for product in q.run(): groceries_list.append(product.item) #displays the grocery list template with the current groceries as input output = template('templates/grocery_list', data=groceries_list) return output
def delete_contact(key_str): key = ndb.Key(urlsafe=key_str) key.delete() contacts = bottle.request.session['contacts'] #del_contact = lambda c: c['key_str'] != key_str get_contact = lambda c: c['key_str'] == key_str contact = filter(get_contact, contacts) if 'photo_url' in contact: delete_serving_url(contact['photo_url']) # filter out the deleted contact from session contact dictionary #bottle.request.session['contacts'] = filter(not get_contact, contacts) #return change_path(index, '/', contacts=contacts) #refresh_path('/') bottle.redirect('/')
def signup_submit(): form = SignupForm(bottle.request.forms.decode()) if form.validate(): try: user = User( username=form.username.data, password=form.password.data, email=form.email.data, ) except: print("Failed to create new user: {0}".format(sys.exc_info()[0])) # Add new user to database user.put() user_key_str = user.key.urlsafe() session_add('user_key_str', user_key_str) session_add('username', form.username.data) bottle.redirect('/') else: return signup_form(form)
def login_submit(form=None): if form is None: form = LoginForm(bottle.request.forms.decode()) if form.validate(): user = User.query(User.username == form.username.data).get() # Check that username and password are valid; otherwise raise error # messages and re-render form if user is None: form.username.errors.append('Unknown username') return login_form(form) elif form.password.data != user.password: form.password.errors.append('Invalid password') return login_form(form) user_key_str = user.key.urlsafe() session_add('user_key_str', user_key_str) session_add('username', form.username.data) bottle.redirect('/') else: return login_form(form)
def process_search(): search_query = request.GET.get('search_query', '').strip() query = search_query.lower() show_daverank = False results = False number_pages = 10 number_videos = 5 #Move this stuff to its own procedure tomorrow! if query.find('--') == 0: if query.find('--forum') == 0: redirect_url = 'http://www.udacity-forums.com/cs101/search/?q=' + urllib.quote( query[8:]) return redirect(redirect_url) if query.find('--cs373') == 0: redirect_url = 'http://www.udacity-forums.com/cs373/search/?q=' + urllib.quote( query[8:]) return redirect(redirect_url) if query.find('--python') == 0: redirect_url = 'http://docs.python.org/search.html?q=' + urllib.quote( query[9:]) return redirect(redirect_url) if query.find('--searchwithpeterdotinfo') == 0: redirect_url = 'http://searchwithpeter.info/secretplans.html?q=' + urllib.quote( query[25:]) return redirect(redirect_url) if query.find('--showmore') == 0: query = query[11:] search_query = query number_pages = 20 number_videos = 10 if query.find('--daverank') == 0: query = query[11:] search_query = query show_daverank = True if query.find('python') == 0: pyquery = query[7:] else: pyquery = query ddgurl_root = 'http://duckduckgo.com/?q=python+' ddgurl_suffix = urllib.quote(pyquery) + '&format=json' response = urllib.urlopen(ddgurl_root + ddgurl_suffix) response_json = response.read() pythonterm = json.loads(response_json) if pythonterm: pyterm_info = {} if pythonterm['AbstractSource'] == 'Python Documentation': pyterm = BeautifulSoup(pythonterm['AbstractText']) try: pyterm_code = pyterm.find('code').string pyterm.pre.decompose() pyterm_info['code'] = pyterm_code except: pyterm_info['code'] = None pyterm_desc = pyterm.get_text() pyterm_info['desc'] = pyterm_desc pyterm_info['url'] = pythonterm['AbstractURL'] results = True else: pyterm_info = None query_words = query.split() for word in query_words: if word in stopwords: query_words.remove(word) query_urls = [] for term in query_words: # Get all SearchTerm objects that match the search_query. q = SearchTerm.all().filter('term =', term).get() if q: query_urls.append(set(q.urls)) if query_urls: query_url_set = set.intersection(*query_urls) query_url_list = list(query_url_set) if len(query_url_list) > 0: results = True if len(query_url_list) > 30: query_url_list = query_url_list[0:30] page_results = Page.all().filter( 'url IN', query_url_list).order('-dave_rank').fetch(number_pages) page_dicts = [] for page in page_results: page_info = {} query_index = page.text.find(query) if query_index != -1: i = page.text.find(' ', query_index - 25) excerpt_words = page.text[i:].split(' ') page_info['exact_match'] = True else: excerpt_words = page.text.split(' ') page_info['exact_match'] = False excerpt = ' '.join(excerpt_words[:50]) page_info['text'] = excerpt page_info['title'] = page.title page_info['url'] = page.url page_info['daverank'] = page.dave_rank page_info['doc'] = page.doc page_dicts.append(page_info) page_dicts.sort(key=itemgetter('exact_match'), reverse=True) video_results = Video.all().filter( 'url IN', query_url_list).order('-views').fetch(number_videos) video_dicts = [] for video in video_results: video_info = {} subtitles = video.text.lower() query_index = subtitles.find(query) time_string = '' if query_index != -1: subtitle_list = subtitles.splitlines() for phrase in subtitle_list: if phrase.find(query) != -1: timestamp_index = subtitle_list.index(phrase) - 1 timestamp = subtitle_list[timestamp_index] if len(timestamp) > 1: minutes = timestamp[3:5] seconds = timestamp[6:8] time_string = '#t=' + minutes + 'm' + seconds + 's' start = 60 * int(minutes) + int(seconds) if time_string: url = video.url + time_string video_info['exact_match'] = True else: url = video.url start = 0 video_info['exact_match'] = False video_info['title'] = video.title video_info['url'] = url video_info['subtitle'] = video.text[-20:query_index:20] video_info['id'] = video.id video_info['start'] = start video_dicts.append(video_info) video_dicts.sort(key=itemgetter('exact_match'), reverse=True) else: page_dicts = None video_dicts = None query_string_words = query.split() return template('templates/results', search_query=search_query, query_string_words=query_string_words, page_dicts=page_dicts, video_dicts=video_dicts, pyterm_info=pyterm_info, show_daverank=show_daverank, results=results)
def logout_submit(dest='/'): bottle.request.session.delete() bottle.redirect(dest)
def process_search(): search_query = request.GET.get("search_query", "").strip() query = search_query.lower() show_daverank = False results = False number_pages = 10 number_videos = 5 # Move this stuff to its own procedure tomorrow! if query.find("--") == 0: if query.find("--forum") == 0: redirect_url = "http://www.udacity-forums.com/cs101/search/?q=" + urllib.quote(query[8:]) return redirect(redirect_url) if query.find("--cs373") == 0: redirect_url = "http://www.udacity-forums.com/cs373/search/?q=" + urllib.quote(query[8:]) return redirect(redirect_url) if query.find("--python") == 0: redirect_url = "http://docs.python.org/search.html?q=" + urllib.quote(query[9:]) return redirect(redirect_url) if query.find("--searchwithpeterdotinfo") == 0: redirect_url = "http://searchwithpeter.info/secretplans.html?q=" + urllib.quote(query[25:]) return redirect(redirect_url) if query.find("--showmore") == 0: query = query[11:] search_query = query number_pages = 20 number_videos = 10 if query.find("--daverank") == 0: query = query[11:] search_query = query show_daverank = True if query.find("python") == 0: pyquery = query[7:] else: pyquery = query ddgurl_root = "http://duckduckgo.com/?q=python+" ddgurl_suffix = urllib.quote(pyquery) + "&format=json" response = urllib.urlopen(ddgurl_root + ddgurl_suffix) response_json = response.read() pythonterm = json.loads(response_json) if pythonterm: pyterm_info = {} if pythonterm["AbstractSource"] == "Python Documentation": pyterm = BeautifulSoup(pythonterm["AbstractText"]) try: pyterm_code = pyterm.find("code").string pyterm.pre.decompose() pyterm_info["code"] = pyterm_code except: pyterm_info["code"] = None pyterm_desc = pyterm.get_text() pyterm_info["desc"] = pyterm_desc pyterm_info["url"] = pythonterm["AbstractURL"] results = True else: pyterm_info = None query_words = query.split() for word in query_words: if word in stopwords: query_words.remove(word) query_urls = [] for term in query_words: # Get all SearchTerm objects that match the search_query. q = SearchTerm.all().filter("term =", term).get() if q: query_urls.append(set(q.urls)) if query_urls: query_url_set = set.intersection(*query_urls) query_url_list = list(query_url_set) if len(query_url_list) > 0: results = True if len(query_url_list) > 30: query_url_list = query_url_list[0:30] page_results = Page.all().filter("url IN", query_url_list).order("-dave_rank").fetch(number_pages) page_dicts = [] for page in page_results: page_info = {} query_index = page.text.find(query) if query_index != -1: i = page.text.find(" ", query_index - 25) excerpt_words = page.text[i:].split(" ") page_info["exact_match"] = True else: excerpt_words = page.text.split(" ") page_info["exact_match"] = False excerpt = " ".join(excerpt_words[:50]) page_info["text"] = excerpt page_info["title"] = page.title page_info["url"] = page.url page_info["daverank"] = page.dave_rank page_info["doc"] = page.doc page_dicts.append(page_info) page_dicts.sort(key=itemgetter("exact_match"), reverse=True) video_results = Video.all().filter("url IN", query_url_list).order("-views").fetch(number_videos) video_dicts = [] for video in video_results: video_info = {} subtitles = video.text.lower() query_index = subtitles.find(query) time_string = "" if query_index != -1: subtitle_list = subtitles.splitlines() for phrase in subtitle_list: if phrase.find(query) != -1: timestamp_index = subtitle_list.index(phrase) - 1 timestamp = subtitle_list[timestamp_index] if len(timestamp) > 1: minutes = timestamp[3:5] seconds = timestamp[6:8] time_string = "#t=" + minutes + "m" + seconds + "s" start = 60 * int(minutes) + int(seconds) if time_string: url = video.url + time_string video_info["exact_match"] = True else: url = video.url start = 0 video_info["exact_match"] = False video_info["title"] = video.title video_info["url"] = url video_info["subtitle"] = video.text[-20:query_index:20] video_info["id"] = video.id video_info["start"] = start video_dicts.append(video_info) video_dicts.sort(key=itemgetter("exact_match"), reverse=True) else: page_dicts = None video_dicts = None query_string_words = query.split() return template( "templates/results", search_query=search_query, query_string_words=query_string_words, page_dicts=page_dicts, video_dicts=video_dicts, pyterm_info=pyterm_info, show_daverank=show_daverank, results=results, )