예제 #1
0
    def _substitute_geochart(self, *, html):
        users = self.challenge.get_participants().select_related(
            "user_profile", "verification")
        country_data = (users.exclude(
            user_profile__country="").values("user_profile__country").annotate(
                country_count=Count("user_profile__country")).order_by(
                    "-country_count").values_list("user_profile__country",
                                                  "country_count"))
        content = render_to_string(
            "grandchallenge/partials/geochart.html",
            {
                "user_count":
                users.count(),
                "country_data": [{
                    "id": countries.numeric(c[0], padded=True),
                    "participants": c[1],
                } for c in country_data],
            },
        )

        s = Substitution(
            tag_name="project_statistics",
            replacement=format_html("<h1>Statistics</h1>{}", content),
        )
        return s.sub(html)
예제 #2
0
    def _substitute_geochart(self, *, html):
        users = self.challenge.get_participants().select_related(
            "user_profile"
        )
        country_data = (
            users.exclude(user_profile__country="")
            .values("user_profile__country")
            .annotate(country_count=Count("user_profile__country"))
            .order_by("-country_count")
            .values_list("user_profile__country", "country_count")
        )
        content = render_to_string(
            "grandchallenge/partials/geochart.html",
            {
                "user_count": users.count(),
                "country_data": json.dumps(
                    [["Country", "#Participants"]] + list(country_data)
                ),
            },
        )

        s = Substitution(
            tag_name="project_statistics",
            replacement=format_html("<h1>Statistics</h1>{}", content),
        )
        return s.sub(html)
    def cleaned_html(self):
        out = clean(self.html)

        if "project_statistics" in out:
            out = self._substitute_geochart(html=out)

        if "google_group" in out:
            s = Substitution(
                tag_name="google_group",
                replacement=render_to_string(
                    "grandchallenge/partials/google_group.html"),
                use_arg=True,
            )
            out = s.sub(out)

        return out
예제 #4
0
def test_argument_substitution(inp, output):
    s = Substitution(tag_name="foo",
                     replacement=mark_safe("bar{}"),
                     use_arg=True)
    assert s.sub(inp) == output
    assert isinstance(s.sub(inp), type(inp))
예제 #5
0
def test_safe_substitutions(inp, content, typ):
    s = Substitution(tag_name="foo", replacement=content)
    assert isinstance(s.sub(inp), typ)
예제 #6
0
def test_no_spaces(tag_name):
    with pytest.raises(ValueError) as e:
        Substitution(tag_name=tag_name, replacement="blah")
    assert "not a valid tag name" in str(e)
예제 #7
0
def test_substitution(tag_name, content, inp, out):
    s = Substitution(tag_name=tag_name, replacement=content)
    assert s.sub(inp) == out