homepage.py 6.18 KB
Newer Older
rgaudin's avatar
rgaudin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 nu

import os
import tempfile

import yaml
from jinja2 import Environment, FileSystemLoader, select_autoescape

from data import data_dir
from backend.catalog import get_package


def get_ansible_group_vars():
    with open(os.path.join(data_dir, "ansiblecube", "group_vars", "all"), "r") as fp:
        return yaml.load(fp.read())


ANSIBLE_GROUP_VARS = get_ansible_group_vars()


def get_domain(name):
    return name.replace(" ", "_").replace("_", "-").lower()


def language_is_bidirectional(lang_code):
    return lang_code in ("ar",)


jinja_env = Environment(
    loader=FileSystemLoader(
        os.path.join(data_dir, "ansiblecube", "roles", "home", "templates")
    ),
    autoescape=select_autoescape(["html", "xml", "txt"]),
)
jinja_env.filters["language_bidi"] = language_is_bidirectional


def save_homepage(html):
    with tempfile.NamedTemporaryFile("w", delete=False, suffix=".html") as fp:
        fp.write(html)
        fp.close()
        return fp.name


def generate_homepage(logger, options):
    """ generate an ideascube lookalike HTML homepage from options """
    hostname = get_domain(options["name"])
    fqdn = "{hostname}.{tld}".format(
        hostname=hostname, tld=ANSIBLE_GROUP_VARS.get("tld")
    )
    cards = []
    if options["edupi"]:
        edupi_fqdn = "edupi.{fqdn}".format(fqdn=fqdn)
        if options["language"] == "fr":
            title = "Ressources"
            category = "Accès"
            description = "Accès à diverses ressources"
        else:
            title = "Resources"
            category = "Access"
            description = "Access to various resources"
        cards.append(
            {
                "url": "http://{}".format(edupi_fqdn),
                "css_class": "",
                "category_class": "access",
                "category": category,
                "title": title,
                "description": description,
                "fa": "file",
            }
        )

    if options["wikifundi_languages"]:
        wikifundi_fqdn = "wikifundi.{fqdn}".format(fqdn=fqdn)
        if "fr" in options["wikifundi_languages"]:
            cards.append(
                {
                    "url": "http://fr.{}".format(wikifundi_fqdn),
                    "css_class": "",
                    "category_class": "create",
                    "category": "WIKI",
                    "title": "WikiFundi",
                    "description": "Environnement qui vous permet de créer des articles Wikipédia hors-ligne (en français)",
                    "fa": "pencil",
                }
            )
        if "en" in options["wikifundi_languages"]:
            cards.append(
                {
                    "url": "http://en.{}".format(wikifundi_fqdn),
                    "css_class": "",
                    "category_class": "create",
                    "category": "WIKI",
                    "title": "WikiFundi",
                    "description": "Offline editable environment that provides a similar experience to editing Wikipedia online (in English)",
                    "fa": "pencil",
                }
            )

    if options["aflatoun_languages"]:
        aflatoun_fqdn = "aflatoun.{fqdn}".format(fqdn=fqdn)
        if options["language"] == "fr":
            description = "Appentissage social/finance pour les enfants et les jeunes"
            category = "Apprendre"
rgaudin's avatar
rgaudin committed
107
            url = "http://{}/go/fr".format(aflatoun_fqdn)
rgaudin's avatar
rgaudin committed
108
109
110
        else:
            description = "Social and Financial Education for Children and Young People"
            category = "Learn"
rgaudin's avatar
rgaudin committed
111
            url = "http://{}".format(aflatoun_fqdn)
rgaudin's avatar
rgaudin committed
112
113
        cards.append(
            {
rgaudin's avatar
rgaudin committed
114
                "url": url,
rgaudin's avatar
rgaudin committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
                "css_class": "",
                "category_class": "learn",
                "category": category,
                "title": "Aflatoun",
                "description": description,
                "fa": "book",
            }
        )

    if options["kalite_languages"]:
        kalite_fqdn = "khanacademy.{fqdn}".format(fqdn=fqdn)
        if "fr" in options["kalite_languages"]:
            cards.append(
                {
129
                    "url": "http://{}/go/fr".format(kalite_fqdn),
rgaudin's avatar
rgaudin committed
130
131
132
133
134
135
136
                    "css_class": "khanacademy",
                    "category_class": "learn",
                    "category": "Appendre",
                    "title": "Khan Academy",
                    "description": "Apprendre via des vidéos et des exercices.",
                }
            )
rgaudin's avatar
rgaudin committed
137
138
139
140
141
142
143
144
145
146
147
        if "es" in options["kalite_languages"]:
            cards.append(
                {
                    "url": "http://{}/go/es".format(kalite_fqdn),
                    "css_class": "khanacademy",
                    "category_class": "learn",
                    "category": "Aprender",
                    "title": "Khan Academy",
                    "description": "Aprende con videos y ejercicios.",
                }
            )
rgaudin's avatar
rgaudin committed
148
149
150
        if "en" in options["kalite_languages"]:
            cards.append(
                {
151
                    "url": "http://{}/go/en".format(kalite_fqdn),
rgaudin's avatar
rgaudin committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
                    "css_class": "khanacademy",
                    "category_class": "learn",
                    "category": "Learn",
                    "title": "Khan Academy",
                    "description": "Learn with videos and exercises.",
                }
            )

    if options["packages"]:
        kiwix_fqdn = "kiwix.{fqdn}".format(fqdn=fqdn)
        for package_id in options["packages"]:
            package = get_package(logger, package_id)
            print(package)
            cards.append(
                {
                    "url": "http://{fqdn}/{id}".format(
                        fqdn=kiwix_fqdn, id=package.get("langid", package_id)
                    ),
                    "css_class": "zim_{}".format(package_id.rsplit(".", 1)[0]),
                    "category_class": "read",
                    "category": "ZIM",
                    "title": package.get("name"),
                    "description": package.get("description"),
                    "fa": "",
                }
            )
    context = {"name": options["name"], "cards": cards}
    content = jinja_env.get_template("home.html").render(**context)
    return content