]> git.ipfire.org Git - ipfire.org.git/commitdiff
Changes on the CMS.
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Jul 2009 18:53:53 +0000 (20:53 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Jul 2009 18:53:53 +0000 (20:53 +0200)
There is a new Javascript class and the web module is derived
into many files. ipfire.py behaves different and there is
a new info.json file that should make some things easier.

I also hope that it will be faster or even as fast as it has
been before these changes.

19 files changed:
www/include/jquery.min.js [moved from www/include/jquery.js with 100% similarity]
www/info.json [new file with mode: 0644]
www/ipfire.py [changed mode: 0644->0755]
www/pages/cluster/__init__.py
www/pages/news.rss/__init__.py
www/pages/source/__init__.py
www/pages/static/__init__.py
www/pages/static/download.xml
www/pages/static/index.xml
www/template.inc
www/web/__init__.py
www/web/banners.py [new file with mode: 0644]
www/web/elements.py [new file with mode: 0644]
www/web/info.py [new file with mode: 0644]
www/web/javascript.py [new file with mode: 0644]
www/web/json.py [new file with mode: 0644]
www/web/lang.py [new file with mode: 0644]
www/web/menu.py [new file with mode: 0644]
www/web/news.py [new file with mode: 0644]

diff --git a/www/info.json b/www/info.json
new file mode 100644 (file)
index 0000000..0ff83a0
--- /dev/null
@@ -0,0 +1,7 @@
+{
+       "name"     : "IPFire",
+       "sname"    : "ipfire",
+       "slogan"   : "Security now!",
+       "releases" : { "stable"  : "IPFire 2.5 - Core 28",
+                                  "testing" : "IPFire 3.0 alpha 1" }
+}
old mode 100644 (file)
new mode 100755 (executable)
index 830bb15..6296b92
@@ -1,26 +1,24 @@
 #!/usr/bin/python
 
+import cgitb
+cgitb.enable()
+
 import sys
 import cgi
 import imputil
 
-from web import Page
-
-site = cgi.FieldStorage().getfirst("site") or "main"
+site = cgi.FieldStorage().getfirst("site") or "index"
 
 sys.path = [ "pages",] + sys.path
-for page in (site, "static"):
+for s in (site, "static"):
        try:
-               found = imputil.imp.find_module(page)
-               loaded = imputil.imp.load_module(page, found[0], found[1], found[2])
-               content = loaded.__dict__["Content"]
-               sidebar = loaded.__dict__["Sidebar"]
+               found = imputil.imp.find_module(s)
+               loaded = imputil.imp.load_module(s, found[0], found[1], found[2])
+               
+               p = loaded.__dict__["page"]
+
                break
        except ImportError, e:
                pass
 
-c = content(site)
-s = sidebar(site)
-
-p = Page(site, c, s)
 p()
index d8f787c7ced048c04400739055fd5827450ea2f3..f4d5fcf26a1277a1ede3987284460d3535db1619 100644 (file)
@@ -1,22 +1,43 @@
 #!/usr/bin/python
 
 import web
-import web.cluster
+import web.elements
+from web.javascript import Javascript
 
 class Content(web.Content):
-       def __init__(self, name):
-               web.Content.__init__(self, name)
-               
-               self.cluster = web.cluster.Cluster("minerva.ipfire.org")
+       def __init__(self):
+               web.Content.__init__(self)
 
        def __call__(self, lang):
-               ret = "<h3>Icecream Cluster Monitoring</h4>"
+               ret = """<h3>Icecream Cluster Monitoring</h4>
+                       <p>Cluster's load: <span id="loadbar"></span>  - Number of nodes: <span id="count">-</span></p>
+                               <table id="nodes">
+                                       <thead>
+                                               <tr>
+                                                       <th>Name</th>
+                                                       <th>Arch</th>
+                                                       <th>Load</th>
+                                                       <th>Jobs</th>
+                                                       <th>Speed</th>
+                                               </tr>
+                                       </thead>
+                                       <tbody>
+                                       </tbody>
+                               </table>"""
 
-               ret += """<script type="text/javascript">
+               return ret
+
+page = web.Page()
+page.content = Content()
+page.sidebar = web.elements.Sidebar()
+
+page.javascript = Javascript(jquery=1)
+page.javascript.jquery_plugin("progressbar")
+page.javascript.write("""<script type="text/javascript">
                                nodes = new Array();
 
                                update = function() {
-                                       $.getJSON("http://www.ipfire.org/rpc.py", { type: "cluster" },
+                                       $.getJSON("/rpc.py", { type: "cluster" },
                                                function(data) {
                                                        var count = 0;
                                                        $.each(data.nodes, function(i, node) {
@@ -60,23 +81,4 @@ class Content(web.Content):
                                        update();
                                        setInterval("update()", 2000);
                                })
-                       </script>"""            
-
-               ret += """<p>Cluster's load: <span id="loadbar"></span>  - Number of nodes: <span id="count">-</span></p>
-                               <table id="nodes">
-                                       <thead>
-                                               <tr>
-                                                       <th>Name</th>
-                                                       <th>Arch</th>
-                                                       <th>Load</th>
-                                                       <th>Jobs</th>
-                                                       <th>Speed</th>
-                                               </tr>
-                                       </thead>
-                                       <tbody>
-                                       </tbody>
-                               </table>"""
-
-               return ret
-
-Sidebar = web.Sidebar
+                       </script>""")
index 0e0a0ba41a7a7170ced0df863420a90d328780c9..85a23e6599b889eeaff240a520920a9232952211 100644 (file)
@@ -5,12 +5,13 @@ import time
 from urllib import quote
 
 import web
+import web.news
 
 class Content(web.Content):
-       def __init__(self, name):
-               web.Content.__init__(self, name)
+       def __init__(self):
+               web.Content.__init__(self)
                
-               self.news = web.News(15)
+               self.news = web.news.News(15)
 
        def __call__(self, lang="en"):
                s = ""
@@ -36,9 +37,5 @@ class Content(web.Content):
                        </item>\n""" % item
                return s
 
-class Sidebar(web.Sidebar):
-       def __init__(self, name):
-               web.Sidebar.__init__(self, name)
-       
-       def content(self, lang):
-               pass
+page = web.Page()
+page.content = Content()
index 236b4e05c97e43bad0bda23603f777aeadf4da60..bf597dc6a1afcd70e83109cd349760bbe0f515b1 100644 (file)
@@ -51,8 +51,8 @@ class SourceObject:
 
 
 class Content(web.Content):
-       def __init__(self, name):
-               web.Content.__init__(self, name)
+       def __init__(self):
+               web.Content.__init__(self)
                
                self.dirs = []
 
@@ -85,5 +85,6 @@ class Content(web.Content):
                        ret += b()
                return ret
 
-Sidebar = web.Sidebar
-
+page = web.Page()
+page.content = Content()
+page.sidebar = web.Sidebar()
index 1ef0857efd80aaad25be79e648a90d8ede84bd8b..36d7550ebf7a1a65dd32903619357e5110737b61 100644 (file)
@@ -5,8 +5,11 @@ import os
 from xml.dom.minidom import parseString
 
 import web
+from web.banners import Banners
+from web.elements import Box, Releases
+from web.news import News
 
-class Xml:
+class Xml(object):
        def __init__(self, file):
                file = "%s/pages/static/%s.xml" % (os.getcwd(), file,)
                f = open(file)
@@ -14,10 +17,10 @@ class Xml:
                f.close()
 
                self.xml = parseString(data).getElementsByTagName("Site")[0]
-       
+
        def getAttribute(self, node, attr):
                return node.getAttribute(attr).strip()
-       
+
        def getText(self, node):
                ret = ""
                for i in node.childNodes:
@@ -34,7 +37,7 @@ class Content(Xml):
                for paragraphs in self.xml.getElementsByTagName("Paragraphs"):
                        for paragraph in paragraphs.getElementsByTagName("Paragraph"):
                                if self.getAttribute(paragraph, "news") == "1":
-                                       news = web.News(int(self.getAttribute(paragraph, "count")))
+                                       news = News(int(self.getAttribute(paragraph, "count")))
                                        ret += news(lang).encode("utf-8")
                                        continue
 
@@ -45,7 +48,7 @@ class Content(Xml):
                                                heading = self.getText(heading)
                                                break
 
-                               b = web.Box(heading)
+                               b = Box(heading)
 
                                # Content
                                for content in paragraph.getElementsByTagName("Content"):
@@ -70,10 +73,14 @@ class Sidebar(Xml):
                sidebar = self.xml.getElementsByTagName("Sidebar")[0]
                for paragraph in sidebar.getElementsByTagName("Paragraph"):
                        if self.getAttribute(paragraph, "banner") == "1":
-                               b = web.Banners()
+                               b = Banners()
                                ret += """<h4>%(title)s</h4><a href="%(link)s" target="_blank">
                                                <img class="banner" src="%(uri)s" /></a>""" % b.random()
                                continue
+                       elif self.getAttribute(paragraph, "releases") == "1":
+                               r = Releases()
+                               ret += r(lang)
+                               continue
 
                        # Heading
                        for heading in paragraph.getElementsByTagName("Heading"):
@@ -95,3 +102,7 @@ class Sidebar(Xml):
                                        ret += s
 
                return ret
+
+page = web.Page()
+page.content = Content(page.site)
+page.sidebar = Sidebar(page.site)
index 8d045cd28ac8a50af8e3d63f5896ec26b586d794..1b30e4c1b2cbc162b5e5daebc787cc7006546502 100644 (file)
        </Paragraphs>
 
        <Sidebar>
+               <Paragraph releases="1" />
+
                <Paragraph>
                    <Heading lang="en"><![CDATA[<span>Torrent</span> clients]]></Heading>
                        <Heading lang="de"><![CDATA[<span>Torrent</span> Clients]]></Heading>   
                                ]]></Content>
                </Paragraph>
                
-               <Paragraph>
+               <!-- <Paragraph>
                        <Heading lang="en"><![CDATA[<span>Other</span> Tools]]></Heading>
                        <Heading lang="de"><![CDATA[<span>Andere</span> Tools]]></Heading>
 
                                        <li><a href="http://wiki.ipfire.org/de/tools/mac_os">Mac OS</a></li>
                                </ul>
                                ]]></Content>
-               </Paragraph>
+               </Paragraph> -->
 
                <Paragraph banner="1" />
 
index 9cc399bb792a48e17379adaf2f010aa204229e0f..f479d93e9f48f8f7f1e410382239792f813b76df 100644 (file)
        </Paragraphs>
        
        <Sidebar>
-               <Paragraph>
-                       <Heading><![CDATA[<span>info</span>rmation]]></Heading>
+               <Paragraph releases="1" />
 
-                       <Content lang="en" raw="1"><![CDATA[
-                               <p><strong>Current release:</strong><br />IPFire 2.5</p>
-                               <p><strong>Current update level:</strong><br />Core 28</p>
-                               <p><strong>Current unstables:</strong><br />IPFire 3.0 alpha 1</p>
-                               ]]></Content>
-                       <Content lang="de" raw="1"><![CDATA[
-                               <p><strong>Aktuelle Version:</strong><br />IPFire 2.5</p>
-                               <p><strong>Aktuelles Update-Level:</strong><br />Core 28</p>
-                               <p><strong>Testversionen:</strong><br />IPFire 3.0 alpha 1</p>
-                               ]]></Content>
-               </Paragraph>
-               
                <Paragraph>
                        <Heading><![CDATA[<span>Internet Relay</span> Chat]]></Heading>
                        <Content><![CDATA[
index b43875e0c01717a872d514e973fd6cb143f79e18..9c0707cb4d1ee3594404a2da038ccd048e0363a2 100644 (file)
@@ -13,6 +13,7 @@
                <link rel="stylesheet" type="text/css" href="/include/ie6.css" />
                <script src="/include/correctpng.js" type="text/javascript"></script>
        <![endif]-->
+               %(javascript)s
        </head>
        <body>
                <div id="header">
index 8a79a6def252c23b58e76691069e36d6e555661c..e137f5f5fdd63cf61bb3f4e55478bba81b37bbc1 100644 (file)
@@ -4,13 +4,17 @@
 import os
 import cgi
 import time
-import random
-import simplejson as json
+
+try:
+       import cStringIO as StringIO
+except ImportError:
+       import StringIO
 
 from http import HTTPResponse, WebError
 
-import cgitb
-cgitb.enable()
+import info
+import lang
+import menu
 
 class Data:
        def __init__(self):
@@ -18,48 +22,55 @@ class Data:
        
        def w(self, s):
                self.output += "%s\n" % s
+       
+       write = w
                
        def __call__(self):
                return self.output
 
 
-class Json:
-       def __init__(self, file):
-               f = open(file)
-               data = f.read()
-               data = data.replace('\n', '') # Remove all \n
-               data = data.replace('\t', ' ') # Remove all \t
-               self.json = json.loads(data)
-               f.close()
-
-
-class Page(Data):
+class Page(object):
+       def __init__(self):
+               self.io = StringIO.StringIO()
+
+               self.info = info.Info()
+               self.langs = lang.Languages()
+               self.menu = menu.Menu(self.langs.current)
+               self.site = self.title = cgi.FieldStorage().getfirst("site", default="index")
+
+               self.content = None
+               self.javascript = None
+               self.sidebar = None
+
+       @property
+       def data(self):
+               ret = { "content" : "",
+                                "javascript": "",
+                                "lang"   : self.langs.current,
+                                "languages" : self.langs.menu(self.site),
+                                "menu"   : self.menu(),
+                                "name"   : self.info["name"],
+                                "server" : os.environ["SERVER_NAME"].replace("ipfire", "<span>ipfire</span>"),
+                                "sidebar" : "",
+                                "slogan" : self.info["slogan"],
+                                "sname"  : self.info["sname"],
+                                "title"  : "%s - %s" % (os.environ["SERVER_NAME"], self.title,),                                
+                                "year"   : time.strftime("%Y"),}
+
+               if self.content:
+                       ret["content"] = self.content(self.langs.current)
+               if self.javascript:
+                       ret["javascript"] = self.javascript(self.langs.current)
+               if self.sidebar:
+                       ret["sidebar"] = self.sidebar(self.langs.current)
+
+               return ret
+                       
        def include(self, file):
                f = open(file)
                output = f.read()
                f.close()
-               self.w(output % self.data)
-
-       def menu(self):
-               m = Menu(self.langs.current)
-               return m()
-
-       def __init__(self, title, content, sidebar=None):
-               self.output  = ""
-               self.title   = title
-               self.langs   = Languages()
-               self.data    = {"server": os.environ["SERVER_NAME"].replace("ipfire", "<span>ipfire</span>"),
-                                               "title" : "%s - %s" % (os.environ["SERVER_NAME"], title,),
-                                               "menu"  : self.menu(),
-                                               "document_name" : title,
-                                               "lang"  : self.langs.current,
-                                               "languages" : self.langs.menu(title),
-                                               "year"  : time.strftime("%Y"),
-                                               "slogan" : "Security today!",
-                                               "content" : content(self.langs.current),
-                                               "sidebar" : "", }
-               if sidebar:
-                       self.data["sidebar"] = sidebar(self.langs.current)
+               self.io.write(output % self.data)
 
        def __call__(self):
                type = "text/html"
@@ -73,137 +84,11 @@ class Page(Data):
                except WebError:
                        code = 500
                h = HTTPResponse(code, type=type)
-               h.execute(self.output)
-
-
-class News(Json):
-       def __init__(self, limit=3):
-               Json.__init__(self, "news.json")
-               self.news = []
-               for key in sorted(self.json.keys()):
-                       self.news.insert(0, self.json[key])
-               if limit:
-                       self.news = self.news[:limit]
-
-       def html(self, lang="en"):
-               s = ""
-               for item in self.news:
-                       for i in ("content", "subject",):
-                               if type(item[i]) == type({}):
-                                       item[i] = item[i][lang]
-                       b = Box(item["subject"], "%(date)s - by %(author)s" % item)
-                       b.w(item["content"])
-                       if item["link"]:
-                               if lang == "en":
-                                       b.w("""<p><a href="%(link)s" target="_blank">Read more.</a></p>""" % item)
-                               elif lang == "de":
-                                       b.w("""<p><a href="%(link)s" target="_blank">Mehr Informationen.</a></p>""" % item)
-                       s += b()
-               return s
-
-       __call__ = html
-
-       def headlines(self, lang="en"):
-               headlines = []
-               for item in self.news:
-                       if type(item["subject"]) == type({}):
-                               item["subject"] = item["subject"][lang]
-                       headlines.append((item["subject"],))
-               return headlines
-
-       def items(self):
-               return self.news
-
-
-class Menu(Json):
-       def __init__(self, lang):
-               self.lang = lang
-               Json.__init__(self, "menu.json")
-
-       def __call__(self):
-               s = """<div id="menu"><ul>\n"""
-               for item in self.json.values():
-                       item["active"] = ""
-
-                       # Grab language
-                       if type(item["name"]) == type({}):
-                               item["name"] = item["name"][self.lang]
-
-                       # Add language attribute to local uris
-                       if item["uri"].startswith("/"):
-                               item["uri"] = "/%s%s" % (self.lang, item["uri"],)
-
-                       if os.environ["REQUEST_URI"].endswith(item["uri"]):
-                               item["active"] = "class=\"active\""
-
-                       s += """<li><a href="%(uri)s" %(active)s>%(name)s</a></li>\n""" % item
-               s += "</ul></div>"
-               return s
-
-
-class Banners(Json):
-       def __init__(self, lang="en"):
-               self.lang = lang
-               Json.__init__(self, "banners.json")
-
-       def random(self):
-               banner = random.choice(self.json.values())
-               return banner
-
+               h.execute(self.io.getvalue())
 
-class Languages:
-       def __init__(self, doc=""):
-               self.available = []
-
-               for lang in ("de", "en",):
-                       self.append(lang,)
                
-               self.current = cgi.FieldStorage().getfirst("lang") or "en"
-
-       def append(self, lang):
-               self.available.append(lang)
-
-       def menu(self, doc):
-               s = ""
-               for lang in self.available:
-                       s += """<a href="/%(lang)s/%(doc)s"><img src="/images/%(lang)s.gif" alt="%(lang)s" /></a>""" % \
-                               { "lang" : lang, "doc" : doc, }
-               return s
-
-
-class Box(Data):
-       def __init__(self, headline, subtitle=""):
-               Data.__init__(self)
-               self.w("""<div class="post"><h3>%s</h3><a name="%s"></a>""" % (headline,headline,))
-               if subtitle:
-                       self.w("""<div class="post_info">%s</div>""" % (subtitle,))
-
-       def __call__(self):
-               self.w("""<br class="clear" /></div>""")
-               return Data.__call__(self)
-
-
-class Sidebar(Data):
-       def __init__(self, name):
-               Data.__init__(self)
-
-       def content(self, lang):
-               #self.w("""<h4>Test Page</h4>
-               #       <p>Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
-               #       sed diam nonumy eirmod tempor invidunt ut labore et dolore magna
-               #       aliquyam erat, sed diam voluptua. At vero eos et accusam et justo
-               #       duo dolores et ea rebum.</p>""")
-               banners = Banners()
-               self.w("""<h4>%(title)s</h4><a href="%(link)s" target="_blank">
-                       <img class="banner" src="%(uri)s" /></a>""" % banners.random())
-
-       def __call__(self, lang):
-               self.content(lang)
-               return Data.__call__(self)
-
-
 class Content(Data):
-       def __init__(self, name):
+       def __init__(self):
                Data.__init__(self)
 
        def content(self):
diff --git a/www/web/banners.py b/www/web/banners.py
new file mode 100644 (file)
index 0000000..60172d5
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/python
+
+import random
+
+from json import Json
+
+class Banners(Json):
+       def __init__(self, lang="en"):
+               self.lang = lang
+               Json.__init__(self, "banners.json")
+
+       def random(self):
+               banner = random.choice(self.json.values())
+               return banner
+
diff --git a/www/web/elements.py b/www/web/elements.py
new file mode 100644 (file)
index 0000000..9cc615c
--- /dev/null
@@ -0,0 +1,50 @@
+#!/usr/bin/python
+
+from . import Data
+from banners import Banners
+from info import Info
+
+class Box(Data):
+       def __init__(self, headline, subtitle=""):
+               Data.__init__(self)
+               self.w("""<div class="post"><h3>%s</h3><a name="%s"></a>""" % (headline,headline,))
+               if subtitle:
+                       self.w("""<div class="post_info">%s</div>""" % (subtitle,))
+
+       def __call__(self):
+               self.w("""<br class="clear" /></div>""")
+               return Data.__call__(self)
+
+class Sidebar(Data):
+       def __init__(self):
+               Data.__init__(self)
+
+       def content(self, lang):
+               banners = Banners()
+               self.w("""<h4>%(title)s</h4><a href="%(link)s" target="_blank">
+                       <img class="banner" src="%(uri)s" /></a>""" % banners.random())
+
+       def __call__(self, lang):
+               self.content(lang)
+               return Data.__call__(self)
+
+class Releases(Data):
+       def __init__(self):
+               Data.__init__(self)
+       
+       def content(self, lang):
+               info = Info()
+               if lang == "de":
+                       self.write("""<h4><span>release</span> informationen</h4>
+                               <p><strong>Aktuelle Version</strong>:<br />%(stable)s</p>
+                               <p><strong>Testversionen</strong>:<br />%(testing)s</p>
+                       """ % info["releases"])
+               else:
+                       self.write("""<h4><span>info</span>rmation</h4>
+                               <p><strong>Current version</strong>:<br />%(stable)s</p>
+                               <p><strong>Current unstables</strong>:<br />%(testing)s</p>
+                       """ % info["releases"])
+       
+       def __call__(self, lang):
+               self.content(lang)
+               return Data.__call__(self)
diff --git a/www/web/info.py b/www/web/info.py
new file mode 100644 (file)
index 0000000..2baf722
--- /dev/null
@@ -0,0 +1,14 @@
+#!/usr/bin/python
+
+from json import Json
+
+class Info(Json):
+       def __init__(self):
+               Json.__init__(self, "info.json")
+
+       def get(self, key):
+               if not self.json.has_key(key):
+                       raise KeyError
+               return self.json[key]
+       
+       __getitem__ = get
diff --git a/www/web/javascript.py b/www/web/javascript.py
new file mode 100644 (file)
index 0000000..c45b9f6
--- /dev/null
@@ -0,0 +1,22 @@
+#!/usr/bin/python
+
+try:
+       import cStringIO as StringIO
+except ImportError:
+       import StringIO
+
+class Javascript(object):
+       def __init__(self, jquery=0):
+               self.io = StringIO.StringIO()
+
+               if jquery:
+                       self.io.write("""<script src="/include/jquery.min.js" type="text/javascript"></script>""")
+       
+       def __call__(self, lang="en"):
+               return self.io.getvalue()
+
+       def jquery_plugin(self, plugin):
+               self.io.write("""<script src="/include/jquery.%s.min.js" type="text/javascript"></script>""" % plugin)
+
+       def write(self, s):
+               self.io.write(s)
diff --git a/www/web/json.py b/www/web/json.py
new file mode 100644 (file)
index 0000000..ef0a85d
--- /dev/null
@@ -0,0 +1,12 @@
+#!/usr/bin/python
+
+import simplejson as json
+
+class Json:
+       def __init__(self, file):
+               f = open(file)
+               data = f.read()
+               data = data.replace('\n', '') # Remove all \n
+               data = data.replace('\t', ' ') # Remove all \t
+               self.json = json.loads(data)
+               f.close()
diff --git a/www/web/lang.py b/www/web/lang.py
new file mode 100644 (file)
index 0000000..e489da5
--- /dev/null
@@ -0,0 +1,22 @@
+#!/usr/bin/python
+
+import cgi
+
+class Languages:
+       def __init__(self, doc=""):
+               self.available = []
+
+               for lang in ("de", "en",):
+                       self.append(lang,)
+               
+               self.current = cgi.FieldStorage().getfirst("lang") or "en"
+
+       def append(self, lang):
+               self.available.append(lang)
+
+       def menu(self, doc):
+               s = ""
+               for lang in self.available:
+                       s += """<a href="/%(lang)s/%(doc)s"><img src="/images/%(lang)s.gif" alt="%(lang)s" /></a>""" % \
+                               { "lang" : lang, "doc" : doc, }
+               return s
diff --git a/www/web/menu.py b/www/web/menu.py
new file mode 100644 (file)
index 0000000..c02b78c
--- /dev/null
@@ -0,0 +1,30 @@
+#!/usr/bin/python
+
+import os
+
+from json import Json
+
+class Menu(Json):
+       def __init__(self, lang):
+               self.lang = lang
+               Json.__init__(self, "menu.json")
+
+       def __call__(self):
+               s = """<div id="menu"><ul>\n"""
+               for item in self.json.values():
+                       item["active"] = ""
+
+                       # Grab language
+                       if type(item["name"]) == type({}):
+                               item["name"] = item["name"][self.lang]
+
+                       # Add language attribute to local uris
+                       if item["uri"].startswith("/"):
+                               item["uri"] = "/%s%s" % (self.lang, item["uri"],)
+
+                       if os.environ["REQUEST_URI"].endswith(item["uri"]):
+                               item["active"] = "class=\"active\""
+
+                       s += """<li><a href="%(uri)s" %(active)s>%(name)s</a></li>\n""" % item
+               s += "</ul></div>"
+               return s
diff --git a/www/web/news.py b/www/web/news.py
new file mode 100644 (file)
index 0000000..5daf057
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/python
+
+from elements import Box
+from json import Json
+
+class News(Json):
+       def __init__(self, limit=3):
+               Json.__init__(self, "news.json")
+               self.news = []
+               for key in sorted(self.json.keys()):
+                       self.news.insert(0, self.json[key])
+               if limit:
+                       self.news = self.news[:limit]
+
+       def html(self, lang="en"):
+               s = ""
+               for item in self.news:
+                       for i in ("content", "subject",):
+                               if type(item[i]) == type({}):
+                                       item[i] = item[i][lang]
+                       b = Box(item["subject"], "%(date)s - by %(author)s" % item)
+                       b.w(item["content"])
+                       if item["link"]:
+                               if lang == "en":
+                                       b.w("""<p><a href="%(link)s" target="_blank">Read more.</a></p>""" % item)
+                               elif lang == "de":
+                                       b.w("""<p><a href="%(link)s" target="_blank">Mehr Informationen.</a></p>""" % item)
+                       s += b()
+               return s
+
+       __call__ = html
+
+       def headlines(self, lang="en"):
+               headlines = []
+               for item in self.news:
+                       if type(item["subject"]) == type({}):
+                               item["subject"] = item["subject"][lang]
+                       headlines.append((item["subject"],))
+               return headlines
+
+       def items(self):
+               return self.news