本文整理匯總了Python中questions.models.Answer.save方法的典型用法代碼示例。如果您正苦於以下問題:Python Answer.save方法的具體用法?Python Answer.save怎麽用?Python Answer.save使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類questions.models.Answer
的用法示例。
在下文中一共展示了Answer.save方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: doimport
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
def doimport(self,data):
from django.contrib.auth.models import User
from questions.models import Question, Answer
from tagging.models import Tag
try:
question = Question()
question.content = data['question']
question.tags = data['tags']
question.author = User.objects.get(pk=0) #FIXME: System smrtr user: use constant?
question.save() # Save to allow m2m
# Create correct answer
c = Answer()
c.content = data['correct']
c.is_correct = True
c.question = question
c.save()
# Save incorrect answers
data['incorrect'] = filter(lambda x: len(x)>0, data['incorrect']) # Remove empty items
for incorrect in data['incorrect']:
ic = Answer()
ic.content = incorrect
ic.is_correct = False
ic.question = question
ic.save()
except:
print "Error importing:" + data['question']
示例2: test_creator_num_answers
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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: test_percent
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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)
示例5: create_answer
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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()
示例6: test_creator_num_answers
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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)
示例7: post
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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))
示例8: apply_answer
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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()
示例9: create_answer
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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
示例10: handle
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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()
示例11: answer
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
def answer(request):
if request.method == 'POST':
form = AnswerForm(request.POST)
if form.is_valid():
user = request.user
answer = Answer()
answer.user = request.user
answer.question = form.cleaned_data.get('question')
answer.description = form.cleaned_data.get('description')
answer.save()
user.profile.notify_answered(answer.question)
return redirect(u'/questions/{0}/'.format(answer.question.pk))
else:
question = form.cleaned_data.get('question')
return render(request, 'questions/question.html', {'question': question, 'form': form})
else:
return redirect('/questions/')
示例12: test_delete_answer_removes_flag
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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())
示例13: reply
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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)
示例14: test_delete_last_answer_of_question
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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)
示例15: test_new_answer_updates_question
# 需要導入模塊: from questions.models import Answer [as 別名]
# 或者: from questions.models.Answer import save [as 別名]
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()