[rt-devel] Re: Issue with new users in RT CLI

Abhijit Menon-Sen ams at wiw.org
Tue Oct 14 07:39:28 EDT 2003


At 2003-10-13 13:39:16 +0100, iain.price at post.serco.com wrote:
>
> It would also be nice if the ticket id came back from a new ticket

(Somebody posted a patch for this earlier.)

> Here is my patch

I think it might be better to avoid creating a special object altogether
(for reasons discussed earlier, such as bogus email being sent, and more
than one transaction being performed).

Something like the appended should work in theory (I picked Queue rather
than the more complex User/Tickets for this experiment). Unfortunately,
I get "RT::Queue::Validate unimplemented" when I try to run it.

Earlier, the logic was:

    if ('new') { Create new object.    }
    else       { Load existing object. }

    # At this point, we have a valid object.

    if (changes) { Make changes to object.          }
    else         { Return information about object. }

Now, it is:

    if (!new) { Load existing object. }

    if (changes) {
        if (new) { Create object all at once. }
        else     { Apply changes one by one.  }
    }
    else {
        if (new) { Return a suitable default form.  }
        else     { Return information about object. }
    }

(Of course, the code below could be cleaned up further. It's still just
a proof of concept.)

-- ams

%# REST/1.0/Forms/queue/default
%#
<%ARGS>
$id
$format => 's'
$changes => {}
</%ARGS>
<%perl>
my @data;
my $queue = new RT::Queue $session{CurrentUser};
my @fields = qw(Name Description CorrespondAddress CommentAddress
                InitialPriority FinalPriority DefaultDueIn Disabled);
my %fields = map { lc $_ => $_ } @fields;

my ($c, $o, $k, $e) = ("", [], {}, 0);

if ($id ne 'new') {
    $queue->Load($id);
    unless ($queue->Id) {
        return [ "# Queue $id does not exist.", [], {}, 1 ];
    }
}

my %values;
foreach (%$changes) {
    if (exists $fields{lc $_}) {
        $values{$fields{lc $_}} = $changes->{$_};
    }
    else {
        $values{$_} = $changes->{$_};
    }
}

if (%values == 0) {
    my @data;

    if ($id eq 'new') {
        return [
            "# New queue.",
            [ "id", @fields ],
            {
                id => 'queue/new',
                Name => '<queue name>',
                Description => '',
                CommentAddress => '',
                CorrespondAddress => '',
                InitialPriority => '',
                FinalPriority => '',
                DefaultDueIn => '',
                Disabled => 0
            },
            0
        ];
    }

    push @data, [ id => "queue/".$queue->Id ];
    foreach my $key (@fields) {
        push @data, [ $key => $queue->$key ];
    }

    my %k = map {@$_} @data;
    $o = [ map {$_->[0]} @data ];
    $k = \%k;
}
else {
    if ($id eq 'new') {
        $queue->Create(%values);
        if ($queue->Id) {
            $id = $queue->Id;
            $c = "# queue/$id created.";
        }
        else {
            $c = "# Couldn't create new queue.";
        }
    }
    else {
        my (@comments, $n, $s);

        foreach my $key (keys %values) {
            my $val = $values{$key};

            if (lc $key eq 'name' && $val eq '<queue name>') {
                $n = 0;
                $s = "Please set the Queue name.";
                goto SET;
            }

            if (exists $fields{lc $key}) {
                my $set = "Set$key";
                next if $val eq $queue->$key();
                ($n, $s) = $queue->$set($val);
            }
            elsif ($key ne 'id') {
                $n = 0;
                $s = "Unknown field: $key";
            }

        SET:
            if ($n == 0) {
                $e = 1;
                push @comments, "# $key: $s";
                unless (@$o) {
                    @$o = ("id", @fields);
                    %$k = %values;
                }
            }
        }

        push(@comments, "# Queue $id updated.") unless @comments;
        $c = join("\n", @comments) if @comments;
    }
}

return [ $c, $o, $k, $e ];
</%perl>



More information about the Rt-devel mailing list