Hello,
I am writing a plugin using trac 0.12.2 and have come up against an issue when
writing changes to the database. When I try to do it "properly" (according to
the api notes) it stops working. I've read #9968 and #9536 but I do not think
they apply...
Basically, when handling a POST to change a session ID I first check (*) if the
new session ID exists. If it does, then I use a with_transaction() block to
make the changes. The problem (*) is how I do the check:
A) use deprecated `get_db_cnx` and the transaction commits OK.
B) use `get_read_db` and the transaction appears to succeed but the changes are
lost.
I wondered if this might be due to using req.redirect but that works ok when
using get_db_cnx() so it looks to me like the first db request outlives the
scope of _session_exists() but I get no errors or exceptions when using
`with_transaction` (you may be able to guess that I am mostly a (windoze) C++
programmer learning python).
The code outline below describes the problem, can someone help me understand
how I should be doing this for it to work?
Many thanks,
~ mark c
P.S. Would this be better on trac-dev rather than trac-users?
Environmanet:-
Trac 0.12.2
Genshi 0.6
pysqlite 2.4.1
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]
setuptools 0.6c11
SQLite 3.5.9
jQuery: 1.4.2
Plugin code:-
{{{
class MyAdminPanel(Component):
implements(IAdminPanelProvider, ITemplateProvider)
def render_admin_panel(self, req, cat, page, path_info):
data = None
templ_html = 'admin_sessions.html'
# Do we have a specific session to edit?
if path_info:
data = self._edit_session(req, cat, page, path_info)
if not data:
data = self._list_sessions(req)
return templ_html, data
def _session_exists(self, sid):
sid_exists = False
## (*) use one of these two lines to make it work (or not) !
db = get_read_db(self.env)
#db = self.env.get_db_cnx()
cursor = db.cursor()
cursor.execute("SELECT sid FROM session WHERE sid=%s", (sid,))
if cursor.fetchone():
sid_exists = True
return sid_exists
def _edit_session(self, req, cat, page, path_info):
# Check for a session for user `path_info`...
data = None
sdata = self.get_user_session(path_info)
if sdata:
if req.method == 'POST':
if req.args.get('save'):
# declare sqls, args and msgs as lists
if sdata.user != req.args.get('username'):
# first check for a clash (a la session.py)...
if self._session_exists(req.args.get('username')):
raise TracError( (blah) )
(generate sql and args to update the required tables)
if sqls:
# apply the changes...
try:
@self.env.with_transaction()
def _update_session(db):
crsr = db.cursor()
for idx in range(len(sqls)):
crsr.execute(sqls[idx], args[idx])
# success! add messages for feedback...
for msg in msgs:
add_notice(req, msg)
except:
add_warning(req, _( <blah> ))
else:
add_warning(req, _('No changes detected?'))
else:
add_warning(req, _('Unrecognised POST request!'))
# redirect to clear the POST data.
req.redirect(req.href.admin(cat, page, path_info))
# setup data...
data = { 'session': sdata, 'view' : 'detail' }
return data
}}}
--
You received this message because you are subscribed to the Google Groups "Trac
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/trac-users?hl=en.