[Bps-public-commit] rt-extension-automaticassignment branch, master, updated. 8f27e17a38d66f1941862fb91cb7fad97e9cd392

Shawn Moore shawn at bestpractical.com
Fri Jul 29 17:04:02 EDT 2016


The branch, master has been updated
       via  8f27e17a38d66f1941862fb91cb7fad97e9cd392 (commit)
       via  71df27ef3bf80511f73f7eb5229cd304bd410f93 (commit)
       via  4c0ead01982d634983d32afc02c7cc10648c8df2 (commit)
       via  f486edb7134c8596a0aab9fb7619e8c3598a6ee8 (commit)
      from  946f76831bcf67956687f3a24e07e5974c020e11 (commit)

Summary of changes:
 lib/RT/Extension/AutomaticAssignment.pm            |  26 +++---
 .../AutomaticAssignment/Chooser/Random.pm          |   2 +-
 .../AutomaticAssignment/Chooser/RoundRobin.pm      |   2 +-
 .../AutomaticAssignment/Chooser/TicketStatus.pm    |   2 +-
 .../AutomaticAssignment/Chooser/TimeLeft.pm        |   2 +-
 .../AutomaticAssignment/Filter/UserDates.pm        | 104 +++++++++++++++++++++
 6 files changed, 123 insertions(+), 15 deletions(-)
 create mode 100644 lib/RT/Extension/AutomaticAssignment/Filter/UserDates.pm

- Log -----------------------------------------------------------------
commit f486edb7134c8596a0aab9fb7619e8c3598a6ee8
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Fri Jul 29 21:00:59 2016 +0000

    Call ->WhoHaveRight as late in the process as possible

diff --git a/lib/RT/Extension/AutomaticAssignment.pm b/lib/RT/Extension/AutomaticAssignment.pm
index 1eda3aa..4992c5b 100644
--- a/lib/RT/Extension/AutomaticAssignment.pm
+++ b/lib/RT/Extension/AutomaticAssignment.pm
@@ -20,12 +20,6 @@ sub _UnfilteredOwnersForTicket {
 
     my $users = RT::Users->new(RT->SystemUser);
     $users->LimitToPrivileged;
-    $users->WhoHaveRight(
-        Right               => 'OwnTicket',
-        Object              => $ticket,
-        IncludeSystemRights => 1,
-        IncludeSuperusers   => 1,
-    );
 
     return $users;
 }
