当前位置: 首页>>代码示例>>Python>>正文


Python Question.get_by_id方法代码示例

本文整理汇总了Python中models.Question.get_by_id方法的典型用法代码示例。如果您正苦于以下问题:Python Question.get_by_id方法的具体用法?Python Question.get_by_id怎么用?Python Question.get_by_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Question的用法示例。


在下文中一共展示了Question.get_by_id方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: list_questions_for_user

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
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)
开发者ID:ahbuntu,项目名称:ece1779,代码行数:31,代码来源:views.py

示例2: create_nearby_question

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def create_nearby_question(question_id):
    """Workaround for Prospective Searchs shortcomings; we need to create 
    NearbyQuestion objects for each User/Question pair."""
    prospective_users = ProspectiveUser.all()
    question = Question.get_by_id(question_id)
    for user_to_test in prospective_users:

        if user_to_test.login == question.added_by:
            continue # No need to create a search for your own questions

        # create a new document and subscribe to it
        distance_to_origin = get_location_distance_in_km(user_to_test.origin_location.lat,
                                                         user_to_test.origin_location.lon,
                                                         question.location.lat,
                                                         question.location.lon)
        nearby_prospective_question = NearbyQuestion(
            for_prospective_user_id=user_to_test.key.id(),
            for_question_id=question_id,
            origin_latitude=user_to_test.origin_location.lat,
            origin_longitude=user_to_test.origin_location.lon,
            origin_radius=user_to_test.notification_radius_in_km,
            origin_distance_in_km=distance_to_origin
        )

        # TODO: (potentially) only required for debugging purposes. Prospective_search.match might not required a saved entity.
        # nearby_prospective_question.put()

        # "Documents are assigned to a particular topic when calling match()"
        prospective_search.match(
            nearby_prospective_question
        )
开发者ID:ahbuntu,项目名称:ece1779,代码行数:33,代码来源:views.py

示例3: answer

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def answer(safe_answer_key):
    """Provides a single answer. Used for AJAX calls."""
    rev_key = ndb.Key(urlsafe=safe_answer_key)
    answer = rev_key.get()
    question_id = answer.key.parent().id()
    question = Question.get_by_id(question_id)
    return render_template('includes/answer.html', answer=answer, question=question)
开发者ID:ahbuntu,项目名称:ece1779,代码行数:9,代码来源:views.py

示例4: get

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
    def get(self):
        qId = int(self.request.get('id'))
        question = Question.get_by_id(qId)
        responses = Response.all()
        responses.filter("question =", question)

        values = {'responses': responses, 'question': question}
        self.response.out.write(template.render('templates/responses.html', values))
开发者ID:nmccarthy,项目名称:Yammer-Integration-Questionnaire,代码行数:10,代码来源:questionnaire.py

示例5: match_prospective_search

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def match_prospective_search():
    """Callback on prospective search match."""
    if request.method == "POST":
        logging.info("received a match")
        webapp2Request = request.form
        nearby_question = prospective_search.get_document(webapp2Request)
        prospective_user = ProspectiveUser.get_by_id(nearby_question.for_prospective_user_id)
        question = Question.get_by_id(nearby_question.for_question_id)

        notify_new_question(prospective_user.login, question)
    return '', 200
开发者ID:ahbuntu,项目名称:ece1779,代码行数:13,代码来源:views.py

示例6: delete_question

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def delete_question(question_id):
    """Delete a question."""
    question = Question.get_by_id(question_id)
    if request.method == "POST":
        try:
            remove_question_from_search_index(question)
            question.key.delete()
            flash(u'Question %s successfully deleted.' % question_id, 'success')
            return redirect(url_for('list_questions_for_user'))
        except CapabilityDisabledError:
            flash(u'App Engine Datastore is currently in read-only mode.', 'info')
            return redirect(url_for('list_questions_for_user'))
开发者ID:ahbuntu,项目名称:ece1779,代码行数:14,代码来源:views.py

