Permalink
Browse files

[svn] * Added restcontroller command that generates a RESTful control…

…ler template

  and provides the appropriate map.resource command to add. Suggested by
  Matthew Scott.

--HG--
branch : trunk
  • Loading branch information...
bbangert
bbangert committed Jan 5, 2007
1 parent ead4636 commit a28d75c6d69b74faac70cd9eb696dd46a8222ea8
Showing with 156 additions and 0 deletions.
  1. +3 −0 CHANGELOG
  2. +105 −0 pylons/commands.py
  3. +47 −0 pylons/templates/restcontroller.py_tmpl
  4. +1 −0 setup.py
View
@@ -2,6 +2,9 @@ Pylons Changelog
=================
0.9.5 (**svn**)
+* Added restcontroller command that generates a RESTful controller template
+ and provides the appropriate map.resource command to add. Suggested by
+ Matthew Scott.
* Fixed SQLObject pylons.database.PackageHub error:
exceptions.NameError: global name 'CONFIG' is not defined
* Fixed pylons.database.session_context not working outside of requests
View
@@ -120,6 +120,111 @@ def command(self):
msg = str(sys.exc_info()[1])
raise BadCommand('An unknown error occurred. %s' % msg)
+class RestControllerCommand(Command):
+ """Create a REST Controller and accompanying functional test
+
+ The RestController command will create a REST-based Controller file for use
+ with the map.resource REST-based dispatching. This template includes the
+ methods that map.resource dispatches to in addition to doc strings for
+ clarification on when the methods will be called.
+
+ Example usage::
+
+ yourproj% paster restcontroller comment comments
+ Creating yourproj/yourproj/controllers/comments.py
+ Creating yourproj/yourproj/tests/functional/test_comments.py
+
+ If you'd like to have controllers underneath a directory, just include
+ the path as the controller name and the necessary directories will be
+ created for you::
+
+ yourproj% paster restcontroller admin/tracback admin/trackbacks
+ Creating yourproj/controllers/admin
+ Creating yourproj/yourproj/controllers/admin/trackbacks.py
+ Creating yourproj/yourproj/tests/functional/test_admin_trackbacks.py
+ """
+ summary = __doc__.splitlines()[0]
+ usage = 'CONTROLLER_NAME'
+
+ min_args = 2
+ max_args = 2
+ group_name = 'pylons'
+
+ default_verbosity = 3
+
+ parser = Command.standard_parser(simulate=True)
+ parser.add_option('--no-test',
+ action='store_true',
+ dest='no_test',
+ help="Don't create the test; just the controller")
+
+ def command(self):
+ """Main command to create controller"""
+ try:
+ file_op = FileOp(source_dir=os.path.join(
+ os.path.dirname(__file__), 'templates'))
+ try:
+ singularname, singulardirectory = file_op.parse_path_name_args(self.args[0])
+ pluralname, pluraldirectory = file_op.parse_path_name_args(self.args[1])
+ except:
+ raise BadCommand('No egg_info directory was found')
+
+ # Check the name isn't the same as the package
+ base_package = file_op.find_dir('controllers', True)[0]
+ if base_package.lower() == pluralname.lower():
+ raise BadCommand(
+ 'Your controller name should not be the same as '
+ 'the package name %r.'% base_package
+ )
+ # Validate the name
+ for name in [singularname, pluralname]:
+ name = name.replace('-', '_')
+ validate_name(name)
+
+ # Setup the controller
+ fullname = os.path.join(pluraldirectory, pluralname)
+ controller_name = util.class_name_from_module_name(
+ pluralname.split('/')[-1])
+ if not fullname.startswith(os.sep):
+ fullname = os.sep + fullname
+ testname = fullname.replace(os.sep, '_')[1:]
+
+ nameprefix = ''
+ if pluraldirectory:
+ nameprefix = pluraldirectory.replace(os.path.sep, '_') + '_'
+
+ file_op.template_vars.update(
+ {'classname': controller_name,
+ 'pluralname': pluralname,
+ 'singularname': singularname,
+ 'name': controller_name,
+ 'nameprefix': nameprefix,
+ 'fname': os.path.join(pluraldirectory, pluralname)}
+ )
+
+ resource_command = "\nTo create the appropriate RESTful mapping, add a map statement to your\n"
+ resource_command += "config/routing.py file near the top like this:\n\n"
+ controller_c = ''
+ if nameprefix:
+ controller_c = ", controller='%s', \n\t" % '/'.join([pluraldirectory, pluralname])
+ controller_c += "path_prefix='/%s', name_prefix='%s_'" % (pluraldirectory, pluraldirectory)
+ command = "map.resource('%s', '%s'%s)\n" % (singularname, pluralname, controller_c)
+ resource_command += command
+ file_op.copy_file(template='restcontroller.py_tmpl',
+ dest=os.path.join('controllers', pluraldirectory),
+ filename=pluralname)
+ if not self.options.no_test:
+ file_op.copy_file(template='test_controller.py_tmpl',
+ dest=os.path.join('tests', 'functional'),
+ filename='test_'+testname)
+ print resource_command
+ except BadCommand, e:
+ raise BadCommand('An error occurred. %s' % e)
+ except:
+ msg = str(sys.exc_info()[1])
+ raise BadCommand('An unknown error occurred. %s' % msg)
+
+
class ShellCommand(Command):
"""Open an interactive shell with the Pylons app loaded
@@ -0,0 +1,47 @@
+from ${base_package}.lib.base import *
+
+class ${classname}Controller(BaseController):
+ """REST Controller styled on the Atom Publishing Protocol"""
+ def index(self, format='html'):
+ """GET /: All items in the collection."""
+ # url_for('${nameprefix}${pluralname}')
+ pass
+
+ def create(self):
+ """POST /: Create a new item."""
+ # url_for('${nameprefix}${pluralname}')
+ pass
+
+ def new(self, format='html'):
+ """GET /new: Form to create a new item."""
+ # url_for('${nameprefix}new_${singularname}')
+ pass
+
+ def update(self, id):
+ """PUT /id: Update an existing item."""
+ # Forms posted to this method should contain a hidden field:
+ # <input type="hidden" name="_method" value="PUT" />
+ # Or using helpers:
+ # h.form(h.url_for('${nameprefix}${singularname}', id=ID), method='put')
+ # url_for('${nameprefix}${singularname}', id=ID)
+ pass
+
+ def delete(self, id):
+ """DELETE /id: Delete an existing item."""
+ # Forms posted to this method should contain a hidden field:
+ # <input type="hidden" name="_method" value="DELETE" />
+ # Or using helpers:
+ # h.form(h.url_for('${nameprefix}${singularname}', id=ID), method='delete')
+ # url_for('${nameprefix}${singularname}', id=ID)
+ pass
+
+ def show(self, id, format='html'):
+ """GET /id: Show a specific item."""
+ # url_for('${nameprefix}${singularname}', id=ID)
+ pass
+
+ def edit(self, id, format='html'):
+ """GET /id;edit: Form to edit an existing item."""
+ # url_for('${nameprefix}edit_${singularname}', id=ID)
+ pass
+
View
@@ -97,6 +97,7 @@
entry_points="""
[paste.paster_command]
controller=pylons.commands:ControllerCommand
+ restcontroller=pylons.commands:RestControllerCommand
shell=pylons.commands:ShellCommand
[paste.paster_create_template]

0 comments on commit a28d75c

Please sign in to comment.