Skip to content

Commit 0386030

Browse files
author
Richard Jones
committed
improvements to session management
1 parent e3cf39d commit 0386030

File tree

7 files changed

+175
-101
lines changed

7 files changed

+175
-101
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed:
1717
- Add filter() to XML-RPC interface (sf patch #1966456)
1818
- Fix indexerror when there are no messages to an issue (sf patch #1894249)
1919
- Prevent broken pipe errors in csv export (sf patch #1911449)
20+
- Session API and cleanup thanks anatoly t.
2021

2122

2223
2008-03-01 1.4.4

doc/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Nathaniel Smith,
170170
Leonardo Soto,
171171
Maciej Starzyk,
172172
Mitchell Surface,
173+
Anatoly T.,
173174
Jon C. Thomason
174175
Mike Thompson,
175176
Michael Twomey,

roundup/backends/sessions_dbm.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#$Id: sessions_dbm.py,v 1.9 2007-09-27 06:18:53 jpend Exp $
1+
#$Id: sessions_dbm.py,v 1.10 2008-08-18 05:04:01 richard Exp $
22
"""This module defines a very basic store that's used by the CGI interface
33
to store session and one-time-key information.
44
@@ -139,15 +139,15 @@ def updateTimestamp(self, sessid):
139139
if sess is None or now > sess + 60:
140140
self.set(sessid, __timestamp=now)
141141

142-
def clean(self, now):
143-
"""Age sessions, remove when they haven't been used for a week.
144-
"""
142+
def clean(self):
143+
''' Remove session records that haven't been used for a week. '''
144+
now = time.time()
145145
week = 60*60*24*7
146146
for sessid in self.list():
147147
sess = self.get(sessid, '__timestamp', None)
148148
if sess is None:
149-
sess=time.time()
150149
self.updateTimestamp(sessid)
150+
continue
151151
interval = now - sess
152152
if interval > week:
153153
self.destroy(sessid)

roundup/backends/sessions_rdbms.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#$Id: sessions_rdbms.py,v 1.7 2007-09-25 19:49:19 jpend Exp $
1+
#$Id: sessions_rdbms.py,v 1.8 2008-08-18 05:04:01 richard Exp $
22
"""This module defines a very basic store that's used by the CGI interface
33
to store session and one-time-key information.
44
@@ -84,10 +84,11 @@ def updateTimestamp(self, infoid):
8484
self.name, self.db.arg, self.name, self.db.arg),
8585
(now, infoid, now-60))
8686

87-
def clean(self, now):
88-
"""Age sessions, remove when they haven't been used for a week.
89-
"""
90-
old = now - 60*60*24*7
87+
def clean(self):
88+
''' Remove session records that haven't been used for a week. '''
89+
now = time.time()
90+
week = 60*60*24*7
91+
old = now - week
9192
self.cursor.execute('delete from %ss where %s_time < %s'%(self.name,
9293
self.name, self.db.arg), (old, ))
9394

roundup/cgi/TranslationService.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# translate(domain, msgid, mapping, context, target_language, default)
1414
#
1515

16-
__version__ = "$Revision: 1.5 $"[11:-2]
17-
__date__ = "$Date: 2008-08-07 05:51:32 $"[7:-2]
16+
__version__ = "$Revision: 1.6 $"[11:-2]
17+
__date__ = "$Date: 2008-08-18 05:04:01 $"[7:-2]
1818

1919
from roundup import i18n
2020
from roundup.cgi.PageTemplates import Expressions, PathIterator, TALES
@@ -38,8 +38,6 @@ def gettext(self, msgid):
3838
if not isinstance(msgid, unicode):
3939
msgid = unicode(msgid, 'utf8')
4040
msgtrans=self.ugettext(msgid)
41-
if not isinstance(msgtrans,unicode):
42-
msgtrans=unicode(msgtrans, 'utf8')
4341
return msgtrans.encode(self.OUTPUT_ENCODING)
4442

4543
def ngettext(self, singular, plural, number):
@@ -48,8 +46,6 @@ def ngettext(self, singular, plural, number):
4846
if not isinstance(plural, unicode):
4947
plural = unicode(plural, 'utf8')
5048
msgtrans=self.ungettext(singular, plural, number)
51-
if not isinstance(msgtrans,unicode):
52-
msgtrans=unicode(msgtrans, 'utf8')
5349
return msgtrans.encode(self.OUTPUT_ENCODING)
5450

5551
class TranslationService(TranslationServiceMixin, i18n.RoundupTranslations):

roundup/cgi/actions.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#$Id: actions.py,v 1.72 2008-08-07 06:33:00 richard Exp $
1+
#$Id: actions.py,v 1.73 2008-08-18 05:04:01 richard Exp $
22

3-
import re, cgi, StringIO, urllib, Cookie, time, random, csv, codecs
3+
import re, cgi, StringIO, urllib, time, random, csv, codecs
44

55
from roundup import hyperdb, token, date, password
66
from roundup.i18n import _
@@ -741,13 +741,8 @@ def finishRego(self):
741741
# re-open the database for real, using the user
742742
self.client.opendb(user)
743743

744-
# if we have a session, update it
745-
if hasattr(self.client, 'session'):
746-
self.client.db.getSessionManager().set(self.client.session,
747-
user=user, last_use=time.time())
748-
else:
749-
# new session cookie
750-
self.client.set_cookie(user, expire=None)
744+
# update session data
745+
self.client.session_api.set(user=user)
751746

752747
# nice message
753748
message = self._('You are now registered, welcome!')
@@ -779,7 +774,7 @@ class RegisterAction(RegoCommon, EditCommon):
779774

780775
def handle(self):
781776
"""Attempt to create a new user based on the contents of the form
782-
and then set the cookie.
777+
and then remember it in session.
783778
784779
Return 1 on successful login.
785780
"""
@@ -876,15 +871,10 @@ def handle(self):
876871

877872
class LogoutAction(Action):
878873
def handle(self):
879-
"""Make us really anonymous - nuke the cookie too."""
874+
"""Make us really anonymous - nuke the session too."""
880875
# log us out
881876
self.client.make_user_anonymous()
882-
883-
# construct the logout cookie
884-
now = Cookie._getdate()
885-
self.client.additional_headers['Set-Cookie'] = \
886-
'%s=deleted; Max-Age=0; expires=%s; Path=%s;' % (
887-
self.client.cookie_name, now, self.client.cookie_path)
877+
self.client.session_api.destroy()
888878

889879
# Let the user know what's going on
890880
self.client.ok_message.append(self._('You are logged out'))
@@ -924,11 +914,10 @@ def handle(self):
924914
# now we're OK, re-open the database for real, using the user
925915
self.client.opendb(self.client.user)
926916

927-
# set the session cookie
917+
# save user in session
918+
self.client.session_api.set(user=self.client.user)
928919
if self.form.has_key('remember'):
929-
self.client.set_cookie(self.client.user, expire=86400*365)
930-
else:
931-
self.client.set_cookie(self.client.user, expire=None)
920+
self.client.session_api.update(set_cookie=True, expire=24*3600*365)
932921

933922
# If we came from someplace, go back there
934923
if self.form.has_key('__came_from'):

0 commit comments

Comments
 (0)