[Jifty-commit] r1790 - in jifty/trunk: . lib/Jifty lib/Jifty/Action/Record t/TestApp-Plugin-REST/lib/TestApp/Plugin/REST/Action t/TestApp-Plugin-REST/t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Aug 11 02:08:57 EDT 2006


Author: nelhage
Date: Fri Aug 11 02:08:55 2006
New Revision: 1790

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/Makefile.PL
   jifty/trunk/lib/Jifty/Action/Record/Create.pm
   jifty/trunk/lib/Jifty/I18N.pm
   jifty/trunk/lib/Jifty/Request.pm
   jifty/trunk/lib/Jifty/RightsFrom.pm
   jifty/trunk/lib/Jifty/Web.pm
   jifty/trunk/lib/Jifty/Web/Menu.pm
   jifty/trunk/t/TestApp-Plugin-REST/lib/TestApp/Plugin/REST/Action/DoSomething.pm
   jifty/trunk/t/TestApp-Plugin-REST/t/02-basic-use.t

Log:
Pushing all my Jifty changes for the last few days, since my mirror forget it was a mirror

Modified: jifty/trunk/Makefile.PL
==============================================================================
--- jifty/trunk/Makefile.PL	(original)
+++ jifty/trunk/Makefile.PL	Fri Aug 11 02:08:55 2006
@@ -22,6 +22,7 @@
 requires('Email::LocalDelivery');
 requires('Email::MIME');
 requires('Email::MIME::Creator');
+requires('Email::MIME::ContentType');
 requires('Email::Send' => '1.99_01'); # Email::Send::Jifty::Test
 requires('Email::Simple');
 requires('Exporter::Lite');

Modified: jifty/trunk/lib/Jifty/Action/Record/Create.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Action/Record/Create.pm	(original)
+++ jifty/trunk/lib/Jifty/Action/Record/Create.pm	Fri Aug 11 02:08:55 2006
@@ -71,7 +71,7 @@
 
     # Handle errors?
     unless ( $record->id ) {
-        $self->result->error(_("An error occurred.  Try again later"));
+        $self->result->error($msg || _("An error occurred.  Try again later"));
         $self->log->error(_("Create of %1 failed: %2", ref($record), $msg));
         return;
     }

Modified: jifty/trunk/lib/Jifty/I18N.pm
==============================================================================
--- jifty/trunk/lib/Jifty/I18N.pm	(original)
+++ jifty/trunk/lib/Jifty/I18N.pm	Fri Aug 11 02:08:55 2006
@@ -4,6 +4,8 @@
 package Jifty::I18N;
 use base 'Locale::Maketext';
 use Locale::Maketext::Lexicon ();
+use Email::MIME::ContentType;
+use Encode::Guess qw(iso-8859-1);
 
 =head1 NAME
 
@@ -69,4 +71,52 @@
     return $self;
 }
 
+=head2 promote_encoding STRING [CONTENT-TYPE]
+
+Return STRING promoted to our best-guess of an appropriate
+encoding. STRING should B<not> have the UTF-8 flag set when passed in.
+
+Optionally, you can pass a MIME content-type string as a second
+argument. If it contains a charset= parameter, we will use that
+encoding. Failing that, we use Encode::Guess to guess between UTF-8
+and iso-latin-1. If that fails, and the string validates as UTF-8, we
+assume that. Finally, we fall back on returning the string as is.
+
+=cut
+
+# XXX TODO This possibly needs to be more clever and/or configurable
+
+sub promote_encoding {
+    my $class = shift;
+    my $string = shift;
+    my $content_type = shift;
+
+    $content_type = Email::MIME::ContentType::parse_content_type($content_type) if $content_type;
+    my $charset = $content_type->{attributes}->{charset} if $content_type;
+
+    # XXX TODO Is this the right thing? Maybe we should just return
+    # the string as-is.
+    Encode::_utf8_off($string);
+
+    if($charset) {
+        $string = Encode::decode($charset, $string);
+    } else {
+        my $encoding = Encode::Guess->guess($string);
+        if(!ref($encoding)) {
+            eval {
+                # Try utf8
+                $string = Encode::decode_utf8($string, 1);
+            };
+            if($@) {
+                warn "Unknown encoding -- none specified, couldn't guess, not valid UTF-8";
+            }
+        } else {
+            $string = $encoding->decode($string) if $encoding;
+        }
+    }
+
+    return $string;
+
+}
+
 1;

