[Bps-public-commit] r17399 - in Prophet/trunk/lib/Prophet: CLI/Command Server Server/ViewHelpers

jesse at bestpractical.com jesse at bestpractical.com
Mon Dec 29 00:52:21 EST 2008


Author: jesse
Date: Mon Dec 29 00:52:21 2008
New Revision: 17399

Modified:
   Prophet/trunk/lib/Prophet/CLI/Command/Server.pm
   Prophet/trunk/lib/Prophet/ChangeSet.pm
   Prophet/trunk/lib/Prophet/Record.pm
   Prophet/trunk/lib/Prophet/Server.pm
   Prophet/trunk/lib/Prophet/Server/Controller.pm
   Prophet/trunk/lib/Prophet/Server/Dispatcher.pm
   Prophet/trunk/lib/Prophet/Server/ViewHelpers/Function.pm
   Prophet/trunk/lib/Prophet/Server/ViewHelpers/Widget.pm

Log:
* setup template roots within prophet::server

Modified: Prophet/trunk/lib/Prophet/CLI/Command/Server.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/CLI/Command/Server.pm	(original)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Server.pm	Mon Dec 29 00:52:21 2008
@@ -18,7 +18,6 @@
      }
     my $server = $server_class->new( $self->arg('port') || 8080 );
     $server->app_handle( $self->app_handle );
-    $server->setup_template_roots();
     return $server;
 }
 

Modified: Prophet/trunk/lib/Prophet/ChangeSet.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/ChangeSet.pm	(original)
+++ Prophet/trunk/lib/Prophet/ChangeSet.pm	Mon Dec 29 00:52:21 2008
@@ -216,7 +216,7 @@
     );
 
     my $body = '';
-
+    
     for my $change ( $self->changes ) {
         next if $args{change_filter} && !$args{change_filter}->($change);
         $body .= $change->as_string( header_callback => $args{change_header} ) || next;

Modified: Prophet/trunk/lib/Prophet/Record.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Record.pm	(original)
+++ Prophet/trunk/lib/Prophet/Record.pm	Mon Dec 29 00:52:21 2008
@@ -217,12 +217,19 @@
     my $props = $args{props};
 
     $self->default_props($props);
-    $self->canonicalize_props($props);
-    $self->validate_props($props) or return undef;
+    $self->canonicalize_props($props); 
+
+    # XXX TODO - this should be a real exception 
+    return undef unless (keys %$props);
 
+    $self->validate_props($props) or return undef;
     $self->_create_record(props => $props, uuid => $uuid);
 }
 
+
+
+
+# _create_record is a helper routine, used both by create and by databasesetting::create
 sub _create_record {
     my $self = shift;
     my %args = validate( @_, { props => 1, uuid => 1 } );
@@ -239,7 +246,6 @@
 
 }
 
-
 =head2 load { uuid => $UUID } or { luid => $UUID }
 
 Loads a Prophet record off disk by its uuid or luid.
@@ -280,7 +286,6 @@
     );
 }
 
-
 sub loaded {
     my $self = shift;
     return $self->uuid ? 1 : 0;
@@ -760,7 +765,7 @@
 
 sub atom_value {
     my $self     = shift;
-    my $value_in = shift;
+    my $value_in = shift || '';
 
     if ($value_in =~ /^\$[gu]uid/) {
         return $self->uuid;

Modified: Prophet/trunk/lib/Prophet/Server.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server.pm	(original)
+++ Prophet/trunk/lib/Prophet/Server.pm	Mon Dec 29 00:52:21 2008
@@ -55,6 +55,7 @@
         warn "Publisher backend is not available. Install one of the "
             . "Net::Rendezvous::Publish::Backend modules from CPAN.";
     }
+    $self->setup_template_roots();
     $self->SUPER::run(@_);
 }
 
@@ -87,13 +88,14 @@
      '/static/prophet/jquery/js/jquery.bgiframe.min.js', 
      '/static/prophet/jquery/js/jquery-autocomplete.js', 
      '/static/prophet/jquery/js/superfish.js', 
+     '/static/prophet/jquery/js/supersubs.js', 
      '/static/prophet/jquery/js/jquery.tablesorter.min.js'
 }
 
 
 
 
