예제 #1
0
    def success(this_try):
        if create:
            slug = Slugs(slug1=this_try)
            slug.save()

            return slug

        else:
            return this_try
예제 #2
0
    def success(this_try):
        if create:
            slug = Slugs(slug1=this_try)
            slug.save()

            return slug

        else:
            return this_try
예제 #3
0
    def __handleQuestion(self,
                         type,
                         row,
                         type_Column,
                         level_number_column,
                         text_question_Column,
                         _questions_rows,
                         _choices_array,
                         qNumber,
                         questionset,
                         log,
                         _checks,
                         _debug,
                         questionnaire,
                         mode=EXACT_MATCH,
                         percentage=0.75,
                         infer_function=None):
        try:
            slug = None
            text_en = None
            if level_number_column.value.startswith('h'):
                text_en = str(level_number_column.value) + '. ' + str(
                    text_question_Column.value)
            else:
                level = len(level_number_column.value.split('.')) - 1

                text_en = 'h%s. %s' % (str(level),
                                       str(text_question_Column.value))

            dataType_column = None
            if type == self.CATEGORY:
                dataType_column = CommentPlaceholder()
            else:
                dataType_column = row[3]

            if row[7].value:
                slug = row[7].value
            else:
                slug = convert_text_to_slug(str(row[1].value)[:50])

                slug = self.get_slug(slug, questionnaire)

            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

            #If has dependencies
            if row[8].value:
                try:
                    dependencies_list = row[8]
                    list_dep_aux = dependencies_list.value.split('|')
                    question_num_parent = None
                    try:
                        question_num_parent = _questions_rows.get(
                            list_dep_aux[0]).number
                    except AttributeError:
                        ''' If this is a merge, the dependant question can already be on the questionset,
                            lets try looking for it
                        '''
                        try:
                            question = Question.objects.get(
                                slug_fk__slug1=list_dep_aux[0],
                                questionset=questionset)
                            _questions_rows[
                                list_dep_aux[0]] = question_num_parent

                            question_num_parent = question.number

                            _choices_array[
                                list_dep_aux[0]] = self.__getChoices(question)

                        except Question.DoesNotExist:
                            raise Exception(
                                'The dependant with slug %s does not exist.' %
                                (list_dep_aux[0]))

                    index_aux = int(str(list_dep_aux[1])) - 1
                    choice_parent_list = _choices_array.get(list_dep_aux[0])
                    choice_parent = choice_parent_list[index_aux]
                    _checks = 'dependent=\"%s,%s\"' % (
                        str(question_num_parent), str(choice_parent))
                except:
                    raise

            try:
                questionNumber = self.__handleQuestionNumber(
                    level_number_column.value, qNumber, questionset)

            except:
                if type == self.QUESTION:
                    log += "\n%s - Error to create question number %s" % (
                        type_Column.row, text_en)
                elif type == self.CATEGORY:
                    log += "\n%s - Error to create Category number %s" % (
                        type_Column.row, text_en)

                self.writeLog(log)
                raise

            #print slug
            #Create or load slug
            slugs = Slugs.objects.filter(slug1=slug, description=text_en)
            if len(slugs) <= 0:
                slug_db = Slugs(slug1=slug, description=text_en)
                slug_db.save()
            else:
                slug_db = slugs[0]

            visible_default = False
            if row[10].value:
                if str(row[10].value).lower() == 'visible':
                    visible_default = True

            is_category = None
            is_stats = None

            if type == self.QUESTION:
                is_stats = True
                is_category = False
            elif type == self.CATEGORY:
                is_stats = False
                is_category = True

            try:
                question = Question.objects.get(slug_fk__slug1=slug_db.slug1,
                                                questionset=questionset)

                question.text_en = text_en
                question.number = str(questionNumber)
                question.type = dataType_column.value
                question.help_text = helpText
                question.stats = is_stats
                question.category = is_category
                question.tooltip = _tooltip
                question.checks = _checks
                question.visible_default = visible_default

            except Question.DoesNotExist:
                question = Question(questionset=questionset,
                                    text_en=text_en,
                                    number=str(questionNumber),
                                    type=dataType_column.value,
                                    help_text=helpText,
                                    slug=slug,
                                    slug_fk=slug_db,
                                    stats=is_stats,
                                    category=is_category,
                                    tooltip=_tooltip,
                                    checks=_checks,
                                    visible_default=visible_default,
                                    disposition=self.__processDisposition(
                                        row[11].value.lower()))

            if dataType_column.value in ['open-validated']:
                ardict = {}

                if row[4].value:
                    # some basic types dont need regex
                    known_validations = {
                        "integer": "[+-]?\d+",
                        "decimal": "[+-]?\d*([.]\d*)?",
                        "scientific": "[+-]?\d*([.]\d*)?e[+-]?\d*([.]\d*)?",
                        "range": "[+\-]?\d*([.]\d*);[+\-]?\d*([.]\d*)",
                        "date": "\d{2}/\d{2}/\d{4}",
                        "time": "\d{2}:\d{2}:\d{2}",
                        "datetime": "\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}",
                        "text": ".*"
                    }
                    try:
                        ardict['regex'] = known_validations[row[4].value]
                        ardict['base'] = row[4].value
                    except KeyError:
                        # If this is not known, try to validate it as a regex
                        try:
                            re.compile(row[4].value)
                            ardict['regex'] = row[4].value
                        except re.error:
                            raise Exception(
                                "--ERROR: The regex on row %d, column 4 is not valid"
                                % (type_Column.row))

                if row[5].value:
                    split = row[5].value.split('|')
                    lensplit = len(split)

                    if lensplit == 1:
                        ardict['unit'] = split[0]
                        question.help_text = ""

                    elif lensplit == 2:
                        ardict['unit'] = split[0]
                        ardict['unit_desc'] = split[1]
                        question.help_text = ""

                    elif lensplit == 3:
                        ardict['unit'] = split[0]
                        ardict['unit_desc'] = split[1]
                        question.help_text = split[2]

                    else:
                        raise Exception(
                            "-- ERROR: Invalid number of segments on help text row %d, column 5. Max syntax is unit|desc|help_text"
                            % (type_Column.row))

                question.metadata = json.dumps(ardict)

            if not _debug:
                question.save()

                if type == self.QUESTION:
                    log += '\n%s - Question created %s ' % (type_Column.row,
                                                            question)
                elif type == self.CATEGORY:
                    log += '\n%s - Category created %s ' % (type_Column.row,
                                                            question)

            _questions_rows[slug] = question

            if type == self.QUESTION:
                if dataType_column.value in [
                        'choice', 'choice-freeform', 'choice-multiple',
                        'choice-multiple-freeform'
                ]:
                    _choices_array_aux = []
                    # Parse of values list
                    values_list = row[4]
                    if (values_list != None and values_list.value != None):
                        list_aux = values_list.value.split('|')

                        _choices_array[slug] = self.__processChoices(
                            type_Column.row,
                            question,
                            list_aux,
                            log,
                            debug=_debug,
                            mode=mode,
                            match_percentage=percentage,
                            infer_function=infer_function)

                if dataType_column.value in [
                        'choice-yesno', 'choice-yesnodontknow'
                ]:
                    _choices_array[slug] = ['yes', 'no', 'dontknow']

        except:
            log += "\n%s - Error to save question %s" % (type_Column.row,
                                                         text_en)

            self.writeLog(log)
            raise
