1. eduardo schettino
  2. avalanche

Commits

eduardo schettino  committed 1165c6c

tutorial. step_6 uses a_request.

  • Participants
  • Parent commits 04bc143
  • Branches default

Comments (0)

Files changed (6)

File doc/tutorial/tasks/app.yaml

View file
   static_dir: static
 
 - url: /.*
-  script: tasks_5.app
+  script: tasks_6.app
 
 
 libraries:

File doc/tutorial/tasks/tasks.py

View file
-from google.appengine.ext import db
-import jinja2
-
-from avalanche.core import RequestHandler
-from avalanche.router import Route
-from avalanche.core import WSGIApplication
-from avalanche.snow import make_handler, JinjaRenderer, BaseHandler
-from avalanche.params import url_path_param, url_query_param, post_group_param
-
-
-############ Models
-
-class Task(db.Model):
-    name = db.StringProperty(required=True)
-    closed = db.BooleanProperty(default=False)
-    created = db.DateTimeProperty(auto_now_add=True)
-
-    @staticmethod
-    def id2task(task_id):
-        """@param task_id: (str)"""
-        return Task.get_by_id(int(task_id))
-
-
-############ Request Handlers
-
-class FlashMixinHandler(BaseHandler):
-    @url_query_param('flash')
-    def ctx_flash(self, flash):
-        return {'flash': flash}
-
-
-class TaskRequestHandler(FlashMixinHandler, BaseHandler):
-
-    MSG_TASK_NEW = 'New task successfully added'
-    MSG_TASK_NAME = 'Please enter a name for the task'
-    MSG_TASK_CLOSED = 'Task was successfully closed'
-
-    renderer = JinjaRenderer(jinja2.Environment(
-            loader=jinja2.FileSystemLoader('templates'),
-            undefined=jinja2.DebugUndefined,
-            autoescape=True,
-            ))
-
-    context_get = ['a_get', 'ctx_flash',]
-
-
-
-
-class ListTasks(TaskRequestHandler):
-    """list all open tasks"""
-    template = 'task_list.html'
-
-    @url_query_param('show_closed', 'closed', bool)
-    def a_get(self, show_closed):
-        if show_closed:
-            tasks = Task.all().order('created')
-        else:
-            tasks = Task.all().filter('closed = ', False).order('created')
-        return {'tasks': tasks,
-                'show_closed': show_closed,
-                }
-
-
-class NewTask(TaskRequestHandler):
-    """add a new task"""
-    template = 'task_new.html'
-
-    def a_get(self): # pragma: no cover
-        pass
-
-    @post_group_param('data', 'task')
-    def a_post(self, data):
-        name = data.get('name')
-        if name:
-            Task(name=name).put()
-            self.redirect_to('task-list', flash=self.MSG_TASK_NEW)
-        else:
-            self.redirect_to('task-new', flash=self.MSG_TASK_NAME)
-
-class CloseTask(TaskRequestHandler):
-    """mark a task as closed"""
-
-    @url_path_param('task', 'task_id', Task.id2task)
-    def a_post(self, task):
-        task.closed = True
-        task.put()
-        self.redirect_to('task-list', flash=self.MSG_TASK_CLOSED)
-
-
-########### WSGI App
-
-def tasks_app():
-    route_raw = [('/', ListTasks, 'task-list'),
-                 ('/new', NewTask, 'task-new'),
-                 ('/close/<task_id>', CloseTask, 'task-close')
-                 ]
-    routes = []
-    for path, handler, name in route_raw:
-        handler_class = make_handler(RequestHandler, handler)
-        routes.append(Route(path, handler_class, name))
-    return WSGIApplication(routes, debug=True)
-
-app = tasks_app()
+tasks_6.py

File doc/tutorial/tasks/tasks_6.py

View file
         name = data.get('name')
         if name:
             Task(name=name).put()
-            self.redirect_to('task-list', flash=self.MSG_TASK_NEW)
+            self.a_redirect('task-list', flash=self.MSG_TASK_NEW)
         else:
-            self.redirect_to('task-new', flash=self.MSG_TASK_NAME)
+            self.a_redirect('task-new', flash=self.MSG_TASK_NAME)
 
 class CloseTask(TaskRequestHandler):
     """mark a task as closed"""
     def a_post(self, task):
         task.closed = True
         task.put()
-        self.redirect_to('task-list', flash=self.MSG_TASK_CLOSED)
+        self.a_redirect('task-list', flash=self.MSG_TASK_CLOSED)
 
 
 ########### WSGI App

File doc/tutorial/tasks/tests/test_tasks.py