-override handle_request => sub {
+sub handle_request {
     my ( $self, $cgi ) = validate_pos( @_, { isa => 'Prophet::Server' }, { isa => 'CGI' } );
     $self->cgi($cgi);
     $self->nav( Prophet::Web::Menu->new( cgi => $self->cgi ) );
@@ -113,15 +115,16 @@
 
     my $dispatcher_class = ref( $self->app_handle ) . "::Server::Dispatcher";
     if ( !$self->app_handle->try_to_require($dispatcher_class) ) {
-        $dispatcher_class = "Prophet::Server::Dispatcher";
+              $dispatcher_class = "Prophet::Server::Dispatcher";
     }
 
+
     my $d = $dispatcher_class->new( server => $self );
 
     $d->run( $cgi->request_method . $cgi->path_info, $d )
         || $self->_send_404;
 
-};
+}
 
 sub update_record_prop {
     my $self = shift;

Modified: Prophet/trunk/lib/Prophet/Server/Controller.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server/Controller.pm	(original)
+++ Prophet/trunk/lib/Prophet/Server/Controller.pm	Mon Dec 29 00:52:21 2008
@@ -3,13 +3,11 @@
 use Prophet::Util;
 use Prophet::Web::Result;
 
-has cgi => (is => 'rw', isa => 'CGI');
-has failure_message => ( is => 'rw', isa => 'Str');
-has functions => (is => 'rw', isa => 'HashRef');
-has app_handle => (is => 'rw', isa => 'Prophet::App');
-has result => ( is => 'ro', isa => 'Prophet::Web::Result');
-
-
+has cgi             => ( is => 'rw', isa => 'CGI' );
+has failure_message => ( is => 'rw', isa => 'Str' );
+has functions       => ( is => 'rw', isa => 'HashRef' );
+has app_handle      => ( is => 'rw', isa => 'Prophet::App' );
+has result          => ( is => 'ro', isa => 'Prophet::Web::Result' );
 
 =head1 NAME
 
@@ -27,50 +25,57 @@
     my $self = shift;
 
     my $functions = {};
-   foreach my $param ($self->cgi->all_parameters){
+    foreach my $param ( $self->cgi->all_parameters ) {
         next unless $param =~ /^prophet-function-(.*)$/;
         my $name = $1;
-        warn "Duplicate function definition for @{[$name]}." if (exists $functions->{$name});
-
+        warn "Duplicate function definition for @{[$name]}." if ( exists $functions->{$name} );
 
         my $function_data = $self->cgi->param($param);
-        my $attr = $self->string_to_hash($function_data);
+        my $attr          = $self->string_to_hash($function_data);
         $attr->{name} = $name;
 
+        # For now, always execute
+        $attr->{execute} = 1;
+
+        # We MUST validate any function we're going to canonicalize
+        $attr->{validate} = 1 if $attr->{execute};
+
+        # We MUST canonicalize any function we're going to validate
+        $attr->{canonicalize} = 1 if $attr->{validate};
+
         $functions->{$name} = $attr;
         $functions->{$name}->{params} = $self->params_for_function_from_cgi($name);
-   } 
-   $self->functions($functions);
+    }
+
+    $self->functions($functions);
 }
 
 sub params_for_function_from_cgi {
-    my $self   = shift;
+    my $self     = shift;
     my $function = shift;
 
     my $values;
     for my $field ( $self->cgi->all_parameters ) {
         if ( $field =~ /^prophet-field-function-$function-prop-(.*)$/ ) {
-        my $name     = $1;
-        $values->{$name} = {
-            prop           => $name,
-            value          => $self->cgi->param($field),
-            original_value => $self->cgi->param( "original-value-" . $field )
-        };
-    }
-    elsif ($field =~ /^prophet-fill-function-$function-prop-(.*)$/) {
-        my $name = $1;
-        my $meta = {};
-        my $value = $self->cgi->param($field);
-        next unless ($value =~ /^function-(.*)\|result-(.*)$/);
-        $values->{$name} = {
-            prop           => $name,
-            from_function => $1,
-            from_result => $2
-        };
-    } 
-    else {
+            my $name = $1;
+            $values->{$name} = {
+                prop           => $name,
+                value          => $self->cgi->param($field),
+                original_value => $self->cgi->param( "original-value-" . $field )
+            };
+        } elsif ( $field =~ /^prophet-fill-function-$function-prop-(.*)$/ ) {
+            my $name  = $1;
+            my $meta  = {};
+            my $value = $self->cgi->param($field);
+            next unless ( $value =~ /^function-(.*)\|result-(.*)$/ );
+            $values->{$name} = {
+                prop          => $name,
+                from_function => $1,
+                from_result   => $2
+            };
+        } else {
             next;
-    }
+        }
 
     }
 
