Beispiel #1
0
 def validate_title(self, field):
     query = Survey.find(Survey.event_id == self.event.id,
                         db.func.lower(Survey.title) == field.data.lower(),
                         Survey.title != field.object_data,
                         ~Survey.is_deleted)
     if query.count():
         raise ValidationError(_("There is already an survey named \"{}\" on this event".format(escape(field.data))))
Beispiel #2
0
def _get_active_surveys(event):
    if not event.has_feature('surveys'):
        return []
    from indico.modules.events.surveys.models.surveys import Survey
    return (Survey.find(Survey.is_active, Survey.event_id == int(event.id))
                  .order_by(db.func.lower(Survey.title))
                  .all())
Beispiel #3
0
def _get_active_surveys(event):
    if not event.has_feature('surveys'):
        return []
    from indico.modules.events.surveys.models.surveys import Survey
    return (Survey.find(Survey.is_active,
                        Survey.event_id == int(event.id)).order_by(
                            db.func.lower(Survey.title)).all())
Beispiel #4
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)
Beispiel #5
0
 def validate_title(self, field):
     query = Survey.find(Survey.event_id == self.event.id,
                         db.func.lower(Survey.title) == field.data.lower(),
                         Survey.title != field.object_data,
                         ~Survey.is_deleted)
     if query.count():
         raise ValidationError(_("There is already an survey named \"{}\" on this event".format(escape(field.data))))
Beispiel #6
0
def send_start_notifications():
    active_surveys = Survey.find_all(Survey.is_active, ~Survey.start_notification_sent, Survey.notifications_enabled)
    try:
        for survey in active_surveys:
            survey.send_start_notification()
    finally:
        db.session.commit()
Beispiel #7
0
def _render_questionnaire_preview(survey):
    # load the survey once again with all the necessary data
    survey = (Survey.find(id=survey.id).options(
        joinedload(Survey.sections).joinedload(SurveySection.children)).one())
    tpl = get_template_module(
        'events/surveys/management/_questionnaire_preview.html')
    form = make_survey_form(survey)()
    return tpl.render_questionnaire_preview(survey, form, get_field_types())
Beispiel #8
0
 def _process(self):
     surveys = Survey.find(event_id=self.event.id,
                           is_deleted=False).order_by(
                               db.func.lower(Survey.title)).all()
     return WPManageSurvey.render_template('management/survey_list.html',
                                           self.event,
                                           event=self.event,
                                           surveys=surveys)
Beispiel #9
0
 def _clone_surveys(self, new_event):
     survey_attrs = get_simple_column_attrs(Survey) - {'uuid', 'start_dt', 'end_dt', '_last_friendly_submission_id'}
     for old_survey in self.old_event.surveys:
         if old_survey.is_deleted:
             continue
         survey = Survey()
         survey.populate_from_attrs(old_survey, survey_attrs)
         item_map = {}
         for old_item in old_survey.items:
             item = self._clone_item(survey, old_item)
             if old_item.parent:
                 assert old_item.parent != old_item
                 try:
                     item.parent = item_map[old_item.parent]
                 except KeyError:
                     item.parent = item_map[old_item.parent] = self._clone_item(survey, old_item.parent)
             item_map[old_item] = item
         new_event.surveys.append(survey)
Beispiel #10
0
    def _process(self):
        surveys = Survey.find_all(Survey.is_visible, Survey.event_id == int(self.event.id),
                                  _eager=(Survey.questions, Survey.submissions))
        if _can_redirect_to_single_survey(surveys):
            return redirect(url_for('.display_survey_form', surveys[0]))

        return self.view_class.render_template('display/survey_list.html', self.event, surveys=surveys,
                                               event=self.event, states=SurveyState,
                                               was_survey_submitted=was_survey_submitted)
Beispiel #11
0
    def _process(self):
        surveys = Survey.find_all(Survey.is_visible, Survey.event_id == int(self.event.id),
                                  _eager=(Survey.questions, Survey.submissions))
        if _can_redirect_to_single_survey(surveys):
            return redirect(url_for('.display_survey_form', surveys[0]))

        return self.view_class.render_template('display/survey_list.html', self.event, surveys=surveys,
                                               event=self.event, states=SurveyState,
                                               was_survey_submitted=was_survey_submitted)
