[Jifty-commit] r579 - in jifty/trunk: lib/Jifty lib/Jifty/Web t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Feb 13 19:47:09 EST 2006


Author: alexmv
Date: Mon Feb 13 19:47:08 2006
New Revision: 579

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Request.pm
   jifty/trunk/lib/Jifty/Web.pm
   jifty/trunk/lib/Jifty/Web/PageRegion.pm
   jifty/trunk/t/02-form-protocol.t

Log:
 r9083 at zoq-fot-pik:  chmrr | 2006-02-13 19:46:06 -0500
  * During Jifty::Web->handle_request, only validate once if possible
  * During from_webform handling, transform into data structure to
    reduce code duplicaction


Modified: jifty/trunk/lib/Jifty/Request.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Request.pm	(original)
+++ jifty/trunk/lib/Jifty/Request.pm	Mon Feb 13 19:47:08 2006
@@ -145,7 +145,8 @@
         }
         $self->add_action(moniker   => $a->{moniker},
                           class     => $a->{class},
-                          # TODO: ORDER
+                          order     => $a->{order},
+                          active    => exists $a->{active} ? $a->{active} : 1,
                           arguments => \%arguments,
                          );
     }
@@ -169,7 +170,9 @@
 
 =head2 from_cgi CGI
 
-Calls C<from_webform> with the CGI's parameters.
+Calls C<from_webform> with the CGI's parameters, after doing Mason
+parameter mapping, and splitting C<|>'s in argument names.  See
+L</argument munging>.
 
 Returns itself.
 
@@ -220,11 +223,11 @@
     $self->_extract_continuations_from_webform(%args);
 
     # Pull in all of the arguments
+    # XXX ALEX: I think this breaks J:CLONE
     $self->arguments(\%args);
 
     # Extract actions and state variables
-    $self->_extract_actions_from_webform(%args);
-    $self->_extract_state_variables_from_webform(%args);
+    $self->from_data_structure($self->webform_to_data_structure(%args));
 
     $self->just_validating(1) if defined $args{'J:VALIDATE'} and length $args{'J:VALIDATE'};
 
@@ -250,6 +253,7 @@
             $self->add_action(moniker => $2, class => $value, order => $1, arguments => {}, active => 1);
         } elsif ($key =~ /^J:A:F-(\w+)-(.+)/s and $self->action($2)) {
             $self->action($2)->argument($1 => $value);
+            $self->action($2)->modified(1);
         } elsif ($key =~ /^J:V-(.*)/s) {
             $self->add_state_variable(key => $1, value => $value);
         }
@@ -273,26 +277,12 @@
         $self->remove_action($2);
     } elsif ($key =~ /^J:A:F-(\w+)-(.+)/s and $self->action($2)) {
         $self->action($2)->delete($1);
+        $self->action($2)->modified(1);
     } elsif ($key =~ /^J:V-(.*)/s) {
         $self->remove_state_variable($1);
     }
 }
 
-
-sub _extract_state_variables_from_webform {
-    my $self = shift;
-    my %args = (@_);
-
-    foreach my $state_variable ( keys %args ) {
-        if(  $state_variable  =~ /^J:V-(.*)$/s) {
-            $self->add_state_variable(
-                key     => $1,
-                value   => $args{$state_variable}
-            );
-        }
-    }
-}
-
 =head2 parse_form_field_name FIELDNAME
 
 Takes a form field name generated by a Jifty action.
@@ -335,39 +325,50 @@
     return ( $type, $argument, $moniker );
 }
 
