[tools 1/2] testbot/web: Better initialize and check the Days main page parameter.

Francois Gouget fgouget at codeweavers.com
Tue Mar 22 07:21:28 CDT 2022


Set the FormPage property descriptors list. This allows
FormPage::Validate() to do most of the validation work and removes the
need for a custom Validate() method.
Treat "00" the same as "0".
Also remove the need for redefining OnAction().

Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
---
 testbot/web/index.pl | 42 ++++++++++++++++--------------------------
 1 file changed, 16 insertions(+), 26 deletions(-)

diff --git a/testbot/web/index.pl b/testbot/web/index.pl
index 1336d1e0e6..605e23b6f1 100644
--- a/testbot/web/index.pl
+++ b/testbot/web/index.pl
@@ -2,6 +2,7 @@
 # WineTestBot status page
 #
 # Copyright 2009 Ge van Geldorp
+# Copyright 2013-2022 Francois Gouget
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -31,6 +32,7 @@ use WineTestBot::Branches;
 use WineTestBot::Users;
 use WineTestBot::Utils;
 
+my $DAYS_DEFAULT = 4;
 
 
 sub SortKeys($$)
@@ -239,6 +241,7 @@ package StatusPage;
 use ObjectModel::CGI::FreeFormPage;
 our @ISA = qw(ObjectModel::CGI::FreeFormPage);
 
+use ObjectModel::BasicPropertyDescriptor;
 use ObjectModel::Collection;
 
 use WineTestBot::Config;
@@ -253,29 +256,20 @@ sub _initialize($$$)
 {
   my ($self, $Request, $RequiredRole) = @_;
 
-  $self->{Days} = $self->GetParam("Days");
-  $self->Validate();
-
   $self->{start} = Time();
-  $self->SUPER::_initialize($Request, $RequiredRole);
-}
-
-sub Validate($)
-{
-  my ($self) = @_;
+  my @PropertyDescriptors = (
+    CreateBasicPropertyDescriptor("Days", "Days ", !1, 1, "N", 2),
+  );
+  $self->SUPER::_initialize($Request, $RequiredRole, \@PropertyDescriptors);
 
-  if (!defined $self->{Days} or !$self->{Days})
+  if (!$self->GetParam("Days") or !$self->Validate() or
+      !int($self->GetParam("Days"))) # 00 case!
   {
-    $self->{Days} = $DefaultIndexDays;
+    # Replace with the default value so the cutoff can be calculated
+    $self->SetParam("Days", $DAYS_DEFAULT);
   }
-  elsif ($self->{Days} !~ /^[0-9]{1,2}$/)
-  {
-    $self->{ErrField} = "Days";
-    $self->{ErrMessage} = "The number of days must be between 1 and 99";
-    $self->{Days} = $DefaultIndexDays;
-    return undef;
-  }
-  return $self->SUPER::Validate();
+  # The action is a no-op so unset it to not need to redefine OnAction()
+  $self->SetParam("Action", undef);
 }
 
 sub OutputDot($$)
@@ -351,7 +345,8 @@ sub GenerateBody($)
 
   print "<h2><a name='jobs'></a>Jobs</h2>\n";
   my $Jobs = CreateJobs();
-  my $CutOff = time() - $self->{Days} * 24 * 60 * 60;
+  my $Days = $self->GetParam("Days");
+  my $CutOff = time() - $Days * 24 * 60 * 60;
   $Jobs->AddFilter("Submitted", [$CutOff], ">=");
   my $JobsCollectionBlock = new JobStatusBlock($Jobs, $self);
 
@@ -385,7 +380,7 @@ sub GenerateBody($)
 <form id="Days" action='/' method='get'>
   <div class='ItemProperty'><label>Show jobs for the past</label>
     <div class='ItemValue'>
-      <input type='text' name='Days' maxlength='2' size='2' value='$self->{Days}' /> days
+      <input type='text' name='Days' maxlength='2' size='2' value='$Days' /> days
       <input type='submit' value='Update'/>
     </div>
   </div>
@@ -400,11 +395,6 @@ EOF
   print "<p class='GeneralFooterText'>Generated in ", Elapsed($self->{start}), " s</p>\n";
 }
 
-sub OnAction($$)
-{
-  my ($self, $Action) = @_;
-  return $self->Validate() ? 1 : undef;
-}
 
 package main;
 
-- 
2.30.2




More information about the wine-devel mailing list