Exemplo n.º 1
4
def get_valid_questionnaire(user=None):
    """
    Assumes fixture 'sample.json' is loaded
    """
    if user is None:
        user = create_new_user()
    return Questionnaire.create_new(
        configuration_code='sample', data={'foo': 'bar'},
        user=user)
Exemplo n.º 2
3
 def test_create_new_sets_correct_version(self):
     q_1 = get_valid_questionnaire(self.user)
     q_1.status = 4
     q_1.save()
     # Create a new version
     q_2 = Questionnaire.create_new(
         configuration_code='sample', data={'faz': 'taz'}, user=self.user,
         previous_version=q_1)
     # Version 2 is deleted
     q_2.is_deleted = True
     q_2.save()
     # Create a new version based on v1
     q_3 = Questionnaire.create_new(
         configuration_code='sample', data={'faz': 'taz'}, user=self.user,
         previous_version=q_1)
     self.assertEqual(q_3.version, 3)
Exemplo n.º 3
1
 def test_protect_published_item(self, mock_update_geometry):
     questionnaire = Questionnaire.create_new(
         configuration_code='sample', data={}, user=self.user, status=4
     )
     self.assertEqual(questionnaire.status, 4)
     questionnaire.data = {'foo': 'bar'}
     self.assertRaises(ValidationError, lambda: questionnaire.save())
Exemplo n.º 4
0
 def test_previous_version_only_allows_certain_status(self):
     previous = Questionnaire(status=2)
     with self.assertRaises(ValidationError):
         Questionnaire.create_new(configuration_code='sample',
                                  data={},
                                  user=self.user,
                                  previous_version=previous)
Exemplo n.º 5
0
def questionnaire(self):
    questionnaire1 = Questionnaire(name='Questionnaire One',
                                   subject=self.subject1,
                                   topic=self.topic1,
                                   sub_topic=self.subtopic1)
    questionnaire1.save()
    self.questionnaire1 = questionnaire1
Exemplo n.º 6
0
 def create_questionnaire(self, status, user=None):
     Questionnaire.create_new(
         configuration_code='sample',
         data={'foo': 'bar'},
         user=user or self.user,
         status=status
     )
Exemplo n.º 7
0
 def setUp(self):
     view = QuestionnaireLinkSearchView(configuration_code='approaches')
     self.request = RequestFactory().get(
         reverse('approaches:questionnaire_link_search'),
         HTTP_X_REQUESTED_WITH='XMLHttpRequest')
     user1 = create_new_user()
     user2 = create_new_user(id=2, email='*****@*****.**')
     self.request.user = user1
     self.view = self.setup_view(view, self.request)
     self.q1 = Questionnaire.create_new(
         configuration_code='technologies',
         data={'qg_name': [{'name': {'en': 'Tech 1'}}]}, user=user1)
     self.q2 = Questionnaire.create_new(
         configuration_code='approaches',
         data={'qg_name': [{'name': {'en': 'App 2'}}]}, user=user1)
     self.q3 = Questionnaire.create_new(
         configuration_code='approaches',
         data={'qg_name': [{'name': {'en': 'App 3'}}]}, user=user1)
     self.q3.status = 4
     self.q3.save()
     self.q4 = Questionnaire.create_new(
         configuration_code='approaches',
         data={'qg_name': [{'name': {'en': 'App 4'}}]}, user=user2)
     self.q4.status = 4
     self.q4.save()
     self.q5 = Questionnaire.create_new(
         configuration_code='approaches',
         data={'qg_name': [{'name': {'en': 'App 5'}}]}, user=user2)
