Changeset 2267


Ignore:
Timestamp:
06/19/19 20:02:16 (5 years ago)
Author:
nanardon
Message:

Add a way to dump object and all related objects

Location:
trunk/LATMOS-Accounts
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/bin/la-query

    r2241 r2267  
    7474(Only if obj_id is specified) Include also read-only attributes as comments. 
    7575 
     76=item --recur 
     77 
     78Dump also all related objects 
     79 
    7680=item --fmt format 
    7781 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Objects.pm

    r2235 r2267  
    839839    my ($self, $config, $base) = @_; 
    840840 
     841    $config->{level} ||= 0; 
    841842    my $otype = $self->type; 
    842843    $base ||= $self->base; 
    843844    my $dump; 
    844845    if (ref $self) { 
    845         $dump .= sprintf "# base %s: object %s/%s\n", 
     846        $dump .= sprintf "%s# base %s: object %s/%s\n", 
     847            ' ' x $config->{level}, 
    846848            $base->label, $self->type, $self->id; 
    847849    } 
    848     $dump .= sprintf "# %s\n", scalar(localtime); 
     850    $dump .= sprintf( 
     851        "%s# %s\n",  
     852        ' ' x $config->{level}, 
     853        scalar(localtime) 
     854    ); 
    849855 
    850856    foreach my $attr (sort { $a cmp $b } $base->list_canonical_fields($otype, 
     
    866872                    $_ ||= ''; 
    867873                    s/\r?\n/\\n/g; 
    868                     $dump .= sprintf("%s%s:%s\n",  
     874                    $dump .= sprintf("%s%s%s%s:%s\n", 
     875                        ' ' x $config->{level}, 
    869876                        $oattr->ro ? '# (ro) ' : '', 
     877                        $config->{level} ? $self->type . '[' . $self->id . '].' : '', 
    870878                        $attr, $_ ? " $_" : ''); 
    871879                } 
     
    883891        } 
    884892    } 
     893 
     894    if (ref $self && $config->{recur}) { 
     895          my %subobj = $self->ListChildObjects; 
     896          foreach my $otype (keys %subobj) { 
     897              foreach my $oname (sort @{ $subobj{$otype} }) { 
     898                  my $obj = $self->base->get_object($otype, $oname) or next; 
     899                  $dump .= "\n"; 
     900                  $dump .= $obj->dump({ %{$config || {}}, recur => $config->{recur}, level => $config->{level} + 2 }); 
     901              } 
     902          } 
     903    } 
     904 
    885905    return $dump; 
    886906} 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm

    r2265 r2267  
    6666        address => 'user', 
    6767        employment => 'user', 
     68        nethost => [ 'owner', 'user' ], 
    6869    }, 
    6970} 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/objects.pm

    r2266 r2267  
    313313 
    314314sub ParentObject { } 
     315 
     316=head2 ListChildObjects 
     317 
     318Return objects having this object as Parent 
     319 
     320=cut 
     321 
     322sub ListChildObjects { 
     323    my ($self) = @_; 
     324 
     325    my $Subotype = $self->GetOtypeDef or return; 
     326 
     327    my %res; 
     328 
     329    foreach my $otype (keys %$Subotype) { 
     330        foreach my $attr (ref $Subotype->{$otype} ? @{ $Subotype->{$otype} } : $Subotype->{$otype}) { 
     331            my @list = $self->base->search_objects( $otype, $attr . '=' . $self->id ); 
     332            if (@list) { 
     333                push(@{ $res{$otype} }, @list); 
     334            } 
     335        } 
     336    } 
     337 
     338    %res; 
     339} 
    315340 
    316341sub _get_ikey { 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli/Base.pm

    r2231 r2267  
    225225Instead displaying attribute list use C<format> as formating string 
    226226 
     227=item recur 
     228 
     229Dump object and all related objects 
     230 
    227231=back 
    228232 
     
    241245                        'fmt=s'      => \my $fmt, 
    242246                        'filefmt=s'  => \my $filefmt, 
     247                        'recur'      => \my $recur, 
    243248                    }, @_ 
    244249                ); 
     
    271276                    } else { 
    272277                        $_->text_dump( $self->Context->Out, { 
     278                            recur => $recur, 
    273279                            empty_attr => $empty_attr, 
    274280                            only_rw => !$with_ro, 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli/Object.pm

    r2241 r2267  
    519519        }); 
    520520    } 
     521    if (1) { # TODO test SQL base 
     522        $self->add_func('extract', { 
     523            help => 'extract information about objects', 
     524            code => sub { 
     525                my ($env, $action) = @_; 
     526                foreach my $obj (sort @{$env->{_objects}}) { 
     527                    print $OUT $obj->dump({ recur => 1 }); 
     528                } 
     529                $env->rollback; 
     530            }, 
     531        }); 
     532    } 
    521533 
    522534    return $self; 
Note: See TracChangeset for help on using the changeset viewer.