def get_result(**options):
     """ Return the result of get_caption using options.
     If cardinality is set we change the cardinality of the question."""
     q2c = Question2TexChart(**options)
     if options.get("cardinality") is not None:
         q2c.cardinality = options.get("cardinality")
     return q2c.get_caption()
Exemple #2
0
    def treat_question(self, question):
        LOGGER.info("Treating, %s %s", question.pk, question.text)
        options = self.tconf.get(survey_name=self.survey.name, question_text=question.text)
        multiple_charts = options.get("multiple_charts")
        if not multiple_charts:
            multiple_charts = {"": options.get("chart")}
        question_synthesis = ""
        i = 0
        for chart_title, opts in list(multiple_charts.items()):
            i += 1
            if chart_title:
                # "" is False, by default we do not add section or anything
                mct = options["multiple_chart_type"]
                question_synthesis += "\\%s{%s}" % (mct, chart_title)
            tex_type = opts.get("type")
            if tex_type == "raw":
                question_synthesis += Question2TexRaw(question, **opts).tex()
            elif tex_type == "sankey":
                other_question_text = opts["question"]
                other_question = Question.objects.get(text=other_question_text)
                q2tex = Question2TexSankey(question, other_question=other_question)
                question_synthesis += q2tex.tex()
            elif tex_type in ["pie", "cloud", "square", "polar"]:
                q2tex = Question2TexChart(question, latex_label=i, **opts)
                question_synthesis += q2tex.tex()
            elif locate(tex_type) is None:
                msg = "{} '{}' {}".format(
                    _("We could not render a chart because the type"),
                    tex_type,
                    _(
                        "is not a standard type nor the path to an "
                        "importable valid Question2Tex child class. "
                        "Choose between 'raw', 'sankey', 'pie', 'cloud', "
                        "'square', 'polar' or 'package.path.MyQuestion2Tex"
                        "CustomClass'"
                    ),
                )
                LOGGER.error(msg)
                question_synthesis += msg
            else:
                q2tex_class = locate(tex_type)
                # The use will probably know what type he should use in his
                # custom class
                opts["type"] = None
                q2tex = q2tex_class(question, latex_label=i, **opts)
                question_synthesis += q2tex.tex()
        section_title = Question2Tex.html2latex(question.text)
        return """
\\clearpage{}
\\section{%s}

\\label{sec:%s}

%s

""" % (
            section_title,
            question.pk,
            question_synthesis,
        )
 def test_get_tex(self):
     """ The header and order of the question is correct. """
     question = self.survey.questions.get(text="Aèbc?")
     self.assertIsNotNone(Question2TexChart(question).tex())
     color = OrderedDict()
     groups = {
         '1é': ['1e', '1é', '1ë'],
         '2é': ['2e', '2é', '2ë'],
         '3é': ['3e', '3é', '3ë'],
     }
     color["1b"] = "green!80"
     color["1a"] = "cyan!50"
     color["1é"] = "red!80"
     chart = Question2TexChart(question, color=color,
                               group_together=groups).tex()
     expected_color = settings.SURVEY_DEFAULT_PIE_COLOR
     self.assertIn(expected_color, chart)
     color["1"] = "yellow!70"
     chart = Question2TexChart(question,
                               color=color,
                               group_together=groups,
                               sort_answer={
                                   "1b": 1,
                                   "1a": 2,
                                   "1": 3,
                                   "1é": 4
                               }).tex()
     expected_colors = ["red!80", "yellow!70", "cyan!50", "green!80"]
     for expected_color in expected_colors:
         self.assertIn(expected_color, chart)
     self.assertIn(
         "1/1b,\n            1/1a,\n            1/1,\n            4/1é",
         chart, "User defined sort does not seem to works.")
     chart = Question2TexChart(question,
                               color=color,
                               group_together=groups,
                               sort_answer="cardinal").tex()
     self.assertIn(
         "4/1\xe9,\n            1/Left blank,\n            1/1,\n           "
         " 1/1a,\n            1/1b", chart,
         "Cardinal sort does not seem to works. {}".format(chart))
     chart = Question2TexChart(question,
                               color=color,
                               group_together=groups,
                               sort_answer="alphanumeric").tex()
     self.assertIn(
         "1/1,\n            1/1a,\n            1/1b,\n            4/1é",
         chart,
         "Alphanumeric sort does not seem to works.. {}".format(chart))
     chart = Question2TexChart(question,
                               group_together=groups,
                               sort_answer="unknown_option").tex()
     self.assertIn(
         "4/1\xe9,\n            1/Left blank,\n            1/1,\n           "
         " 1/1a,\n            1/1b", chart,
         "Default behavior does not sort by cardinality. {}".format(chart))
 def test_cloud_tex(self):
     """ We can create a cloud chart. """
     question = self.survey.questions.get(text="Aèbc?")
     self.assertIsNotNone(Question2TexChart(question, type="cloud").tex())
 def test_no_results(self):
     """ We manage having no result at all. """
     question = self.survey.questions.get(text="Dèef?")
     self.assertIn("No answers for this question.",
                   Question2TexChart(question).tex())