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)
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 _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
def _process_args(self): RHManageSurveysBase._process_args(self) self.section = SurveySection.find_one( SurveySection.id == request.view_args['section_id'], ~Survey.is_deleted, _join=SurveySection.survey, _eager=SurveySection.survey) self.survey = self.section.survey
def _checkParams(self, params): RHManageSurveysBase._checkParams(self, params) self.section = SurveySection.find_one( SurveySection.id == request.view_args['section_id'], ~Survey.is_deleted, _join=SurveySection.survey, _eager=SurveySection.survey) self.survey = self.section.survey
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
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
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)
def _process(self): form = SectionForm() if form.validate_on_submit(): section = SurveySection(survey=self.survey) form.populate_obj(section) db.session.add(section) db.session.flush() flash( _('Section "{title}" added').format(title=section.title), 'success') logger.info('Survey section {} added by {}'.format( section, session.user)) return jsonify_data( questionnaire=_render_questionnaire_preview(self.survey)) return jsonify_template( 'events/surveys/management/edit_survey_item.html', form=form)
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)
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 {} moved to section {} by {}'.format(item, section, session.user)) logger.info('Items in {} reordered by {}'.format(section, session.user)) if changed_section is not None: for position, item in enumerate(changed_section.children, 1): item.position = position
def _checkParams(self, params): RHManageSurveysBase._checkParams(self, params) self.section = SurveySection.find_one(SurveySection.id == request.view_args['section_id'], ~Survey.is_deleted, _join=SurveySection.survey, _eager=SurveySection.survey) self.survey = self.section.survey
def _process_args(self): RHManageSurveysBase._process_args(self) self.section = SurveySection.find_one(SurveySection.id == request.view_args['section_id'], ~Survey.is_deleted, _join=SurveySection.survey, _eager=SurveySection.survey) self.survey = self.section.survey