示例7: question

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def question(request, qid, **kwargs):

    session_data = {}
    if local.request.session['uid']:
        session_data['user_name'] = User.get_by_id(local.request.session['uid'])[0].name
    try:
        question = Question.get_by_id(qid)[0]
    except TypeError:
        return not_found(request)

    question.views += 1
    question.update()

    edit = question.latest_edit()[0]
    category = Category.get_by_id(question.category_id)[0]
    user = User.get_by_id(question.user_id)[0]
    
    question_data =  {
        'title' : str(edit.title),
        'category' : str(category.name),
        'votes' : str(question.votes),
        'author' : str(user.name),
        'author_id' : str(user.id),
        'avatar' : str(user.avatar),
        'views' : str(question.views),
        'created' : str(question.created),
        'body' : str(edit.body),
        }
    
    try:
        answers_list = Answer.get(where=('question_id', qid), order=('votes', 'desc'))
        answer_data_list = []
        for answer in answers_list:
            answer_user = User.get_by_id(answer.user_id)[0]
            answer_edit = AnswerEdit.get(where=('answer_id', answer.id))[0]
            answer_data = {
                'votes' : str(answer.votes),
                'author' : str(answer_user.name),
                'author_id' : str(answer_user.id),
                'avatar' : str(answer_user.avatar),
                'body' : str(answer_edit.body),
                }
            answer_data_list.append(answer_data)
            
    except TypeError:
        answer_data_list = False
    
    page = Page(session_data)
    page.title = str(edit.title) + ' - Meno'
    content = Thread(question_data, answer_data_list)
    local.request.session['last'] = request.base_url
    return respond(page.render(content))
开发者ID:lachlanmarks,项目名称:meno,代码行数:54,代码来源:views.py

示例8: edit_question

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def edit_question(question_id):
    """Edit a question."""
    question = Question.get_by_id(question_id)
    form = QuestionForm(obj=question)
    user = users.get_current_user()
    if request.method == "POST":
        if form.validate_on_submit():
            question.content=form.data.get('content')
            question.location=form.data.get('location')
            question.put()
            flash(u'Question %s successfully modified.' % question_id, 'success')
            return redirect(url_for('list_questions_for_user'))
    return render_template('edit_question.html', question=question, form=form, user=user)
开发者ID:ahbuntu,项目名称:ece1779,代码行数:15,代码来源:views.py

示例9: answers_for_question

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def answers_for_question(question_id):
    """Provides a listing of the question and all of its associated answers."""
    question = Question.get_by_id(question_id)
    user = users.get_current_user()
    answerform = AnswerForm()
    answers = Answer.answers_for(question)
    accepted_answer = None
    if question.accepted_answer_key:
        accepted_answer = question.accepted_answer_key.get()

    # If the user owns the question then register for Channel notifications
    channel_token = None
    if (question.added_by == user):
        channel_id = question_answers_channel_id(user, question)
        channel_token = safe_channel_create(channel_id)

    return render_template('answers_for_question.html', answers=answers, question=question, user=user, form=answerform, channel_token=channel_token, accepted_answer=accepted_answer)
开发者ID:ahbuntu,项目名称:ece1779,代码行数:19,代码来源:views.py

示例10: get_questions

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def get_questions():
    """returns the questions in JSON format"""
    lat = request.args.get('lat', 0, type=float)
    lon = request.args.get('lon', 0, type=float)
    radius = request.args.get('radius', 0, type=float)

    if lat == 0 and lon == 0 and radius == 0:
        questions = Question.all()
    else:
        radius_in_metres = float(radius) * 1000.0
        q = "distance(location, geopoint(%f, %f)) <= %f" % (float(lat), float(lon), float(radius_in_metres))

        # build the index if not already done
        if search.get_indexes().__len__() == 0:
            rebuild_question_search_index()

        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)

    dataset = []
    for question in questions:
        # This conversion can be performed using a custom JsonEncoder implementation,
        # but I didn't have much success. Some good links below -
        # http://stackoverflow.com/questions/1531501/json-serialization-of-google-app-engine-models
        # https://gist.github.com/erichiggins/8969259
        # https://gist.github.com/bengrunfeld/062d0d8360667c47bc5b
        details = {'key': question.key.id(),
                   'added_by': question.added_by.nickname(),
                   'content': question.content,
                   'timestamp': question.timestamp.strftime('%m-%d-%y'),
                   'location': {'lat': question.location.lat,
                                'lon': question.location.lon}
                   }
        dataset.append(details)

    return jsonify(result=dataset)
