Move template and user retrieval to page class
authorSimo Sorce <simo@redhat.com>
Thu, 19 Dec 2013 03:44:25 +0000 (22:44 -0500)
committerSimo Sorce <simo@redhat.com>
Thu, 19 Dec 2013 04:05:57 +0000 (23:05 -0500)
Signed-off-by: Simo Sorce <simo@redhat.com>
examples/ipsilon.conf
src/root.py
src/util/page.py
src/util/user.py [new file with mode: 0755]
templates/index.html

index cca7786..fa75839 100644 (file)
@@ -1,5 +1,6 @@
 [global]
 log.screen = True
+base.mount = "/idp"
 base.dir = "../"
 admin.config.db = "/var/lib/ipsilon/adminconfig.sqlite"
 user.prefs.db = "/var/lib/ipsilon/userprefs.sqlite"
index 50247f7..a352641 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from util import data
 from util import page
 import cherrypy
 
-class Site(object):
-    def __init__(self, value):
-        # implement lookup of sites id for link/name
-        self.link = value
-        self.name = value
-
-class User(object):
-    def __init__(self, username):
-        if username is None:
-            self.name = None
-            self._userdata = dict()
-        else:
-            self._userdata = self._get_user_data(username)
-            self.name = username
-
-    def _get_user_data(self, username):
-        store = data.Store()
-        return store._get_user_preferences(username)
-
-    @property
-    def is_admin(self):
-        if 'is_admin' in self._userdata:
-            if self._userdata['is_admin'] == '1':
-                return True
-        return False
-
-    @is_admin.setter
-    def is_admin(self, value):
-        if value is True:
-            self._userdata['is_admin'] = '1'
-        else:
-            self._userdata['is_admin'] = '0'
-
-    @property
-    def fullname(self):
-        if 'fullname' in self._userdata:
-            return self._userdata['fullname']
-        else:
-            return self.name
-
-    @fullname.setter
-    def fullname(self, value):
-        self._userdata['fullname'] = value
-
-    @property
-    def sites(self):
-        if 'sites' in self._userdata:
-            d = []
-            for site in self._userdata['sites']:
-                d.append(Site(site))
-        else:
-            return []
-
-    @sites.setter
-    def sites(self):
-        #TODO: implement setting sites via the user object ?
-        raise AttributeError
-
 class Root(page.Page):
 
     def root(self):
-        tmpl = self._env.get_template('index.html')
-        return tmpl.render(title='Root', user=User(self.username))
+        return self._template('index.html', title='Root')
index 4236e9f..15cbed0 100755 (executable)
@@ -17,6 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from util import user
 import cherrypy
 
 def protect():
@@ -31,10 +32,12 @@ def protect():
 class Page(object):
     def __init__(self, template_env):
         self._env = template_env
+        self.basepath = cherrypy.config.get('base.mount', "")
         self.username = None
 
     def __call__(self, *args, **kwargs):
         self.username = cherrypy.session.get('user', None)
+        self.user = user.User(self.username)
 
         if len(args) > 0:
             op = getattr(self, args[0], None)
@@ -47,6 +50,10 @@ class Page(object):
 
         return self.default(*args, **kwargs)
 
+    def _template(self, *args, **kwargs):
+        t = self._env.get_template(args[0])
+        return t.render(basepath=self.basepath, user=self.user, **kwargs)
+
     def default(self, *args, **kwargs):
         raise cherrypy.HTTPError(404)
 
diff --git a/src/util/user.py b/src/util/user.py
new file mode 100755 (executable)
index 0000000..1241340
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/bin/python
+#
+# Copyright (C) 2013  Simo Sorce <simo@redhat.com>
+#
+# see file 'COPYING' for use and warranty information
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from util import data
+
+class Site(object):
+    def __init__(self, value):
+        # implement lookup of sites id for link/name
+        self.link = value
+        self.name = value
+
+class User(object):
+    def __init__(self, username):
+        if username is None:
+            self.name = None
+            self._userdata = dict()
+        else:
+            self._userdata = self._get_user_data(username)
+            self.name = username
+
+    def _get_user_data(self, username):
+        store = data.Store()
+        return store._get_user_preferences(username)
+
+    @property
+    def is_admin(self):
+        if 'is_admin' in self._userdata:
+            if self._userdata['is_admin'] == '1':
+                return True
+        return False
+
+    @is_admin.setter
+    def is_admin(self, value):
+        if value is True:
+            self._userdata['is_admin'] = '1'
+        else:
+            self._userdata['is_admin'] = '0'
+
+    @property
+    def fullname(self):
+        if 'fullname' in self._userdata:
+            return self._userdata['fullname']
+        else:
+            return self.name
+
+    @fullname.setter
+    def fullname(self, value):
+        self._userdata['fullname'] = value
+
+    @property
+    def sites(self):
+        if 'sites' in self._userdata:
+            d = []
+            for site in self._userdata['sites']:
+                d.append(Site(site))
+        else:
+            return []
+
+    @sites.setter
+    def sites(self):
+        #TODO: implement setting sites via the user object ?
+        raise AttributeError
+
index b38d105..157c938 100644 (file)
@@ -3,8 +3,8 @@
 <head>
     <meta charset="UTF-8"></meta>
     <title>{{ title }}</title>
-    <link href="ui/ipsilon.css" type="text/css" rel="stylesheet"></link>
-    <link href="ui/favicon.ico" type="image/ico" rel="icon"></link>
+    <link href="{{ basepath }}/ui/ipsilon.css" type="text/css" rel="stylesheet"></link>
+    <link href="{{ basepath }}/ui/favicon.ico" type="image/ico" rel="icon"></link>
 </head>
 <body>
     <div id="container">