Exemplo n.º 8
0
 def setUp(self):
     view = QuestionnaireLinkSearchView(configuration_code='approaches')
     self.request = RequestFactory().get(
         reverse('approaches:questionnaire_link_search'),
         HTTP_X_REQUESTED_WITH='XMLHttpRequest')
     user1 = create_new_user()
     user2 = create_new_user(id=2, email='*****@*****.**')
     self.request.user = user1
     self.view = self.setup_view(view, self.request)
     self.q1 = Questionnaire.create_new(
         configuration_code='technologies',
         data={'qg_name': [{
             'name': {
                 'en': 'Tech 1'
             }
         }]},
         user=user1)
     self.q2 = Questionnaire.create_new(
         configuration_code='approaches',
         data={'qg_name': [{
             'name': {
                 'en': 'App 2'
             }
         }]},
         user=user1)
     self.q3 = Questionnaire.create_new(
         configuration_code='approaches',
         data={'qg_name': [{
             'name': {
                 'en': 'App 3'
             }
         }]},
         user=user1)
     self.q3.status = 4
     self.q3.save()
     self.q4 = Questionnaire.create_new(
         configuration_code='approaches',
         data={'qg_name': [{
             'name': {
                 'en': 'App 4'
             }
         }]},
         user=user2)
     self.q4.status = 4
     self.q4.save()
     self.q5 = Questionnaire.create_new(
         configuration_code='approaches',
         data={'qg_name': [{
             'name': {
                 'en': 'App 5'
             }
         }]},
         user=user2)
Exemplo n.º 9
0
 def test_create_new_adds_language(self):
     language = 'en'
     ret = Questionnaire.create_new(
         configuration_code='sample', data={}, user=self.user)
     ret_languages = ret.questionnairetranslation_set.all()
     self.assertEqual(len(ret_languages), 1)
     self.assertEqual(ret_languages[0].language, language)
Exemplo n.º 10
0
 def test_create_new_sets_created(self):
     date = datetime.now()
     questionnaire = Questionnaire.create_new(configuration_code='sample',
                                              data={},
                                              user=self.user,
                                              created=date)
     self.assertEqual(questionnaire.created, date)
Exemplo n.º 11
0
 def test_create_new_keeps_users_from_previous_version(self):
     user2 = create_new_user(id=2,
                             email='*****@*****.**',
                             lastname='foo2',
                             firstname='bar2')
     user3 = create_new_user(id=3,
                             email='*****@*****.**',
                             lastname='foo3',
                             firstname='bar3')
     previous = get_valid_questionnaire(self.user)
     previous.status = 4
     previous.add_user(user2, settings.QUESTIONNAIRE_EDITOR)
     previous.add_user(user3, settings.QUESTIONNAIRE_REVIEWER)
     previous_users = previous.get_users()
     self.assertEqual(len(previous_users), 3)
     self.assertIn((settings.QUESTIONNAIRE_COMPILER, self.user),
                   previous_users)
     self.assertIn((settings.QUESTIONNAIRE_EDITOR, user2), previous_users)
     self.assertIn((settings.QUESTIONNAIRE_REVIEWER, user3), previous_users)
     # Compiler starts a new version
     q = Questionnaire.create_new(configuration_code='sample',
                                  data={},
                                  user=user2,
                                  previous_version=previous)
     current_users = q.get_users()
     self.assertEqual(len(current_users), 3)
     self.assertEqual(current_users, previous_users)
Exemplo n.º 12
0
 def test_allow_changes_unpublished_item(self):
     questionnaire = Questionnaire.create_new(
         configuration_code='sample', data={}, user=self.user, status=1
     )
     questionnaire.data = {'foo': 'bar'}
     questionnaire.save()
     self.assertEqual(questionnaire.data, {'foo': 'bar'})
Exemplo n.º 13
0
 def test_allow_changes_unpublished_item(self):
     questionnaire = Questionnaire.create_new(configuration_code='sample',
                                              data={},
                                              user=self.user,
                                              status=1)
     questionnaire.data = {'foo': 'bar'}
     questionnaire.save()
     self.assertEqual(questionnaire.data, {'foo': 'bar'})
Exemplo n.º 14
0
 def test_create_new_adds_language(self):
     language = 'en'
     ret = Questionnaire.create_new(configuration_code='sample',
                                    data={},
                                    user=self.user)
     ret_languages = ret.questionnairetranslation_set.all()
     self.assertEqual(len(ret_languages), 1)
     self.assertEqual(ret_languages[0].language, language)
Exemplo n.º 15
0
 def test_protect_published_item(self, mock_update_geometry):
     questionnaire = Questionnaire.create_new(configuration_code='sample',
                                              data={},
                                              user=self.user,
                                              status=4)
     self.assertEqual(questionnaire.status, 4)
     questionnaire.data = {'foo': 'bar'}
     self.assertRaises(ValidationError, lambda: questionnaire.save())
