本文整理汇总了Python中questionnaire.services.users.UserQuestionnaireService类的典型用法代码示例。如果您正苦于以下问题:Python UserQuestionnaireService类的具体用法?Python UserQuestionnaireService怎么用?Python UserQuestionnaireService使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UserQuestionnaireService类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_submit_changes_draft_answers_to_submitted_and_not_create_new_instances
def test_submit_changes_draft_answers_to_submitted_and_not_create_new_instances(self):
data = self.data
old_primary = MultiChoiceAnswer.objects.create(response=self.option1, question=self.question1, **self.initial)
old_answer_1 = NumericalAnswer.objects.create(response=int(data['Number-0-response']), question=self.question2, **self.initial)
old_answer_2 = NumericalAnswer.objects.create(response=int(data['Number-1-response']), question=self.question3, **self.initial)
answer_group = AnswerGroup.objects.create(grouped_question=self.question_group)
answer_group.answer.add(old_primary, old_answer_1, old_answer_2)
user_service = UserQuestionnaireService(self.user.user_profile.country, self.questionnaire)
user_service.submit()
primary = MultiChoiceAnswer.objects.get(response__id=int(data['MultiChoice-0-response']), question=self.question1)
answer_1 = NumericalAnswer.objects.get(response=int(data['Number-0-response']), question=self.question2)
answer_2 = NumericalAnswer.objects.get(response=int(data['Number-1-response']), question=self.question3)
self.assertEqual(old_primary.id, primary.id)
self.assertEqual(Answer.SUBMITTED_STATUS, primary.status)
self.assertEqual(old_answer_1.id, answer_1.id)
self.assertEqual(Answer.SUBMITTED_STATUS, answer_1.status)
self.assertEqual(old_answer_2.id, answer_2.id)
self.assertEqual(Answer.SUBMITTED_STATUS, answer_2.status)
answer_group = AnswerGroup.objects.filter(grouped_question=self.question_group)
self.assertEqual(1, answer_group.count())
示例2: test_user_knows_its_documents_in_a_questionnaire
def test_user_knows_its_documents_in_a_questionnaire(self):
m = mock_open()
with patch('__main__.open', m, create=True):
filename = "some_filename.txt"
with open(filename, 'w') as document:
document.write("Some stuff")
self.document = open(filename, 'rb')
document_in = SupportDocument.objects.create(path=File(self.document), country=self.country,
questionnaire=self.questionnaire)
questionnaire_2 = Questionnaire.objects.create(name="haha", year=2013)
document_not_in = SupportDocument.objects.create(path=File(self.document), country=self.country,
questionnaire=questionnaire_2)
user_service = UserQuestionnaireService(self.country, self.questionnaire)
documents = user_service.attachments()
self.assertEqual(1, documents.count())
self.assertIn(document_in, documents)
self.assertNotIn(document_not_in, documents)
os.system("rm -rf %s" % filename)
os.system("rm -rf media/user_uploads/*")
示例3: post
def post(self, request, *args, **kwargs):
user_country = self.request.user.user_profile.country
questionnaire = Questionnaire.objects.get(id=self.kwargs['questionnaire_id'])
user_questionnaire = UserQuestionnaireService(user_country, questionnaire)
if not user_questionnaire.required_sections_answered():
return self._reload_section_with_required_answers_errors(request, user_questionnaire, *args, **kwargs)
return self._submit_answers(request, user_questionnaire, *args, **kwargs)
示例4: get
def get(self, request, *args, **kwargs):
if kwargs.get('questionnaire_id'):
questionnaire = Questionnaire.objects.get(id=kwargs.get('questionnaire_id'))
else:
questionnaire = Questionnaire.objects.get(status=Questionnaire.PUBLISHED)
user_questionnaire_service = UserQuestionnaireService(self.request.user, questionnaire)
context = {'all_sections_questionnaires': user_questionnaire_service.all_sections_questionnaires(),
'ordered_sections': questionnaire.sections.order_by('order')}
return render(request, self.template_name, context)
示例5: test_user_knows_answers_given_questionnaire
def test_user_knows_answers_given_questionnaire(self):
data = self.data
old_primary = MultiChoiceAnswer.objects.create(response=self.option1, question=self.question1, **self.initial)
old_answer_1 = NumericalAnswer.objects.create(response=int(data['Number-0-response']), question=self.question2,
**self.initial)
old_answer_2 = NumericalAnswer.objects.create(response=int(data['Number-1-response']), question=self.question3,
**self.initial)
answer_group = AnswerGroup.objects.create(grouped_question=self.question_group)
answer_group.answer.add(old_primary, old_answer_1, old_answer_2)
other_questionnaire = Questionnaire.objects.create(name="JRF 2013 Core English",
description="From dropbox as given by Rouslan")
other_section_1 = Section.objects.create(title="Reported Cases of Selected Vaccine Preventable Diseases (VPDs)",
order=1, questionnaire=other_questionnaire, name="Reported Cases")
other_sub_section = SubSection.objects.create(title="Reported cases for the year 2013", order=1,
section=other_section_1)
other_question1 = Question.objects.create(text='Disease', UID='C00011', answer_type='Number')
other_question_group = QuestionGroup.objects.create(subsection=other_sub_section, order=1)
other_question_group.question.add(other_question1)
other_answer_1 = NumericalAnswer.objects.create(response=1, question=other_question1)
section_2 = Section.objects.create(title="Section 2", order=2, questionnaire=self.questionnaire,
name="section2")
sub_section2 = SubSection.objects.create(title="subsection 2", order=1, section=section_2)
question1 = Question.objects.create(text='question 1', UID='C00013', answer_type='Number')
question2 = Question.objects.create(text='question 2', UID='C00014', answer_type='Number')
question_group = QuestionGroup.objects.create(subsection=sub_section2, order=1)
question_group.question.add(question1, question2)
QuestionGroupOrder.objects.create(question=question1, order=1, question_group=question_group)
QuestionGroupOrder.objects.create(question=question2, order=2, question_group=question_group)
answer_1 = NumericalAnswer.objects.create(response=1, question=question1, status=Answer.DRAFT_STATUS,
country=self.country, questionnaire=self.questionnaire)
answer_2 = NumericalAnswer.objects.create(response=2, question=question2, status=Answer.DRAFT_STATUS,
country=self.country, questionnaire=self.questionnaire)
answer_group = AnswerGroup.objects.create(grouped_question=question_group)
answer_group.answer.add(answer_1, answer_2)
user_service = UserQuestionnaireService(self.user.user_profile.country, self.questionnaire)
user_answers = user_service.questionnaire_answers()
self.assertEqual(5, len(user_answers))
self.assertIn(old_primary, user_answers)
self.assertIn(old_answer_1, user_answers)
self.assertIn(old_answer_2, user_answers)
self.assertNotIn(other_answer_1, user_answers)
self.assertIn(answer_1, user_answers)
self.assertIn(answer_2, user_answers)
示例6: test_user_knows_answer_version_of_questionnaire_is_the_same_as_draft_if_draft_exists
def test_user_knows_answer_version_of_questionnaire_is_the_same_as_draft_if_draft_exists(self):
data = self.data
old_primary = MultiChoiceAnswer.objects.create(response=self.option1, question=self.question1, **self.initial)
old_answer_1 = NumericalAnswer.objects.create(response=int(data['Number-0-response']), question=self.question2, **self.initial)
old_answer_2 = NumericalAnswer.objects.create(response=int(data['Number-1-response']), question=self.question3, **self.initial)
answer_group = AnswerGroup.objects.create(grouped_question=self.question_group)
answer_group.answer.add(old_primary, old_answer_1, old_answer_2)
user_service = UserQuestionnaireService(self.user, self.questionnaire)
self.assertEqual(self.initial['version'], user_service.answer_version())
示例7: UploadDocument
class UploadDocument(AdvancedMultiplePermissionsRequiredMixin, CreateView):
GET_permissions = {'any': ('auth.can_submit_responses', 'auth.can_view_users', 'auth.can_edit_questionnaire')}
POST_permissions = {'any': ('auth.can_submit_responses', )}
model = SupportDocument
template_name = 'questionnaires/entry/upload.html'
form_class = SupportDocumentUploadForm
success_url = None
def get(self, request, *args, **kwargs):
self.questionnaire = Questionnaire.objects.get(id=kwargs.get('questionnaire_id'))
self.success_url = reverse('upload_document', args=(self.questionnaire.id, ))
self.user_questionnaire_service = UserQuestionnaireService(get_country(self.request), self.questionnaire)
return super(UploadDocument, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.questionnaire = Questionnaire.objects.get(id=kwargs.get('questionnaire_id'))
self.success_url = reverse('upload_document', args=(self.questionnaire.id, ))
self.user_questionnaire_service = UserQuestionnaireService(get_country(self.request), self.questionnaire)
return super(UploadDocument, self).post(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(UploadDocument, self).get_context_data(**kwargs)
upload_data_initial = {'questionnaire': self.questionnaire, 'country': get_country(self.request)}
attachments = self.user_questionnaire_service.attachments()
initial = {'questionnaire': self.questionnaire, 'user': self.request.user}
context.update({'upload_form': self.form_class(initial=upload_data_initial),
'button_label': 'Upload', 'id': 'id-upload-form', 'questionnaire': self.questionnaire,
'action': reverse('upload_document', args=(self.questionnaire.id,)),
'documents': attachments,
'section_form': SectionForm(initial=initial),
'ordered_sections': self.questionnaire.sections.order_by('order'),
'preview': self._check_preview_mode(),
'new_section_action': reverse("new_section_page", args=(self.questionnaire.id,))})
return context
def _check_preview_mode(self):
return self.user_questionnaire_service.preview() or self.questionnaire.is_finalized() or \
self.questionnaire.is_published() or 'preview' in self.request.GET
def form_valid(self, form):
messages.success(self.request, "File was uploaded successfully")
return super(UploadDocument, self).form_valid(form)
def form_invalid(self, form):
return render(self.request, self.template_name, {'upload_form': form,
'button_label': 'Upload', 'id': 'id-upload-form',
'questionnaire': self.questionnaire,
'documents': self.user_questionnaire_service.attachments(),
'ordered_sections': self.questionnaire.sections.order_by(
'order')})
示例8: test_user_knows_answer_version_of_questionnaire_is_plus_1_of_the_latest_submitted_answers
def test_user_knows_answer_version_of_questionnaire_is_plus_1_of_the_latest_submitted_answers(self):
data = self.data.copy()
old_primary = MultiChoiceAnswer.objects.create(response=self.option1, question=self.question1, **self.initial)
old_answer_1 = NumericalAnswer.objects.create(response=int(data['Number-0-response']), question=self.question2, **self.initial)
old_answer_2 = NumericalAnswer.objects.create(response=int(data['Number-1-response']), question=self.question3, **self.initial)
answer_group = AnswerGroup.objects.create(grouped_question=self.question_group)
answer_group.answer.add(old_primary, old_answer_1, old_answer_2)
user_service = UserQuestionnaireService(self.user, self.questionnaire)
user_service.submit()
self.assertEqual(self.initial['version']+1, user_service.answer_version())
示例9: post
def post(self, request, *args, **kwargs):
questionnaire = Questionnaire.objects.get(id=self.kwargs['questionnaire_id'])
section = Section.objects.get(id=self.kwargs['section_id'])
user_questionnaire_service = UserQuestionnaireService(self.request.user, questionnaire)
initial = {'country': self.request.user.user_profile.country, 'status': 'Draft',
'version': user_questionnaire_service.answer_version()}
formsets = QuestionnaireEntryFormService(section, initial=initial, data=request.POST)
context = {'questionnaire': questionnaire, 'section': section,
'formsets': formsets, 'ordered_sections': Section.objects.order_by('order')}
if formsets.is_valid():
return self._form_valid(request, formsets, user_questionnaire_service, context)
return self._form_invalid(request, context)
示例10: test_knows_all_sections_questionnaire_entry_services
def test_knows_all_sections_questionnaire_entry_services(self):
section_2 = Section.objects.create(title="section 2", order=2, questionnaire=self.questionnaire, name="section 2")
section_3 = Section.objects.create(title="section 3", order=3, questionnaire=self.questionnaire, name="section 3")
user_service = UserQuestionnaireService(self.user.user_profile.country, self.questionnaire)
all_section_questionnaires = user_service.all_sections_questionnaires()
self.assertEqual(3, len(all_section_questionnaires))
self.assertIsInstance(all_section_questionnaires[self.section_1], QuestionnaireEntryFormService)
self.assertEqual(self.section_1, all_section_questionnaires[self.section_1].section)
self.assertIsInstance(all_section_questionnaires[section_2], QuestionnaireEntryFormService)
self.assertEqual(section_2, all_section_questionnaires[section_2].section)
self.assertIsInstance(all_section_questionnaires[section_3], QuestionnaireEntryFormService)
self.assertEqual(section_3, all_section_questionnaires[section_3].section)
示例11: get
def get(self, request, *args, **kwargs):
query_params = {'id': self.kwargs['questionnaire_id'],
'region__in': get_regions(request), 'status__in': get_questionnaire_status(request)}
query_params = filter_empty_values(query_params)
questionnaire = Questionnaire.objects.get(**query_params)
section = Section.objects.get(id=self.kwargs['section_id'])
country = get_country(self.request)
self.user_questionnaire_service = UserQuestionnaireService(country, questionnaire, request.GET.get("version"))
get_version = self.user_questionnaire_service.GET_version
initial = {'status': 'Draft', 'country': country,
'version': get_version, 'questionnaire': questionnaire}
required_answers = 'show' in request.GET
formsets = QuestionnaireEntryFormService(section, initial=initial, highlight=required_answers,
edit_after_submit=self.user_questionnaire_service.edit_after_submit)
printable = 'printable' in request.GET
version = request.GET.get('version', None)
preview = self._check_preview_mode(questionnaire)
region = self.request.user.user_profile.region
section_initial = {'questionnaire': questionnaire, 'region': region, 'user': request.user}
context = {'questionnaire': questionnaire,
'section': section, 'printable': printable,
'preview': preview, 'formsets': formsets,
'ordered_sections': questionnaire.sections.order_by('order'),
'section_form': SectionForm(initial=section_initial),
'new_section_action': reverse('new_section_page', args=(questionnaire.id, )),
'subsection_form': SubSectionForm(initial=section_initial),
'subsection_action': reverse('new_subsection_page', args=(questionnaire.id, section.id)),
'the_version': version or get_version,
'country': country,
'documents': self.user_questionnaire_service.attachments()}
# slow in some instances take 1.5 secs
return self.render_to_response(context)
示例12: test_knows_unanswered_required_question_in_section
def test_knows_unanswered_required_question_in_section(self):
data = self.data.copy()
old_primary = MultiChoiceAnswer.objects.create(response=self.option1, question=self.question1, **self.initial)
old_answer_1 = NumericalAnswer.objects.create(response=int(data['Number-0-response']), question=self.question2, **self.initial)
old_answer_2 = NumericalAnswer.objects.create(response=int(data['Number-1-response']), question=self.question3, **self.initial)
answer_group = AnswerGroup.objects.create(grouped_question=self.question_group)
answer_group.answer.add(old_primary, old_answer_1, old_answer_2)
required_question = Question.objects.create(text='required', UID='C00330', answer_type='Number', is_required=True)
self.question_group.question.add(required_question)
QuestionGroupOrder.objects.create(question_group=self.question_group, question=required_question, order=4)
user_service = UserQuestionnaireService(self.user.user_profile.country, self.questionnaire)
self.assertFalse(user_service.answered_required_questions_in(self.section_1))
示例13: test_user_knows_if_he_should_see_preview_when_status_is_submitted
def test_user_knows_if_he_should_see_preview_when_status_is_submitted(self):
data = self.data
old_primary = MultiChoiceAnswer.objects.create(response=self.option1, question=self.question1, **self.initial)
old_answer_1 = NumericalAnswer.objects.create(response=int(data['Number-0-response']), question=self.question2,
**self.initial)
old_answer_2 = NumericalAnswer.objects.create(response=int(data['Number-1-response']), question=self.question3,
**self.initial)
answer_group = AnswerGroup.objects.create(grouped_question=self.question_group)
answer_group.answer.add(old_primary, old_answer_1, old_answer_2)
user_country = self.user.user_profile.country
user_service = UserQuestionnaireService(user_country, self.questionnaire)
self.assertFalse(user_service.preview())
user_service.submit()
self.assertTrue(user_service.preview())
示例14: test_user_knows_answers_given_questionnaire_and_version
def test_user_knows_answers_given_questionnaire_and_version(self):
data = self.data
version_1_primary_answer = MultiChoiceAnswer.objects.create(response=self.option1, question=self.question1,
**self.initial)
version_1_answer_1 = NumericalAnswer.objects.create(response=int(data['Number-0-response']),
question=self.question2, **self.initial)
version_1_answer_2 = NumericalAnswer.objects.create(response=int(data['Number-1-response']),
question=self.question3, **self.initial)
answer_group = AnswerGroup.objects.create(grouped_question=self.question_group)
answer_group.answer.add(version_1_answer_1, version_1_answer_2, version_1_primary_answer)
user_service = UserQuestionnaireService(self.user.user_profile.country, self.questionnaire, version=1)
user_answers = user_service.questionnaire_answers()
self.assertEqual(3, len(user_answers))
self.assertIn(version_1_primary_answer, user_answers)
self.assertIn(version_1_answer_1, user_answers)
self.assertIn(version_1_answer_2, user_answers)
version_1_primary_answer.status = Answer.SUBMITTED_STATUS
version_1_primary_answer.save()
version_1_answer_1.status = Answer.SUBMITTED_STATUS
version_1_answer_1.save()
version_1_answer_2.status = Answer.SUBMITTED_STATUS
version_1_answer_2.save()
initial = self.initial.copy()
del initial['version']
version_2_primary = MultiChoiceAnswer.objects.create(response=self.option1, question=self.question1, version=2,
**initial)
version_2_answer_1 = NumericalAnswer.objects.create(response=int(data['Number-0-response']),
question=self.question2, version=2, **initial)
version_2_answer_2 = NumericalAnswer.objects.create(response=int(data['Number-1-response']),
question=self.question3, version=2, **initial)
answer_group = AnswerGroup.objects.create(grouped_question=self.question_group)
answer_group.answer.add(version_2_primary, version_2_answer_1, version_2_answer_2)
user_service = UserQuestionnaireService(self.country, self.questionnaire, version=2)
user_answers = user_service.questionnaire_answers()
self.assertEqual(3, len(user_answers))
self.assertIn(version_2_primary, user_answers)
self.assertIn(version_2_answer_1, user_answers)
self.assertIn(version_2_answer_2, user_answers)
示例15: test_user_knows_its_country_answers
def test_user_knows_its_country_answers(self):
data = self.data
old_primary = MultiChoiceAnswer.objects.create(response=self.option1, question=self.question1, **self.initial)
old_answer_1 = NumericalAnswer.objects.create(response=int(data['Number-0-response']), question=self.question2, **self.initial)
old_answer_2 = NumericalAnswer.objects.create(response=int(data['Number-1-response']), question=self.question3, **self.initial)
answer_group = AnswerGroup.objects.create(grouped_question=self.question_group)
answer_group.answer.add(old_primary, old_answer_1, old_answer_2)
user_service = UserQuestionnaireService(self.user.user_profile.country, self.questionnaire)
user_answers = user_service.all_answers()
self.assertEqual(3, len(user_answers))
self.assertIn(old_primary, user_answers)
self.assertIn(old_answer_1, user_answers)
self.assertIn(old_answer_2, user_answers)