本文整理匯總了Python中questions.models.Answer類的典型用法代碼示例。如果您正苦於以下問題:Python Answer類的具體用法?Python Answer怎麽用?Python Answer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Answer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_percent
def test_percent(self, switch_is_active):
"""Test user API with all defaults."""
switch_is_active.return_value = True
u = user()
u.save()
add_permission(u, Profile, 'view_kpi_dashboard')
question = Question(title='Test Question',
content='Lorem Ipsum Dolor',
creator_id=u.id)
question.save()
answer = Answer(question=question, creator_id=u.id,
content="Test Answer")
answer.save()
question.solution = answer
question.save()
url = reverse('api_dispatch_list',
kwargs={'resource_name': 'kpi_solution',
'api_name': 'v1'})
self.client.login(username=u.username, password='testpass')
response = self.client.get(url + '?format=json')
eq_(200, response.status_code)
r = json.loads(response.content)
eq_(r['objects'][0]['with_solutions'], 1)
eq_(r['objects'][0]['without_solutions'], 0)
示例2: test_creator_num_answers
def test_creator_num_answers(self):
question = Question.objects.all()[0]
answer = Answer(question=question, creator_id=47963,
content="Test Answer")
answer.save()
eq_(answer.creator_num_answers, 2)
示例3: update
def update(request, id):
if request.method != 'POST':
return return_json(post_error, 400)
question_text, answers, correct_index = edit_post_vars(request)
q = Question.objects.get(pk=id)
q.question = question_text
q.save()
q.answer_set.all().delete()
i=0;
for answer in answers:
a = Answer(question=q, choice=answer, correct=(i == correct_index))
a.save()
i += 1
data = {
'result': 'success',
'id': id,
}
json_result = json.JSONEncoder().encode(data)
return return_json(json_result)
示例4: create_answer
def create_answer(self, name):
question = Question.objects.order_by("?")[:1][0]
author = UserWithAvatar.objects.order_by("?")[:1][0]
pub_date = timezone.now()
text = self.lorem
answer = Answer(question=question, author=author, pub_date=pub_date, text=text)
answer.save()
示例5: test_creator_num_answers
def test_creator_num_answers(self):
"""Test retrieval of answer count for creator of a particular answer"""
question = Question.objects.all()[0]
answer = Answer(question=question, creator_id=47963,
content="Test Answer")
answer.save()
question.solution = answer
question.save()
eq_(answer.creator_num_answers, 1)
示例6: post
def post(self, request, *args, **kwargs):
survey = Survey.objects.filter(id=self.kwargs['pk']).first()
for question_id, option_id in request.POST.items():
if question_id.isdigit():
question = Question.objects.filter(id=question_id).first()
option = Option.objects.filter(id=option_id).first()
if question and option:
answer = Answer(option=option, user=None if request.user.is_anonymous() else request.user)
answer.save()
return HttpResponseRedirect("/survey/%d/results/" %(survey.id))
示例7: create_answer
def create_answer(question, tiki_post, tiki_thread):
"""Create an answer to a question from a Tiki post."""
creator = get_django_user(tiki_post)
created = datetime.fromtimestamp(tiki_post.commentDate)
content = converter.convert(tiki_post.data)
ans = Answer(question=question, creator=creator, content=content, created=created, updated=created)
ans.save(no_update=True, no_notify=True) # don't send a reply notification
# Set answer as solution
if tiki_post.type == "o" and tiki_thread.type == "o":
question.solution = ans
return ans
示例8: handle
def handle(self, *args, **options):
i = 0
k = 0
n = int(args[0])
allusers = UserProfile.objects.all()
tags = Tag.objects.all()
while i < n:
titleNumber = random.randint(0, 50)
textNumber = random.randint(0, 300)
rate = random.randint(-100, 100)
number = random.randint(0, 10)
numberOfTags = random.randint(0, 5)
tagsList = []
for q in range(0, numberOfTags):
randomTag = random.randint(0, len(tags) - 1)
tagsList.insert(q, tags[randomTag])
title = "".join(
random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits + string.whitespace)
for x in range(titleNumber)
)
token = "".join(
random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits + string.whitespace)
for x in range(textNumber)
)
i = i + 1
randomUserId = random.randint(0, len(allusers) - 1)
user = allusers[randomUserId]
q = Question(title=title, text=token, rate=rate, author=user)
q.save()
q.tags.add(*tagsList)
for j in range(0, number):
randomUserId = random.randint(0, len(allusers) - 1)
userComment = allusers[randomUserId]
rateComment = random.randint(-100, 100)
textComment = random.randint(0, 300)
text = "".join(
random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits + string.whitespace)
for x in range(textComment)
)
k = k + 1
a = Answer(k, text=text, rate=rateComment, answers_question=q, author=userComment)
a.save()
示例9: test_delete_answer_removes_flag
def test_delete_answer_removes_flag(self):
"""Deleting an answer also removes the flags on that answer."""
question = Question(title='Test Question',
content='Lorem Ipsum Dolor',
creator_id=118533)
question.save()
answer = Answer(question=question, creator_id=47963,
content="Test Answer")
answer.save()
FlaggedObject.objects.create(
status=0, content_object=answer,
reason='language', creator_id=118533)
eq_(1, FlaggedObject.objects.count())
answer.delete()
eq_(0, FlaggedObject.objects.count())
示例10: test_delete_last_answer_of_question
def test_delete_last_answer_of_question(self):
"""Deleting the last_answer of a Question should update the question.
"""
question = Question.objects.get(pk=1)
last_answer = question.last_answer
# add a new answer and verify last_answer updated
answer = Answer(question=question, creator_id=47963,
content="Test Answer")
answer.save()
question = Question.objects.get(pk=question.id)
eq_(question.last_answer.id, answer.id)
# delete the answer and last_answer should go back to previous value
answer.delete()
question = Question.objects.get(pk=question.id)
eq_(question.last_answer.id, last_answer.id)
eq_(Answer.objects.filter(pk=answer.id).count(), 0)
示例11: reply
def reply(request, question_id):
"""Post a new answer to a question."""
question = get_object_or_404(Question, pk=question_id)
answer_preview = None
if question.is_locked:
raise PermissionDenied
form = AnswerForm(request.POST)
# NOJS: delete images
if 'delete_images' in request.POST:
for image_id in request.POST.getlist('delete_image'):
ImageAttachment.objects.get(pk=image_id).delete()
return answers(request, question_id=question_id, form=form)
# NOJS: upload image
if 'upload_image' in request.POST:
upload_imageattachment(request, question)
return answers(request, question_id=question_id, form=form)
if form.is_valid():
answer = Answer(question=question, creator=request.user,
content=form.cleaned_data['content'])
if 'preview' in request.POST:
answer_preview = answer
else:
answer.save()
ct = ContentType.objects.get_for_model(answer)
# Move over to the answer all of the images I added to the
# reply form
up_images = question.images.filter(creator=request.user)
up_images.update(content_type=ct, object_id=answer.id)
statsd.incr('questions.answer')
if Setting.get_for_user(request.user,
'questions_watch_after_reply'):
QuestionReplyEvent.notify(request.user, question)
return HttpResponseRedirect(answer.get_absolute_url())
return answers(request, question_id=question_id, form=form,
answer_preview=answer_preview)
示例12: test_new_answer_updates_question
def test_new_answer_updates_question(self):
"""Test saving a new answer updates the corresponding question.
Specifically, last_post and num_replies should update."""
question = Question(title='Test Question',
content='Lorem Ipsum Dolor',
creator_id=118533)
question.save()
eq_(0, question.num_answers)
eq_(None, question.last_answer)
answer = Answer(question=question, creator_id=47963,
content="Test Answer")
answer.save()
question = Question.objects.get(pk=question.id)
eq_(1, question.num_answers)
eq_(answer, question.last_answer)
question.delete()
示例13: apply_answer
def apply_answer(question, answer, user):
try:
a = Answer.objects.get(question=question, user=user)
except:
if answer != "0":
a = Answer(question=question, user=user, answer=answer)
a.save()
else:
if answer == "0":
a.delete()
else:
a.answer = answer
a.save()
示例14: new
def new(request):
if request.method != 'POST':
return return_json(post_error)
question_text, answers, correct_index = edit_post_vars(request)
q = Question(question=question_text)
q.save()
i = 0;
for answer in answers:
a = Answer(question=q, choice=answer, correct=(i == correct_index))
a.save()
i += 1
data = {
'result': 'success',
'id': q.id
}
json_result = json.JSONEncoder().encode(data)
return HttpResponse(json_result, content_type="application/json")
示例15: add_answer_to_db
def add_answer_to_db(self, tweet):
question = Question.objects.get_current_question()
person = Person.objects.filter(twitter_username=tweet.user.screen_name)
#TODO: could this sort of logic be moved to the model?
if not person:
# Get the users real name
user = self.twitter_api.GetUser(tweet.user.screen_name)
full_name_list = user.name.split(" ")
first_name = full_name_list[0]
middle_names = " ".join(full_name_list[1:-1])
if len(full_name_list) > 1:
surname = full_name_list[-1]
else:
surname = ""
person = Person(twitter_username=tweet.user.screen_name,
first_name=first_name,
middle_names=middle_names,
surname=surname)
person.save()
else:
# get person from the query set.
# Inelegant could this be modified with custom save() on the model?
person = person[0]
# Remove @FavouriteQueston from the tweet (+2 is for @ and space)
answer_text = tweet.text[len(self.twitter_account) + 2:]
# Decode HTML encoded entities from Twitter
h = HTMLParser.HTMLParser()
answer_text = h.unescape(answer_text)
a = Answer(answer_text=answer_text,
person=person,
question=question,
tweet_id=tweet.id)
a.save()