예제 #4
0
def save_slug(slugName, desc):
    slugsAux = Slugs()
    slugsAux.slug1 = slugName
    slugsAux.description = desc
    slugsAux.save()
예제 #5
0
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from questionnaire.models import Question
from searchengine.models import Slugs
import sys

print '\nbegin copying slugs to searchengine.Slugs ...\n'

for question in Question.objects.all():
    print 'adding slug ' + question.slug
    if not question.slug_fk:
        slugs = Slugs.objects.filter(slug1=question.slug,
                                     description=question.text)
        if len(slugs) > 0:
            question.slug_fk = slugs[0]
            question.save()
        else:
            s1 = Slugs(slug1=question.slug, description=question.text)
            s1.save()
            question.slug_fk = s1
            question.save()

print '\nend!'
예제 #6
0
파일: views.py 프로젝트: bastiao/catalogue
def save_slug(slugName, desc):
    slugsAux = Slugs()
    slugsAux.slug1 = slugName
    slugsAux.description = desc
    slugsAux.save()
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from questionnaire.models import Question
from searchengine.models import Slugs
import sys

print '\nbegin copying slugs to searchengine.Slugs ...\n'

for question in Question.objects.all():
	print 'adding slug ' + question.slug
	if not question.slug_fk:
		slugs = Slugs.objects.filter(slug1=question.slug, description=question.text)
		if len(slugs) > 0:
			question.slug_fk = slugs[0]
			question.save()
		else:
			s1 = Slugs(slug1=question.slug, description=question.text)
			s1.save()
			question.slug_fk = s1
			question.save()

