version inicial, con base de datos, sesiones y templates
authordanigm <dani@danigm.net>
Sun, 25 Oct 2009 12:17:44 +0000 (13:17 +0100)
committerdanigm <dani@danigm.net>
Sun, 25 Oct 2009 12:17:44 +0000 (13:17 +0100)
__init__.py [new file with mode: 0644]
app.py [new file with mode: 0644]
db.py [new file with mode: 0644]
static/css/index.css [new file with mode: 0644]
templates/index.html [new file with mode: 0644]
templates/layout.html [new file with mode: 0644]
view/__init__.py [new file with mode: 0644]
view/index.py [new file with mode: 0644]
view/reset.py [new file with mode: 0644]

diff --git a/__init__.py b/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/app.py b/app.py
new file mode 100644 (file)
index 0000000..87c376a
--- /dev/null
+++ b/app.py
@@ -0,0 +1,46 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import web
+import db
+
+urls = ('/reset', 'view.reset.Reset',
+        '/(.*)', 'view.index.Index')
+
+app = web.application(urls, globals())
+
+# templates
+render = web.template.render('templates', base='layout')
+
+# session with reloader
+if web.config.get('_session') is None:
+    store = web.session.DiskStore('sessions')
+    session = web.session.Session(app, store, {'count': 0})
+    web.config._session = session
+else:
+    session = web.config._session
+
+def session_hook():
+    web.ctx.session = session
+    web.ctx.render = render
+app.add_processor(web.loadhook(session_hook))
+
+# Sqlalchemy processor
+def load_sqla(handler):
+    web.ctx.orm = db.connect()
+    try:
+        return handler()
+    except web.HTTPError:
+       web.ctx.orm.commit()
+       raise
+    except:
+        web.ctx.orm.rollback()
+        raise
+    finally:
+        web.ctx.orm.commit()
+
+app.add_processor(load_sqla)
+
+if __name__ == '__main__':
+    app.run()
+
diff --git a/db.py b/db.py
new file mode 100644 (file)
index 0000000..23d4713
--- /dev/null
+++ b/db.py
@@ -0,0 +1,29 @@
+from sqlalchemy import *
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import sessionmaker, scoped_session
+
+Base = declarative_base()
+metadata = Base.metadata
+
+class Test(Base):
+    __tablename__ = 'test'
+
+    id = Column(Integer, primary_key=True)
+    test = Column(String(128))
+    atime = Column(DateTime(), nullable=False, server_default=text('current_timestamp'))
+
+    def __init__(self, test):
+        self.test = test
+
+def connect(database='sqlite:///database.sqlite'):
+    db = create_engine(database, echo=False)
+    session = scoped_session(sessionmaker(bind=db))
+    return session
+
+def create(database='sqlite:///database.sqlite'):
+    db = create_engine(database, echo=False)
+    metadata.create_all(db)
+
+if __name__ == '__main__':
+    create()
+
diff --git a/static/css/index.css b/static/css/index.css
new file mode 100644 (file)
index 0000000..bf948e2
--- /dev/null
@@ -0,0 +1 @@
+body{ background-color: #eee; }
diff --git a/templates/index.html b/templates/index.html
new file mode 100644 (file)
index 0000000..4d24cc4
--- /dev/null
@@ -0,0 +1,9 @@
+$def with (testlist)
+
+$var title: Index
+$var cssfiles: /static/css/index.css
+
+<ol>
+$for test in testlist
+    <li>$test.test</li>
+</ol>
diff --git a/templates/layout.html b/templates/layout.html
new file mode 100644 (file)
index 0000000..7f8cbec
--- /dev/null
@@ -0,0 +1,15 @@
+$def with (content)
+
+<html>
+    <head>
+        <title>$content.title</title>
+
+        $if content.get('cssfiles', ''):
+            $for f in content.cssfiles.split():
+                <link rel="stylesheet" href="$f" type="text/css" media="screen" charset="utf-8"/>
+    </head>
+
+    <body>
+        $:content
+    </body>
+</html>
diff --git a/view/__init__.py b/view/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/view/index.py b/view/index.py
new file mode 100644 (file)
index 0000000..b0718c9
--- /dev/null
@@ -0,0 +1,17 @@
+import web
+import db
+
+class Index:
+    def GET(self, *args):
+        orm = web.ctx.orm
+        session = web.ctx.session
+
+        session.count += 1
+
+        test = db.Test(args[0])
+        orm.add(test)
+
+        all = orm.query(db.Test).all()
+        
+        return web.ctx.render.index(all)
+
diff --git a/view/reset.py b/view/reset.py
new file mode 100644 (file)
index 0000000..8cbf10b
--- /dev/null
@@ -0,0 +1,13 @@
+import web
+import db
+
+class Reset:
+    def GET(self):
+        session = web.ctx.session
+        orm = web.ctx.orm
+        all = orm.query(db.Test).all()
+        # deleting all
+        [orm.delete(a) for a in all]
+        session.kill()
+        raise web.redirect("/")
+