Exemplo n.º 16
0
def get_valid_questionnaire(user=None):
    """
    Assumes fixture 'sample.json' is loaded
    """
    if user is None:
        user = create_new_user()
    return Questionnaire.create_new(configuration_code='sample',
                                    data={'foo': 'bar'},
                                    user=user)
Exemplo n.º 17
0
 def test_create_new_sets_correct_version(self):
     q_1 = get_valid_questionnaire(self.user)
     q_1.status = 4
     q_1.save()
     # Create a new version
     q_2 = Questionnaire.create_new(configuration_code='sample',
                                    data={'faz': 'taz'},
                                    user=self.user,
                                    previous_version=q_1)
     # Version 2 is deleted
     q_2.is_deleted = True
     q_2.save()
     # Create a new version based on v1
     q_3 = Questionnaire.create_new(configuration_code='sample',
                                    data={'faz': 'taz'},
                                    user=self.user,
                                    previous_version=q_1)
     self.assertEqual(q_3.version, 3)
Exemplo n.º 18
0
 def test_previous_version_pending_updates_same_questionnaire(self):
     previous = get_valid_questionnaire(self.user)
     previous.status = 1
     q = Questionnaire.create_new(configuration_code='sample',
                                  data={'faz': 'taz'},
                                  user=self.user,
                                  previous_version=previous)
     self.assertEqual(q, previous)
     self.assertEqual(previous.data, {'faz': 'taz'})
Exemplo n.º 19
0
 def test_previous_version_public_keeps_same_code(self):
     previous = get_valid_questionnaire(self.user)
     previous.status = 4
     q = Questionnaire.create_new(configuration_code='sample',
                                  data={'faz': 'taz'},
                                  user=self.user,
                                  previous_version=previous)
     self.assertNotEqual(q, previous)
     self.assertEqual(q.code, previous.code)
     self.assertEqual(q.version, previous.version + 1)
Exemplo n.º 20
0
Arquivo: views.py Projeto: maxatp/qcat
    def done(self, form_list, form_dict, **kwargs):

        baseConfig = BaseConfig()

        baseDict = {}
        for step, stepData in form_dict.items():
            baseDict[baseConfig.getConfigByStep(step)] = stepData.cleaned_data
        json = {'base': baseDict}
        questionnaire = Questionnaire.create_new(json=json)
        messages.success(
            self.request, _('The questionnaire was successfully submitted.'))
        return redirect(questionnaire)
Exemplo n.º 21
0
 def test_create_new_keeps_languages_from_previous_version(self):
     previous = get_valid_questionnaire(self.user)
     previous.status = 4
     previous.add_translation_language(language='fr')
     previous.add_translation_language(language='es')
     self.assertEqual(len(previous.translations), 3)
     self.assertEqual(previous.original_locale, 'en')
     q = Questionnaire.create_new(
         configuration_code='sample', data={}, user=self.user,
         previous_version=previous)
     self.assertEqual(len(q.translations), 3)
     self.assertEqual(q.original_locale, 'en')
     self.assertEqual(sorted(previous.translations), sorted(q.translations))
Exemplo n.º 22
0
 def test_create_new_keeps_languages_from_previous_version(self):
     previous = get_valid_questionnaire(self.user)
     previous.status = 4
     previous.add_translation_language(language='fr')
     previous.add_translation_language(language='es')
     self.assertEqual(len(previous.translations), 3)
     self.assertEqual(previous.original_locale, 'en')
     q = Questionnaire.create_new(configuration_code='sample',
                                  data={},
                                  user=self.user,
                                  previous_version=previous)
     self.assertEqual(len(q.translations), 3)
     self.assertEqual(q.original_locale, 'en')
     self.assertEqual(sorted(previous.translations), sorted(q.translations))
