Skip to content

Commit

Permalink
Working session backend, added FormEncode
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrusev committed Dec 13, 2011
1 parent e366fda commit ee58485
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 13 deletions.
6 changes: 6 additions & 0 deletions amon/web/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import formencode
from formencode import validators

class CreateUserForm(formencode.Schema):
username = validators.String(not_empty=True)
password = validators.String(not_empty=True)
11 changes: 4 additions & 7 deletions amon/web/libs/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
Sessions module for the Tornado framework.
Milan Cermak <[email protected]>
This module implements sessions for Tornado. It can store
session data in files or MySQL databse, Memcached, Redis
and MongoDB.
USAGE:
======
Expand Down Expand Up @@ -198,7 +194,7 @@ def _next_regeneration_at(self):
return datetime.datetime.utcnow() + self.regeneration_interval

def invalidate(self):
"""Destorys the session, both server-side and client-side.
"""Destroys the session, both server-side and client-side.
As a best practice, it should be used when the user logs out of
the application."""
self.delete() # remove server-side
Expand Down Expand Up @@ -267,7 +263,7 @@ def deserialize(datastring):

class MongoDBSession(BaseSession):
"""Class implementing the MongoDB based session storage.
All sessions are stored in a collection "tornado_sessions" in the db
All sessions are stored in a collection "sessions" in the db
you specify in the session_storage setting.
The session document structure is following:
Expand All @@ -284,7 +280,7 @@ class MongoDBSession(BaseSession):
def __init__(self, **kwargs):
super(MongoDBSession, self).__init__(**kwargs)

self.db = mongo # pymongo Collection object - amon_sessions
self.db = mongo # pymongo Collection object - sessions
if not kwargs.has_key('session_id'):
self.save()

Expand All @@ -305,6 +301,7 @@ def save(self):
'user_agent': self.user_agent}, # new document
upsert=True)
self.db.database.connection.end_request()
self.dirty = False

@staticmethod
def load(session_id):
Expand Down
3 changes: 2 additions & 1 deletion amon/web/templates/create_user.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
To access the application, please create a user using the form below.
</div>
<div class="login_box">
<form action="{{ base_url|url('create_user') }}" method='post'>
<ul>
<li>
<label for="" class='background-label username'></label>
Expand All @@ -29,7 +30,7 @@
</li>

</ul>

</form>
</div>

</div>
Expand Down
21 changes: 21 additions & 0 deletions amon/web/views/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from amon.web.views.base import BaseView
from amon.web.template import render
from amon.web.forms import CreateUserForm
from formencode.validators import Invalid as InvalidForm



class LoginView(BaseView):

Expand All @@ -18,8 +22,25 @@ def initialize(self):


def get(self):
try:
print repr(self.session)
except:
pass
_template = render(template='create_user.html')
self.write(_template)

def post(self):
form_data = {
"username": self.get_argument('username', None),
"password": self.get_argument('password', None),
}
try:
valid_data = CreateUserForm.to_python(form_data)
except InvalidForm, e:
self.session['errors'] = e.unpack_errors()
self.save_session()
self.redirect('/create_user')




9 changes: 8 additions & 1 deletion amon/web/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def write_error(self, status_code, **kwargs):
self.write(_template)

def _create_session(self):
session_id = self.get_secure_cookie('session_cookie_name', 'session_id')
session_id = self.get_secure_cookie('session_id')

kw = {'security_model': [],
'duration': 900,
Expand All @@ -55,4 +55,11 @@ def _create_session(self):

return new_session

def save_session(self):
if self.session is not None and self.session._delete_cookie:
self.clear_cookie('session_id')
elif self.session is not None:
self.session.refresh() # advance expiry time and save session
self.set_secure_cookie('session_id', self.session.session_id, expires=self.session.expires)


3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ def read(filename):
'data_files' : data_files,
'install_requires':
[
'pymongo==2.0',
'pymongo>=2.0',
'tornado>=2.0',
'formencode==1.2.4',
'Jinja2>=2.4',
'pip'
],
Expand Down
1 change: 0 additions & 1 deletion tests/web/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def setUp(self):
self.model = CommonModel()
self.model.mongo.database = 'amon_test'


# TODO - save some real exceptions and check if they are properly saved
def test_unread(self):
unread = self.model.get_unread_values()
Expand Down
14 changes: 12 additions & 2 deletions tests/web/session_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from amon.web.server import application
from tornado.httpclient import HTTPRequest
from nose.tools import eq_
from amon.backends.mongodb import MongoBackend

mongo = MongoBackend()
mongo.database = 'amon'

class DummyRequest(HTTPRequest):

Expand All @@ -26,11 +30,17 @@ def initialize(self):

class SessionTest(unittest.TestCase):


def setUp(self):
self.collection = mongo.get_collection('sessions')
self.collection.remove()

def test_session_add(self):
test_class.session['key'] = 'value'
test_class.session.save()
eq_(1, self.collection.count())
eq_(test_class.session['key'], 'value')






Expand Down

0 comments on commit ee58485

Please sign in to comment.