Ejemplo n.º 1
0
def test_survey_clone(db, create_event, dummy_event):
    survey = Survey(event=dummy_event, title='test')

    first_section = SurveySection(title='section 1',
                                  display_as_section=True,
                                  position=2)
    survey.items.append(first_section)
    first_section.children.append(
        SurveyQuestion(title='question in s1',
                       field_type='text',
                       is_required=True))

    second_section = SurveySection(title='section 2',
                                   display_as_section=True,
                                   position=1)
    survey.items.append(second_section)
    second_section.children.append(SurveyText(description='My text'))
    second_section.children.append(
        SurveyQuestion(title='What is your name?',
                       field_type='text',
                       is_required=False))

    db.session.flush()

    new_event = create_event()
    cloner = EventSurveyCloner(dummy_event)
    cloner.run(new_event, {}, {}, False)

    assert len(new_event.surveys) == 1
    assert len(new_event.surveys[0].items) == len(survey.items)
    for i, item in enumerate(new_event.surveys[0].items):
        for attr in get_simple_column_attrs(SurveyItem):
            assert getattr(item, attr) == getattr(survey.items[i], attr)
Ejemplo n.º 2
0
 def _process_args(self):
     RHManageSurveysBase._process_args(self)
     self.question = SurveyQuestion.find_one(
         SurveyQuestion.id == request.view_args['question_id'],
         ~Survey.is_deleted,
         _join=SurveyQuestion.survey,
         _eager=SurveyQuestion.survey)
     self.survey = self.question.survey
Ejemplo n.º 3
0
 def _checkParams(self, params):
     RHManageSurveysBase._checkParams(self, params)
     self.question = SurveyQuestion.find_one(
         SurveyQuestion.id == request.view_args['question_id'],
         ~Survey.is_deleted,
         _join=SurveyQuestion.survey,
         _eager=SurveyQuestion.survey)
     self.survey = self.question.survey
Ejemplo n.º 4
0
def add_survey_question(section, field_cls, data):
    """Add a question to a survey.

    :param section: The `SurveySection` to which the question will be added.
    :param field_cls: The field class of this question.
    :param data: The `FieldConfigForm.data` to populate the question with.
    :return: The added `SurveyQuestion`.
    """
    question = SurveyQuestion()
    field = field_cls(question)
    field.update_object(data)
    section.children.append(question)
    db.session.flush()
    logger.info('Survey question %s added by %s', question, session.user)
    return question
Ejemplo n.º 5
0
    def _process(self):
        try:
            field_cls = get_field_types()[request.view_args['type']]
        except KeyError:
            raise NotFound

        form = field_cls.config_form()
        if form.validate_on_submit():
            question = SurveyQuestion()
            field_cls(question).save_config(form)
            self.section.children.append(question)
            db.session.flush()
            flash(
                _('Question "{title}" added').format(title=question.title),
                'success')
            logger.info('Survey question {} added by {}'.format(
                question, session.user))
            return jsonify_data(
                questionnaire=_render_questionnaire_preview(self.survey))
        return jsonify_template(
            'events/surveys/management/edit_survey_item.html', form=form)
Ejemplo n.º 6
0
 def migrate_question(self, old_question, position):
     question = SurveyQuestion()
     question.position = position
     question.title = _sanitize(old_question.questionValue)
     question.description = _sanitize(old_question.description)
     if old_question.help:
         help_text = _sanitize(old_question.help)
         question.description += "\n\nHelp: {}".format(
             help_text) if question.description else help_text
     question.is_required = old_question.required
     question.field_data = {}
     class_name = old_question.__class__.__name__
     if class_name == 'Textbox':
         question.field_type = 'text'
     elif class_name == 'Textarea':
         question.field_type = 'text'
         question.field_data['multiline'] = True
     elif class_name == 'Password':
         question.field_type = 'text'
     elif class_name in ('Checkbox', 'Radio', 'Select'):
         question.field_data['options'] = []
         question.field_type = 'single_choice' if class_name in (
             'Radio', 'Select') else 'multiselect'
         if question.field_type == 'single_choice':
             question.field_data['display_type'] = class_name.lower()
         if class_name == 'Radio':
             question.field_data['radio_display_type'] = 'vertical'
         for option in old_question.choiceItems:
             question.field_data['options'].append({
                 'option': option,
                 'id': unicode(uuid4())
             })
     self.print_success(" - Question: {}".format(question.title))
     return question
Ejemplo n.º 7
0
 def _checkParams(self, params):
     RHManageSurveysBase._checkParams(self, params)
     self.question = SurveyQuestion.find_one(SurveyQuestion.id == request.view_args['question_id'],
                                             ~Survey.is_deleted,
                                             _join=SurveyQuestion.survey, _eager=SurveyQuestion.survey)
     self.survey = self.question.survey
Ejemplo n.º 8
0
 def migrate_question(self, old_question, position):
     question = SurveyQuestion()
     question.position = position
     question.title = _sanitize(old_question.questionValue)
     question.description = _sanitize(old_question.description)
     if old_question.help:
         help_text = _sanitize(old_question.help)
         question.description += "\n\nHelp: {}".format(help_text) if question.description else help_text
     question.is_required = old_question.required
     question.field_data = {}
     class_name = old_question.__class__.__name__
     if class_name == 'Textbox':
         question.field_type = 'text'
     elif class_name == 'Textarea':
         question.field_type = 'text'
         question.field_data['multiline'] = True
     elif class_name == 'Password':
         question.field_type = 'text'
     elif class_name in ('Checkbox', 'Radio', 'Select'):
         question.field_data['options'] = []
         question.field_type = 'single_choice' if class_name in ('Radio', 'Select') else 'multiselect'
         if question.field_type == 'single_choice':
             question.field_data['display_type'] = class_name.lower()
         if class_name == 'Radio':
             question.field_data['radio_display_type'] = 'vertical'
         for option in old_question.choiceItems:
             question.field_data['options'].append({'option': option, 'id': unicode(uuid4())})
     self.print_success(" - Question: {}".format(question.title))
     return question
Ejemplo n.º 9
0
 def migrate_question(self, old_question, position):
     question = SurveyQuestion()
     question.position = position
     question.title = _sanitize(old_question.questionValue)
     question.description = _sanitize(old_question.description)
     if old_question.help:
         help_text = _sanitize(old_question.help)
         question.description += "\n\nHelp: {}".format(help_text) if question.description else help_text
     question.is_required = old_question.required
     question.field_data = {}
     class_name = old_question.__class__.__name__
     if class_name == "Textbox":
         question.field_type = "text"
     elif class_name == "Textarea":
         question.field_type = "text"
         question.field_data["multiline"] = True
     elif class_name == "Password":
         question.field_type = "text"
     elif class_name in ("Checkbox", "Radio", "Select"):
         question.field_data["options"] = []
         question.field_type = "single_choice" if class_name in ("Radio", "Select") else "multiselect"
         if question.field_type == "single_choice":
             question.field_data["display_type"] = class_name.lower()
         if class_name == "Radio":
             question.field_data["radio_display_type"] = "vertical"
         for option in old_question.choiceItems:
             question.field_data["options"].append({"option": option, "id": unicode(uuid4())})
     self.print_success(" - Question: {}".format(question.title))
     return question
Ejemplo n.º 10
0
 def _process_args(self):
     RHManageSurveysBase._process_args(self)
     self.question = SurveyQuestion.find_one(SurveyQuestion.id == request.view_args['question_id'],
                                             ~Survey.is_deleted,
                                             _join=SurveyQuestion.survey, _eager=SurveyQuestion.survey)
     self.survey = self.question.survey