def test_get_results(self): index = [{ "name": 'test1', "population": 100, "lat": 10, "long": -15 }, { "name": 'other2', "population": 300, "lat": 30, "long": -35 }, { "name": 'other3', "population": 400, "lat": 40, "long": -45 }] name = "test1" population = 100 latitude = 10 longitude = -15 self.assertEqual( get_results(name, population, latitude, longitude, index), [{ "name": 'test1', "population": 100, "lat": 10, "long": -15, "score": 1.0 }])
def change_article_button(update, context): query = update.callback_query query.answer() data = query.data.split(';') choice = int(data[1]) results = queries.get_results(update.callback_query.message.chat.id) query.edit_message_text(results[choice], reply_markup=get_article_markup(results, choice))
def api_name(): name = request.args.get('name', None) population = int(request.args.get('population', 0)) lat = float(request.args.get('lat', 0.0)) long = float(request.args.get('long', 0.0)) index = queries.get_index() sorted_results = queries.get_results(name, population, lat, long, index) return jsonify(sorted_results)
def save_button(update, context): query = update.callback_query query.answer() phrases = queries.get_phrases(query.message.chat.id) data = query.data.split(';') choice = int(data[1]) queries.add_article(query.message.chat.id, queries.get_results(query.message.chat.id)[choice]) categories = queries.get_categories(query.message.chat.id) categories = '\n'.join( [str(i) + '. ' + categories[i] for i in range(len(categories))]) if not categories: categories = phrases['no_categories'] query.message.reply_text(phrases['save_article_category'] + categories) return 1
def scores(): session = request.cookies.get('session') user_object = db.query(User).filter_by(session_token=session).first() # get user results count, results = get_results(user_object.id) # loop through results and create dict with important info result_list = [] for result in results: result_container = {} score_percent = str( (result.questions_correct / result.questions_total) * 100) test_object = db.query(Test).filter_by(id=result.test_id).first() # shorten percent if not 100 (more than 2 digits) if score_percent != '100.0': result_container['score_percent'] = int(score_percent[:2]) else: result_container['score_percent'] = 100 result_container['test_title'] = test_object.title result_list.insert(0, result_container) response = make_response( render_template("scores.html", result_list=result_list)) return response
def assessments(): if request.method == 'GET': session = request.cookies.get('session') # if user is logged in, get key from db if session: user_object = db.query(User).filter_by( session_token=session).first() key = user_object.key # positive, so get assessments tests = db.query(Test).all() def just_title(obj): return obj.title test_names = map(just_title, tests) # get latest results count, results = get_results(user_object.id) # send response with key and list response = make_response( render_template("overview.html", key=key, assessments=list(test_names), results_count=count)) else: return render_template('index.html') elif request.method == 'POST': key = request.form.get("id") user_object = db.query(User).filter_by(key=key).first() # check if key is stored if user_object: # generate token for session session_token = str(uuid.uuid4()) # save session token to the database user_object.session_token = session_token db.add(user_object) db.commit() # positive, so get assessments tests = db.query(Test).all() def just_title(obj): return obj.title test_names = map(just_title, tests) # print('Try out ', test_names) # get latest results count, results = get_results(user_object.id) # print('Got Tests: ', list) # create response with cookie and login information response = make_response( render_template("overview.html", key=key, assessments=list(test_names), results_count=count)) response.set_cookie("session", session_token) print('Existed and sending back') else: print('Key does not exist') response = make_response( render_template( "index.html", error="Key was not found. If you lost it, get a new one..." )) return response