Exemplo n.º 23
0
 def test_create_new_copies_compilers(self):
     questionnaire = get_valid_questionnaire(self.user)
     user_2 = create_new_user(id=2, email='*****@*****.**')
     user_3 = create_new_user(id=3, email='*****@*****.**')
     questionnaire.add_user(user_2, 'compiler')
     questionnaire.add_user(user_3, 'editor')
     questionnaire.status = 4
     users = questionnaire.get_users()
     users_ids = [u[1].id for u in users]
     self.assertEqual(len(users_ids), 3)
     questionnaire_2 = Questionnaire.create_new(
         configuration_code='sample', data=questionnaire.data,
         user=self.user, previous_version=questionnaire)
     new_users = questionnaire_2.get_users()
     new_users_ids = [u[1].id for u in new_users]
     self.assertListEqual(sorted(users_ids), sorted(new_users_ids))
Exemplo n.º 24
0
 def test_create_new_copies_compilers(self):
     questionnaire = get_valid_questionnaire(self.user)
     user_2 = create_new_user(id=2, email='*****@*****.**')
     user_3 = create_new_user(id=3, email='*****@*****.**')
     questionnaire.add_user(user_2, 'compiler')
     questionnaire.add_user(user_3, 'editor')
     questionnaire.status = 4
     users = questionnaire.get_users()
     users_ids = [u[1].id for u in users]
     self.assertEqual(len(users_ids), 3)
     questionnaire_2 = Questionnaire.create_new(
         configuration_code='sample',
         data=questionnaire.data,
         user=self.user,
         previous_version=questionnaire)
     new_users = questionnaire_2.get_users()
     new_users_ids = [u[1].id for u in new_users]
     self.assertListEqual(sorted(users_ids), sorted(new_users_ids))
Exemplo n.º 25
0
 def test_get_metadata(self):
     questionnaire = Questionnaire.create_new(
         configuration_code='sample', data={}, user=self.user)
     metadata = questionnaire.get_metadata()
     self.assertIsInstance(metadata, dict)
     self.assertEqual(len(metadata), 11)
     self.assertEqual(metadata['created'], questionnaire.created)
     self.assertEqual(metadata['updated'], questionnaire.updated)
     self.assertEqual(
         metadata['compilers'],
         [{'id': self.user.id, 'name': str(self.user)}])
     self.assertEqual(metadata['editors'], [])
     self.assertEqual(metadata['code'], questionnaire.code)
     self.assertEqual(
         metadata['configuration'],
         Configuration.objects.get(code='sample', edition='2015'))
     self.assertEqual(metadata['translations'], ['en'])
     self.assertEqual(metadata['status'], ('draft', 'Draft'))
     self.assertEqual(metadata['flags'], [])
Exemplo n.º 26
0
 def test_get_metadata(self):
     questionnaire = Questionnaire.create_new(configuration_code='sample',
                                              data={},
                                              user=self.user)
     metadata = questionnaire.get_metadata()
     self.assertIsInstance(metadata, dict)
     self.assertEqual(len(metadata), 11)
     self.assertEqual(metadata['created'], questionnaire.created)
     self.assertEqual(metadata['updated'], questionnaire.updated)
     self.assertEqual(metadata['compilers'], [{
         'id': self.user.id,
         'name': str(self.user)
     }])
     self.assertEqual(metadata['editors'], [])
     self.assertEqual(metadata['code'], questionnaire.code)
     self.assertEqual(
         metadata['configuration'],
         Configuration.objects.get(code='sample', edition='2015'))
     self.assertEqual(metadata['translations'], ['en'])
     self.assertEqual(metadata['status'], ('draft', 'Draft'))
     self.assertEqual(metadata['flags'], [])
Exemplo n.º 27
0
 def test_create_new_keeps_users_from_previous_version(self):
     user2 = create_new_user(
         id=2, email='*****@*****.**', lastname='foo2', firstname='bar2')
     user3 = create_new_user(
         id=3, email='*****@*****.**', lastname='foo3', firstname='bar3')
     previous = get_valid_questionnaire(self.user)
     previous.status = 4
     previous.add_user(user2, settings.QUESTIONNAIRE_EDITOR)
     previous.add_user(user3, settings.QUESTIONNAIRE_REVIEWER)
     previous_users = previous.get_users()
     self.assertEqual(len(previous_users), 3)
     self.assertIn(
         (settings.QUESTIONNAIRE_COMPILER, self.user), previous_users)
     self.assertIn((settings.QUESTIONNAIRE_EDITOR, user2), previous_users)
     self.assertIn((settings.QUESTIONNAIRE_REVIEWER, user3), previous_users)
     # Compiler starts a new version
     q = Questionnaire.create_new(
         configuration_code='sample', data={}, user=user2,
         previous_version=previous)
     current_users = q.get_users()
     self.assertEqual(len(current_users), 3)
     self.assertEqual(current_users, previous_users)