开发者ID:ahbuntu,项目名称:ece1779,代码行数:44,代码来源:views.py

示例11: accept_answer

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def accept_answer(safe_answer_key):
    """Accept the answer for a question."""

    rev_key = ndb.Key(urlsafe=safe_answer_key)
    answer = rev_key.get()
    question_id = answer.key.parent().id()
    question = Question.get_by_id(question_id)

    # questionform = QuestionForm(obj=question)
    accepted_answer=None
    if request.method == "POST":
        # if questionform.validate_on_submit():
        question.accepted_answer_key=answer.key
        accepted_answer=answer
        question.put()
        flash(u'Answer %s successfully accepted.' % question_id, 'success')
    elif question.accepted_answer_key:
        accepted_answer=question.accepted_answer_key.get()

    return redirect(url_for('answers_for_question', question_id=question_id, accepted_answer=accepted_answer))
开发者ID:ahbuntu,项目名称:ece1779,代码行数:22,代码来源:views.py

示例12: get

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
    def get(self):
        try:
            q = int(self.request.get('id'))
            logging.info('Question ID: %d' % q)
            question = Question.get_by_id(q)

            # Using the language of the question
            template_strings = MainApp.lang_strings[question.lang]
            template_strings['question'] = question.text
            template_strings['answr'] = question.answr
            template_strings['q_id'] = q
            logging.info("%s -> %s" % (question.text, question.answr))

            template_path = os.path.join(os.path.dirname(__file__), 'templates', 'answr.html')

            # Output the response
            self.response.out.write(template.render(template_path, template_strings))
        except Exception, e:
            logging.warning('Something went wrong with question permalink: %s' % str(e))
            self.redirect('/')
开发者ID:lupino3,项目名称:answr,代码行数:22,代码来源:main.py

示例13: list_questions

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def list_questions():
    """Lists all questions posted on the site - available to anonymous users"""
    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)):
        radius_in_metres = float(radius) * 1000.0
        q = "distance(location, geopoint(%f, %f)) <= %f" % (float(latitude), float(longitude), float(radius_in_metres))

        # build the index if not already done
        if search.get_indexes().__len__() == 0:
            rebuild_question_search_index()

        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)

        search_form.latitude.data = float(latitude)
        search_form.longitude.data = float(longitude)
        search_form.distance_in_km.data = radius_in_metres/1000.0
    else:
        questions = Question.all()

    channel_token = None
    if (user):
        channel_token = safe_channel_create(user_channel_id(user))
    return render_template('list_questions.html', questions=questions, form=form, user=user, login_url=login_url, search_form=search_form, channel_token=channel_token)
开发者ID:ahbuntu,项目名称:ece1779,代码行数:42,代码来源:views.py

示例14: new_answer

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
def new_answer(question_id):
    """Create a new answer corresponding to a question."""
    question = Question.get_by_id(question_id)
    answerform = AnswerForm()
    if request.method == "POST" and answerform.validate_on_submit():
        answer = Answer(
            content=answerform.content.data,
            added_by=users.get_current_user(),
            location=get_location(answerform.location.data),
            for_question=question,
            parent=question.key
        )
        try:
            answer.put()
            notify_new_answer(answer)
            answer_id = answer.key.id()

            flash(u'Answer %s successfully saved.' % answer_id, 'success')
            return redirect(url_for('answers_for_question', question_id=question_id))
        except CapabilityDisabledError:
            flash(u'App Engine Datastore is currently in read-only mode.', 'info')
            return redirect(url_for('answers_for_question', question_id=question_id))

    return render_template('new_answer.html', question=question, form=answerform)
开发者ID:ahbuntu,项目名称:ece1779,代码行数:26,代码来源:views.py

示例15: fetch_question

# 需要导入模块: from models import Question [as 别名]
# 或者: from models.Question import get_by_id [as 别名]
 def fetch_question(self, id):
     self.values['id'] = id
     self.question_entity = Question.get_by_id(int(id))
开发者ID:JeanCSM,项目名称:quizzical,代码行数:5,代码来源:edit.py


注:本文中的models.Question.get_by_id方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。