From bf7992a600f730d85a005565f73c4ae508cf801c Mon Sep 17 00:00:00 2001
From: Jan Dittberner <jan@dittberner.info>
Date: Sat, 4 Nov 2006 20:24:36 +0000
Subject: [PATCH] - initial work on a mod_python based web frontend - add logs
 to svn:ignore - use psycopg2 in backend

git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@198 a67ec6bc-e5d5-0310-a910-815c51eb3124
---
 backend/GnuviechAdmin/ServiceFacade.py        |  4 +-
 backend/GnuviechAdmin/SessionManager.py       |  6 +--
 frontend/web/gnuviechadmin/__init__.py        |  0
 .../web/gnuviechadmin/frontend/__init__.py    |  0
 frontend/web/gnuviechadmin/frontend/web.py    | 42 +++++++++++++++++++
 frontend/web/index.xml                        |  9 ++++
 6 files changed, 56 insertions(+), 5 deletions(-)
 create mode 100644 frontend/web/gnuviechadmin/__init__.py
 create mode 100644 frontend/web/gnuviechadmin/frontend/__init__.py
 create mode 100644 frontend/web/gnuviechadmin/frontend/web.py
 create mode 100644 frontend/web/index.xml

diff --git a/backend/GnuviechAdmin/ServiceFacade.py b/backend/GnuviechAdmin/ServiceFacade.py
index a91cd42..9b1e079 100644
--- a/backend/GnuviechAdmin/ServiceFacade.py
+++ b/backend/GnuviechAdmin/ServiceFacade.py
@@ -6,7 +6,7 @@
 from SessionManager import *
 from DomainManager import *
 import Settings
-import psycopg
+import psycopg2
 
 class ServiceFacade:
     """
@@ -16,7 +16,7 @@ class ServiceFacade:
     def __init__(self):
         connstr = 'host=%(dbhost)s user=%(dbuser)s ' + \
                   'password=%(dbpassword)s dbname=%(dbname)s'
-        dbconn = psycopg.connect(connstr % Settings.DBSETTINGS)
+        dbconn = psycopg2.connect(connstr % Settings.DBSETTINGS)
         self.sessionManager = SessionManager(dbconn)
         self.domainManager = DomainManager(dbconn)
 
diff --git a/backend/GnuviechAdmin/SessionManager.py b/backend/GnuviechAdmin/SessionManager.py
index e795d9d..6d6ace1 100644
--- a/backend/GnuviechAdmin/SessionManager.py
+++ b/backend/GnuviechAdmin/SessionManager.py
@@ -4,7 +4,7 @@ Session manager class for gnuviech-admin tool backend
 $Id$
 """
 import Settings
-import os, sha, time, logging, psycopg
+import os, sha, time, logging, psycopg2
 from threading import Timer
 
 SESSIONTIMEOUT=120 # 2 minutes
@@ -50,8 +50,8 @@ class SessionManager:
     def newSession(self, login, password):
         cr = self._dbconn.cursor()
         cr.execute('SELECT * FROM sysuser WHERE name=%(login)s AND md5pass=md5(%(password)s)' %
-                   {'login': psycopg.QuotedString(login),
-                    'password' : psycopg.QuotedString(password)})
+                   {'login': psycopg2.QuotedString(login),
+                    'password' : psycopg2.QuotedString(password)})
         self._dbconn.commit()
         result = cr.fetchall()
         if cr.rowcount == 1:
diff --git a/frontend/web/gnuviechadmin/__init__.py b/frontend/web/gnuviechadmin/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/frontend/web/gnuviechadmin/frontend/__init__.py b/frontend/web/gnuviechadmin/frontend/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/frontend/web/gnuviechadmin/frontend/web.py b/frontend/web/gnuviechadmin/frontend/web.py
new file mode 100644
index 0000000..94ed617
--- /dev/null
+++ b/frontend/web/gnuviechadmin/frontend/web.py
@@ -0,0 +1,42 @@
+from mod_python import apache, Session
+from genshi.template import TemplateLoader, TemplateNotFound
+from genshi import ParseError
+
+def findtemplate(uri):
+  templates = {"/" : "index.xml"}
+  if uri in templates:
+    return templates[uri]
+  return None
+
+def handler(req):
+  session = Session.Session(req)
+  try:
+    session['hits'] += 1
+  except:
+    session['hits'] = 1
+
+  session.save()
+
+  template = findtemplate(req.uri)
+  if template:
+
+    loader = TemplateLoader([req.document_root()])
+    try:
+      req.content_type = "text/html; charset=UTF-8"
+      tmpl = loader.load(template)
+      stream = tmpl.generate(title='Hello World: Reloaded',
+        hits=session['hits'])
+      pagebuffer = stream.render('xhtml')
+    except TemplateNotFound, tnf:
+      req.content_type = "text/plain; charset=UTF-8"
+      pagebuffer = str(tnf)
+    except ParseError, pe:
+      req.content_type = "text/plain; charset=UTF-8"
+      pagebuffer = str(pe)
+
+    #pagebuffer = "Hits: %d\n" % session['hits']
+    #pagebuffer += "Yippieh: I found %s -> %s!" % (req.uri, template)
+    
+    req.write(pagebuffer)
+    return (apache.OK)
+  return (apache.HTTP_NOT_FOUND)
diff --git a/frontend/web/index.xml b/frontend/web/index.xml
new file mode 100644
index 0000000..c98137c
--- /dev/null
+++ b/frontend/web/index.xml
@@ -0,0 +1,9 @@
+<html>
+<head>
+<title>$title</title>
+</head>
+<body>
+<h1>$title</h1>
+<p>Hits: $hits</p>
+</body>
+</html>