Exemplo n.º 28
0
 def test_requires_data(self):
     questionnaire = Questionnaire()
     with self.assertRaises(ValidationError):
         questionnaire.full_clean()
Exemplo n.º 29
0
 def test_create_new_sets_status(self):
     questionnaire = Questionnaire.create_new(configuration_code='sample',
                                              data={},
                                              user=self.user,
                                              status=2)
     self.assertEqual(questionnaire.status, 2)
Exemplo n.º 30
0
 def test_has_primary_key(self):
     questionnaire = Questionnaire(data={})
     self.assertTrue(hasattr(questionnaire, 'id'))
Exemplo n.º 31
0
 def test_create_new_raises_error_if_invalid_status(self):
     with self.assertRaises(ValidationError):
         Questionnaire.create_new(configuration_code='sample',
                                  data={},
                                  user=self.user,
                                  status=-1)
Exemplo n.º 32
0
 def test_previous_version_only_allows_certain_status(self):
     previous = Questionnaire(status=2)
     with self.assertRaises(ValidationError):
         Questionnaire.create_new(
             configuration_code='sample', data={}, user=self.user,
             previous_version=previous)
Exemplo n.º 33
0
 def create_questionnaire(self, status, user=None):
     Questionnaire.create_new(configuration_code='sample',
                              data={'foo': 'bar'},
                              user=user or self.user,
                              status=status)
Exemplo n.º 34
0
 def test_create_new_sets_data(self):
     questionnaire = Questionnaire.create_new(
         configuration_code='sample', data={}, user=self.user)
     self.assertEqual(questionnaire.data, {})
Exemplo n.º 35
0
 def test_create_new_adds_configuration(self):
     configuration = Configuration.latest_by_code('sample')
     ret = Questionnaire.create_new(
         configuration_code='sample', data={}, user=self.user)
     self.assertEqual(ret.configuration, configuration)
Exemplo n.º 36
0
 def test_create_new_returns_new_object(self):
     returned = Questionnaire.create_new(configuration_code='sample',
                                         data={'foo': 'bar'},
                                         user=self.user)
     new_questionnaire = Questionnaire.objects.get(pk=returned.id)
     self.assertEqual(returned, new_questionnaire)
