def get_picked_choice(request, section, question, participant_uid, default = None, lang = None): """ Only works on questions with single choice questions right now. :param section: SurveySection instance :param question: Question instance :param participant_uid: Respondent (survey participant) id. :param default: Return this if not found. :return: Choice object or default. """ if not ISurveySection.providedBy(section): raise TypeError("section must be a SurveySection object") #pragma: no coverage if not IQuestion.providedBy(question): raise TypeError("question must be a Question object") #pragma: no coverage if lang is None: lang = request.locale_name question_widget = get_question_widget(request, question) if question_widget.allow_choices == True and question_widget.multichoice == False: choice_id = section.responses.get(participant_uid, {}).get(question.cluster) if choice_id: query = "type_name == 'Choice' and cluster == '%s'" % choice_id for docid in request.root.catalog.query(query + " and language == '%s'" % lang)[1]: #Generator with one item in this case for obj in request.resolve_docids(docid, perm = None): return obj for docid in request.root.catalog.query(query)[1]: #Generator that may have more items, but we're interested in any of them for obj in request.resolve_docids(docid, perm = None): return obj return default
def _question_by_type_or_id(obj): if IQuestion.providedBy(obj): return obj.cluster #Assume cluster id as argument if obj and isinstance(obj, string_types): return obj raise TypeError("%r was not a Question instance or a cluster id of a question" % obj)
def responses(self, section, question): assert ISurveySection.providedBy(section), "Not a survey section" assert IQuestion.providedBy(question), "Not a question" results = [] for response in section.responses.values(): results.append(response.get(question.cluster, "")) return results
def get_question_widget(request, context): """ :param context: Question or QuestionType instance. :return: QuestionWidget instance specified by the QuestionType. """ if IQuestion.providedBy(context): context = get_question_type(request, context) if not IQuestionType.providedBy(context): raise TypeError("context must be a Question or QuestionType object") #pragma: no coverage return request.registry.queryAdapter(context, IQuestionWidget, name = context.input_widget)
def responses(self, section, question): assert ISurveySection.providedBy(section), "Not a survey section" assert IQuestion.providedBy(question), "Not a question" results = {} for response in section.responses.values(): val = response.get(question.cluster, "") if val in results: results[val] += 1 else: results[val] = 1 return results
def get_question_type(request, context): """ :param context: Question instance. :return: corresponding QuestionType instance. """ if not IQuestion.providedBy(context): raise TypeError("context must be a Question object") #pragma: no coverage if not context.question_type: raise ValueError("%r doesn't have the question_type attribute set" % context) #pragma: no coverage for docid in request.root.catalog.query("uid == '%s'" % context.question_type)[1]: for obj in request.resolve_docids(docid, perm = None): #Generator with one item in this case return obj raise ValueError("%r has a question_type set that doesn't exist" % context)
def get_all_choices(question, request, only_from_type = False, locale_name = None, include_omitted = False): """ :return: Choice objects :rtype: List """ if locale_name == None: locale_name = request.locale_name question_type = None if IQuestion.providedBy(question): question_type = request.get_question_type(question) elif IQuestionType.providedBy(question): question_type = question question = None else: raise TypeError("question must be either a QuestionType or Question object.") #Get from type choices = list(question_type.get_choices(locale_name)) if question and only_from_type == False: choices.extend(question.get_choices(locale_name)) if include_omitted == False: choices = [x for x in choices if x.omit_from_score_count == False] return choices
def get_choices(self, question): assert IQuestion.providedBy(question) question_type = self.request.get_question_type(question) results = [x for x in question_type.get_choices(self.request.locale_name)] results.extend([x for x in question.get_choices(self.request.locale_name)]) return results
def question_type_indexer(context, default): if IQuestion.providedBy(context): res = getattr(context, 'question_type', None) return res and res or default return default