-import sys, os
-sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/..'))
-
-from urlparse import urlparse
-import urllib
-
-from google.appengine.ext import testbed
-from webob import Request
-from avalanche.core import RequestHandler
-from avalanche.snow import make_handler
-
-from tasks_5 import tasks_app, Task, TaskRequestHandler
-from tasks_5 import ListTasks, NewTask, CloseTask
-from tasks_5 import FlashMixinHandler
-
-# create test app
-def pytest_funcarg__app(request):
-    def create():
-        my_testbed = testbed.Testbed()
-        my_testbed.activate()
-        my_testbed.init_datastore_v3_stub()
-
-        app = tasks_app()
-        app.testbed = my_testbed
-        return app
-
-    def deactivate(app):
-        app.testbed.deactivate()
-
-    return request.cached_setup(
-        setup=create,
-        teardown=deactivate,
-        scope="function")
-
-
-def create_handler(app, handler_class, path='', POST=None, **req_kwargs):
-    """helper to setup request handler instances"""
-    request = Request.blank(path, POST=POST, **req_kwargs)
-    handler_class = make_handler(RequestHandler, handler_class)
-    return handler_class(app, request)
-
-
-
-###############################################
-
-
-class TestTaskModel(object):
-    def test_id2task(self, app):
-        task_key = Task(name='my first task').put()
-        got = Task.id2task(str(task_key.id()))
-        assert task_key == got.key()
-
-
-class TestFlahsHandler(object):
-    def test_ctx_flash(self, app):
-        handler = create_handler(app, FlashMixinHandler)
-        ctx = handler.ctx_flash(flash='flash message')
-        assert ctx['flash'] == 'flash message'
-
-
-class TestListTasks(object):
-    def test_get_display_not_closed(self, app):
-        Task(name='my first task').put()
-        Task(name='task 2', closed=True).put()
-        Task(name='task 3').put()
-
-        handler = create_handler(app, ListTasks)
-        ctx = handler.a_get(show_closed=False)
-        tasks = list(ctx['tasks'])
-        assert len(tasks) == 2 # closed task is not included
-        assert tasks[0].name == "my first task"
-        assert tasks[1].name == "task 3"
-        assert ctx['show_closed'] == False
-
-    def test_get_display_all(self, app):
-        Task(name='my first task').put()
-        Task(name='task 2', closed=True).put()
-        Task(name='task 3').put()
-
-        handler = create_handler(app, ListTasks)
-        ctx = handler.a_get(show_closed=True)
-        tasks = list(ctx['tasks'])
-        assert len(tasks) == 3
-        assert tasks[0].name == "my first task"
-        assert tasks[1].name == "task 2"
-        assert tasks[2].name == "task 3"
-        assert ctx['show_closed'] == True
-
-
-class TestNewTask(object):
-    def test_get(self, app):
-        pass
-
-    def test_post_save(self, app):
-        post_data = {'name':'task test xxx'}
-        handler = create_handler(app, NewTask)
-        handler.a_post(post_data)
-
-        # task is saved in DB
-        saved = Task.all().get()
-        assert saved
-        assert 'task test xxx' == saved.name
-
-        # page is redirected to list page
-        assert handler.response.status_int == 302
-        location = urlparse(handler.response.location)
-        assert location.path == handler.uri_for('task-list')
-        # flash message in page
-        query = "flash=" + urllib.quote_plus(TaskRequestHandler.MSG_TASK_NEW)
-        assert location.query == query
-
-
-    def test_post_error(self, app):
-        post_data = {'wrong_name': 'task test xxx'}
-        handler = create_handler(app, NewTask)
-        handler.a_post(data=post_data)
-
-        # task is not saved in DB
-        saved = Task.all().get()
-        assert not saved
-
-        # page is redirected to list page
-        assert handler.response.status_int == 302
-        location = urlparse(handler.response.location)
-        assert location.path == handler.uri_for('task-new')
-        # flash message in page
-        query = "flash=" + urllib.quote_plus(TaskRequestHandler.MSG_TASK_NAME)
-        assert location.query == query
-
-
-class TestCloseTask(object):
-    def test_post(self, app):
-        task_before = Task(name="task abc")
-        task_before.put()
-        assert not task_before.closed
-
-        handler = create_handler(app, CloseTask)
-        handler.a_post(task_before)
-
-        # task is closed
-        task_after = Task.get_by_id(task_before.key().id())
-        assert task_after.closed
-
-        # page is redirected to list page
-        assert handler.response.status_int == 302
-        location = urlparse(handler.response.location)
-        assert location.path == handler.uri_for('task-list')
-        # flash message in page
-        query = "flash=" + urllib.quote_plus(TaskRequestHandler.MSG_TASK_CLOSED)
-        assert location.query == query

File doc/tutorial/tasks/tests/test_tasks_6.py

View file
 import sys, os
 sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/..'))
 
-from urlparse import urlparse
-import urllib
+from google.appengine.ext import testbed
 
