]> git.ipfire.org Git - ipfire.org.git/blame - www/web/__init__.py
Adjustments of website CSS.
[ipfire.org.git] / www / web / __init__.py
CommitLineData
879aa787
MT
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4import os
5import cgi
6import time
7import random
8import simplejson as json
9
10from http import HTTPResponse, WebError
11
12class Data:
13 def __init__(self):
14 self.output = ""
15
16 def w(self, s):
17 self.output += "%s\n" % s
18
19 def __call__(self):
20 return self.output
21
22
23class Json:
24 def __init__(self, file):
25 f = open(file)
26 data = f.read()
27 data = data.replace('\n', '') # Remove all \n
7e9fbd00 28 data = data.replace('\t', ' ') # Remove all \t
879aa787
MT
29 self.json = json.loads(data)
30 f.close()
31
32
33class Page(Data):
34 def include(self, file):
35 f = open(file)
36 output = f.read()
37 f.close()
38 self.w(output % self.data)
39
40 def menu(self):
41 m = Menu(self.langs.current)
42 return m()
43
44 def __init__(self, title, content, sidebar=None):
45 self.output = ""
7e9fbd00 46 self.title = title
879aa787
MT
47 self.langs = Languages()
48 self.data = {"server": os.environ["SERVER_NAME"].replace("ipfire", "<span>ipfire</span>"),
49 "title" : "%s - %s" % (os.environ["SERVER_NAME"], title,),
50 "menu" : self.menu(),
51 "document_name" : title,
52 "lang" : self.langs.current,
53 "languages" : self.langs.menu(title),
54 "year" : time.strftime("%Y"),
55 "slogan" : "Security today!",
56 "content" : content(self.langs.current),
57 "sidebar" : "", }
58 if sidebar:
59 self.data["sidebar"] = sidebar(self.langs.current)
60
61 def __call__(self):
7e9fbd00 62 type = "text/html"
879aa787 63 try:
7e9fbd00
MT
64 if self.title.endswith(".rss"):
65 self.include("rss.inc")
66 type = "application/rss+xml"
67 else:
68 self.include("template.inc")
879aa787
MT
69 code = 200
70 except WebError:
71 code = 500
7e9fbd00 72 h = HTTPResponse(code, type=type)
879aa787
MT
73 h.execute(self.output)
74
75
76class News(Json):
77 def __init__(self, limit=3):
78 Json.__init__(self, "news.json")
7e9fbd00
MT
79 self.news = []
80 for key in sorted(self.json.keys()):
81 self.news.insert(0, self.json[key])
879aa787
MT
82 if limit:
83 self.news = self.news[:limit]
879aa787
MT
84
85 def html(self, lang="en"):
86 s = ""
87 for item in self.news:
88 for i in ("content", "subject",):
89 if type(item[i]) == type({}):
90 item[i] = item[i][lang]
7e9fbd00 91 b = Box(item["subject"], "%(date)s - by %(author)s" % item)
879aa787 92 b.w(item["content"])
7e9fbd00
MT
93 if item["link"]:
94 if lang == "en":
95 b.w("""<p><a href="%(link)s" target="_blank">Read more.</a></p>""" % item)
96 elif lang == "de":
97 b.w("""<p><a href="%(link)s" target="_blank">Mehr Informationen.</a></p>""" % item)
879aa787
MT
98 s += b()
99 return s
100
101 __call__ = html
102
103 def headlines(self, lang="en"):
104 headlines = []
105 for item in self.news:
106 if type(item["subject"]) == type({}):
107 item["subject"] = item["subject"][lang]
108 headlines.append((item["subject"],))
109 return headlines
110
7e9fbd00
MT
111 def items(self):
112 return self.news
113
879aa787
MT
114
115class Menu(Json):
116 def __init__(self, lang):
117 self.lang = lang
118 Json.__init__(self, "menu.json")
119
120 def __call__(self):
121 s = """<div id="menu"><ul>\n"""
122 for item in self.json.values():
123 item["active"] = ""
124
125 # Grab language
126 if type(item["name"]) == type({}):
127 item["name"] = item["name"][self.lang]
128
129 # Add language attribute to local uris
130 if item["uri"].startswith("/"):
131 item["uri"] = "/%s%s" % (self.lang, item["uri"],)
132
78f68ecb
MT
133 if os.environ["REQUEST_URI"].endswith(item["uri"]):
134 item["active"] = "class=\"active\""
879aa787
MT
135
136 s += """<li><a href="%(uri)s" %(active)s>%(name)s</a></li>\n""" % item
137 s += "</ul></div>"
138 return s
139
140
141class Banners(Json):
142 def __init__(self, lang="en"):
143 self.lang = lang
144 Json.__init__(self, "banners.json")
145
146 def random(self):
147 banner = random.choice(self.json.values())
148 return banner
149
150
151class Languages:
152 def __init__(self, doc=""):
153 self.available = []
154
155 for lang in ("de", "en",):
156 self.append(lang,)
157
158 self.current = cgi.FieldStorage().getfirst("lang") or "en"
159
160 def append(self, lang):
161 self.available.append(lang)
162
163 def menu(self, doc):
164 s = ""
165 for lang in self.available:
166 s += """<a href="/%(lang)s/%(doc)s"><img src="/images/%(lang)s.gif" alt="%(lang)s" /></a>""" % \
167 { "lang" : lang, "doc" : doc, }
168 return s
169
170
171class Box(Data):
172 def __init__(self, headline, subtitle=""):
173 Data.__init__(self)
174 self.w("""<div class="post"><h3>%s</h3>""" % (headline,))
175 if subtitle:
478d167b 176 self.w("""<div class="post_info">%s</div>""" % (subtitle,))
879aa787
MT
177
178 def __call__(self):
179 self.w("""<br class="clear" /></div>""")
180 return Data.__call__(self)
181
182
183class Sidebar(Data):
184 def __init__(self, name):
185 Data.__init__(self)
186
187 def content(self, lang):
8d2d0298
MT
188 #self.w("""<h4>Test Page</h4>
189 # <p>Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
190 # sed diam nonumy eirmod tempor invidunt ut labore et dolore magna
191 # aliquyam erat, sed diam voluptua. At vero eos et accusam et justo
192 # duo dolores et ea rebum.</p>""")
879aa787
MT
193 banners = Banners()
194 self.w("""<h4>%(title)s</h4><a href="%(link)s" target="_blank">
a206fac6 195 <img class="banner" src="%(uri)s" /></a>""" % banners.random())
879aa787
MT
196
197 def __call__(self, lang):
198 self.content(lang)
199 return Data.__call__(self)
200
201
202class Content(Data):
203 def __init__(self, name):
204 Data.__init__(self)
205
206 def content(self):
207 self.w("""<h3>Test Page</h3>
208 <p>Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
209 sed diam nonumy eirmod tempor invidunt ut labore et dolore magna
210 aliquyam erat, sed diam voluptua. At vero eos et accusam et justo
211 duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
212 sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
213 consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt
214 ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero
215 eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
216 gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
217 Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
218 nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
219 sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
220 rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
221 ipsum dolor sit amet.</p>""")
222
223 b = Box("Test box one", "Subtitle of box")
224 b.write("""<p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit
225 esse molestie consequat, vel illum dolore eu feugiat nulla facilisis
226 at vero eros et accumsan et iusto odio dignissim qui blandit praesent
227 luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
228 Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
229 nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
230 volutpat.</p>""")
231 self.w(b())
232
233 b = Box("Test box two", "Subtitle of box")
234 b.write("""<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
235 suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem
236 vel eum iriure dolor in hendrerit in vulputate velit esse molestie
237 consequat, vel illum dolore eu feugiat nulla facilisis at vero eros
238 et accumsan et iusto odio dignissim qui blandit praesent luptatum
239 zzril delenit augue duis dolore te feugait nulla facilisi.</p>""")
240 self.w(b())
241
242 def __call__(self, lang="en"):
243 self.content()
244 return Data.__call__(self)