Exemplo n.º 37
0
    def import_questionnaire(self,
                             merge=None,
                             mode=EXACT_MATCH,
                             percentage=0.75,
                             infer_function=None):
        _debug = False

        qNumber = QuestionNumber()
        slugs = []

        wb = load_workbook(filename=self.file_path, data_only=True)
        ws = wb.get_active_sheet()
        log = ''

        # Cell B1: Name of questionnaire
        name = ws.cell('B1').value
        slugQ = convert_text_to_slug(ws.cell('B1').value)
        disable = False

        questionnaire = None
        if merge != None:
            try:
                questionnaire = Questionnaire.objects.get(id=merge)

            except Questionnaire.DoesNotExist:
                raise Exception(
                    'Questionnaire does not exist, so cant merge against it.')

        else:
            questionnaire = Questionnaire(name=name,
                                          disable=disable,
                                          slug=slugQ,
                                          redirect_url='/')
            log += '\nQuestionnaire created %s ' % questionnaire
            if not _debug:
                questionnaire.save()
                log += '\nQuestionnaire saved %s ' % questionnaire

        try:
            _choices_array = {}
            _questions_rows = {}

            #############################
            # TIPS:
            # Type of Row: QuestionSet, Category, Question
            # Columns: Type, Text/Question, Level/Number, Data type, Value list, Help text/Description, Tooltip, Slug, Stats
            #############################
            for row in ws.rows[2:]:
                if len(row) > 0 and row[0].value != None:
                    type_Column = row[0]

                    text_question_Column = row[1]

                    if (text_question_Column.value != None):
                        text_question_Column.value = text_question_Column.value.encode(
                            'ascii', 'ignore')
                    level_number_column = row[2]
                    _checks = ''

                    # Type = QUESTIONSET
                    # Columns required:  Type, Text/Question
                    # Columns optional:  Help text/Description, Tooltip
                    if str(type_Column.value) == "QuestionSet":

                        sortid = str(level_number_column.value)
                        try:
                            qNumber.getNumber('h0', sortid)
                        except:
                            self.writeLog(log)
                            raise
                        text_en = 'h1. %s' % text_question_Column.value

                        slug_qs = None
                        if row[7].value:
                            slug_qs = row[7].value
                        else:
                            slug_qs = str(slugQ) + "_" + convert_text_to_slug(
                                str(text_question_Column.value))

                        if row[5].value:
                            helpText = row[5].value
                        else:
                            helpText = ""
                        tooltip = False
                        if row[6].value:
                            if str(row[6].value).lower() == 'yes':
                                tooltip = True

                        questionset = None
                        created = False
                        try:
                            questionset = QuestionSet.objects.get(
                                questionnaire=questionnaire,
                                sortid=sortid,
                                heading=slug_qs)

                        except QuestionSet.DoesNotExist:
                            questionset = QuestionSet(
                                questionnaire=questionnaire,
                                sortid=sortid,
                                heading=slug_qs,
                                checks='required',
                                text_en=text_en,
                                help_text=helpText,
                                tooltip=tooltip)
                            created = True

                        if created:
                            log += '\n%s - QuestionSet created %s - %s ' % (
                                type_Column.row, sortid, text_en)
                        else:
                            log += '\n%s - QuestionSet retrieved %s - %s ' % (
                                type_Column.row, sortid, text_en)

                        try:
                            if not _debug:
                                questionset.save()
                                log += '\n%s - QuestionSet saved %s - %s ' % (
                                    type_Column.row, sortid, text_en)
                        except:

                            log += "\n%s - Error to save questionset %s - %s" % (
                                type_Column.row, sortid, text_en)
                            self.writeLog(log)
                            raise

                        #if not created:
                        #    last_question = Question.objects.filter(questionset=questionset).order_by('-id')[0]
                        #    qNumber.setState(last_question.number)

                    # Type = CATEGORY
                    # Columns required:  Type, Text/Question, Level/Number, Category
                    # Columns optional:  Help text/Description, Slug, Tooltip, Dependencies
                    elif str(type_Column.value) == "Category":
                        self.__handleQuestion(self.CATEGORY,
                                              row,
                                              type_Column,
                                              level_number_column,
                                              text_question_Column,
                                              _questions_rows,
                                              _choices_array,
                                              qNumber,
                                              questionset,
                                              log,
                                              _checks,
                                              _debug,
                                              questionnaire,
                                              mode=mode,
                                              percentage=percentage,
                                              infer_function=infer_function)
                    # Type = QUESTION
                    # Columns required:  Type, Text/Question, Level/Number, Data Type, Category, Stats
                    # Columns optional:  Value List, Help text/Description, Tooltip, Dependencies
                    else:
                        self.__handleQuestion(self.QUESTION,
                                              row,
                                              type_Column,
                                              level_number_column,
                                              text_question_Column,
                                              _questions_rows,
                                              _choices_array,
                                              qNumber,
                                              questionset,
                                              log,
                                              _checks,
                                              _debug,
                                              questionnaire,
                                              mode=mode,
                                              percentage=percentage,
                                              infer_function=infer_function)

        except:
            log += '\nError to save questionsets and questions of the questionnaire %s ' % questionnaire
            self.writeLog(log)
            raise

        log += '\nQuestionnaire %s, questionsets, questions and choices created with success!! ' % questionnaire
        self.writeLog(log)

        #raise Exception('Dont commit me dude')
        return True
Exemplo n.º 38
0
 def top(self, request):
     questionnaire = Questionnaire.get_active()
     serializer = self.get_serializer(questionnaire)
     return Response(serializer.data)
Exemplo n.º 39
0
 def test_create_new_calls_get_language(self, mock_get_language):
     mock_get_language.return_value = 'en'
     Questionnaire.create_new(configuration_code='sample',
                              data={},
                              user=self.user)
     mock_get_language.assert_called_once_with()