Beispiel #12
0
    def _checkParams(self, params):
        RHSurveyBaseDisplay._checkParams(self, params)
        self.survey = (Survey.find(
            Survey.id == request.view_args['survey_id'],
            Survey.is_visible).options(joinedload(Survey.submissions)).options(
                joinedload(Survey.sections).joinedload(
                    SurveySection.children)).one())

        if not self.survey.is_active:
            flash(_('This survey is not active'), 'error')
            return redirect(url_for('.display_survey_list', self.event))
        elif was_survey_submitted(self.survey):
            flash(_('You have already answered this survey'), 'error')
            return redirect(url_for('.display_survey_list', self.event))
Beispiel #13
0
 def _process(self):
     form = SurveyForm(obj=FormDefaults(require_user=True), event=self.event)
     if form.validate_on_submit():
         survey = Survey(event=self.event)
         # add a default section so people can start adding questions right away
         survey.items.append(SurveySection(display_as_section=False))
         form.populate_obj(survey)
         db.session.add(survey)
         db.session.flush()
         flash(_('Survey created'), 'success')
         logger.info('Survey %s created by %s', survey, session.user)
         return jsonify_data(flash=False)
     return jsonify_template('events/surveys/management/edit_survey.html', event=self.event, form=form,
                             survey=None)
Beispiel #14
0
 def _process(self):
     form = SurveyForm(obj=FormDefaults(require_user=True),
                       event=self.event)
     if form.validate_on_submit():
         survey = Survey(event_new=self.event.as_event)
         # add a default section so people can start adding questions right away
         survey.items.append(SurveySection(display_as_section=False))
         form.populate_obj(survey)
         db.session.add(survey)
         db.session.flush()
         flash(_('Survey created'), 'success')
         logger.info('Survey {} created by {}'.format(survey, session.user))
         return redirect(url_for('.manage_survey', survey))
     return WPManageSurvey.render_template('management/edit_survey.html',
                                           self.event,
                                           event=self.event,
                                           form=form,
                                           survey=None)
Beispiel #15
0
    def _process(self):
        form = make_survey_form(self.survey)()
        if form.validate_on_submit():
            submission = self._save_answers(form)
            save_submitted_survey_to_session(submission)
            self.survey.send_submission_notification(submission)
            flash(_('Your answers has been saved'), 'success')
            return redirect(url_for('.display_survey_list', self.event))

        surveys = Survey.find_all(Survey.is_visible, Survey.event_id == int(self.event.id))
        if not _can_redirect_to_single_survey(surveys):
            back_button_endpoint = '.display_survey_list'
        elif self.event.getType() != 'conference':
            back_button_endpoint = 'event.conferenceDisplay'
        else:
            back_button_endpoint = None
        return self.view_class.render_template('display/survey_questionnaire.html', self.event, form=form,
                                               event=self.event, survey=self.survey,
                                               back_button_endpoint=back_button_endpoint)
Beispiel #16
0
    def _process(self):
        form = make_survey_form(self.survey)()
        if form.validate_on_submit():
            submission = self._save_answers(form)
            save_submitted_survey_to_session(submission)
            self.survey.send_submission_notification(submission)
            flash(_('Your answers has been saved'), 'success')
            return redirect(url_for('.display_survey_list', self.event))

        surveys = Survey.find_all(Survey.is_visible, Survey.event_id == int(self.event.id))
        if not _can_redirect_to_single_survey(surveys):
            back_button_endpoint = '.display_survey_list'
        elif self.event.getType() != 'conference':
            back_button_endpoint = 'event.conferenceDisplay'
        else:
            back_button_endpoint = None
        return self.view_class.render_template('display/survey_questionnaire.html', self.event, form=form,
                                               event=self.event, survey=self.survey,
                                               back_button_endpoint=back_button_endpoint)
Beispiel #17
0
 def _process_args(self):
     RHManageSurveysBase._process_args(self)
     self.survey = Survey.find_one(id=request.view_args['survey_id'], is_deleted=False)
