def manage_questions(self): manage_questions.need() post = self.request.POST if 'cancel' in self.request.POST: url = self.request.resource_url(self.context) return HTTPFound(location = url) if 'save' in post: self.process_question_ids(self.context) self.add_flash_message(_('Updated')) self.response['organisation'] = org = find_interface(self.context, IOrganisation) picked_questions = set() survey_sections = [] for section in self.context.values(): if not ISurveySection.providedBy(section): continue picked_questions.update(section.question_ids) survey_sections.append(section) self.response['survey_sections'] = survey_sections if not survey_sections: msg = _(u"no_sections_added_notice", default = u"You need to add survey sections and then use this view to manage the questions.") self.add_flash_message(msg) #Load all question objects that haven't been picked questions = org.questions self.response['available_questions'] = [questions[x] for x in questions if x not in picked_questions] return self.response
def process_question_ids(self, survey): sect_id_questions = self.request.POST.dict_of_lists() for section in survey.values(): if ISurveySection.providedBy(section): sect_id_questions.setdefault(section.__name__, []) for (sect_id, question_ids) in sect_id_questions.items(): if sect_id in survey: #Might be other things than section ids within the post survey[sect_id].set_question_ids(question_ids)
def schema(self): schema = createSchema(self.context.schemas['edit']) #FIXME: Translations up for refactoring! if ISurvey.providedBy(self.context): self.trans_util.add_translations_schema(schema['heading_translations'], self.context) if ISurveySection.providedBy(self.context): self.trans_util.add_translations_schema(schema['heading_translations'], self.context) self.trans_util.add_translations_schema(schema['description_translations'], self.context, richtext=True) if IQuestion.providedBy(self.context): self.trans_util.add_translations_schema(schema['question_text'], self.context) if IChoice.providedBy(self.context): add_translations_node(schema, 'title_translations') if ITextSection.providedBy(self.context): add_translations_node(schema, 'title_translations', based_on = 'title') add_translations_node(schema, 'description_translations', based_on = 'description') return schema
def __call__(self): return_url = self.request.resource_url(self.context) try: self.context.check_open() except SurveyUnavailableError as exc: self.add_flash_message(exc.msg) return HTTPFound(location = return_url) survey_sections = [x for x in self.context.values() if ISurveySection.providedBy(x)] question_count = sum([len(x.question_ids) for x in survey_sections]) if not question_count: msg = _(u"no_questions_notice", default = u"There aren't any questions yet. You need to add survey sections and questions, " u"otherwise invited users won't be able to do anything.") self.add_flash_message(msg) return HTTPFound(location = return_url) return super(InvitationEmailsForm, self).__call__()
def results_view(self): """ Results screen """ sections = [] section_results = {} #Loop through all sections and save them in sections #Also add section data with section.__name__ as key for section in self.context.values(): if not ISurveySection.providedBy(section): continue sections.append(section) section_results[section.__name__] = section.question_format_results() self.response['sections'] = sections self.response['section_results'] = section_results self.response['get_questions_for_section'] = self._get_questions return self.response
def check_safe_delete(self, request): root = find_root(self) results = [] #This code is ugly and could probably be done in a better way. for org in [x for x in root.values() if IOrganisation.providedBy(x)]: for surv in [x for x in org['surveys'].values() if ISurvey.providedBy(x)]: for surv_sect in [x for x in surv.values() if ISurveySection.providedBy(x)]: if self.__name__ in surv_sect.question_ids: results.append(surv_sect) if not results: return True #FIXME: Only flash messages can handle html right now out = u"<br/><br/>" rurl = request.resource_url out += ",<br/>".join([u'<a href="%s">%s</a>' % (rurl(x), x.title) for x in results]) request.session.flash(_(u"Can't delete this since it's used in the following survey sections: ${out}", mapping = {'out': out})) return False
def export(self): """ Results screen """ output = StringIO.StringIO() writer = csv.writer(output, delimiter=';', quotechar='"', quoting=csv.QUOTE_ALL) writer.writerow([self.context.title.encode('utf-8')]) languages = self.context.get_language_participants lrow = [_(u"Languages").encode('utf-8'), _(u"Total").encode('utf-8')] langs = set(languages.keys()) | set(self.context.get_available_languages()) for lang in langs: lrow.append("%s (%s)" % (self.trans_util.title_for_code(lang).encode('utf-8'), self.trans_util.title_for_code_default(lang).encode('utf-8'))) writer.writerow(lrow) lrow = [""] for lang in langs: if lang in languages: lrow.append(len(languages[lang])) else: lrow.append(0) lrow.insert(1, sum(lrow[1:])) writer.writerow(lrow) writer.writerow([]) for section in self.context.values(): if not ISurveySection.providedBy(section): continue writer.writerow([]) writer.writerow(['Section: %s' % section.title.encode('utf-8')]) writer.writerow([]) #Sort all questions according to which type they are results = {} errors = 0 for question in self._get_questions(section): if question is None: errors += 1 continue q_type = question.get_question_type() if q_type not in results: results[q_type] = dict( obj = question.get_type_object(), questions = [], ) results[q_type]['questions'].append(question) if errors: msg = _(u"unexportable_response_data_due_to_missing_question ", default = _(u"Export encountered problem(s). " u"The response information contain data that can't be linked to any question. " u"They were probably deleted after the completion of the survey. This happend ${count} time(s)."), mapping = {'count': errors}) self.add_flash_message(msg) #Loop trough all sorted questions for type_questions in results.values(): writer.writerow(type_questions['obj'].csv_header()) for question in type_questions['questions']: title = question.get_title().encode('utf-8') for qresult in question.csv_export(section.question_format_results().get(question.__name__)): qrow = [title] qrow.extend(qresult) writer.writerow(qrow) title = "" contents = output.getvalue() output.close() response = Response(content_type='text/csv', body=contents) return response