Exemplo n.º 40
0
 def test_requires_data(self):
     questionnaire = Questionnaire()
     with self.assertRaises(ValidationError):
         questionnaire.full_clean()
Exemplo n.º 41
0
 def test_create_new_calls_get_language(self, mock_get_language):
     mock_get_language.return_value = 'en'
     Questionnaire.create_new(
         configuration_code='sample', data={}, user=self.user)
     mock_get_language.assert_called_once_with()
Exemplo n.º 42
0
 def test_create_new_returns_new_object(self):
     returned = Questionnaire.create_new(
         configuration_code='sample', data={'foo': 'bar'}, user=self.user)
     new_questionnaire = Questionnaire.objects.get(pk=returned.id)
     self.assertEqual(returned, new_questionnaire)
Exemplo n.º 43
0
 def test_create_new_adds_configuration(self):
     configuration = Configuration.latest_by_code('sample')
     ret = Questionnaire.create_new(configuration_code='sample',
                                    data={},
                                    user=self.user)
     self.assertEqual(ret.configuration, configuration)
Exemplo n.º 44
0
 def test_create_new_sets_status(self):
     questionnaire = Questionnaire.create_new(
         configuration_code='sample', data={}, user=self.user, status=2)
     self.assertEqual(questionnaire.status, 2)
Exemplo n.º 45
0
 def test_has_uuid(self):
     questionnaire = Questionnaire(data={})
     self.assertIsInstance(questionnaire.uuid, uuid.UUID)
Exemplo n.º 46
0
 def test_create_new_raises_error_if_invalid_status(self):
     with self.assertRaises(ValidationError):
         Questionnaire.create_new(
             configuration_code='sample', data={}, user=self.user,
             status=-1)
Exemplo n.º 47
0
 def test_create_new_sets_data(self):
     questionnaire = Questionnaire.create_new(configuration_code='sample',
                                              data={},
                                              user=self.user)
     self.assertEqual(questionnaire.data, {})
Exemplo n.º 48
0
 def test_create_new_sets_created(self):
     date = datetime.now()
     questionnaire = Questionnaire.create_new(
         configuration_code='sample', data={}, user=self.user, created=date)
     self.assertEqual(questionnaire.created, date)
