summaryrefslogtreecommitdiff
path: root/docs/quick_tutorial/sessions/tutorial
diff options
context:
space:
mode:
Diffstat (limited to 'docs/quick_tutorial/sessions/tutorial')
-rw-r--r--docs/quick_tutorial/sessions/tutorial/__init__.py13
-rw-r--r--docs/quick_tutorial/sessions/tutorial/home.pt10
-rw-r--r--docs/quick_tutorial/sessions/tutorial/tests.py44
-rw-r--r--docs/quick_tutorial/sessions/tutorial/views.py29
4 files changed, 96 insertions, 0 deletions
diff --git a/docs/quick_tutorial/sessions/tutorial/__init__.py b/docs/quick_tutorial/sessions/tutorial/__init__.py
new file mode 100644
index 000000000..34ad1087e
--- /dev/null
+++ b/docs/quick_tutorial/sessions/tutorial/__init__.py
@@ -0,0 +1,13 @@
+from pyramid.config import Configurator
+from pyramid.session import UnencryptedCookieSessionFactoryConfig
+
+
+def main(global_config, **settings):
+ my_session_factory = UnencryptedCookieSessionFactoryConfig(
+ 'itsaseekreet')
+ config = Configurator(settings=settings,
+ session_factory=my_session_factory)
+ config.add_route('home', '/')
+ config.add_route('hello', '/howdy')
+ config.scan('.views')
+ return config.make_wsgi_app() \ No newline at end of file
diff --git a/docs/quick_tutorial/sessions/tutorial/home.pt b/docs/quick_tutorial/sessions/tutorial/home.pt
new file mode 100644
index 000000000..0b27ba1d8
--- /dev/null
+++ b/docs/quick_tutorial/sessions/tutorial/home.pt
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <title>Quick Tour: ${name}</title>
+</head>
+<body>
+<h1>Hi ${name}</h1>
+<p>Count: ${view.counter}</p>
+</body>
+</html> \ No newline at end of file
diff --git a/docs/quick_tutorial/sessions/tutorial/tests.py b/docs/quick_tutorial/sessions/tutorial/tests.py
new file mode 100644
index 000000000..4381235ec
--- /dev/null
+++ b/docs/quick_tutorial/sessions/tutorial/tests.py
@@ -0,0 +1,44 @@
+import unittest
+
+from pyramid import testing
+
+
+class TutorialViewTests(unittest.TestCase):
+ def setUp(self):
+ self.config = testing.setUp()
+
+ def tearDown(self):
+ testing.tearDown()
+
+ def test_home(self):
+ from .views import TutorialViews
+
+ request = testing.DummyRequest()
+ inst = TutorialViews(request)
+ response = inst.home()
+ self.assertEqual('Home View', response['name'])
+
+ def test_hello(self):
+ from .views import TutorialViews
+
+ request = testing.DummyRequest()
+ inst = TutorialViews(request)
+ response = inst.hello()
+ self.assertEqual('Hello View', response['name'])
+
+
+class TutorialFunctionalTests(unittest.TestCase):
+ def setUp(self):
+ from tutorial import main
+ app = main({})
+ from webtest import TestApp
+
+ self.testapp = TestApp(app)
+
+ def test_home(self):
+ res = self.testapp.get('/', status=200)
+ self.assertIn(b'<h1>Hi Home View', res.body)
+
+ def test_hello(self):
+ res = self.testapp.get('/howdy', status=200)
+ self.assertIn(b'<h1>Hi Hello View', res.body)
diff --git a/docs/quick_tutorial/sessions/tutorial/views.py b/docs/quick_tutorial/sessions/tutorial/views.py
new file mode 100644
index 000000000..a4659d265
--- /dev/null
+++ b/docs/quick_tutorial/sessions/tutorial/views.py
@@ -0,0 +1,29 @@
+from pyramid.view import (
+ view_config,
+ view_defaults
+ )
+
+
+@view_defaults(renderer='home.pt')
+class TutorialViews:
+ def __init__(self, request):
+ self.request = request
+
+ @property
+ def counter(self):
+ session = self.request.session
+ if 'counter' in session:
+ session['counter'] += 1
+ else:
+ session['counter'] = 1
+
+ return session['counter']
+
+
+ @view_config(route_name='home')
+ def home(self):
+ return {'name': 'Home View'}
+
+ @view_config(route_name='hello')
+ def hello(self):
+ return {'name': 'Hello View'}