Example #1
0
 def _sort_sections(self):
     sections = {section.id: section for section in self.survey.sections}
     section_ids = map(int, request.form.getlist('section_ids'))
     for position, section_id in enumerate(section_ids, 1):
         sections[section_id].position = position
     logger.info('Sections in %s reordered by %s', self.survey,
                 session.user)
Example #2
0
 def _sort_items(self):
     section = SurveySection.find_one(survey=self.survey,
                                      id=request.form['section_id'],
                                      _eager=SurveySection.children)
     section_items = {x.id: x for x in section.children}
     item_ids = map(int, request.form.getlist('item_ids'))
     changed_section = None
     for position, item_id in enumerate(item_ids, 1):
         try:
             section_items[item_id].position = position
         except KeyError:
             # item is not in section, was probably moved
             item = SurveyItem.find_one(
                 SurveyItem.survey == self.survey,
                 SurveyItem.id == item_id,
                 SurveyItem.type != SurveyItemType.section,
                 _eager=SurveyItem.parent)
             changed_section = item.parent
             item.position = position
             item.parent = section
             logger.info('Item %s moved to section %s by %s', item, section,
                         session.user)
     logger.info('Items in %s reordered by %s', section, session.user)
     if changed_section is not None:
         for position, item in enumerate(changed_section.children, 1):
             item.position = position
Example #3
0
 def _process(self):
     db.session.delete(self.text)
     db.session.flush()
     flash(_('Text item deleted'), 'success')
     logger.info('Survey question %s deleted by %s', self.text,
                 session.user)
     return jsonify_data(
         questionnaire=_render_questionnaire_preview(self.text.survey))
 def _process(self):
     if self.survey.state == SurveyState.finished:
         self.survey.end_dt = None
         self.survey.start_notification_sent = False
     else:
         self.survey.open()
     self.survey.send_start_notification()
     flash(_("Survey is now open"), 'success')
     logger.info("Survey %s opened by %s", self.survey, session.user)
     return redirect(url_for('.manage_survey', self.survey))
Example #5
0
 def _process(self):
     db.session.delete(self.question)
     db.session.flush()
     flash(
         _('Question "{title}" deleted').format(title=self.question.title),
         'success')
     logger.info('Survey question %s deleted by %s', self.question,
                 session.user)
     return jsonify_data(
         questionnaire=_render_questionnaire_preview(self.survey))
 def send_submission_notification(self, submission):
     if not self.notifications_enabled:
         return
     template_module = get_template_module(
         'events/surveys/emails/new_submission_email.txt',
         submission=submission)
     email = make_email(bcc_list=self.new_submission_emails,
                        template=template_module)
     send_email(email, event=self.event, module='Surveys')
     logger.info('Sending submission notification for survey %s', self)
 def _process(self):
     submission_ids = set(map(int, request.form.getlist('submission_ids')))
     for submission in self.survey.submissions[:]:
         if submission.id in submission_ids:
             self.survey.submissions.remove(submission)
             logger.info('Submission %s deleted from survey %s', submission, self.survey)
             self.event.log(EventLogRealm.management, EventLogKind.negative, 'Surveys',
                            'Submission removed from survey "{}"'.format(self.survey.title),
                            data={'Submitter': submission.user.full_name
                                  if not submission.is_anonymous else 'Anonymous'})
     return jsonify(success=True)
 def send_start_notification(self):
     if not self.notifications_enabled or self.start_notification_sent or not self.event.has_feature(
             'surveys'):
         return
     template_module = get_template_module(
         'events/surveys/emails/start_notification_email.txt', survey=self)
     email = make_email(bcc_list=self.start_notification_recipients,
                        template=template_module)
     send_email(email, event=self.event, module='Surveys')
     logger.info('Sending start notification for survey %s', self)
     self.start_notification_sent = True
 def _process(self):
     form = SurveyForm(event=self.event, obj=self._get_form_defaults())
     if form.validate_on_submit():
         form.populate_obj(self.survey)
         db.session.flush()
         flash(_('Survey modified'), 'success')
         logger.info('Survey %s modified by %s', self.survey, session.user)
         return jsonify_data(flash=False)
     return jsonify_template('events/surveys/management/edit_survey.html',
                             event=self.event,
                             form=form,
                             survey=self.survey)
Example #10
0
 def _process(self):
     form = TextForm(obj=FormDefaults(self.text))
     if form.validate_on_submit():
         form.populate_obj(self.text)
         db.session.flush()
         flash(_('Text item updated'), 'success')
         logger.info('Survey text item %s modified by %s', self.text,
                     session.user)
         return jsonify_data(
             questionnaire=_render_questionnaire_preview(self.survey))
     return jsonify_template('forms/form_common_fields_first.html',
                             form=form)