print '\nend!'
예제 #8
0
    def __handleQuestion(
        self,
        type,
        row,
        type_Column,
        level_number_column,
        text_question_Column,
        _questions_rows,
        _choices_array,
        qNumber,
        questionset,
        log,
        _checks,
        _debug,
        questionnaire,
        mode=EXACT_MATCH,
        percentage=0.75,
        infer_function=None,
    ):
        try:
            slug = None
            text_en = None
            if level_number_column.value.startswith("h"):
                text_en = str(level_number_column.value) + ". " + str(text_question_Column.value)
            else:
                level = len(level_number_column.value.split(".")) - 1

                text_en = "h%s. %s" % (str(level), str(text_question_Column.value))

            dataType_column = None
            if type == self.CATEGORY:
                dataType_column = CommentPlaceholder()
            else:
                dataType_column = row[3]

            if row[7].value:
                slug = row[7].value
            else:
                slug = convert_text_to_slug(str(row[1].value)[:50])

                slug = self.get_slug(slug, questionnaire)

            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

            # If has dependencies
            if row[8].value:
                try:
                    dependencies_list = row[8]
                    list_dep_aux = dependencies_list.value.split("|")
                    question_num_parent = None
                    try:
                        question_num_parent = _questions_rows.get(list_dep_aux[0]).number
                    except AttributeError:
                        """ If this is a merge, the dependant question can already be on the questionset,
                            lets try looking for it
                        """
                        try:
                            question = Question.objects.get(slug_fk__slug1=list_dep_aux[0], questionset=questionset)
                            _questions_rows[list_dep_aux[0]] = question_num_parent

                            question_num_parent = question.number

                            _choices_array[list_dep_aux[0]] = self.__getChoices(question)

                        except Question.DoesNotExist:
                            raise Exception("The dependant with slug %s does not exist." % (list_dep_aux[0]))

                    index_aux = int(str(list_dep_aux[1])) - 1
                    choice_parent_list = _choices_array.get(list_dep_aux[0])
                    choice_parent = choice_parent_list[index_aux]
                    _checks = 'dependent="%s,%s"' % (str(question_num_parent), str(choice_parent))
                except:
                    raise

            try:
                questionNumber = self.__handleQuestionNumber(level_number_column.value, qNumber, questionset)

            except:
                if type == self.QUESTION:
                    log += "\n%s - Error to create question number %s" % (type_Column.row, text_en)
                elif type == self.CATEGORY:
                    log += "\n%s - Error to create Category number %s" % (type_Column.row, text_en)

                self.writeLog(log)
                raise

            # print slug
            # Create or load slug
            slugs = Slugs.objects.filter(slug1=slug, description=text_en)
            if len(slugs) <= 0:
                slug_db = Slugs(slug1=slug, description=text_en)
                slug_db.save()
            else:
                slug_db = slugs[0]

            visible_default = False
            if row[10].value:
                if str(row[10].value).lower() == "visible":
                    visible_default = True

            is_category = None
            is_stats = None

            if type == self.QUESTION:
                is_stats = True
                is_category = False
            elif type == self.CATEGORY:
                is_stats = False
                is_category = True

            try:
                question = Question.objects.get(slug_fk__slug1=slug_db.slug1, questionset=questionset)

                question.text_en = text_en
                question.number = str(questionNumber)
                question.type = dataType_column.value
                question.help_text = helpText
                question.stats = is_stats
                question.category = is_category
                question.tooltip = _tooltip
                question.checks = _checks
                question.visible_default = visible_default

            except Question.DoesNotExist:
                question = Question(
                    questionset=questionset,
                    text_en=text_en,
                    number=str(questionNumber),
                    type=dataType_column.value,
                    help_text=helpText,
                    slug=slug,
                    slug_fk=slug_db,
                    stats=is_stats,
                    category=is_category,
                    tooltip=_tooltip,
                    checks=_checks,
                    visible_default=visible_default,
                    disposition=self.__processDisposition(row[11].value.lower()),
                )

            if dataType_column.value in ["open-validated"]:
                ardict = {}

                if row[4].value:
                    # some basic types dont need regex
                    known_validations = {
                        "integer": "[+-]?\d+",
                        "decimal": "[+-]?\d*([.]\d*)?",
                        "scientific": "[+-]?\d*([.]\d*)?e[+-]?\d*([.]\d*)?",
                        "range": "[+\-]?\d*([.]\d*);[+\-]?\d*([.]\d*)",
                        "date": "\d{2}/\d{2}/\d{4}",
                        "time": "\d{2}:\d{2}:\d{2}",
                        "datetime": "\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}",
                        "text": ".*",
                    }
                    try:
                        ardict["regex"] = known_validations[row[4].value]
                        ardict["base"] = row[4].value
                    except KeyError:
                        # If this is not known, try to validate it as a regex
                        try:
                            re.compile(row[4].value)
                            ardict["regex"] = row[4].value
                        except re.error:
                            raise Exception("--ERROR: The regex on row %d, column 4 is not valid" % (type_Column.row))

                if row[5].value:
                    split = row[5].value.split("|")
                    lensplit = len(split)

                    if lensplit == 1:
                        ardict["unit"] = split[0]
                        question.help_text = ""

                    elif lensplit == 2:
                        ardict["unit"] = split[0]
                        ardict["unit_desc"] = split[1]
                        question.help_text = ""

                    elif lensplit == 3:
                        ardict["unit"] = split[0]
                        ardict["unit_desc"] = split[1]
                        question.help_text = split[2]

                    else:
                        raise Exception(
                            "-- ERROR: Invalid number of segments on help text row %d, column 5. Max syntax is unit|desc|help_text"
                            % (type_Column.row)
                        )

                question.metadata = json.dumps(ardict)

            if not _debug:
                question.save()

                if type == self.QUESTION:
                    log += "\n%s - Question created %s " % (type_Column.row, question)
                elif type == self.CATEGORY:
                    log += "\n%s - Category created %s " % (type_Column.row, question)

            _questions_rows[slug] = question

            if type == self.QUESTION:
                if dataType_column.value in [
                    "choice",
                    "choice-freeform",
                    "choice-multiple",
                    "choice-multiple-freeform",
                ]:
                    _choices_array_aux = []
                    # Parse of values list
                    values_list = row[4]
                    if values_list != None and values_list.value != None:
                        list_aux = values_list.value.split("|")

                        _choices_array[slug] = self.__processChoices(
                            type_Column.row,
                            question,
                            list_aux,
                            log,
                            debug=_debug,
                            mode=mode,
                            match_percentage=percentage,
                            infer_function=infer_function,
                        )

                if dataType_column.value in ["choice-yesno", "choice-yesnocomment", "choice-yesnodontknow"]:
                    _choices_array[slug] = ["yes", "no", "dontknow"]

        except:
            log += "\n%s - Error to save question %s" % (type_Column.row, text_en)

            self.writeLog(log)
            raise