[Rt-commit] rt branch, 4.0/cf-date-parse-in-search, created. rt-4.0.10-121-g34254bb
? sunnavy
sunnavy at bestpractical.com
Tue Mar 19 11:29:37 EDT 2013
The branch, 4.0/cf-date-parse-in-search has been created
at 34254bb3670459bff30d86ba2bf5a7e8d0e8e662 (commit)
- Log -----------------------------------------------------------------
commit 0f4023d69025a58bb0c02b1f07f6c8e6e91ce1e7
Author: sunnavy <sunnavy at bestpractical.com>
Date: Sun Oct 2 17:43:30 2011 +0800
tests for the cf date parse in search using RT::Date
diff --git a/t/customfields/date_search.t b/t/customfields/date_search.t
index 6d7bfd0..ea5995f 100644
--- a/t/customfields/date_search.t
+++ b/t/customfields/date_search.t
@@ -2,7 +2,7 @@
use warnings;
use strict;
-use RT::Test nodata => 1, tests => 13;
+use RT::Test nodata => 1, tests => 17;
my $q = RT::Queue->new(RT->SystemUser);
ok( $q->Create( Name => 'DateCFTest' . $$ ), 'create queue' );
@@ -18,6 +18,7 @@ ok(
'create cf date'
);
ok( $cf->AddToObject($q), 'date cf apply to queue' );
+my $cf_name = $cf->Name;
my $ticket = RT::Ticket->new(RT->SystemUser);
@@ -79,6 +80,22 @@ is( $ticket->CustomFieldValues->First->Content, '2010-05-04', 'date in db is' );
}
{
+ my $tickets = RT::Tickets->new(RT->SystemUser);
+ $tickets->FromSQL( "'CF.{$cf_name}' = 'May 4 2010'" );
+ is( $tickets->Count, 1, 'found the ticket with = May 4 2010' );
+
+ $tickets->FromSQL( "'CF.{$cf_name}' < 'May 4 2010'" );
+ is( $tickets->Count, 0, 'did not find the ticket with < May 4 2010' );
+
+ $tickets->FromSQL( "'CF.{$cf_name}' < 'May 5 2010'" );
+ is( $tickets->Count, 1, 'found the ticket with < May 5 2010' );
+
+ $tickets->FromSQL( "'CF.{$cf_name}' > 'May 3 2010'" );
+ is( $tickets->Count, 1, 'found the ticket with > May 3 2010' );
+}
+
+
+{
my $tickets = RT::Tickets->new(RT->SystemUser);
$tickets->LimitCustomField(
diff --git a/t/customfields/datetime_search.t b/t/customfields/datetime_search.t
index 707610b..f3efa77 100644
--- a/t/customfields/datetime_search.t
+++ b/t/customfields/datetime_search.t
@@ -2,7 +2,7 @@
use warnings;
use strict;
-use RT::Test nodata => 1, tests => 14;
+use RT::Test nodata => 1, tests => 18;
RT->Config->Set( 'Timezone' => 'EST5EDT' ); # -04:00
my $q = RT::Queue->new(RT->SystemUser);
@@ -19,6 +19,7 @@ ok(
'create cf datetime'
);
ok( $cf->AddToObject($q), 'date cf apply to queue' );
+my $cf_name = $cf->Name;
my $ticket = RT::Ticket->new(RT->SystemUser);
@@ -77,6 +78,23 @@ is(
is( $tickets->Count, 0, 'did not find the ticket with wrong datetime: 2010-05-05' );
}
+{
+ my $tickets = RT::Tickets->new(RT->SystemUser);
+ $tickets->FromSQL( "'CF.{$cf_name}' = 'May 4 2010 7am'" );
+ is( $tickets->Count, 1, 'found the ticket with = May 4 2010 7am' );
+
+ $tickets->FromSQL( "'CF.{$cf_name}' = 'May 4 2010 8am'" );
+ is( $tickets->Count, 0, 'did not find the ticket with = May 4 2010 8am' );
+
+ $tickets->FromSQL( "'CF.{$cf_name}' > 'May 3 2010 7am'" );
+ is( $tickets->Count, 1, 'found the ticket with > May 3 2010 7am' );
+
+ $tickets->FromSQL( "'CF.{$cf_name}' < 'May 4 2010 8am'" );
+ is( $tickets->Count, 1, 'found the ticket with < May 4 2010 8am' );
+
+}
+
+
my $tickets = RT::Tickets->new( RT->SystemUser );
$tickets->UnLimit;
while( my $ticket = $tickets->Next ) {
commit 75cb3c2e598c4a5fa329913c91294ebf8423b511
Author: sunnavy <sunnavy at bestpractical.com>
Date: Tue Mar 19 21:32:27 2013 +0800
use RT::Date to parse cf date values in tickets search
to distinguish if user inputs date or datetime, here is the rule:
1. if cf type is Date, it's a date.
2. if cf type is DateTime, unless there is "hh:mm:ss" or "midnight", as
long as the parsed time is at midnight(00:00:00), it's a date.
we treat midnight datetimes as dates sometimes specially is because if user
searches a DateTime cf with a value like '=' '2013-03-21', we assume he means to
get all the tickets with the cf values in the range of the whole day.
diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index 39efecd..3fd3a3b 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1539,6 +1539,29 @@ sub _CustomFieldLimit {
}
}
+ if ( $cf && $cf->Type =~ /^Date(?:Time)?$/ ) {
+ my $date = RT::Date->new( $self->CurrentUser );
+ $date->Set( Format => 'unknown', Value => $value );
+ if ( $date->Unix ) {
+
+ if (
+ $cf->Type eq 'Date'
+ || $value =~ /^\s*(?:today|tomorrow|yesterday)\s*$/i
+ || ( $value !~ /midnight|\d+:\d+:\d+/i
+ && $date->Time( Timezone => 'user' ) eq '00:00:00' )
+ )
+ {
+ $value = $date->Date( Timezone => 'user' );
+ }
+ else {
+ $value = $date->DateTime;
+ }
+ }
+ else {
+ $RT::Logger->warn("$value is not a valid date string");
+ }
+ }
+
my $single_value = !$cf || !$cfid || $cf->SingleValue;
my $cfkey = $cfid ? $cfid : "$queue.$field";
@@ -1634,27 +1657,12 @@ sub _CustomFieldLimit {
}
else {
# need special treatment for Date
- if ( $cf and $cf->Type eq 'DateTime' and $op eq '=' ) {
-
- if ( $value =~ /:/ ) {
- # there is time speccified.
- my $date = RT::Date->new( $self->CurrentUser );
- $date->Set( Format => 'unknown', Value => $value );
- $self->_SQLLimit(
- ALIAS => $TicketCFs,
- FIELD => 'Content',
- OPERATOR => "=",
- VALUE => $date->ISO,
- %rest,
- );
- }
- else {
+ if ( $cf and $cf->Type eq 'DateTime' and $op eq '=' && $value !~ /:/ ) {
# no time specified, that means we want everything on a
# particular day. in the database, we need to check for >
# and < the edges of that day.
my $date = RT::Date->new( $self->CurrentUser );
$date->Set( Format => 'unknown', Value => $value );
- $date->SetToMidnight( Timezone => 'server' );
my $daystart = $date->ISO;
$date->AddDay;
my $dayend = $date->ISO;
@@ -1679,7 +1687,6 @@ sub _CustomFieldLimit {
);
$self->_CloseParen;
- }
}
elsif ( $op eq '=' || $op eq '!=' || $op eq '<>' ) {
if ( length( Encode::encode_utf8($value) ) < 256 ) {
commit 34254bb3670459bff30d86ba2bf5a7e8d0e8e662
Author: sunnavy <sunnavy at bestpractical.com>
Date: Tue Mar 19 21:43:47 2013 +0800
the whole day search for a datetime cf should be a right open interval
if user searches a DateTime cf with '=' '2012-03-21', we should limit the
cf values '>=' '2012-03-21 00:00:00' and '<' '2012-03-22 00:00:00', i.e.
[ '2012-03-21 00:00:00', '2012-03-22 00:00:00' )
diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index 3fd3a3b..3b834e0 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1680,7 +1680,7 @@ sub _CustomFieldLimit {
$self->_SQLLimit(
ALIAS => $TicketCFs,
FIELD => 'Content',
- OPERATOR => "<=",
+ OPERATOR => "<",
VALUE => $dayend,
%rest,
ENTRYAGGREGATOR => 'AND',
-----------------------------------------------------------------------
More information about the Rt-commit
mailing list