Beispiel #18
0
 def _visible(event):
     return (event.has_feature('surveys') and bool(
         Survey.find(Survey.is_visible, Survey.event_id == int(
             event.id)).count()))
Beispiel #19
0
 def _process(self):
     surveys = Survey.find(event_id=self.event.id, is_deleted=False).order_by(db.func.lower(Survey.title)).all()
     return WPManageSurvey.render_template('management/survey_list.html',
                                           self.event, event=self.event, surveys=surveys)
Beispiel #20
0
 def has_data(self):
     return Survey.has_rows()
Beispiel #21
0
 def _checkParams(self, params):
     RHManageSurveysBase._checkParams(self, params)
     self.survey = Survey.find_one(id=request.view_args['survey_id'],
                                   is_deleted=False)
Beispiel #22
0
    def migrate_survey(self, evaluation):
        survey = Survey(event_new=self.event)
        title = convert_to_unicode(evaluation.title)
        if title and not title.startswith('Evaluation for '):
            survey.title = sanitize_user_input(title)
        if not survey.title:
            survey.title = "Evaluation"
        survey.introduction = sanitize_user_input(evaluation.announcement)
        if evaluation.contactInfo:
            contact_text = "Contact: ".format(
                sanitize_user_input(evaluation.contactInfo))
            survey.introduction += "\n\n{}".format(
                contact_text) if survey.introduction else contact_text
        survey.submission_limit = evaluation.submissionsLimit if evaluation.submissionsLimit else None
        survey.anonymous = evaluation.anonymous
        # Require the user to login if the survey is not anonymous or if logging in was required before
        survey.require_user = not survey.anonymous or evaluation.mandatoryAccount

        if evaluation.startDate.date() == date.min or evaluation.endDate.date(
        ) == date.min:
            survey.start_dt = self.event.end_dt
            survey.end_dt = survey.start_dt + timedelta(days=7)
        else:
            survey.start_dt = self._naive_to_aware(evaluation.startDate)
            survey.end_dt = self._naive_to_aware(evaluation.endDate)
        if survey.end_dt < survey.start_dt:
            survey.end_dt = survey.end_dt + timedelta(days=7)

        for kind, notification in evaluation.notifications.iteritems():
            survey.notifications_enabled = True
            recipients = set(notification._toList) | set(notification._ccList)
            if kind == 'evaluationStartNotify':
                survey.start_notification_emails = list(recipients)
            elif kind == 'newSubmissionNotify':
                survey.new_submission_emails = list(recipients)

        self.print_success('%[cyan]{}%[reset]'.format(survey))

        question_map = {}
        section = SurveySection(survey=survey, display_as_section=False)
        for position, old_question in enumerate(evaluation._questions):
            question = self.migrate_question(old_question, position)
            question_map[old_question] = question
            section.children.append(question)

        for i, old_submission in enumerate(evaluation._submissions, 1):
            submission = self.migrate_submission(old_submission, question_map,
                                                 i)
            survey.submissions.append(submission)
        survey._last_friendly_submission_id = len(survey.submissions)

        return survey
Beispiel #23
0
 def _process_args(self):
     RHManageSurveysBase._process_args(self)
     self.survey = Survey.find_one(id=request.view_args['survey_id'],
                                   is_deleted=False)
Beispiel #24
0
 def _visible(event):
     return (event.has_feature('surveys') and
             bool(Survey.find(Survey.is_visible, Survey.event_id == int(event.id)).count()))
Beispiel #25
0
 def has_data(self):
     return Survey.has_rows()
Beispiel #26
0
 def _checkParams(self, params):
     RHManageSurveysBase._checkParams(self, params)
     self.survey = Survey.find_one(id=request.view_args['survey_id'], is_deleted=False)
