Changeset 2352


Ignore:
Timestamp:
05/26/20 02:14:04 (4 years ago)
Author:
nanardon
Message:

Add basic template features

Location:
trunk/LATMOS-Accounts
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/MANIFEST

    r2296 r2352  
    100100lib/LATMOS/Accounts/Bases/Sql/Stat.pm 
    101101lib/LATMOS/Accounts/Bases/Sql/Sutype.pm 
     102lib/LATMOS/Accounts/Bases/Sql/Templates.pm 
    102103lib/LATMOS/Accounts/Bases/Sql/User.pm 
    103104lib/LATMOS/Accounts/Bases/Sql/Userstatus.pm 
     
    159160man/man8/latmos-accounts-faq.pod 
    160161man/man8/latmos-accounts.pod 
     162patchset/logas_in_cli.patch 
    161163patchset/no_useless_load.patch 
    162164sample/allowed_values.ini 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases.pm

    r2351 r2352  
    433433        return; 
    434434    } 
    435     foreach my $cfield (keys %cdata) { 
    436         $self->check_allowed_values($otype, $cfield, $cdata{$cfield}) or do { 
    437             my $last = LATMOS::Accounts::Log::lastmessage(LA_ERR); 
    438             $self->log(LA_ERR, "Cannot create $otype, wrong value%s", ($last ? ": $last" : '')); 
    439             return; 
    440         }; 
    441     } 
     435 
     436    my %data; 
    442437 
    443438    # populating default value 
     
    445440        my %default = $self->compute_default($otype, $id, %cdata); 
    446441        foreach my $k (keys %default) { 
    447             $cdata{$k} = $default{$k} unless(exists($cdata{$k})); 
    448         } 
    449     } 
    450  
    451     my %data; 
     442            $data{$k} = $default{$k}; 
     443        } 
     444    } 
     445 
     446    if ( $self->is_supported_object('templates') ) { 
     447        if ( $cdata{template} ) { 
     448            my $template = $self->get_object('templates', $cdata{template} ) or do { 
     449                $self->log(LA_ERR, "Cannot load template $cdata{template}"); 
     450                return; 
     451            }; 
     452 
     453            my $TOType = $template->_get_attributes('objecttype'); 
     454            if ( $TOType ne $otype ) { 
     455                $self->log(LA_ERR, "Template $cdata{template} is for object type $TOType, not $otype"); 
     456                return; 
     457            } 
     458 
     459            my $text = join('\n', $template->_get_attributes('data')); 
     460            my %tdata = LATMOS::Accounts::Utils::parse_obj_text($text || ''); 
     461 
     462            foreach my $k ( keys %tdata ) { 
     463                $data{$k} = $tdata{$k}; 
     464            } 
     465 
     466            delete( $cdata{template} ); 
     467        } 
     468    } 
     469 
    452470    my $sub; 
    453471    foreach my $cfield (keys %cdata) { 
     
    462480            $data{$cfield} = $cdata{$cfield}; 
    463481        } 
     482    } 
     483 
     484    foreach my $cfield (keys %data) { 
     485        $self->check_allowed_values($otype, $cfield, $data{$cfield}) or do { 
     486            my $last = LATMOS::Accounts::Log::lastmessage(LA_ERR); 
     487            $self->log(LA_ERR, "Cannot create $otype, wrong value%s", ($last ? ": $last" : '')); 
     488            return; 
     489        }; 
    464490    } 
    465491 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm

    r2296 r2352  
    1818our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; 
    1919 
    20 sub SCHEMA_VERSION { 36 }; 
     20sub SCHEMA_VERSION { 37 }; 
    2121 
    2222=head1 NAME 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/objects.pm

    r2328 r2352  
    262262                ) ]; 
    263263            }, 
     264        }, 
     265        template => { 
     266            reference => 'templates', 
     267            can_values => sub { 
     268                $base->search_objects('templates', 'objectType=' . $class->type, 'exported=1') 
     269            }, 
     270            ro => sub { ref $_[0] ? 1 : 0 }, 
     271            set => sub {}, 
    264272        }, 
    265273    ); 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/upgrade.pm

    r2296 r2352  
    13181318            ], 
    13191319        }, 
     1320        { 
     1321            ver => 37, 
     1322            sql => [ 
     1323                qq{ 
     1324                CREATE TABLE public."templates" 
     1325                ( 
     1326                  objectType text not NULL, 
     1327                  data text[], 
     1328                  CONSTRAINT template_pkey PRIMARY KEY (name) 
     1329                ) 
     1330                INHERITS (public.objects) 
     1331                WITH ( 
     1332                  OIDS=FALSE 
     1333                ); 
     1334 
     1335                CREATE INDEX templates_rev_idx 
     1336                  ON public."templates" 
     1337                  USING btree 
     1338                  (rev); 
     1339 
     1340                CREATE INDEX templates_exported_idx 
     1341                  ON public."templates" 
     1342                  USING btree 
     1343                  (exported); 
     1344 
     1345                CREATE TRIGGER templates_rev_tg 
     1346                  BEFORE UPDATE OR DELETE 
     1347                  ON public."templates" 
     1348                  FOR EACH ROW 
     1349                  EXECUTE PROCEDURE public.rev_tg_f(); 
     1350                } 
     1351            ], 
     1352        }, 
    13201353    ); 
     1354 
    13211355 
    13221356    my $dbi = $self->{_db}; 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm

    r2175 r2352  
    106106} 
    107107 
     108sub _parse_line { 
     109    my ( $line, $attributes ) = @_; 
     110 
     111    $line =~ /^#/ and return; 
     112    # Empty line are skipped (or space only) 
     113    $line =~ /^\s*$/ and return; 
     114    chomp($line); 
     115    my ($attr, $value) = $line =~ /^\s*(\S+):\s*(.*)\s*$/ or 
     116        die "Malformed input file\n"; 
     117    $value =~ s/\s*$//; 
     118    $value =~ s/\\n/\n/g; 
     119    if ($attributes->{$attr}) { 
     120        if (ref $attributes->{$attr}) { 
     121            push(@{ $attributes->{$attr} }, $value); 
     122        } else { 
     123            my $temp = $attributes->{$attr}; 
     124            $attributes->{$attr} = [ $temp, $value ]; 
     125        } 
     126    } else { 
     127        $attributes->{$attr} = $value eq '' ? undef : $value; 
     128        # Don't remember why this is here 
     129        #$attr eq 'exported' && !defined $attributes->{$attr} and $attributes->{$attr} = 1; 
     130    } 
     131} 
     132 
    108133=head2 parse_obj_file($handle) 
    109134 
     
    115140    my ($handle) = @_; 
    116141 
    117     my %attributes; 
     142    my $attributes = {}; 
    118143    while (my $line = <$handle>) { 
    119         $line =~ /^#/ and next; 
    120         # Empty line are skipped (or space only) 
    121         $line =~ /^\s*$/ and next; 
    122         chomp($line); 
    123         my ($attr, $value) = $line =~ /^\s*(\S+):\s*(.*)\s*$/ or 
    124             die "Malformed input file\n"; 
    125         $value =~ s/\s*$//; 
    126         $value =~ s/\\n/\n/g; 
    127         if ($attributes{$attr}) { 
    128             if (ref $attributes{$attr}) { 
    129                 push(@{ $attributes{$attr} }, $value); 
    130             } else { 
    131                 my $temp = $attributes{$attr}; 
    132                 $attributes{$attr} = [ $temp, $value ]; 
    133             } 
    134         } else { 
    135             $attributes{$attr} = $value eq '' ? undef : $value; 
    136             # Don't remember why this is here 
    137             #$attr eq 'exported' && !defined $attributes{$attr} and $attributes{$attr} = 1; 
    138         } 
    139     } 
    140     %attributes 
     144        _parse_line( $line, $attributes ); 
     145    } 
     146    %$attributes 
     147} 
     148 
     149=head2 parse_obj_text($text) 
     150 
     151=cut 
     152 
     153sub parse_obj_text { 
     154    my ( $Text ) = @_; 
     155 
     156    my $attributes = {}; 
     157 
     158    foreach (split(/\n/, $Text)) { 
     159        _parse_line( $_, $attributes ); 
     160    } 
     161 
     162    %$attributes 
    141163} 
    142164 
  • trunk/LATMOS-Accounts/t/05_utils.t

    r2071 r2352  
    11use strict; 
    22use warnings; 
    3 use Test::More tests => 46; 
     3use Test::More tests => 48; 
    44use File::Temp qw(mkstemp); 
    55 
     
    2727is($attributes{'attr1'}, 'attr1', "can get attribute from file"); 
    2828ok(eq_set($attributes{'attr'}, [ qw(val1 val2) ]), 
     29        "can get multiple values attribute"); 
     30 
     31my %attributes2 = LATMOS::Accounts::Utils::parse_obj_text( 
     32<<EOF 
     33attr1: attr1 
     34attr: val1 
     35attr: val2 
     36EOF 
     37); 
     38 
     39# now testing 
     40is($attributes2{'attr1'}, 'attr1', "can get attribute from file"); 
     41ok(eq_set($attributes2{'attr'}, [ qw(val1 val2) ]), 
    2942        "can get multiple values attribute"); 
    3043 
Note: See TracChangeset for help on using the changeset viewer.