##// END OF EJS Templates
Added sqlalchemy support...
Marcin Kuzminski -
r49:3ada2f40 default
parent child Browse files
Show More
@@ -0,0 +1,15 b''
1 from sqlalchemy.interfaces import ConnectionProxy
2 import time
3 import logging
4 log = logging.getLogger(__name__)
5
6 class TimerProxy(ConnectionProxy):
7 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
8 now = time.time()
9 try:
10 log.info(">>>>> STARTING QUERY >>>>>")
11 return execute(cursor, statement, parameters, context)
12 finally:
13 total = time.time() - now
14 log.info("Query: %s" % statement % parameters)
15 log.info("<<<<< TOTAL TIME: %f <<<<<" % total)
@@ -0,0 +1,24 b''
1 from sqlalchemy.ext.declarative import declarative_base
2 from sqlalchemy.orm import relation, backref
3 from sqlalchemy import ForeignKey, Column, Table, Sequence
4 from sqlalchemy.types import *
5 from sqlalchemy.databases.sqlite import *
6 from pylons_app.model.meta import Base
7
8
9 class Users(Base):
10 __tablename__ = 'users'
11 __table_args__ = {'useexisting':True}
12 user_id = Column("user_id", SLInteger(), nullable=False, unique=True, default=None, primary_key=1)
13 username = Column("username", SLText(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
14 password = Column("password", SLText(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
15 active = Column("active", SLInteger(), nullable=True, unique=None, default=None)
16 admin = Column("admin", SLInteger(), nullable=True, unique=None, default=None)
17
18 class UserLogs(Base):
19 __tablename__ = 'user_logs'
20 __table_args__ = {'useexisting':True}
21 id = Column("id", SLInteger(), nullable=False, unique=True, default=None, primary_key=1)
22 user_id = Column("user_id", SLInteger(), nullable=True, unique=None, default=None)
23 last_action = Column("last_action", SLText(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
24 last_action_date = Column("last_action_date", SLDateTime(timezone=False), nullable=True, unique=None, default=None)
@@ -31,6 +31,15 b' lang=en'
31 31 cache_dir = %(here)s/data
32 32 repos_name = etelko
33 33
34 ####################################
35 ### BEAKER CACHE ####
36 ####################################
37 beaker.cache.data_dir=/tmp/cache/data
38 beaker.cache.lock_dir=/tmp/cache/lock
39 beaker.cache.regions=short_term
40 beaker.cache.short_term.type=memory
41 beaker.cache.short_term.expire=3600
42
34 43 ################################################################################
35 44 ## WARNING: *THE LINE BELOW MUST BE UNCOMMENTED ON A PRODUCTION ENVIRONMENT* ##
36 45 ## Debug mode will enable the interactive debugging tool, allowing ANYONE to ##
@@ -38,6 +47,21 b' repos_name = etelko'
38 47 ################################################################################
39 48 #set debug = false
40 49
50 ##################################
51 ### LOGVIEW CONFIG ###
52 ##################################
53 logview.sqlalchemy = #faa
54 logview.pylons.templating = #bfb
55 logview.pylons.util = #eee
56
57 #########################################################
58 ### DB CONFIGS - EACH DB WILL HAVE IT'S OWN CONFIG ###
59 #########################################################
60 sqlalchemy.db1.url = sqlite:///%(here)s/auth.sqlite
61 #sqlalchemy.db1.echo = True
62 #sqlalchemy.db1.pool_recycle = 3600
63 sqlalchemy.convert_unicode = true
64
41 65 ################################
42 66 ### LOGGING CONFIGURATION ####
43 67 ################################
@@ -31,6 +31,15 b' lang=en'
31 31 cache_dir = %(here)s/data
32 32 repos_name = etelko
33 33
34 ####################################
35 ### BEAKER CACHE ####
36 ####################################
37 beaker.cache.data_dir=/tmp/cache/data
38 beaker.cache.lock_dir=/tmp/cache/lock
39 beaker.cache.regions=short_term
40 beaker.cache.short_term.type=memory
41 beaker.cache.short_term.expire=3600
42
34 43 ################################################################################
35 44 ## WARNING: *THE LINE BELOW MUST BE UNCOMMENTED ON A PRODUCTION ENVIRONMENT* ##
36 45 ## Debug mode will enable the interactive debugging tool, allowing ANYONE to ##
@@ -38,6 +47,21 b' repos_name = etelko'
38 47 ################################################################################
39 48 #set debug = false
40 49
50 ##################################
51 ### LOGVIEW CONFIG ###
52 ##################################
53 logview.sqlalchemy = #faa
54 logview.pylons.templating = #bfb
55 logview.pylons.util = #eee
56
57 #########################################################
58 ### DB CONFIGS - EACH DB WILL HAVE IT'S OWN CONFIG ###
59 #########################################################
60 sqlalchemy.db1.url = sqlite:///%(here)s/auth.sqlite
61 #sqlalchemy.db1.echo = True
62 #sqlalchemy.db1.pool_recycle = 3600
63 sqlalchemy.convert_unicode = true
64
41 65 ################################
42 66 ### LOGGING CONFIGURATION ####
43 67 ################################
@@ -52,15 +52,15 b' def load_environment(global_conf, app_co'
52 52
53 53 #MULTIPLE DB configs
54 54 # Setup the SQLAlchemy database engine
55 # if config['debug']:
56 # #use query time debugging.
57 # from pylons_app.lib.timer_proxy import TimerProxy
58 # sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.',
59 # proxy=TimerProxy())
60 # else:
61 # sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.')
55 if config['debug']:
56 #use query time debugging.
57 from pylons_app.lib.timerproxy import TimerProxy
58 sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.',
59 proxy=TimerProxy())
60 else:
61 sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.')
62 62
63 #init_model(sa_engine_db1)
63 init_model(sa_engine_db1)
64 64
65 65 # CONFIGURATION OPTIONS HERE (note: all config options will override
66 66 # any Pylons config options)
@@ -52,7 +52,7 b' def make_app(global_conf, full_stack=Tru'
52 52 # 500 when debug is disabled)
53 53 if asbool(config['debug']):
54 54 #don't handle 404, since mercurial does it for us.
55 app = StatusCodeRedirect(app, [400, 401, 403, 500])
55 app = StatusCodeRedirect(app, [400, 401, 403])
56 56 else:
57 57 app = StatusCodeRedirect(app, [400, 401, 403, 500])
58 58
@@ -4,7 +4,8 b' from pylons import request, response, se'
4 4 from pylons.controllers.util import abort, redirect
5 5 from pylons_app.lib import auth
6 6 from pylons_app.lib.base import BaseController, render
7
7 from pylons_app.model import meta
8 from pylons_app.model.db import Users, UserLogs
8 9 log = logging.getLogger(__name__)
9 10
10 11 class ReposController(BaseController):
@@ -16,6 +17,7 b' class ReposController(BaseController):'
16 17 c.staticurl = g.statics
17 18 c.admin_user = session.get('admin_user')
18 19 c.admin_username = session.get('admin_username')
20 self.sa = meta.Session
19 21
20 22 def index(self, format='html'):
21 23 """GET /repos: All items in the collection"""
@@ -4,7 +4,9 b' from pylons import request, response, se'
4 4 from pylons.controllers.util import abort, redirect
5 5
6 6 from pylons_app.lib.base import BaseController, render
7 from pylons_app.lib import auth
7 from formencode import htmlfill
8 from pylons_app.model import meta
9 from pylons_app.model.db import Users, UserLogs
8 10 log = logging.getLogger(__name__)
9 11
10 12 class UsersController(BaseController):
@@ -16,14 +18,13 b' class UsersController(BaseController):'
16 18 c.staticurl = g.statics
17 19 c.admin_user = session.get('admin_user')
18 20 c.admin_username = session.get('admin_username')
19 self.conn, self.cur = auth.get_sqlite_conn_cur()
21 self.sa = meta.Session
20 22
21 23 def index(self, format='html'):
22 24 """GET /users: All items in the collection"""
23 25 # url('users')
24 26
25 self.cur.execute('SELECT * FROM users')
26 c.users_list = self.cur.fetchall()
27 c.users_list = self.sa.query(Users).all()
27 28 return render('/users.html')
28 29
29 30 def create(self):
@@ -52,20 +53,24 b' class UsersController(BaseController):'
52 53 # method='delete')
53 54 # url('user', id=ID)
54 55 try:
55 self.cur.execute("DELETE FROM users WHERE user_id=?", (id,))
56 self.conn.commit()
56 self.sa.delete(self.sa.query(Users).get(id))
57 self.sa.commit()
57 58 except:
58 self.conn.rollback()
59 self.sa.rollback()
59 60 raise
60 61 return redirect(url('users'))
61 62
62 63 def show(self, id, format='html'):
63 64 """GET /users/id: Show a specific item"""
64 65 # url('user', id=ID)
65 self.cur.execute("SELECT * FROM users WHERE user_id=?", (id,))
66 ret = self.cur.fetchone()
67 c.user_name = ret[1]
68 return render('/users_show.html')
66 c.user = self.sa.query(Users).get(id)
67
68 return htmlfill.render(
69 render('/users_show.html'),
70 defaults=c.user.__dict__,
71 encoding="UTF-8",
72 force_defaults=False
73 )
69 74
70 75 def edit(self, id, format='html'):
71 76 """GET /users/id/edit: Form to edit an existing item"""
@@ -32,14 +32,14 b''
32 32 <th>Admin</th>
33 33 <th>Action</th>
34 34 </tr>
35 %for i in c.users_list:
35 %for user in c.users_list:
36 36 <tr>
37 <td>${i[0]}</td>
38 <td>${h.link_to(i[1],h.url('user', id=i[0]))}</td>
39 <td>${i[3]}</td>
40 <td>${i[4]}</td>
37 <td>${user.user_id}</td>
38 <td>${h.link_to(user.username,h.url('user', id=user.user_id))}</td>
39 <td>${user.active}</td>
40 <td>${user.admin}</td>
41 41 <td>
42 ${h.form(url('user', id=i[0]),method='delete')}
42 ${h.form(url('user', id=user.user_id),method='delete')}
43 43 ${h.submit('remove','remove',class_="submit")}
44 44 ${h.end_form()}
45 45 </td>
@@ -1,6 +1,6 b''
1 1 <%inherit file="base/base.html"/>
2 2 <%def name="title()">
3 ${_('User c.user_name')}
3 ${_('User')} - ${c.user.username}
4 4 </%def>
5 5 <%def name="breadcrumbs()">
6 6 ${h.link_to(u'Home',h.url('/'))}
@@ -23,6 +23,28 b''
23 23 </li>
24 24 </ul>
25 25 <div>
26 <h2>${_('User')} - ${c.user_name}</h2>
26 <h2>${_('User')} - ${c.user.username}</h2>
27 ${h.form(url('user', id=c.user.user_id),method='put')}
28 <table>
29 <tr>
30 <td>${_('Username')}</td>
31 <td>${h.text('username')}</td>
32 </tr>
33 <tr>
34 <td>${_('New password')}</td>
35 <td>${h.text('new_password')}</td>
36 </tr>
37 <tr>
38 <td>${_('Active')}</td>
39 <td>${h.checkbox('active')}</td>
40 </tr>
41 <tr>
42 <td></td>
43 <td>${h.submit('save','save')}</td>
44 </tr>
45
46 </table>
47
48 ${h.end_form()}
27 49 </div>
28 50 </%def> No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now