Ejemplo n.º 1
0
def questionnaire(self):
    questionnaire1 = Questionnaire(name='Questionnaire One',
                                   subject=self.subject1,
                                   topic=self.topic1,
                                   sub_topic=self.subtopic1)
    questionnaire1.save()
    self.questionnaire1 = questionnaire1
Ejemplo n.º 2
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
Ejemplo n.º 3
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