Beispiel #27
0
    def migrate_survey(self, evaluation, event):
        survey = Survey(event_id=int(event.id))
        if evaluation.title and not evaluation.title.startswith(
                'Evaluation for '):
            survey.title = _sanitize(evaluation.title)
        if not survey.title:
            survey.title = "Evaluation"
        survey.introduction = _sanitize(evaluation.announcement)
        if evaluation.contactInfo:
            contact_text = "Contact: ".format(_sanitize(
                evaluation.contactInfo))
            survey.introduction += "\n\n{}".format(
                contact_text) if survey.introduction else contact_text
        survey.submission_limit = evaluation.submissionsLimit if evaluation.submissionsLimit else None
        survey.anonymous = evaluation.anonymous
        # Require the user to login if the survey is not anonymous or if logging in was required before
        survey.require_user = not survey.anonymous or evaluation.mandatoryAccount

        if evaluation.startDate.date() == date.min or evaluation.endDate.date(
        ) == date.min:
            survey.start_dt = event.endDate
            survey.end_dt = survey.start_dt + timedelta(days=7)
        else:
            survey.start_dt = localize_as_utc(evaluation.startDate, event.tz)
            survey.end_dt = localize_as_utc(evaluation.endDate, event.tz)
        if survey.end_dt < survey.start_dt:
            survey.end_dt = survey.end_dt + timedelta(days=7)

        for kind, notification in evaluation.notifications.iteritems():
            survey.notifications_enabled = True
            recipients = set(notification._toList) | set(notification._ccList)
            if kind == 'evaluationStartNotify':
                survey.start_notification_emails = list(recipients)
            elif kind == 'newSubmissionNotify':
                survey.new_submission_emails = list(recipients)

        self.print_success(cformat('%{cyan}{}%{reset}').format(survey),
                           always=True,
                           event_id=event.id)

        question_map = {}
        section = SurveySection(survey=survey, display_as_section=False)
        for position, old_question in enumerate(evaluation._questions):
            question = self.migrate_question(old_question, position)
            question_map[old_question] = question
            section.children.append(question)

        for old_submission in evaluation._submissions:
            submission = self.migrate_submission(old_submission, question_map,
                                                 event.tz)
            survey.submissions.append(submission)

        return survey
Beispiel #28
0
    def migrate_survey(self, evaluation, event):
        survey = Survey(event_id=int(event.id))
        if evaluation.title and not evaluation.title.startswith('Evaluation for '):
            survey.title = _sanitize(evaluation.title)
        if not survey.title:
            survey.title = "Evaluation"
        survey.introduction = _sanitize(evaluation.announcement)
        if evaluation.contactInfo:
            contact_text = "Contact: ".format(_sanitize(evaluation.contactInfo))
            survey.introduction += "\n\n{}".format(contact_text) if survey.introduction else contact_text
        survey.submission_limit = evaluation.submissionsLimit if evaluation.submissionsLimit else None
        survey.anonymous = evaluation.anonymous
        # Require the user to login if the survey is not anonymous or if logging in was required before
        survey.require_user = not survey.anonymous or evaluation.mandatoryAccount

        if evaluation.startDate.date() == date.min or evaluation.endDate.date() == date.min:
            survey.start_dt = event.endDate
            survey.end_dt = survey.start_dt + timedelta(days=7)
        else:
            survey.start_dt = localize_as_utc(evaluation.startDate, event.tz)
            survey.end_dt = localize_as_utc(evaluation.endDate, event.tz)
        if survey.end_dt < survey.start_dt:
            survey.end_dt = survey.end_dt + timedelta(days=7)

        for kind, notification in evaluation.notifications.iteritems():
            survey.notifications_enabled = True
            recipients = set(notification._toList) | set(notification._ccList)
            if kind == 'evaluationStartNotify':
                survey.start_notification_emails = list(recipients)
            elif kind == 'newSubmissionNotify':
                survey.new_submission_emails = list(recipients)

        self.print_success(cformat('%{cyan}{}%{reset}').format(survey), always=True, event_id=event.id)

        question_map = {}
        section = SurveySection(survey=survey, display_as_section=False)
        for position, old_question in enumerate(evaluation._questions):
            question = self.migrate_question(old_question, position)
            question_map[old_question] = question
            section.children.append(question)

        for old_submission in evaluation._submissions:
            submission = self.migrate_submission(old_submission, question_map, event.tz)
            survey.submissions.append(submission)

        return survey