def list_questions_for_user(): """Lists all questions posted by a user""" form = QuestionForm() search_form = QuestionSearchForm() user = users.get_current_user() login_url = users.create_login_url(url_for('home')) query_string = request.query_string latitude = request.args.get('lat') longitude = request.args.get('lon') radius = request.args.get('r') # If searching w/ params (GET) if request.method == 'GET' and all(v is not None for v in (latitude, longitude, radius)): q = "distance(location, geopoint(%f, %f)) <= %f" % (float(latitude), float(longitude), float(radius)) index = search.Index(name="myQuestions") results = index.search(q) # TODO: replace this with a proper .query questions = [Question.get_by_id(long(r.doc_id)) for r in results] questions = filter(None, questions) # filter deleted questions if questions: questions = sorted(questions, key=lambda question: question.timestamp) else: questions = Question.all_for(user) channel_token = safe_channel_create(all_user_questions_answers_channel_id(user)) return render_template('list_questions_for_user.html', questions=questions, form=form, user=user, login_url=login_url, search_form=search_form, channel_token=channel_token)