本文整理汇总了Python中questions.models.QuestionVote类的典型用法代码示例。如果您正苦于以下问题:Python QuestionVote类的具体用法?Python QuestionVote怎么用?Python QuestionVote使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QuestionVote类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_vote_updates_count
def test_vote_updates_count(self):
q = Question.objects.all()[0]
eq_(0, q.num_votes_past_week)
vote = QuestionVote(question=q, anonymous_id='abc123')
vote.save()
eq_(1, q.num_votes_past_week)
示例2: question_vote
def question_vote(request, question_id):
"""I have this problem too."""
question = get_object_or_404(Question, pk=question_id)
if question.is_locked:
raise PermissionDenied
if not question.has_voted(request):
vote = QuestionVote(question=question)
if request.user.is_authenticated():
vote.creator = request.user
else:
vote.anonymous_id = request.anonymous.anonymous_id
vote.save()
statsd.incr('questions.votes.question')
if request.is_ajax():
tmpl = 'questions/includes/question_vote_thanks.html'
form = _init_watch_form(request)
html = jingo.render_to_string(request, tmpl, {'question': question,
'watch_form': form})
return HttpResponse(json.dumps({'html': html}))
return HttpResponseRedirect(question.get_absolute_url())
示例3: test_sort
def test_sort(self):
default = reverse('questions.questions')
sorted = urlparams(default, sort='requested')
q = Question.objects.get(pk=2)
qv = QuestionVote(question=q, anonymous_id='abc123')
qv.save()
response = self.client.get(default)
doc = pq(response.content)
eq_('question-1', doc('ol.questions li')[0].attrib['id'])
response = self.client.get(sorted)
doc = pq(response.content)
eq_('question-2', doc('ol.questions li')[0].attrib['id'])
示例4: question_vote
def question_vote(request, question_id):
"""I have this problem too."""
question = get_object_or_404(Question, pk=question_id)
if question.is_locked:
raise PermissionDenied
if not question.has_voted(request):
vote = QuestionVote(question=question)
if request.user.is_authenticated():
vote.creator = request.user
else:
vote.anonymous_id = request.anonymous.anonymous_id
vote.save()
ua = request.META.get("HTTP_USER_AGENT")
if ua:
vote.add_metadata("ua", ua[:1000]) # 1000 max_length
statsd.incr("questions.votes.question")
if request.is_ajax():
tmpl = "questions/includes/question_vote_thanks.html"
form = _init_watch_form(request)
html = jingo.render_to_string(request, tmpl, {"question": question, "watch_form": form})
return HttpResponse(json.dumps({"html": html}))
return HttpResponseRedirect(question.get_absolute_url())
示例5: question_vote
def question_vote(request, question_key, upvote):
question = get_object_or_404(Question, key=question_key)
user = users.get_current_user()
# check it not already voted..
vote = QuestionVote.all() \
.filter('question =', question).filter('user =', user).get()
if vote is None:
vote = QuestionVote(question=question, user=user, upvote=upvote)
vote.put()
question.score += 1 if upvote else -1
question.put()
else:
logging.info('Duplicated vote: %s;;%s', question, user)
redirect_url = request.META.get('HTTP_REFERER', None)
if redirect_url is None:
logging.warning('No referer. Fallback to questions list')
redirect_url = reverse('questions_question_list')
return redirect(redirect_url)
示例6: question_vote
def question_vote(request, question_id):
"""I have this problem too."""
question = get_object_or_404(Question, pk=question_id)
if question.is_locked:
raise PermissionDenied
if not question.has_voted(request):
vote = QuestionVote(question=question)
if request.user.is_authenticated():
vote.creator = request.user
else:
vote.anonymous_id = request.anonymous.anonymous_id
vote.save()
if 'referrer' in request.REQUEST:
referrer = request.REQUEST.get('referrer')
vote.add_metadata('referrer', referrer)
if referrer == 'search' and 'query' in request.REQUEST:
vote.add_metadata('query', request.REQUEST.get('query'))
ua = request.META.get('HTTP_USER_AGENT')
if ua:
vote.add_metadata('ua', ua[:1000]) # 1000 max_length
statsd.incr('questions.votes.question')
if request.is_ajax():
tmpl = 'questions/includes/question_vote_thanks.html'
form = WatchQuestionForm(request.user)
html = jingo.render_to_string(request, tmpl, {'question': question,
'watch_form': form})
return HttpResponse(json.dumps({'html': html}))
return HttpResponseRedirect(question.get_absolute_url())