RESTful + Controller = Restler
Restler is a base controller for Pylons projects that provides a set of default RESTful actions that can be overridden as needed.
Quick Start
Install Pylons 0.9.7
easy_install Pylons==dev
Development of Restler is currently done with Pylons 0.9.7rc1dev
Install Restler
easy_install -U Restler
Or, install from the latest trunk:
svn checkout http://restler.googlecode.com/svn/trunk/ Restler cd Restler python setup.py develop
Note: If you have problems with the easy_installed version of Restler, try the SVN version, which may have fixes that I haven't yet uploaded to PyPI.
Create a new Pylons project
paster create --template=pylons MyProject cd MyProject
Add your database configuration to development.ini
Add a line like this to the [app:main] section:
sqlalchemy.dburi = <db_type>://<user>:<password>@<host>/<database>
Replace <db_type>, <user>, <password>, <host>, and <database> to reflect your setup. For example:
sqlalchemy.dburi = sqlite:///%(here)s/myproject.db
Create a base RestController that's aware of your project's model
Open myproject/lib/base.py and add the following lines below the existing imports:
import restler RestController = restler.make_rest_controller(model)
You can also create a new RestController class in lib.base that inherits from restler.BaseController. Here's an example that shows a quick and dirty way to secure the actions that cause modifications:
class RestController(restler.make_rest_controller(model)):
def __call__(self, environ, start_response):
return super(RestController, self).__call__(environ, start_response)
new = edit = create = update = delete = lambda *args, **kwargs: abort(403)In real life, you'll probably want to use AuthKit or something similar to protect these actions.
Note: The default BaseController generated by paster when creating a new Pylons project is used by the default error and template controllers.
Create some SQLAlchemy mapped orm classes in model/init.py
Coming soon!
Create a controller for each of the Entity classes declared above
Coming soon!
Map URLs for resources/entities to controllers
Coming soon!
Create the database tables for the entity classes you declared above
Coming soon!
Fire up your Pylons app and try it out
paster serve --reload development.ini
You should now be able to Create, Read, Update, and Delete resources.
Look at all the things I'm not doing
Coming soon!
Epilogue
Restler was originally extracted from the byCycle.org Bicycle Trip Planner (http://bycycle.org).
Send feedback, corrections, et cetera to wyatt .DOT. lee .DOT. baldwin .AT. gmail .DOT. com or create an issue.