-from google.appengine.ext import testbed
-from webob import Request
-from avalanche.core import RequestHandler
-from avalanche.snow import make_handler
-
-from tasks_5 import tasks_app, Task, TaskRequestHandler
-from tasks_5 import ListTasks, NewTask, CloseTask
-from tasks_5 import FlashMixinHandler
+from tasks_6 import Task, TaskRequestHandler
+from tasks_6 import ListTasks, NewTask, CloseTask
+from tasks_6 import FlashMixinHandler
 
 # create test app
 def pytest_funcarg__app(request):
         my_testbed = testbed.Testbed()
         my_testbed.activate()
         my_testbed.init_datastore_v3_stub()
+        return my_testbed
 
-        app = tasks_app()
-        app.testbed = my_testbed
-        return app
-
-    def deactivate(app):
-        app.testbed.deactivate()
+    def deactivate(my_testbed):
+        my_testbed.deactivate()
 
     return request.cached_setup(
         setup=create,
         scope="function")
 
 
-def create_handler(app, handler_class, path='', POST=None, **req_kwargs):
-    """helper to setup request handler instances"""
-    request = Request.blank(path, POST=POST, **req_kwargs)
-    handler_class = make_handler(RequestHandler, handler_class)
-    return handler_class(app, request)
-
-
 
 ###############################################
 
 
 
 class TestFlahsHandler(object):
-    def test_ctx_flash(self, app):
-        handler = create_handler(app, FlashMixinHandler)
+    def test_ctx_flash(self):
+        handler = FlashMixinHandler()
         ctx = handler.ctx_flash(flash='flash message')
         assert ctx['flash'] == 'flash message'
 
         Task(name='task 2', closed=True).put()
         Task(name='task 3').put()
 
-        handler = create_handler(app, ListTasks)
+        handler = ListTasks()
         ctx = handler.a_get(show_closed=False)
         tasks = list(ctx['tasks'])
         assert len(tasks) == 2 # closed task is not included
         Task(name='task 2', closed=True).put()
         Task(name='task 3').put()
 
-        handler = create_handler(app, ListTasks)
+        handler = ListTasks()
         ctx = handler.a_get(show_closed=True)
         tasks = list(ctx['tasks'])
         assert len(tasks) == 3
 
 
 class TestNewTask(object):
-    def test_get(self, app):
+    def test_get(self):
         pass
 
     def test_post_save(self, app):
         post_data = {'name':'task test xxx'}
-        handler = create_handler(app, NewTask)
+        handler = NewTask()
         handler.a_post(post_data)
 
         # task is saved in DB
         assert 'task test xxx' == saved.name
 
         # page is redirected to list page
-        assert handler.response.status_int == 302
-        location = urlparse(handler.response.location)
-        assert location.path == handler.uri_for('task-list')
-        # flash message in page
-        query = "flash=" + urllib.quote_plus(TaskRequestHandler.MSG_TASK_NEW)
-        assert location.query == query
+        assert 'task-list' == handler.redirect_info[0]
+        assert {'flash':TaskRequestHandler.MSG_TASK_NEW} == handler.redirect_info[1]
 
 
     def test_post_error(self, app):
         post_data = {'wrong_name': 'task test xxx'}
-        handler = create_handler(app, NewTask)
+        handler = NewTask()
         handler.a_post(data=post_data)
 
         # task is not saved in DB
         assert not saved
 
         # page is redirected to list page
-        assert handler.response.status_int == 302
-        location = urlparse(handler.response.location)
-        assert location.path == handler.uri_for('task-new')
-        # flash message in page
-        query = "flash=" + urllib.quote_plus(TaskRequestHandler.MSG_TASK_NAME)
-        assert location.query == query
+        assert 'task-new' == handler.redirect_info[0]
+        assert {'flash':TaskRequestHandler.MSG_TASK_NAME} == handler.redirect_info[1]
 
 
 class TestCloseTask(object):
         task_before.put()
         assert not task_before.closed
 
-        handler = create_handler(app, CloseTask)
+        handler = CloseTask()
         handler.a_post(task_before)
 
         # task is closed
         assert task_after.closed
 
         # page is redirected to list page
-        assert handler.response.status_int == 302
-        location = urlparse(handler.response.location)
-        assert location.path == handler.uri_for('task-list')
-        # flash message in page
-        query = "flash=" + urllib.quote_plus(TaskRequestHandler.MSG_TASK_CLOSED)
-        assert location.query == query
+        assert 'task-list' == handler.redirect_info[0]
+        assert {'flash':TaskRequestHandler.MSG_TASK_CLOSED} == handler.redirect_info[1]

File doc/understanding.rst

View file
 ++++++++++++++++
 
 Create a WSGI application mapping URI's to handlers and adding some config values.
+This should really be in a different file but we will keep eveything in one module
+to make it easier to follow the tutorial...
 
 .. code-block:: python