-sub _extract_actions_from_webform {
+sub webform_to_data_structure {
     my $self = shift;
     my %args = (@_);
 
+
+    my $data = {actions => {}, variables => {}};
+
+    # Pass path through
+    $data->{path} = $self->path;
+
+    # Are we only setting some actions as active?
     my $active_actions;
     if (exists $args{'J:ACTIONS'}) {
         $active_actions = {};
         $active_actions->{$_} = 1 for split ';', $args{'J:ACTIONS'};
     } # else $active_actions stays undef
 
-    for my $maybe_action (keys %args) {
-        next unless $maybe_action =~ /^J:A-(?:(\d+)-)?(.+)/s;
+    # Mapping from argument types to data structure names
+    my %types = ("J:A:F:F:F" => "doublefallback", "J:A:F:F" => "fallback", "J:A:F" => "value");
 
-        my $order   = $1;
-        my $moniker = $2;
-        my $class = $args{$maybe_action};
-
-        my $arguments = {};
-        for my $type (qw/J:A:F:F:F J:A:F:F J:A:F/) {
-            for my $key (keys %args) {
-                my ($t, $a, $m) = $self->parse_form_field_name($key);
-                $arguments->{$a} = $args{$key} if defined $t and $t eq $type and $m eq $moniker;
-            }
+    # The "sort" here is key; it ensures that action registrations
+    # come before action arguments
+    for my $key (sort keys %args) {
+        my $value = $args{$key};
+        if( $key  =~ /^J:V-(.*)$/s ) {
+            # It's a variable
+            $data->{variables}{$1} = $value;
+        } elsif ($key =~ /^J:A-(?:(\d+)-)?(.+)/s) {
+            # It's an action declatation
+            $data->{actions}{$2} = {
+                order   => $1,
+                moniker => $2,
+                class   => $value,
+                active  => ($active_actions ? ($active_actions->{$2} || 0) : 1),
+            };
+        } else {
+            # It's possibly a form field
+            my ($t, $a, $m) = $self->parse_form_field_name($key);
+            next unless $t and $types{$t} and $data->{actions}{$m};
+            $data->{actions}{$m}{fields}{$a}{$types{$t}} = $value;
         }
+    }
 
-        $self->add_action(
-            moniker => $moniker,
-            class => $class,
-            order => $order,
-            arguments => $arguments,
-            active => ($active_actions ? ($active_actions->{$moniker} || 0) : 1)
-        );
-    } 
+    return $data;
 }
 
 sub _extract_continuations_from_webform {
@@ -605,7 +606,7 @@
                 @_
                );
 
-    my $fragment = $self->{'actions'}->{ $args{'name'} } || Jifty::Request::Fragment->new;
+    my $fragment = $self->{'fragments'}->{ $args{'name'} } || Jifty::Request::Fragment->new;
 
     for my $k (qw/name path wrapper/) {
         $fragment->$k($args{$k}) if defined $args{$k};
@@ -650,7 +651,7 @@
 
 package Jifty::Request::Action;
 use base 'Class::Accessor';
-__PACKAGE__->mk_accessors( qw/moniker arguments class order active/);
+__PACKAGE__->mk_accessors( qw/moniker arguments class order active modified/);
 
 =head2 Jifty::Request::Action
 

Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Mon Feb 13 19:47:08 2006
@@ -214,7 +214,7 @@
     Jifty->web->setup_session;
 
     my @valid_actions;
-            for my $request_action ( $self->request->actions ) {
+    for my $request_action ( $self->request->actions ) {
         $self->log->debug("Found action ".$request_action->class . " " . $request_action->moniker);
         next unless $request_action->active;
         unless ( $self->is_allowed( $request_action->class ) ) {
@@ -227,10 +227,12 @@
         # Make sure we can instantiate the action
         my $action = $self->new_action_from_request($request_action);
         next unless $action;
+        $request_action->modified(0);
 
         # Try validating -- note that this is just the first pass; as
         # actions are run, they may fill in values which alter
         # validation of later actions
+        $self->log->debug("Validating action ".ref($action). " ".$action->moniker);
         $self->response->result( $action->moniker => $action->result );
         $action->validate;
 
@@ -241,22 +243,26 @@
     unless ( $self->request->just_validating ) {
         for my $request_action (@valid_actions) {
 
-            # Pull the action out of the request (again, since
-            # mappings may have affected parameters)
-            my $action = $self->new_action_from_request($request_action);
-            next unless $action;
-
-            # Try validating again
-            $action->result(Jifty::Result->new);
-            $action->result->action_class(ref $action);
-            $self->response->result( $action->moniker => $action->result );
             eval {
-                $self->log->debug("Validating action ".ref($action). " ".$action->moniker);
-                if ($action->validate) { 
-                    $self->log->debug("Running action.");
-                    $action->run; 
+                # Pull the action out of the request (again, since
+                # mappings may have affected parameters).  This
+                # returns the cached version unless the request has
+                # changed
+                my $action = $self->new_action_from_request($request_action);
+                next unless $action;
+                if ($request_action->modified) {
+                    # If the request's action was changed, re-validate
+                    $action->result(Jifty::Result->new);
+                    $action->result->action_class(ref $action);
+                    $self->response->result( $action->moniker => $action->result );
+                    $self->log->debug("Re-validating action ".ref($action). " ".$action->moniker);
+                    next unless $action->validate;
                 }
+            
+                $self->log->debug("Running action.");
+                $action->run; 
             };
+
             if ( my $err = $@ ) {
                 # poor man's exception propagation
                 # We need to get "LAST RULE" exceptions back up to the dispatcher
@@ -540,6 +546,8 @@
 sub new_action_from_request {
     my $self       = shift;
     my $req_action = shift;
+    return $self->{'actions'}{ $req_action->moniker } if 
+      $self->{'actions'}{ $req_action->moniker } and not $req_action->modified;
     $self->new_action(
         class     => $req_action->class,
         moniker   => $req_action->moniker,
@@ -597,8 +605,8 @@
     {
         my $request = Jifty::Request->new();
         $request->path($page);
-        $request->from_webform( map { "J:V-" . $_->key => $_->value }
-                $self->request->state_variables );
+        $request->add_state_variable( key => $_->key, value => $_->value )
+          for $self->request->state_variables;
         my $cont = Jifty::Continuation->new(
             request  => $request,
             response => $self->response,

Modified: jifty/trunk/lib/Jifty/Web/PageRegion.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/PageRegion.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/PageRegion.pm	Mon Feb 13 19:47:08 2006
@@ -238,12 +238,14 @@
     }
 
     # Merge in defaults
-    %arguments = (%{ Jifty->web->request->arguments }, region => $self, 'J:ACTIONS' => '', %arguments);
+    %arguments = (%{ Jifty->web->request->arguments }, region => $self, %arguments);
 
     # Make a fake request and throw it at the dispatcher
     my $subrequest = Jifty::Request->new;
     $subrequest->from_webform( %arguments );
     $subrequest->path( $self->path );
+    # Remove all of the actions
+    $subrequest->remove_action( $_->moniker ) for $subrequest->actions;
     $subrequest->is_subrequest( 1 );
     local Jifty->web->{request} = $subrequest;
 

Modified: jifty/trunk/t/02-form-protocol.t
==============================================================================
--- jifty/trunk/t/02-form-protocol.t	(original)
+++ jifty/trunk/t/02-form-protocol.t	Mon Feb 13 19:47:08 2006
@@ -10,6 +10,7 @@
 J:A:F-something-mymoniker: else
 J:ACTIONS: mymoniker
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -35,6 +36,7 @@
 J:A:F-something-second: bla
 J:ACTIONS: mymoniker;second
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -70,6 +72,7 @@
 J:A:F-something-second: bla
 J:ACTIONS: mymoniker;second
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -104,6 +107,7 @@
 J:A:F-something-second: bla
 J:ACTIONS: mymoniker;second
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -131,6 +135,7 @@
 J:A:F-something-second: bla
 J:ACTIONS: second
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -165,6 +170,7 @@
 J:A:F-id-second: 42
 J:A:F-something-second: bla
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -202,6 +208,7 @@
 J:A:F-id-second: 42
 J:A:F-something-second: bla
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -239,6 +246,7 @@
 J:A:F-something-mymoniker: else
 J:A-mymoniker: DoSomething
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -271,6 +279,7 @@
 J:A:F-something-mymoniker: else
 J:ACTIONS: mymoniker
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -295,6 +304,7 @@
 J:A:F-something-mymoniker: else
 J:ACTIONS: mymoniker
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -318,6 +328,7 @@
 J:A:F-something-mymoniker: else
 J:ACTIONS: mymoniker
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -344,6 +355,7 @@
 J:A:F-something-second: feepy
 J:ACTIONS: mymoniker;second
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -379,6 +391,7 @@
 J:A:F-something-mymoniker: else
 J:ACTIONS: mymoniker
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -404,6 +417,7 @@
 J:A:F-something-mymoniker: else
 J:ACTIONS: mymoniker
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -428,6 +442,7 @@
 J:A:F-something-mymoniker: else
 J:ACTIONS: mymoniker
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -451,6 +466,7 @@
 J:A:F-something-mymoniker: else
 J:ACTIONS: mymoniker
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:
@@ -477,6 +493,7 @@
 J:A:F-something-second: bla
 J:ACTIONS: mymoniker;second
 --- request
+path: ~
 state_variables: {}
 actions:
   mymoniker:


More information about the Jifty-commit mailing list