Modified: jifty/trunk/lib/Jifty/Request.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Request.pm	(original)
+++ jifty/trunk/lib/Jifty/Request.pm	Fri Aug 11 02:08:55 2006
@@ -228,6 +228,16 @@
     use HTML::Mason::Utils;
     my %args = HTML::Mason::Utils::cgi_request_args( $cgi, $cgi->request_method );
 
+    # Either CGI.pm or HTML::Mason should really deal with this for us.
+    for my $k (keys %args) {
+        my $val = $args{$k};
+        if(ref($val)) {
+            $args{$k} = [map {Jifty::I18N->promote_encoding($_, $ENV{CONTENT_TYPE})} @$val];
+        } else {
+            $args{$k} = Jifty::I18N->promote_encoding($val, $ENV{CONTENT_TYPE});
+        }
+    }
+    
     my @splittable_names = grep /=|\|/, keys %args;
     for my $splittable (@splittable_names) {
         delete $args{$splittable};
@@ -279,6 +289,7 @@
     my $key = shift;
     if (@_) {
         my $value = shift;
+        
         $self->arguments->{$key} = $value;
 
         # Continuation type is ofetn undef, so give it a sane default
@@ -317,13 +328,6 @@
 
     defined(my $val = $self->arguments->{$key}) or return undef;
 
-    if (ref $val eq 'ARRAY') {
-        Encode::_utf8_on($_) for @$val;
-    }
-    else {
-        Encode::_utf8_on($val);
-    }
-
     $val;
 }
 

Modified: jifty/trunk/lib/Jifty/RightsFrom.pm
==============================================================================
--- jifty/trunk/lib/Jifty/RightsFrom.pm	(original)
+++ jifty/trunk/lib/Jifty/RightsFrom.pm	Fri Aug 11 02:08:55 2006
@@ -25,6 +25,10 @@
 task. L<Jifty::Record/current_user_can> uses this method to make an
 access control decision if it exists.
 
+Note that this means that you a model class can use Jifty::RightsFrom,
+and still have a custom C<current_user_can> method, and they will not
+interfere with each other.
+
 =cut
 
 package Jifty::RightsFrom;
@@ -81,7 +85,7 @@
     );
     no strict 'refs';
     no warnings 'redefine';
-    local *{ $args{'as'} } = sub { \&{  $args{'sub_name'} }(shift @_, @{ $args{'args'} }, @_ ) };
+    local *{ $args{'as'} } = sub { &{ $args{'sub_name'} }(shift @_, @{ $args{'args'} }, @_ ) };
 
     local @{Jifty::RightsFrom::EXPORT_OK} = ($args{as});
     Jifty::RightsFrom->export_to_level( 2, $args{export_to}, $args{as} );
@@ -89,11 +93,11 @@
 
 =head2 delegate_current_user_can
 
-Seeing and editing task transactions (as well as other activities) are
-based on your rights on the
-task the transactions are on.  Some finagling is necessary because, if
-this is a create call, this object does not have a C<task_id> yet, so
-we must rely on the value in the I<ATTRIBUTES> passed in.
+Make a decision about permissions based on checking permissions on the
+column of this record specified in the call to C<import>. C<create>,
+C<delete>, and C<update> rights all check for the C<update> right on
+the delegated object. On create, we look in the passed attributes for
+an argument with the name of that column.
 
 =cut
 
@@ -103,11 +107,13 @@
     my $col_name    = shift;
     my $right       = shift;
     my %attribs     = @_;
+
     $right = 'update' if $right ne 'read';
     my $obj;
 
     my $column   = $self->column($col_name);
     my $obj_type = $column->refers_to();
+    
 
     # XXX TODO: this card is bloody hard to follow. it's my fault. --jesse
 
@@ -129,6 +135,7 @@
     } else {
         return 0;
     }
+
     return $obj->current_user_can($right);
 }
 

Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Fri Aug 11 02:08:55 2006
@@ -422,6 +422,7 @@
         @_
     );
 