@@ -35,7 +29,7 @@ sub _EligibleOwnersForTicket {
     my $ticket = shift;
     my $config = shift;
 
-    my $users = $self->_UnfilteredOwnersForTicket($ticket);
+    my $users = RT::Users->new(RT->SystemUser);
 
     for my $filter (@{ $config->{filters} }) {
         if (ref($filter) eq 'CODE') {
@@ -124,6 +118,15 @@ sub OwnerForTicket {
     my $users = $self->_EligibleOwnersForTicket($ticket, $config);
     return if !$users;
 
+    # this has to come very late due to how it's implemented as replacing
+    # the collection (using rebless) with a DBIx::SearchBuilder::Union
+    $users->WhoHaveRight(
+        Right               => 'OwnTicket',
+        Object              => $ticket,
+        IncludeSystemRights => 1,
+        IncludeSuperusers   => 1,
+    );
+
     my $user = $self->_ChooseOwnerForTicket($ticket, $users, $config);
 
     return $user;

commit 4c0ead01982d634983d32afc02c7cc10648c8df2
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Fri Jul 29 21:01:28 2016 +0000

    Small fixes

diff --git a/lib/RT/Extension/AutomaticAssignment.pm b/lib/RT/Extension/AutomaticAssignment.pm
index 4992c5b..5a8fbf9 100644
--- a/lib/RT/Extension/AutomaticAssignment.pm
+++ b/lib/RT/Extension/AutomaticAssignment.pm
@@ -50,7 +50,7 @@ sub _ChooseOwnerForTicket {
     my $users  = shift;
     my $config = shift;
 
-    my $class = $config->{chooser};
+    my $class = $config->{chooser}{class};
     return $class->ChooseOwnerForTicket($ticket, $users, $config->{chooser});
 }
 
@@ -68,7 +68,7 @@ sub _ConfigForTicket {
     # merge the queue-specific config into the default config
     my %merged_config = %{ $config->{Default} || {} };
     $merged_config{ $_ } = $config->{QueueDefault}{ $queue }->{ $_ }
-        for keys %{ $config->{QueueDefault}{ $queue } || {} }
+        for keys %{ $config->{QueueDefault}{ $queue } || {} };
 
     # filters not required, since the default list is "users who can own
     # tickets in this queue"
@@ -76,7 +76,7 @@ sub _ConfigForTicket {
 
     # chooser is required
     if (!$merged_config{chooser}) {
-        RT->Logger->error("No AutomaticAssignment chooser defined for queue '$quuee'; automatic assignment cannot occur.");
+        RT->Logger->error("No AutomaticAssignment chooser defined for queue '$queue'; automatic assignment cannot occur.");
         return;
     }
 

commit 71df27ef3bf80511f73f7eb5229cd304bd410f93
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Fri Jul 29 21:01:36 2016 +0000

    UserDates filter

diff --git a/lib/RT/Extension/AutomaticAssignment/Filter/UserDates.pm b/lib/RT/Extension/AutomaticAssignment/Filter/UserDates.pm
new file mode 100644
index 0000000..8ae2c94
--- /dev/null
+++ b/lib/RT/Extension/AutomaticAssignment/Filter/UserDates.pm
@@ -0,0 +1,104 @@
+package RT::Extension::AutomaticAssignment::Filter::UserDates;
+use strict;
+use warnings;
+use base 'RT::Extension::AutomaticAssignment::Filter';
+
+sub _UserCF {
+    my $class = shift;
+    my $name = shift;
+
+    my $cf = RT::CustomField->new(RT->SystemUser);
+    $cf->LoadByName(
+        Name       => $name,
+        LookupType => RT::User->CustomFieldLookupType,
+    );
+    if (!$cf->Id) {
+        die "Unable to load User Custom Field named '$name'";
+    }
+    return $cf;
+}
+
+sub FilterOwnersForTicket {
+    my $class  = shift;
+    my $ticket = shift;
+    my $users  = shift;
+    my $config = shift;
+
+    my $now = RT::Date->new(RT->SystemUser);
+    $now->SetToNow;
+
+    if ($config->{between}) {
+        my ($start_name, $end_name) = @{ $config->{between} };
+        my $start_cf = $class->_UserCF($start_name);
+        my $end_cf = $class->_UserCF($end_name);
+
+        my $subclause = $start_name . '-' . $end_name;
+
+        # start_cf <= now <= end_cf
+        $users->LimitCustomField(
+            SUBCLAUSE       => $subclause,
+            CUSTOMFIELD     => $start_cf->Id,
+            COLUMN          => 'Content',
+            OPERATOR        => '<=',
+            VALUE           => $now->ISO,
+        );
+        $users->LimitCustomField(
+            SUBCLAUSE       => $subclause,
+            CUSTOMFIELD     => $end_cf->Id,
+            COLUMN          => 'Content',
+            OPERATOR        => '>=',
+            VALUE           => $now->ISO,
+            ENTRYAGGREGATOR => 'AND',
+        );
+    }
+    elsif ($config->{except_between}) {
+        my ($start_name, $end_name) = @{ $config->{except_between} };
+        my $start_cf = $class->_UserCF($start_name);
+        my $end_cf = $class->_UserCF($end_name);
+
+        my $subclause = $start_name . '-' . $end_name;
+
+        # allow users for whom start/end is null
+        $users->LimitCustomField(
+            SUBCLAUSE       => "$subclause-begin",
+            CUSTOMFIELD     => $start_cf->Id,
+            COLUMN          => 'Content',
+            OPERATOR        => 'IS',
+            VALUE           => 'NULL',
+        );
+        $users->LimitCustomField(
+            SUBCLAUSE       => "$subclause-end",
+            CUSTOMFIELD     => $end_cf->Id,
+            COLUMN          => 'Content',
+            OPERATOR        => 'IS',
+            VALUE           => 'NULL',
+        );
+
+        # otherwise, "now" has to be less than start, or greater than end
+        # (expressed in the query the opposite way: start has to be greater
+        # than now or end has to be less than now)
+        $users->LimitCustomField(
+            SUBCLAUSE       => "$subclause-begin",
+            CUSTOMFIELD     => $start_cf->Id,
+            COLUMN          => 'Content',
+            OPERATOR        => '>',
+            VALUE           => $now->ISO,
+            ENTRYAGGREGATOR => 'OR',
+        );
+
+        $users->LimitCustomField(
+            SUBCLAUSE       => "$subclause-end",
+            CUSTOMFIELD     => $end_cf->Id,
+            COLUMN          => 'Content',
+            OPERATOR        => '<',
+            VALUE           => $now->ISO,
+            ENTRYAGGREGATOR => 'OR',
+        );
+    }
+    else {
+        die "Unable to filter UserDates; no 'between' or 'except_between' provided.";
+    }
+}
+
+1;
+

commit 8f27e17a38d66f1941862fb91cb7fad97e9cd392
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Fri Jul 29 21:02:32 2016 +0000

    Explicitly ->ItemsArrayRef the users collection before choosing
    
        Choosers won't really be able to use collection methods anyway because
        the users collection has become a DBIx::SearchBuilder::Union instance.
    
        This also opens up the door for array-based filtering rather than SQL-
        based filtering.

diff --git a/lib/RT/Extension/AutomaticAssignment.pm b/lib/RT/Extension/AutomaticAssignment.pm
index 5a8fbf9..6f44163 100644
--- a/lib/RT/Extension/AutomaticAssignment.pm
+++ b/lib/RT/Extension/AutomaticAssignment.pm
@@ -127,7 +127,8 @@ sub OwnerForTicket {
         IncludeSuperusers   => 1,
     );
 
-    my $user = $self->_ChooseOwnerForTicket($ticket, $users, $config);
+    my @users = @{ $users->ItemsArrayRef };
+    my $user = $self->_ChooseOwnerForTicket($ticket, \@users, $config);
 
     return $user;
 }
diff --git a/lib/RT/Extension/AutomaticAssignment/Chooser/Random.pm b/lib/RT/Extension/AutomaticAssignment/Chooser/Random.pm
index 8e2ca4b..5f3db0c 100644
--- a/lib/RT/Extension/AutomaticAssignment/Chooser/Random.pm
+++ b/lib/RT/Extension/AutomaticAssignment/Chooser/Random.pm
@@ -6,7 +6,7 @@ use base 'RT::Extension::AutomaticAssignment::Chooser';
 sub ChooseOwnerForTicket {
     my $class  = shift;
     my $ticket = shift;
-    my @users  = @{ shift->ItemsArrayRef };
+    my @users  = @{ shift(@_) };
     my $config = shift;
 
     return $users[rand @users];
diff --git a/lib/RT/Extension/AutomaticAssignment/Chooser/RoundRobin.pm b/lib/RT/Extension/AutomaticAssignment/Chooser/RoundRobin.pm
index 33cae0d..05799ae 100644
--- a/lib/RT/Extension/AutomaticAssignment/Chooser/RoundRobin.pm
+++ b/lib/RT/Extension/AutomaticAssignment/Chooser/RoundRobin.pm
@@ -7,7 +7,7 @@ use List::Util 'reduce';
 sub ChooseOwnerForTicket {
     my $class  = shift;
     my $ticket = shift;
-    my @users  = @{ shift->ItemsArrayRef };
+    my @users  = @{ shift(@_) };
     my $config = shift;
 
     my $queue = $ticket->Queue;
diff --git a/lib/RT/Extension/AutomaticAssignment/Chooser/TicketStatus.pm b/lib/RT/Extension/AutomaticAssignment/Chooser/TicketStatus.pm
index fbdc109..1960b67 100644
--- a/lib/RT/Extension/AutomaticAssignment/Chooser/TicketStatus.pm
+++ b/lib/RT/Extension/AutomaticAssignment/Chooser/TicketStatus.pm
@@ -6,7 +6,7 @@ use base 'RT::Extension::AutomaticAssignment::Chooser';
 sub ChooseOwnerForTicket {
     my $class  = shift;
     my $ticket = shift;
-    my @users  = @{ shift->ItemsArrayRef };
+    my @users  = @{ shift(@_) };
     my $config = shift;
 
     # for TicketStatus we only consider tickets in the same queue
diff --git a/lib/RT/Extension/AutomaticAssignment/Chooser/TimeLeft.pm b/lib/RT/Extension/AutomaticAssignment/Chooser/TimeLeft.pm
index 5db7ae4..27ce449 100644
--- a/lib/RT/Extension/AutomaticAssignment/Chooser/TimeLeft.pm
+++ b/lib/RT/Extension/AutomaticAssignment/Chooser/TimeLeft.pm
@@ -7,7 +7,7 @@ use List::Util 'reduce';
 sub ChooseOwnerForTicket {
     my $class  = shift;
     my $ticket = shift;
-    my @users  = @{ shift->ItemsArrayRef };
+    my @users  = @{ shift(@_) };
     my $config = shift;
 
     # for TimeLeft we only consider tickets in the same queue

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list