本文整理汇总了Python中questions.models.Question.save方法的典型用法代码示例。如果您正苦于以下问题:Python Question.save方法的具体用法?Python Question.save怎么用?Python Question.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类questions.models.Question
的用法示例。
在下文中一共展示了Question.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def handle(self, *args, **options):
Question.objects.all().delete()
Answer.objects.all().delete()
print('Deleted existing data')
answers = []
with open(settings.BASE_DIR + '/../' + self.csv_file, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter='|')
next(reader, None)
for row in reader:
if(len(answers) > 80):
save_answers(answers)
answers = []
q = Question(question=row[0])
q.save()
print('Saved question with id ' + str(q.id));
answers.append(Answer(question=q, choice=row[1], correct=True))
for i in row[2].split(','):
answers.append(Answer(question=q, choice=i, correct=False))
save_answers(answers)
示例2: ask
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def ask(request):
ask_form = AskQuestion(request.POST or None)
args = {}
args['form'] = ask_form
if request.POST and ask_form.is_valid():
question = Question(text=ask_form.cleaned_data['text'], title=ask_form.cleaned_data['title'])
tags = ask_form.cleaned_data['tags']
g = Tag.objects.all()
getTag = tags.split(', ')
for tag in getTag:
counter = 0
for l in g:
if l.tag == tag:
counter += 1
if counter == 0:
t = Tag(tag=tag)
t.save()
user = auth.get_user(request)
question.author = user
question.save()
a = g.filter(tag__in=getTag)
question.tags.add(*a)
return redirect('questionGet', question_id=question.id)
else:
return render(request, 'ask.html', args)
示例3: doimport
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question 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']
示例4: test_percent
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question 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: gradeQuestion
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def gradeQuestion(request):
questionList = json.loads(request.POST['questions'])
for question in questionList:
sentenceText = question['sentence']
questionText = question['question']
labelText = question['label']
gradeVal = question['grade']
if Sentence.objects.filter(text=sentenceText).exists() :
sentence = Sentence.objects.get(text=sentenceText)
else:
sentence = Sentence(text=sentenceText)
sentence.save()
if Question.objects.filter(text=questionText).exists():
question = Question.objects.get(text=questionText)
else:
question = Question(text=questionText)
question.save()
if Label.objects.filter(text=labelText).exists():
label = Label.objects.get(text = labelText)
else:
label = Label(text = labelText)
label.save()
grade = Grade(sentence=sentence,question=question,label=label,grade=gradeVal)
grade.save()
return HttpResponse('200')
示例6: handle
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def handle(self, *args, **options):
users = list(User.objects.all())
for i in range(10):
t = Topic()
t.name = u'Topic Name {}'.format(i)
t.description = u'Topic Description {}'.format(i)
t.save()
topics = list(Topic.objects.all())
for j in range(100):
q = Question()
q.author = random.choice(users)
q.title = u'title {}'.format(j)
q.text = u'text {}'.format(j)
q.pub_date = datetime.datetime.now()
q.is_published = True
q.save()
q.topics = random.sample(topics, random.randint(1, 6))
questions = list(Question.objects.all())
for k in range(100):
c = Comment()
c.author = random.choice(users)
c.question = random.choice(questions)
c.text = u'text {}'.format(k)
c.pub_date = datetime.datetime.now()
c.save()
示例7: test_notification_created
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def test_notification_created(self):
"""Creating a new question auto-watches it."""
u = User.objects.get(pk=118533)
q = Question(creator=u, title='foo', content='bar')
q.save()
assert check_watch(Question, q.id, u.email, 'reply')
示例8: test_notification_created
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def test_notification_created(self):
"""Creating a new question auto-watches it for answers."""
u = User.objects.get(pk=118533)
q = Question(creator=u, title='foo', content='bar')
q.save()
assert QuestionReplyEvent.is_notifying(u, q)
示例9: setUp
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def setUp(self):
super(TestQuestionMetadata, self).setUp()
# add a new Question to test with
question = Question(title='Test Question',
content='Lorem Ipsum Dolor',
creator_id=1)
question.save()
self.question = question
示例10: test_no_inactive_users
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def test_no_inactive_users(self):
"""Ensure that inactive users' questions don't appear in the feed."""
u = User.objects.get(pk=118533)
u.is_active = False
u.save()
q = Question(title='Test Question', content='Lorem Ipsum Dolor',
creator_id=118533)
q.save()
assert q.id not in [x.id for x in QuestionsFeed().items()]
示例11: create_question
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def create_question(self, name):
user = UserWithAvatar.objects.order_by("?")[:1][0]
pub_date = timezone.now()
title = "Could you help me with " + name + "?"
text = self.lorem * 10
tags = Tag.objects.order_by("?")[:5]
question = Question(author=user, pub_date=pub_date, text=text, title=title)
question.save()
for tag in tags:
question.tags.add(tag)
question.save()
示例12: create
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def create(self, validated_data):
"""
Custom create method. Prepare and create nested choices.
"""
choices_data = validated_data.pop("choices", None)
question = Question(**validated_data)
question.save()
self.create_choices(question, choices_data)
return question
示例13: test_delete_question_removes_flag
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def test_delete_question_removes_flag(self):
"""Deleting a question also removes the flags on that question."""
question = Question(title='Test Question',
content='Lorem Ipsum Dolor',
creator_id=118533)
question.save()
FlaggedObject.objects.create(
status=0, content_object=question,
reason='language', creator_id=118533)
eq_(1, FlaggedObject.objects.count())
question.delete()
eq_(0, FlaggedObject.objects.count())
示例14: ask
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def ask(request):
ask_form = AskQuestion(request.POST or None)
args = {}
args['form'] = ask_form
if request.POST and ask_form.is_valid():
question = Question(text_question=ask_form.cleaned_data['text'], title=ask_form.cleaned_data['title'])
user = auth.get_user(request)
question.user = user
question.save()
return render(request, 'question.html', args)
else:
return render(request, 'question.html', args)
示例15: test_vote_on_question
# 需要导入模块: from questions.models import Question [as 别名]
# 或者: from questions.models.Question import save [as 别名]
def test_vote_on_question(self):
"""
Test that a hitting a URL will incrememt a question's vote count
"""
question_1 = Question(text="How can my team get started with testing?",
votes=3,
created=datetime.datetime.utcnow().replace(tzinfo=utc),
status="new")
question_1.save()
response = self.client.get('/vote/1/up/')
self.assertRedirects(response, '/')
question_1_after_vote = Question.objects.get(id=1)
self.assertEqual(question_1_after_vote.votes, 4)