+
     my %arguments = %{ $args{arguments} };
 
     if ( $args{'moniker'} ) {

Modified: jifty/trunk/lib/Jifty/Web/Menu.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/Menu.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/Menu.pm	Fri Aug 11 02:08:55 2006
@@ -255,7 +255,7 @@
 =head2 as_link
 
 Return this menu item as a C<Jifty::Web::Link>, either the one we were
-initialized with or a new one made from the C</label> and c</url>
+initialized with or a new one made from the C</label> and C</url>
 
 If there's no C</url> and no C</link>, renders just the label.
 

Modified: jifty/trunk/t/TestApp-Plugin-REST/lib/TestApp/Plugin/REST/Action/DoSomething.pm
==============================================================================
--- jifty/trunk/t/TestApp-Plugin-REST/lib/TestApp/Plugin/REST/Action/DoSomething.pm	(original)
+++ jifty/trunk/t/TestApp-Plugin-REST/lib/TestApp/Plugin/REST/Action/DoSomething.pm	Fri Aug 11 02:08:55 2006
@@ -1,10 +1,12 @@
-package Test::Plugin::REST::Action::DoSomething;
+package TestApp::Plugin::REST::Action::DoSomething;
 
 use Jifty::Param::Schema;
+use base qw/TestApp::Plugin::REST::Action/;
 use Jifty::Action schema {
 
 param email =>
     label is 'Email',
+    default is 'example at email.com',
     ajax canonicalizes,
     ajax validates;
 

Modified: jifty/trunk/t/TestApp-Plugin-REST/t/02-basic-use.t
==============================================================================
--- jifty/trunk/t/TestApp-Plugin-REST/t/02-basic-use.t	(original)
+++ jifty/trunk/t/TestApp-Plugin-REST/t/02-basic-use.t	Fri Aug 11 02:08:55 2006
@@ -14,7 +14,7 @@
 use lib 't/lib';
 use Jifty::SubTest;
 
-use Jifty::Test tests => 27;
+use Jifty::Test tests => 52;
 use Jifty::Test::WWW::Mechanize;
 
 my $server  = Jifty::Test->make_server;
@@ -80,12 +80,98 @@
 
 # on PUT    '/=/model/*/*/*' => \&replace_item;
 # on DELETE '/=/model/*/*/*' => \&delete_item;
+
+
 # on GET    '/=/action'      => \&list_actions;
+
+my @actions = qw(TestApp::Plugin::REST::Action::CreateUser
+                 TestApp::Plugin::REST::Action::UpdateUser
+                 TestApp::Plugin::REST::Action::DeleteUser
+                 TestApp::Plugin::REST::Action::DoSomething
+                 Jifty::Action::Autocomplete
+                 Jifty::Action::Redirect);
+
+$mech->get_ok('/=/action/');
+is($mech->status, 200);
+
+for (@actions) {
+    $mech->content_contains($_);
+}
+
+$mech->get_ok('/=/action.yml');
+my @got = @{get_content()};
+
+is(join(",",sort @actions), join(",", sort(@got)), "Got all the actions as YAML");
+
+
 # on GET    '/=/action/*'    => \&list_action_params;
+
+$mech->get_ok('/=/action/DoSomething');
+is($mech->status, 200);
+$mech->get_ok('/=/action/TestApp::Plugin::REST::Action::DoSomething');
+is($mech->status, 200);
+$mech->get_ok('/=/action/TestApp.Plugin.REST.Action.DoSomething');
+is($mech->status, 200);
+
+$mech->content_contains('email');
+$mech->content_contains('Email');
+$mech->content_contains('example at email.com');
+
+$mech->get_ok('/=/action/DoSomething.yml');
+is($mech->status, 200);
+
+
+my %args = %{get_content()};
+
+ok($args{email}, "Action has an email parameter");
+is($args{email}{label}, 'Email', 'email has the correct label');
+is($args{email}{default}, 'email at example.com', 'email has the correct default');
+
+
 # on POST   '/=/action/*'    => \&run_action;
 # 
 
+$mech->post( $URL . '/=/action/DoSomething', { email => 'good at email.com' } );
+
+$mech->content_contains('Something happened!');
+
+$mech->post( $URL . '/=/action/DoSomething', { email => 'bad at email.com' } );
+
+$mech->content_contains('Bad looking email');
+$mech->content_lacks('Something happened!');
+
+$mech->post( $URL . '/=/action/DoSomething', { email => 'warn at email.com' } );
+
+$mech->content_contains('Warning for email');
+$mech->content_contains('Something happened!');
+
+# Test YAML posts
+yaml_post( $URL . '/=/action/DoSomething.yml', { email => 'good at email.com' } );
+
+%content = %{get_content()};
+
+ok($content{success});
+is($content{message}, 'Something happened');
+
+yaml_post( $URL . '/=/action/DoSomething', { email => 'bad at email.com' } );
+
+%content = %{get_content()};
+
+ok(!$content{success});
+is($content{error}, 'Bad looking email');
+
+
 sub get_content { return Jifty::YAML::Load($mech->content)}
+sub yaml_post {
+    my $url = shift;
+    my $data = shift;
+    my $request = HTTP::Request->new('POST', $url);
+    $request->header('Content-Type', 'text/yaml');
+    $request->content(Jifty::YAML::Dump($data));
+
+    $mech->request($request);
+    
+}
 
 1;
 


More information about the Jifty-commit mailing list