[Jifty-commit] r2612 - in jifty/trunk: lib/Jifty/Plugin/REST
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Fri Jan 26 19:03:00 EST 2007
Author: trs
Date: Fri Jan 26 19:03:00 2007
New Revision: 2612
Modified:
jifty/trunk/ (props changed)
jifty/trunk/lib/Jifty/Plugin/REST/Dispatcher.pm
Log:
r19117 at zot: tom | 2007-01-26 15:52:46 -0500
Implementation for PUT and DELETE on model items
Modified: jifty/trunk/lib/Jifty/Plugin/REST/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/REST/Dispatcher.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/REST/Dispatcher.pm Fri Jan 26 19:03:00 2007
@@ -58,16 +58,13 @@
on GET /=/model/<model>/<column>/<key> show item
on GET /=/model/<model>/<column>/<key>/<field> show item field
-on PUT /=/model/<model>/<column>/<key> replace item -- UNIMPLEMENTED!
-on DELETE /=/model/<model>/<column>/<key> delete item -- UNIMPLEMENTED!
+on PUT /=/model/<model>/<column>/<key> replace item
+on DELETE /=/model/<model>/<column>/<key> delete item
on GET /=/action list actions
on GET /=/action/<action> list action params
on POST /=/action/<action> run action
-Note that the PUT and DELETE methods on models are essentially available through
-Update<Model> and Delete<Model> actions.
-
Resources are available in a variety of formats:
@@ -423,22 +420,66 @@
=head2 replace_item
-UNIMPLEMENTED
+Implemented by redispatching to a CreateModel or UpdateModel action
=cut
-sub replace_item {
- die "hey replace item";
-}
+sub replace_item { _dispatch_to_action('Update') }
=head2 delete_item
-UNIMPLEMENTED
+Implemented by redispatching to a DeleteModel action.
=cut
-sub delete_item {
- die "hey delete item";
+sub delete_item { _dispatch_to_action('Delete') }
+
+sub _dispatch_to_action {
+ my $prefix = shift;
+ my ($model, $class, $column, $key) = (model($1), $1, $2, $3);
+ my $rec = $model->new;
+ $rec->load_by_cols( $column => $key );
+
+ if ( not $rec->id ) {
+ abort(404) if $prefix eq 'Delete';
+ $prefix = 'Create' if $prefix eq 'Update';
+ }
+
+ $class =~ s/^[\w\.]+\.//;
+
+ $ENV{REQUEST_METHOD} = 'POST';
+ if ( defined $rec->id ) {
+ Jifty->web->request->argument( 'id' => $rec->id );
+ }
+
+ # CGI.pm doesn't handle form encoded data in PUT requests (in fact,
+ # it doesn't really handle PUT requests properly at all), so we have
+ # to read the request body ourselves and have CGI.pm parse it
+ if ( $ENV{'CONTENT_TYPE'} =~ m|^application/x-www-form-urlencoded$|
+ or $ENV{'CONTENT_TYPE'} =~ m|^multipart/form-data$| )
+ {
+ my $cgi = Jifty->handler->cgi;
+ my $length = defined $ENV{'CONTENT_LENGTH'} ? $ENV{'CONTENT_LENGTH'} : 0;
+ my $data;
+
+ $cgi->read_from_client( \$data, $length, 0 )
+ if $length > 0;
+
+ if ( defined $data ) {
+ my @params = $cgi->all_parameters;
+ $cgi->parse_params( $data );
+ push @params, $cgi->all_parameters;
+
+ my %seen;
+ my @uniq = map { $seen{$_}++ == 0 ? $_ : () } @params;
+
+ # Add only the newly parsed arguments to the Jifty::Request
+ Jifty->web->request->argument( $_ => $cgi->param( $_ ) )
+ for @uniq;
+ }
+ }
+
+ dispatch '/=/action/' . action( $prefix . $class );
}
=head2 list_actions
@@ -540,14 +581,14 @@
sub run_action {
my ($action_name) = action($1) or abort(404);
Jifty::Util->require($action_name) or abort(404);
- my $action = $action_name->new or abort(404);
-
- Jifty->api->is_allowed( $action_name ) or abort(403);
-
+
my $args = Jifty->web->request->arguments;
delete $args->{''};
- $action->argument_values({ %$args });
+ my $action = $action_name->new( arguments => $args ) or abort(404);
+
+ Jifty->api->is_allowed( $action_name ) or abort(403);
+
$action->validate;
local $@;
More information about the Jifty-commit
mailing list