@@ -101,7 +106,8 @@
 sub canonicalize_functions {
     my $self    = shift;
     my $functions = $self->functions;
-    foreach my $function ( keys %$functions ) {
+    foreach my $function ( sort { $functions->{$a}->{order} <=> $functions->{$b}->{order}}  keys %{$functions}) {
+        next unless ($functions->{$function}->{canonicalize});
         foreach my $param ( keys %{ $functions->{$function}->{params} } ) {
             if ( 
                 defined $functions->{$function}->{params}->{$param}->{original_value} &&
@@ -118,45 +124,69 @@
 }
 
 sub validate_functions {
+    my $self    = shift;
+    my $functions = $self->functions;
+    foreach my $function ( sort { $functions->{$a}->{order} <=> $functions->{$b}->{order}}  keys %{$functions}) {
+        next unless ($functions->{$function}->{validate});
+        foreach my $param ( keys %{ $functions->{$function}->{params} } ) {
+            if (0) {
+            }
+        }
 
 }
 
+sub execute_functions {
+    my $self = shift;
+    my $functions = $self->functions;
 
-sub fill_params_from_previous_functions {
-    my $self = shift;   
-    my $function = shift;
-
-    my $params = $self->functions->{$function}->{params};
+    foreach my $function ( sort { $functions->{$a}->{order} <=> $functions->{$b}->{order}}  keys %{$functions}) {
+        $self->_fill_params_from_previous_functions($function); 
 
-    foreach my $param ( keys %$params) {
-            if ( my $from_function = $params->{$param}->{from_function}) {
-                my $from_result = $params->{$param}->{from_result};  
-                my $func_result = $self->result->get($from_function);
-                # XXX TODO - $from_result should be locked down tighter
-                if ($func_result->can($from_result)) { 
-                    $params->{$param}->{value} = $func_result->$from_result();
-                }
+        $self->app_handle->log_debug("About to execute a function - ".$function);
 
-            }
+        next unless ($functions->{$function}->{execute});
 
+        if ($functions->{$function}->{action} eq 'update') {
+            $self->_exec_function_update($functions->{$function});
+        } elsif ($functions->{$function}->{action} eq 'create') {
+            $self->_exec_function_create($functions->{$function});
+        } else {
+            die "I don't know how to handle a ".$functions->{$function}->{action};
+        }
     }
+}
 
+sub _fill_params_from_previous_functions {
+    my $self     = shift;
+    my $function = shift;
+    my $params = $self->functions->{$function}->{params};
+    foreach my $param ( keys %$params ) {
+        if ( my $from_function = $params->{$param}->{from_function} ) {
+            my $from_result = $params->{$param}->{from_result};
+            my $function_result = $self->result->get($from_function);
+
+            # XXX TODO - $from_result should be locked down tighter
+            if ( $function_result->can($from_result) ) {
+                $params->{$param}->{value} = $function_result->$from_result();
+            }
+        }
+    }
 }
 
-sub execute_functions {
+
+sub _get_record_for_function {
     my $self = shift;
-    my $fs = $self->functions;
+    my $function = shift;
 
-    foreach my $function ( sort { $fs->{$a}->{order} <=> $fs->{$b}->{order}}  keys %{$fs}) {
-        $self->fill_params_from_previous_functions($function); 
-        if ($fs->{$function}->{action} eq 'update') {
-            $self->_exec_function_update($fs->{$function});
-        } elsif ($fs->{$function}->{action} eq 'create') {
-            $self->_exec_function_create($fs->{$function});
+    my $functions = $self->functions;
+        if ($functions->{$function}->{action} eq 'update') {
+            return Prophet::Util->instantiate_record( uuid => $function->{uuid}, class=>$function->{class}, app_handle=> $self->app_handle);
+        } elsif ($functions->{$function}->{action} eq 'create') {
+                die $function->{class} ." is not a valid class " unless (UNIVERSAL::isa($function->{class}, 'Prophet::Record'));
+                return  $function->{class}->new(  app_handle => $self->app_handle);
         } else {
-            die "I don't know how to handle a ".$fs->{$function}->{action};
+            die "I don't know how to handle a ".$functions->{$function}->{action};
         }
-
     }
 
 }
@@ -165,12 +195,10 @@
     my $self = shift;
     my $function = shift;
 
-    die $function->{class} ." is not a valid class " unless (UNIVERSAL::isa($function->{class}, 'Prophet::Record'));
-    my $object = $function->{class}->new(  app_handle => $self->app_handle);
+    my $object = $self->_get_record_for_function($function);
     my ( $val, $msg ) = $object->create(
         props => {
-            map {
-                $function->{params}->{$_}->{prop} => $function->{params}->{$_}->{value}
+            map { $function->{params}->{$_}->{prop} => $function->{params}->{$_}->{value}
                 } keys %{ $function->{params} }
             }
 
@@ -189,7 +217,7 @@
     my $self = shift;
     my $function = shift;
 
-    my $object = Prophet::Util->instantiate_record( uuid => $function->{uuid}, class=>$function->{class}, app_handle=> $self->app_handle);
+    my $object = $self->_get_record_for_function($function);
     my ( $val, $msg ) = $object->set_props(
         props => {
             map {
@@ -216,7 +244,6 @@
     return \%attr;
 }
 
-
 __PACKAGE__->meta->make_immutable;
 no Moose;
 

Modified: Prophet/trunk/lib/Prophet/Server/Dispatcher.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server/Dispatcher.pm	(original)
+++ Prophet/trunk/lib/Prophet/Server/Dispatcher.pm	Mon Dec 29 00:52:21 2008
@@ -7,28 +7,28 @@
 sub token_delimiter       {'/'}
 sub case_sensitive_tokens {0}
 
-under 'POST' => sub {
+under ['POST'] => sub {
     on qr'.*' => sub {
         my $self = shift;
         return $self->server->_send_401 if ( $self->server->read_only );
         next_rule;
     };
 
-    under 'records' => sub {
+    under [ 'records'] => sub {
         on qr|^(.*)/(.*)/(.*)$| => sub { shift->server->update_record_prop($1,$2,$3) };
         on qr|^(.*)/(.*).json$| => sub { shift->server->update_record($1,$2) };
         on qr|^(.*).json$|     => sub { shift->server->create_record($1) };
     };
 };
 
-under 'GET' => sub {
+under ['GET'] => sub {
     my $self = shift;
     on qr'^=/prophet/autocomplete' => sub {
         shift->server->show_template('/_prophet_autocompleter') };
     on qr'^static/prophet/(.*)$' => sub { shift->server->send_static_file($1)};
     on qr'replica/+(.*)$' => sub { shift->server->serve_replica($1) };
     on 'records.json' => sub { shift->server->get_record_types };
-    under 'records' => sub {
+    under ['records'] => sub {
         on qr|^(.*)/(.*)/(.*)$| => sub { shift->server->get_record_prop($1,$2,$3); };
         on qr|^(.*)/(.*).json$| => sub { shift->server->get_record($1,$2) };
         on qr|^(.*).json$|      => sub { shift->server->get_record_list($1) };

Modified: Prophet/trunk/lib/Prophet/Server/ViewHelpers/Function.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server/ViewHelpers/Function.pm	(original)
+++ Prophet/trunk/lib/Prophet/Server/ViewHelpers/Function.pm	Mon Dec 29 00:52:21 2008
@@ -12,13 +12,6 @@
 
 =cut
 
-use Template::Declare::Tags;
-
-BEGIN {
- delete ${__PACKAGE__."::"}{meta};
- delete ${__PACKAGE__."::"}{with};
- }
-
 use Moose;
 use Moose::Util::TypeConstraints;
 
@@ -35,6 +28,11 @@
 
 has order => ( isa => 'Int', is => 'ro' );
 
+has validate => ( isa => 'Bool', is => 'rw', default => 1);
+has canonicalize => ( isa => 'Bool', is => 'rw', default => 1);
+has execute => ( isa => 'Bool', is => 'rw', default => 1);
+
+
 has name => (
     isa => 'Str',
     is  => 'rw',
@@ -59,7 +57,10 @@
         action => $self->action,
         type => $self->record->type,
         class => ref($self->record),
-        uuid   => $self->record->uuid
+        uuid   => $self->record->uuid,
+        validate => $self->validate,
+        canonicalize => $self->canonicalize,
+        execute => $self->execute
     );
 
     my $string
@@ -67,13 +68,8 @@
         . join( "|", map { $bits{$_} ? $_ . "=" . $bits{$_} : '' } keys %bits )
         . "|";
 
-    input {
-        attr {
-            type  => 'hidden',
-            name  => "prophet-function-" . $self->name,
-            value => $string
-        };
-    };
+   
+       return qq{<input type="hidden" name="prophet-function-@{[$self->name]}" value="$string" />};
 }
 
 

Modified: Prophet/trunk/lib/Prophet/Server/ViewHelpers/Widget.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server/ViewHelpers/Widget.pm	(original)
+++ Prophet/trunk/lib/Prophet/Server/ViewHelpers/Widget.pm	Mon Dec 29 00:52:21 2008
@@ -61,7 +61,7 @@
         label  => $self->prop,
         class  => 'prop-'.$self->prop.' function-'.$self->function->name,
         value  => $value,
-        type => $self->type
+        ($self->type ? ( type => $self->type) : ())
         
     ));
 



More information about the Bps-public-commit mailing list