def migrate_question(self, old_question, position): question = SurveyQuestion() question.position = position question.title = sanitize_user_input(old_question.questionValue) question.description = sanitize_user_input(old_question.description) if old_question.help: help_text = sanitize_user_input(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
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 migrate_answer(self, old_answer, question): answer = SurveyAnswer(data=JSON.NULL) if old_answer.__class__.__name__ == 'MultipleChoicesAnswer': answer.data = [] for option in old_answer._selectedChoiceItems: answer.data.append(self._get_option_id(question, option)) elif old_answer._question.__class__.__name__ in ('Radio', 'Select'): if old_answer._answerValue: answer.data = self._get_option_id(question, old_answer._answerValue) else: answer.data = sanitize_user_input(old_answer._answerValue) self.print_success(" - Answer: {}".format(answer.data)) return answer
def _migrate_menu(self, event, container, parent=None, used=None): if used is None: used = set() for pos, item in enumerate(container._listLink, 1): data = { 'parent': parent, 'event_new': event, 'is_enabled': item._active, 'position': pos } item_type = item.__class__.__name__ if item_type == 'SystemLink': if item._name in REMOVED_MENU_NAMES: continue name = MENU_ENTRY_NAME_MAP.get(item._name) if name is None: self.print_warning( 'Skipping unknown menu entry: {}'.format(name)) continue data['name'] = name if not parent and data['name'] in NOT_TOP_LEVEL_NAMES: self.print_warning( '%[yellow]Skipping top-level menu entry {}'.format( data['name'])) continue elif data['name'] in used: self.print_error( '%[red!]duplicate menu entry name {}; skipping'.format( data['name'])) continue used.add(data['name']) data['title'] = sanitize_user_input(item._caption) if not data['title']: data['title'] = None self.print_warning( '%[yellow!]Menu entry {} has no title; using default'. format(data['name'])) elif data['title'].lower() in DEFAULT_MENU_TITLES[ data['name']]: data['title'] = None if item._name == 'chatrooms': data['plugin'] = 'chat' data['type'] = MenuEntryType.plugin_link else: data['type'] = MenuEntryType.internal_link elif item_type == 'Spacer': data['type'] = MenuEntryType.separator elif item_type == 'ExternLink': data['type'] = MenuEntryType.user_link data['title'] = sanitize_user_input(item._caption) data['link_url'] = item._URL.strip() if not data['link_url']: if getattr(item, '_listLink', None): self.print_warning( '%[yellow!]Link "{}" has no URL but children'. format(data['title'])) else: self.print_warning( '%[yellow]Skipping link "{}" with no URL'.format( data['title'])) continue if not data['title']: if getattr(item, '_listLink', None): self.print_warning( '%[yellow!]Link has no title but children') else: self.print_warning( '%[yellow]Skipping link with no title') continue elif item_type == 'PageLink': data['type'] = MenuEntryType.page data['title'] = sanitize_user_input(item._caption) data['page'] = EventPage(event_new=event, html=item._page._content) data['page'].legacy_mapping = LegacyPageMapping( event_id=event.id, legacy_page_id=item._page._id) if item._page._isHome: event.default_page = data['page'] else: self.print_error( 'Unexpected menu item type: {}'.format(item_type)) continue entry = MenuEntry(**data) yield entry if getattr(item, '_listLink', None): # child entries if not parent: for sub_entry in self._migrate_menu( event, item, entry, used): yield sub_entry else: self.print_warning('Skipping children inside nested entry')