Changeset 472


Ignore:
Timestamp:
Sep 16, 2004, 2:31:21 PM (17 years ago)
Author:
kake
Message:

Add expiry option to prefs.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Changes

    r471 r472  
    22        Recent Changes now shows changes in the past 24 hours, past week,
    33          past fortnight, and past 30 days.
     4        Preferences now has an option for when your prefs expire.
    45
    560.39    15 September 2004
  • trunk/lib/OpenGuides/CGI.pm

    r471 r472  
    22use strict;
    33use vars qw( $VERSION );
    4 $VERSION = '0.05';
     4$VERSION = '0.06';
    55
    66use Carp qw( croak );
     
    3535      show_minor_edits_in_rc => 1,
    3636      default_edit_type      => "tidying",
     37      cookie_expires         => "never",
    3738  );
    3839
     
    6465      show_minor_edits_in_rc => 1,
    6566      default_edit_type      => "tidying",
     67      cookie_expires         => "never",
    6668  );
    6769
    6870Croaks unless a L<Config::Tiny> object is supplied as C<config>.
     71Acceptable values for C<cookie_expires> are C<never>, C<month>,
     72C<year>; anything else will default to C<month>.
    6973
    7074=cut
     
    7680        unless UNIVERSAL::isa( $config, "Config::Tiny" );
    7781    my $cookie_name = $class->_get_cookie_name( config => $config );
     82    my $expires;
     83    if ( $args{cookie_expires} and $args{cookie_expires} eq "never" ) {
     84        # Gosh, a hack.  YES I AM ASHAMED OF MYSELF.
     85        # Putting no expiry date means cookie expires when browser closes.
     86        # Putting a date later than 2037 makes it wrap round, at least on Linux
     87        # I will only be 62 by the time I need to redo this hack, so I should
     88        # still be alive to fix it.
     89        $expires = "Thu, 31-Dec-2037 22:22:22 GMT";
     90    } elsif ( $args{cookie_expires} and $args{cookie_expires} eq "year" ) {
     91        $expires = "+1y";
     92    } else {
     93        $args{cookie_expires} = "month";
     94        $expires = "+1M";
     95    }
    7896    # Supply 'default' values to stop CGI::Cookie complaining about
    7997    # uninitialised values.  *Real* default should be applied before
     
    88106                    rcmined    => $args{show_minor_edits_in_rc} || 0,
    89107                    defedit    => $args{default_edit_type} || "normal",
     108                    exp        => $args{cookie_expires},
    90109                  },
    91         -expires => "+1M",
     110        -expires => $expires,
    92111    );
    93112    return $cookie;
     
    123142             show_minor_edits_in_rc => $data{rcmined}   || 0,
    124143             default_edit_type      => $data{defedit}   || "normal",
     144             cookie_expires         => $data{exp}       || "month",
    125145           );
    126146}
  • trunk/preferences.cgi

    r471 r472  
    3131    my $rc_minor_eds = $cgi->param("show_minor_edits_in_rc") || 0;
    3232    my $edit_type    = $cgi->param("default_edit_type") || "normal";
     33    my $expires      = $cgi->param("cookie_expires") || "month";
    3334    my $cookie = OpenGuides::CGI->make_prefs_cookie(
    3435        config => $config,
     
    4041        show_minor_edits_in_rc => $rc_minor_eds,
    4142        default_edit_type      => $edit_type,
     43        cookie_expires         => $expires,
    4244    );
    4345    print OpenGuides::Template->output(
     
    5557                      show_minor_edits_in_rc => $rc_minor_eds,
    5658                      default_edit_type      => $edit_type,
     59                      cookie_expires         => $expires,
    5760                    }
    5861    );
  • trunk/t/13_cookies.t

    r471 r472  
    22use Config::Tiny;
    33use OpenGuides::CGI;
    4 use Test::More tests => 16;
     4use Time::Piece;
     5use Time::Seconds;
     6use Test::More tests => 18;
    57
    68eval { OpenGuides::CGI->make_prefs_cookie; };
     
    2729    show_minor_edits_in_rc => 1,
    2830    default_edit_type      => "tidying",
     31    cookie_expires         => "never",
    2932);
    3033isa_ok( $cookie, "CGI::Cookie", "->make_prefs_cookie returns a cookie" );
     34
     35my $expiry_string = $cookie->expires;
     36# Hack off the timezone bit since strptime can't parse it portably.
     37# Timezones taken from RFC 822.
     38$expiry_string =~ s/ (UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|1[A-IK-Z]|\+\d\d\d\d|-\d\d\d\d)$//;
     39print "# (String hacked to $expiry_string)\n";
     40my $expiry = Time::Piece->strptime( $expiry_string, "%a, %d-%b-%Y %T");
     41print "# Expires: " . $cookie->expires . ", ie $expiry\n";
     42my $now = localtime;
     43print "# cookie should still be valid in a year, ie " . ($now + ONE_YEAR) . "\n";
     44ok( $expiry - ( $now + ONE_YEAR ) > 0, "cookie expiry date correct" );
    3145
    3246$ENV{HTTP_COOKIE} = $cookie;
     
    5064is( $prefs{show_minor_edits_in_rc}, 1, "...and minor edits prefs" );
    5165is( $prefs{default_edit_type}, "tidying", "...and default edit prefs" );
     66is( $prefs{cookie_expires}, "never", "...and requested cookie expiry" );
    5267
    5368# Check that cookie parsing fails nicely if no cookie set.
     
    5570%prefs = eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
    5671is( $@, "", "->get_prefs_from_cookie doesn't die if no cookie set" );
    57 is( keys %prefs, 7, "...and returns seven default values" );
    58 
     72is( keys %prefs, 8, "...and returns eight default values" );
  • trunk/templates/preferences.tt

    r471 r472  
    8181    </p>
    8282
     83    <p>
     84      <label for="cookie_expires">Preferences expire:</label>
     85      <select name="cookie_expires">
     86        [% IF cookie_expires == 'never' %]
     87          <option value="month">in one month</option>
     88          <option value="year">in one year</option>
     89          <option value="never" selected="1">never</option>
     90        [% ELSIF cookie_expires == "year" %]
     91          <option value="month">in one month</option>
     92          <option value="year" selected="1">in one year</option>
     93          <option value="never">never</option>
     94        [% ELSE %]
     95          <option value="month" selected="1">in one month</option>
     96          <option value="year">in one year</option>
     97          <option value="never">never</option>
     98        [% END %]
     99      </select>
     100    </p>
     101
    83102    <input type="submit" value="Set it" class="form_button" />
    84103    <input type="hidden" name="action" value="set_preferences">
     
    128147  </p>
    129148
     149  <p>Your preferences will
     150    [% IF cookie_expires == 'never' %]
     151      never expire.
     152    [% ELSIF cookie_expires == 'year' %]
     153      expire in one year.
     154    [% ELSE %]
     155      expire in one month.
     156    [% END %]
     157  </p>
     158
    130159<ul>
    131160  <li><a href="[% full_cgi_url %]">Return to [% site_name %]</a></li>
Note: See TracChangeset for help on using the changeset viewer.