Example #11
0
 def _process(self):
     db.session.delete(self.section)
     db.session.flush()
     if self.section.title:
         message = _('Section "{title}" deleted').format(
             title=self.section.title)
     else:
         message = _('Standalone section deleted')
     flash(message, 'success')
     logger.info('Survey section %s deleted by %s', self.section,
                 session.user)
     return jsonify_data(
         questionnaire=_render_questionnaire_preview(self.survey))
def add_survey_section(survey, data):
    """Add a section to a survey.

    :param survey: The `Survey` to which the section will be added.
    :param data: Attributes of the new `SurveySection`.
    :return: The added `SurveySection`.
    """
    section = SurveySection(survey=survey)
    section.populate_from_dict(data)
    db.session.add(section)
    db.session.flush()
    logger.info('Survey section %s added by %s', section, session.user)
    return section
def add_survey_text(section, data):
    """Add a text item to a survey.

    :param section: The `SurveySection` to which the question will be added.
    :param data: The `TextForm.data` to populate the question with.
    :return: The added `SurveyText`.
    """
    text = SurveyText()
    text.populate_from_dict(data)
    section.children.append(text)
    db.session.flush()
    logger.info('Survey text item %s added by %s', text, session.user)
    return text
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
Example #15
0
 def _process(self):
     form = self.question.field.create_config_form(
         obj=FormDefaults(self.question, **self.question.field_data))
     if form.validate_on_submit():
         old_title = self.question.title
         self.question.field.update_object(form.data)
         db.session.flush()
         flash(
             _('Question "{title}" updated').format(title=old_title),
             'success')
         logger.info('Survey question %s modified by %s', self.question,
                     session.user)
         return jsonify_data(
             questionnaire=_render_questionnaire_preview(self.survey))
     return jsonify_template('forms/form_common_fields_first.html',
                             form=form)
Example #16
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)
Example #17
0
 def _process(self):
     form = ImportQuestionnaireForm()
     if form.validate_on_submit():
         try:
             data = json.load(form.json_file.data.stream)
             self._import_data(data)
         except ValueError as exception:
             logger.info('%s tried to import an invalid JSON file: %s',
                         session.user, exception.message)
             flash(_("Invalid file selected."), 'error')
         else:
             flash(_("The questionnaire has been imported."), 'success')
             logger.info('Questionnaire imported from JSON document by %s',
                         session.user)
         return jsonify_data(
             questionnaire=_render_questionnaire_preview(self.survey))
     return jsonify_form(
         form, form_header_kwargs={'action': request.relative_url})
Example #18
0
 def _process(self):
     form = SectionForm(obj=FormDefaults(self.section))
     if form.validate_on_submit():
         old_title = self.section.title
         form.populate_obj(self.section)
         db.session.flush()
         if old_title:
             message = _('Section "{title}" updated').format(
                 title=old_title)
         else:
             message = _('Standalone section updated')
         flash(message, 'success')
         logger.info('Survey section %s modified by %s', self.section,
                     session.user)
         return jsonify_data(
             questionnaire=_render_questionnaire_preview(self.survey))
     return jsonify_template('forms/form_common_fields_first.html',
                             form=form)
Example #19
0
 def _process(self):
     allow_reschedule_start = self.survey.state in (
         SurveyState.ready_to_open, SurveyState.active_and_clean,
         SurveyState.finished)
     form = ScheduleSurveyForm(
         obj=self._get_form_defaults(),
         survey=self.survey,
         allow_reschedule_start=allow_reschedule_start)
     if form.validate_on_submit():
         if allow_reschedule_start:
             self.survey.start_dt = form.start_dt.data
             if getattr(form, 'resend_start_notification', False):
                 self.survey.start_notification_sent = not form.resend_start_notification.data
         self.survey.end_dt = form.end_dt.data
         flash(_('Survey was scheduled'), 'success')
         logger.info('Survey %s scheduled by %s', self.survey, session.user)
         return jsonify_data(flash=False)
     disabled_fields = ('start_dt', ) if not allow_reschedule_start else ()
     return jsonify_form(form,
                         submit=_('Schedule'),
                         disabled_fields=disabled_fields)
Example #20
0
 def _process(self):
     self.survey.is_deleted = True
     flash(_('Survey deleted'), 'success')
     logger.info('Survey %s deleted by %s', self.survey, session.user)
     return redirect(url_for('.manage_survey_list', self.event))
Example #21
0
 def _process(self):
     self.survey.close()
     flash(_("Survey is now closed"), 'success')
     logger.info("Survey %s closed by %s", self.survey, session.user)
     return redirect(url_for('.manage_survey', self.survey))