Exemplo n.º 49
0
    def import_questionnaire(self, merge=None, mode=EXACT_MATCH, percentage=0.75, infer_function=None):
        _debug = False

        qNumber = QuestionNumber()
        slugs = []

        wb = load_workbook(filename=self.file_path, data_only=True)
        ws = wb.get_active_sheet()
        log = ""

        # Cell B1: Name of questionnaire
        name = ws.cell("B1").value
        slugQ = convert_text_to_slug(ws.cell("B1").value)
        disable = False

        questionnaire = None
        if merge != None:
            try:
                questionnaire = Questionnaire.objects.get(id=merge)

            except Questionnaire.DoesNotExist:
                raise Exception("Questionnaire does not exist, so cant merge against it.")

        else:
            questionnaire = Questionnaire(name=name, disable=disable, slug=slugQ, redirect_url="/")
            log += "\nQuestionnaire created %s " % questionnaire
            if not _debug:
                questionnaire.save()
                log += "\nQuestionnaire saved %s " % questionnaire

        try:
            _choices_array = {}
            _questions_rows = {}

            #############################
            # TIPS:
            # Type of Row: QuestionSet, Category, Question
            # Columns: Type, Text/Question, Level/Number, Data type, Value list, Help text/Description, Tooltip, Slug, Stats
            #############################
            for row in ws.rows[2:]:
                if len(row) > 0 and row[0].value != None:
                    type_Column = row[0]

                    text_question_Column = row[1]

                    if text_question_Column.value != None:
                        text_question_Column.value = text_question_Column.value.encode("ascii", "ignore")
                    level_number_column = row[2]
                    _checks = ""

                    # Type = QUESTIONSET
                    # Columns required:  Type, Text/Question
                    # Columns optional:  Help text/Description, Tooltip
                    if str(type_Column.value) == "QuestionSet":

                        sortid = str(level_number_column.value)
                        try:
                            qNumber.getNumber("h0", sortid)
                        except:
                            self.writeLog(log)
                            raise
                        text_en = "h1. %s" % text_question_Column.value

                        slug_qs = None
                        if row[7].value:
                            slug_qs = row[7].value
                        else:
                            slug_qs = str(slugQ) + "_" + convert_text_to_slug(str(text_question_Column.value))

                        if row[5].value:
                            helpText = row[5].value
                        else:
                            helpText = ""
                        tooltip = False
                        if row[6].value:
                            if str(row[6].value).lower() == "yes":
                                tooltip = True

                        questionset = None
                        created = False
                        try:
                            questionset = QuestionSet.objects.get(
                                questionnaire=questionnaire, sortid=sortid, heading=slug_qs
                            )

                        except QuestionSet.DoesNotExist:
                            questionset = QuestionSet(
                                questionnaire=questionnaire,
                                sortid=sortid,
                                heading=slug_qs,
                                checks="required",
                                text_en=text_en,
                                help_text=helpText,
                                tooltip=tooltip,
                            )
                            created = True

                        if created:
                            log += "\n%s - QuestionSet created %s - %s " % (type_Column.row, sortid, text_en)
                        else:
                            log += "\n%s - QuestionSet retrieved %s - %s " % (type_Column.row, sortid, text_en)

                        try:
                            if not _debug:
                                questionset.save()
                                log += "\n%s - QuestionSet saved %s - %s " % (type_Column.row, sortid, text_en)
                        except:

                            log += "\n%s - Error to save questionset %s - %s" % (type_Column.row, sortid, text_en)
                            self.writeLog(log)
                            raise

                        # if not created:
                        #    last_question = Question.objects.filter(questionset=questionset).order_by('-id')[0]
                        #    qNumber.setState(last_question.number)

                    # Type = CATEGORY
                    # Columns required:  Type, Text/Question, Level/Number, Category
                    # Columns optional:  Help text/Description, Slug, Tooltip, Dependencies
                    elif str(type_Column.value) == "Category":
                        self.__handleQuestion(
                            self.CATEGORY,
                            row,
                            type_Column,
                            level_number_column,
                            text_question_Column,
                            _questions_rows,
                            _choices_array,
                            qNumber,
                            questionset,
                            log,
                            _checks,
                            _debug,
                            questionnaire,
                            mode=mode,
                            percentage=percentage,
                            infer_function=infer_function,
                        )
                    # Type = QUESTION
                    # Columns required:  Type, Text/Question, Level/Number, Data Type, Category, Stats
                    # Columns optional:  Value List, Help text/Description, Tooltip, Dependencies
                    else:
                        self.__handleQuestion(
                            self.QUESTION,
                            row,
                            type_Column,
                            level_number_column,
                            text_question_Column,
                            _questions_rows,
                            _choices_array,
                            qNumber,
                            questionset,
                            log,
                            _checks,
                            _debug,
                            questionnaire,
                            mode=mode,
                            percentage=percentage,
                            infer_function=infer_function,
                        )

        except:
            log += "\nError to save questionsets and questions of the questionnaire %s " % questionnaire
            self.writeLog(log)
            raise

        log += "\nQuestionnaire %s, questionsets, questions and choices created with success!! " % questionnaire
        self.writeLog(log)

        # raise Exception('Dont commit me dude')
        return True
Exemplo n.º 50
-2
 def test_previous_version_public_keeps_same_code(self):
     previous = get_valid_questionnaire(self.user)
     previous.status = 4
     q = Questionnaire.create_new(
         configuration_code='sample', data={'faz': 'taz'}, user=self.user,
         previous_version=previous)
     self.assertNotEqual(q, previous)
     self.assertEqual(q.code, previous.code)
     self.assertEqual(q.version, previous.version + 1)
Exemplo n.º 51
-4
 def test_previous_version_pending_updates_same_questionnaire(self):
     previous = get_valid_questionnaire(self.user)
     previous.status = 1
     q = Questionnaire.create_new(
         configuration_code='sample', data={'faz': 'taz'}, user=self.user,
         previous_version=previous)
     self.assertEqual(q, previous)
     self.assertEqual(previous.data, {'faz': 'taz'})