Changeset 10477


Ignore:
Timestamp:
01/05/11 07:03:56 (2 years ago)
Author:
SvenDowideit
Message:

Item2321: add PATCH partial named elements for array types - so you can now just send {FILEATTACHMENTS:[{name:something.zip comment:yadayadda}]} and the other elements in that and the other attachments remain the same as before

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/MongoDBPlugin/lib/Foswiki/Plugins/MongoDBPlugin.pm

    r10471 r10477  
    248248    #i don't know why, but this is never triggered, but without it, i get a crash. 
    249249    #so, i presume there is a weird case where it happens 
    250     print STDERR "-B---($web . $topic) $key - ".ref($savedMeta->{$key})."\n"; 
    251     print STDERR Dumper($savedMeta->{$key})."\n"; 
    252     print STDERR "\n######################################################## BOOOOOOOOM\n"; 
     250    #print STDERR "-B---($web . $topic) $key - ".ref($savedMeta->{$key})."\n"; 
     251    #print STDERR Dumper($savedMeta->{$key})."\n"; 
     252    #print STDERR "\n######################################################## BOOOOOOOOM\n"; 
    253253    next; 
    254254} 
  • trunk/MongoDBPlugin/lib/Foswiki/Plugins/MongoDBPlugin/DB.pm

    r10471 r10477  
    3030#I wish 
    3131#use constant MONITOR => $Foswiki::cfg{MONITOR}{'Foswiki::Plugins::MongoDBPlugin'} || 0; 
    32 use constant MONITOR => 1; 
     32use constant MONITOR => 0; 
    3333 
    3434sub new { 
  • trunk/RestPlugin/data/System/RestPlugin.txt

    r10345 r10477  
    7070Creates a new element in a container. So if you want to create a new topic, the URI is that of the web you want to place it, and the payload will contain a value that has a =_topic= 
    7171---++++ PATCH 
     72modifies an existing item, changing only the elements sent in the payload - thus allowing you to modify just the topic text, the value of one 'named' FIELD, or one Attachement's data. 
    7273---++++ DELETE 
    7374 
  • trunk/RestPlugin/lib/Foswiki/Plugins/RestPlugin/MANIFEST

    r10227 r10477  
    1111pub/System/RestPlugin/Edit-chalk-10g.png 
    1212pub/System/RestPlugin/formfield_example.png   
    13 pub/System/RestPlugin/Image:Edit-chalk-10bo.png   
     13pub/System/RestPlugin/Image-Edit-chalk-10bo.png   
    1414pub/System/RestPlugin/Wiki-text-icon.png 
    1515 
  • trunk/RestPlugin/lib/Foswiki/UI/Query.pm

    r10308 r10477  
    420420                mapMimeType($requestContentType) ); 
    421421            if ( $elementAlias eq 'topic' ) { 
    422                 copyFrom( $topicObject, $value );    #copy meta.. 
     422                mergeFrom( $topicObject, $value );    #copy meta.. 
    423423 
    424424#print STDERR ")))))".Foswiki::Serialise::serialise( $session, $value, 'perl' )."(((((\n" if MONITOR_ALL; 
     
    426426                  if ( defined( $value->{_text} ) ); 
    427427                $topicObject->save(); 
     428                $res->pushHeader( 'Location', 
     429                    getResourceURI( $topicObject, $elementAlias) ); 
    428430                $result = Foswiki::Serialise::convertMeta($topicObject); 
    429431            } 
     
    431433                if ( ( not defined($attachment) ) or ( $attachment eq '' ) ) { 
    432434                    my $hash = { "FILEATTACHMENT" => $value }; 
    433                     copyFrom( $topicObject, $hash );    #copy meta.. 
     435                    mergeFrom( $topicObject, $hash );    #copy meta.. 
    434436 
    435437#print STDERR ")))))".Foswiki::Serialise::serialise( $session, $value, 'perl' )."(((((\n" if MONITOR_ALL; 
     
    469471                    data => $topicObject 
    470472                ); 
    471             } 
    472             $res->pushHeader( 'Location', 
    473                 getResourceURI( $result, $elementAlias) ); 
     473                $res->pushHeader( 'Location', 
     474                    getResourceURI( $result, $elementAlias) ); 
     475                $result = Foswiki::Serialise::convertMeta($result); ###TODO: Extractme 
     476            } 
     477 
    474478            $res->status(201); 
    475479        } 
     
    723727    my $elementAlias = shift;    #TODO: derive this from the meta.. 
    724728 
     729    #ASSERT($meta->isa('Foswiki::Meta')) if DEBUG; 
     730 
    725731    print STDERR "getResourceURI - getScriptUrl(" 
    726732      . $meta->web . ", " 
     
    799805######################## 
    800806#yes, this is a simplified copy from Foswiki::Meta::copyFrom so we can copy from a random hashref 
    801 #TODO: this is not a PUT/POST, its a PATCH. 
    802807sub copyFrom { 
    803808    my ( $meta, $other, $type, $filter ) = @_; 
    804  
    805     #ASSERT( $meta->{_web} && $meta->{_topic}, '$this is not a topic object' ) 
    806     #  if DEBUG; 
    807     #ASSERT( $other->isa('Foswiki::Meta') && $other->{_web} && $other->{_topic}, 
    808     #    'other is not a topic object' ) 
    809     #  if DEBUG; 
    810809 
    811810    if ($type) { 
     
    817816            { 
    818817                my %datum = %$item; 
    819                 push( @data, \%datum ); 
    820             } 
    821         } 
    822         print STDERR "--------------actually modifying $type..\n" 
    823           if MONITOR_ALL; 
    824         $meta->putAll( $type, @data ); 
     818                push(@data, \%datum) 
     819            } 
     820        } 
     821        $meta->putAll($type, @data) 
    825822    } 
    826823    else { 
     
    828825            unless ( $k =~ /^_/ ) { 
    829826                copyFrom( $meta, $other, $k ); 
     827            } 
     828        } 
     829    } 
     830} 
     831#this is the PATCH version of copyfrom 
     832sub mergeFrom { 
     833    my ( $meta, $other, $type, $filter ) = @_; 
     834 
     835    if ($type) { 
     836        return if $type =~ /^_/; 
     837        foreach my $item ( @{ $other->{$type} } ) { 
     838            if ( !$filter 
     839                || ( $item->{name} && $item->{name} =~ /$filter/ ) ) 
     840            { 
     841                my $old = $meta->get($type, $item->{name}); 
     842                 my %hash = (); 
     843                 #Merge old element with new data - that way keys that are not in the payload still get used. 
     844                 %hash = %$old if (defined($old));  
     845                 @hash{keys(%$item)} = values(%$item); 
     846                 $meta->putKeyed($type, \%hash) 
     847            } 
     848        } 
     849    } 
     850    else { 
     851        foreach my $k ( keys %$other ) { 
     852            unless ( $k =~ /^_/ ) { 
     853                mergeFrom( $meta, $other, $k ); 
    830854            } 
    831855        } 
  • trunk/RestPlugin/test/unit/RestPlugin/RestPluginCurlTests.pm

    r10241 r10477  
    1010use JSON               (); 
    1111use File::Path qw(mkpath); 
     12 
     13use constant MONITOR => 0; 
    1214 
    1315sub new { 
     
    107109      . $url; 
    108110 
    109     print STDERR "\n\n----\n$curlCommand\n----\n"; 
     111    print STDERR "\n\n----\n$curlCommand\n----\n" if MONITOR; 
    110112    my $result = `$curlCommand 2>&1` 
    111113      ;    # grrrr, aparently they output the header info into stderr 
     
    200202        $replytext =~ s/"date":(.*?),/"date":"EXTRACTED_FOR_TESTING",/g; 
    201203 
    202         print STDERR "\njson expected:\n"; 
    203         print STDERR $expectedReplyPayload; 
    204         print STDERR "\njson got:\n"; 
    205         print STDERR $replytext; 
     204        print STDERR "\njson expected:\n" if MONITOR; 
     205        print STDERR $expectedReplyPayload if MONITOR; 
     206        print STDERR "\njson got:\n" if MONITOR; 
     207        print STDERR $replytext if MONITOR; 
    206208        my $replyObj = 
    207209          Foswiki::Serialise::deserialise( $this->{session}, $replytext, 
  • trunk/RestPlugin/test/unit/RestPlugin/RestPluginTests.pm

    r10285 r10477  
    1313 
    1414# Set to 1 for debug 
    15 use constant MONITOR_ALL => 0; 
     15use constant MONITOR_ALL => 1; 
    1616 
    1717my $UI_FN; 
     
    273273    { 
    274274 
    275 #print STDERR "----- ".$fromJSON->{FIELD}[0]->{name}.": ".$fromJSON->{FIELD}[0]->{value}."\n" if MONITOR_ALL; 
     275print STDERR "----- ".$fromJSON->{FIELD}[0]->{name}.": ".$fromJSON->{FIELD}[0]->{value}."\n" if MONITOR_ALL; 
    276276        my $partialItem = JSON::from_json( $replytext, { allow_nonref => 1 } ); 
    277277        $partialItem->{FIELD}[0]->{value} = 'Something new, something blue'; 
     
    299299            'GET', {} ); 
    300300 
    301 #print STDERR "-------reply-----\n".$replytext."\n------------\n" if MONITOR_ALL; 
     301print STDERR "-------reply-----\n".$replytext."\n------------\n" if MONITOR_ALL; 
    302302 
    303303        my $NEWfromJSON = JSON::from_json( $replytext, { allow_nonref => 1 } ); 
     
    308308            'Something new, something blue' 
    309309        ); 
     310        $this->assert_equals( 
     311            $NEWfromJSON->{FIELD}[0]->{name}, 
     312            'Summary' 
     313        ); 
     314        $this->assert_equals( 
     315            $NEWfromJSON->{FIELD}[0]->{title}, 
     316            'Summary' 
     317        ); 
    310318 
    311319        $this->assert_str_not_equals( $NEWfromJSON->{_raw_text}, 
     
    321329    } 
    322330} 
     331 
     332#modify partial item updates 
     333sub testPATCH_OneArrayElementByName_Topic { 
     334    my $this = shift; 
     335 
     336    #GET the topic 
     337    my ( $meta, $text ) = 
     338      Foswiki::Func::readTopic( $this->{test_web}, "Improvement2" ); 
     339    my ( $replytext, $hdr ) = $this->call_UI_query( 
     340        '/' . $this->{test_web} . '/Improvement2/topic.json', 
     341        'GET', {} ); 
     342    my $fromJSON = JSON::from_json( $replytext, { allow_nonref => 1 } ); 
     343    $this->assert_deep_equals( $fromJSON, 
     344        Foswiki::Serialise::convertMeta($meta) ); 
     345 
     346    #send PATCH with only the one  
     347    { 
     348 
     349print STDERR "----- ".$fromJSON->{FIELD}[0]->{name}.": ".$fromJSON->{FIELD}[0]->{value}."\n" if MONITOR_ALL; 
     350        my $partialItem = {"FIELD" => [{"name"=>"Summary", "value" => 'Something new, something blue'}]}; 
     351        my $sendJSON = JSON::to_json($partialItem); 
     352 
     353     print STDERR "------------\n".$sendJSON."\n------------\n" if MONITOR_ALL; 
     354 
     355        ( $replytext, $hdr ) = $this->call_UI_query( 
     356            '/' . $this->{test_web} . '/Improvement2/topic.json', 
     357            'PATCH', { 'POSTDATA' => $sendJSON } ); 
     358 
     359        #my $replyHash =  JSON::from_json( $replytext, { allow_nonref => 1 } ); 
     360    } 
     361 
     362    #then make sure it saved using GET.. 
     363    { 
     364        my ( $meta, $text ) = 
     365          Foswiki::Func::readTopic( $this->{test_web}, "Improvement2" ); 
     366        my ( $replytext, $hdr ) = $this->call_UI_query( 
     367            '/' . $this->{test_web} . '/Improvement2/topic.json', 
     368            'GET', {} ); 
     369 
     370print STDERR "-------reply-----\n".$replytext."\n------------\n" if MONITOR_ALL; 
     371 
     372        my $NEWfromJSON = JSON::from_json( $replytext, { allow_nonref => 1 } ); 
     373        $this->assert_deep_equals( $NEWfromJSON, 
     374            Foswiki::Serialise::convertMeta($meta) ); 
     375        $this->assert_equals( 
     376            $NEWfromJSON->{FIELD}[0]->{value}, 
     377            'Something new, something blue' 
     378        ); 
     379 
     380        $this->assert_str_not_equals( $NEWfromJSON->{_raw_text}, 
     381            $fromJSON->{_raw_text} ); 
     382        $this->assert_equals( $NEWfromJSON->{_text}, $fromJSON->{_text} ); 
     383 
     384        #make sure the other FIELD is still as it was before. 
     385        $this->assert_equals( $NEWfromJSON->{FIELD}[1]->{value}, 
     386            $fromJSON->{FIELD}[1]->{value} ); 
     387        $this->assert_equals( 'work it out yourself!', 
     388            $NEWfromJSON->{FIELD}[1]->{value} ); 
     389        $this->assert_equals( 'Details', $NEWfromJSON->{FIELD}[1]->{name} ); 
     390    } 
     391} 
     392 
     393 
    323394 
    324395#modify partial item updates 
Note: See TracChangeset for help on using the changeset viewer.