[cgiapp] Re: error subclassing CGIApp

2009-08-05 Thread P Kishor
So, I think I have discovered an answer to my problem -- seems like I
can't have setup in BaseCGIApp. If I move the declaration of the
runmodes to the subclassed package MyApp.pm, then things work fine.
Which begs the next question -- how do I declare a bunch of runmodes
in BaseCGIApp so that I don't have to re-declare them in a subclassed
packed?

On Wed, Aug 5, 2009 at 12:22 PM, P Kishorpunk.k...@gmail.com wrote:
 I have a pretty basic app modeled after the Perl CGI-App MVC pattern
 by Mark Rajcok. It looks like so

 index.cgi
 -
 use MyApp;

 my $App = 'MyApp';

 CGI::Application::Dispatch-dispatch(
    table = [
        'welcome'           = { app = $App, rm = 'welcome',          },
        'view'              = { app = $App, rm = 'view',             },
        'account_prefs'     = { app = $App, rm = 'account_prefs',    },
        'account_admin'     = { app = $App, rm = 'account_admin',    },
        ''                  = { app = $App, rm = 'welcome',          },
    ],
 );
 =

 BaseCGIApp.pm
 -
 package BaseCGIApp;

 use strict;
 use base 'CGI::Application';

 use CGI::Simple;
 use CGI::Application::Plugin::DBH qw(dbh dbh_config);
 use CGI::Application::Plugin::Session;
 use CGI::Application::Plugin::LogDispatch;
 use CGI::Application::Plugin::Authentication;

 sub cgiapp_get_query { my $self = shift; CGI::Simple-new(); }

 # Configure the session once during the init stage
 sub cgiapp_init {
    my $self = shift;

    $self-authen-config(
        CREDENTIALS          = [ 'username', 'password' ],
        DRIVER               = [
            'DBI',
            DBH         = $self-dbh,
            TABLE       = 'users',
            CONSTRAINTS = { 'username' = '__CREDENTIAL_1__',
 'MD5:password' = '__CREDENTIAL_2__' },
        ],
        LOGIN_RUNMODE        = 'account_login',
        LOGOUT_RUNMODE       = 'welcome',
        POST_LOGIN_RUNMODE   = 'view',
        POST_LOGIN_CALLBACK  = \_account_update_session_on_login,
    );

 }

 sub setup {
        my $self = shift;

    $self-mode_param(path_info = 1, param = 'rm');
    $self-start_mode('welcome');

    $self-run_modes(

        # unprotected runmodes
        'account_create'    = 'account_create',
        'account_mail_pwd'  = 'account_mail_pwd',

        # protected runmodes
        'account_login'     = 'account_login',
        'account_prefs'     = 'account_prefs',
        'account_admin'     = 'account_admin',

    );

        $self-authen-protected_runmodes(
        'account_prefs',
        'account_admin'
    );

 }

 sub cgiapp_prerun { my $self = shift; }
 sub account_login {..}
 sub account_prefs {..}
 sub account_admin {..}
 =

 MyApp.pm
 -
 package MyApp;

 use strict;
 use base 'BaseCGIApp';

 sub setup {
        my $self = shift;

    $self-start_mode('welcome');
    $self-run_modes(
        'welcome' = 'welcome', # unprotected runmode
        'view'    = 'view',    # protected runmode
        );
    $self-authen-protected_runmodes('view',);
 }

 sub welcome {..}
 sub view {..}
 =

 The following work

 http://../
 http://../welcome

 The following fail with the error CGI::Application::Dispatch error! Not 
 Found

 http://../view
 http://../account_prefs

 In other words, all protected runmodes are not found. What am I doing wrong?

 By the way, if I do away with the BaseCGIApp.pm, and move all the
 runmodes into one single package 'MyApp' then everything works
 properly.

 --
 Puneet Kishor http://www.punkish.org
 Carbon Model http://carbonmodel.org
 Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
 Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor
 Nelson Institute, UW-Madison http://www.nelson.wisc.edu
 ---
 Assertions are politics; backing up assertions with evidence is science
 ===
 Sent from Madison, WI, United States




-- 
Puneet Kishor http://www.punkish.org
Carbon Model http://carbonmodel.org
Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor
Nelson Institute, UW-Madison http://www.nelson.wisc.edu
---
Assertions are politics; backing up assertions with evidence is science
===
Sent from Madison, WI, United States

#  CGI::Application community mailing list  
####
##  To unsubscribe, or change your message delivery options,  ##
##  

Re: [cgiapp] Re: error subclassing CGIApp

2009-08-05 Thread Michael Peters

P Kishor wrote:

So, I think I have discovered an answer to my problem -- seems like I
can't have setup in BaseCGIApp. 


Sure you can. You just need to make sure that you call your base class's setup() 
method in your child classes if you're overridding it. This is pretty standard OO:


  sub setup {
my $self = shift;
$self-SUPER::setup();
# now do my own stuff
  }

--
Michael Peters
Plus Three, LP


#  CGI::Application community mailing list  
####
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
####
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:  http://cgiapp.erlbaum.net/ ##
####




RE: [cgiapp] Re: error subclassing CGIApp

2009-08-05 Thread Alex
But, using setup for authentication is, as far as I could get it from the docs, 
the wrong place to do it. Use cgiapp_prerun instead.

There are some slides showing this at 
http://cgiapp.erlbaum.net/index.cgi?ArticlesAndTutorials 
I used this and made a working template for testing purposes. See the following 
code.

HTH, Alex

[code]
#!/usr/bin/perl

package CatcherInTheRye;

use strict;
use warnings;
use base qw/CGI::Application/;

use CGI::Application::Plugin::Forward;
use CGI::Application::Plugin::Redirect;
use CGI::Application::Plugin::ValidateRM;
use CGI::Application::Plugin::ConfigAuto (qw/cfg/);
use CGI::Application::Plugin::DBH (qw/dbh_config dbh/);

# for development
use CGI::Application::Plugin::DBIProfile;

use Data::Dumper qw/Dumper/;

=head1 NAME

CatcherInTheRye - Perl extension for blah blah blah

=head1 SYNOPSIS

  use CatcherInTheRye;
  blah blah blah

=head1 DESCRIPTION

Stub documentation for CatcherInTheRye, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.

Blah blah blah.

=head2 EXPORT

None by default.


=head1 METHODS

=cut

=head2 cgiapp_init()

Open database connection, setup config files, etc.

=cut

sub cgiapp_init {
my $self = shift;

# Set some defaults for DFV unless they already exist.  
$self-param('dfv_defaults') ||
$self-param('dfv_defaults', {
missing_optional_valid = 1,
filters = 'trim',
msgs = {
any_errors = 'some_errors',
prefix = 'err_',
invalid= 'Invalid',
missing= 'Missing',
format = 'span class=dfv-errors%s/span',
},
});

} # /cgiapp_init




=head2 setup()

Defined runmodes, etc.

=cut

sub setup {
my $self = shift;

$self-start_mode('start');
$self-run_modes([qw/
start
/]);

} # /setup




=head2 cgiapp_prerun()

We know what the runmode will be. Do we want to do anything about it?

=over
=item Authentication?
=item Authorization?
=item Redirection?
=back

=cut

sub cgiapp_prerun {
my $self = shift;
} # /cgiapp_prerun




=head2 cgiapp_get_query()

Bote: uploads are disabled by default in CGI::Simple.

=cut

sub cgiapp_get_query {
require CGI::Simple;
my $q = new CGI::Simple;
return $q;
} # /cgiapp_get_query




=head2 start()

=cut

sub start {
my $self= shift;


return 'start';
} # /start





=head2 cgiapp_postrun()

Output manipulation:

=over
=item add common header/footer?
=item Cleanup your HTML?
=item Rewrite URLs?
=back

=cut

sub cgiapp_postrun {
my $self = shift;
my $output = shift;

} # /cgiapp_postrun




=head2 teardown()

Close database connections (if not persistant), flush out session storage, etc.

=cut

sub teardown {
my $self = shift;

} # /teardown


=head1 SEE ALSO

Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.

If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.

=head1 AUTHOR

A. U. Thor, Elta.u.t...@a.galaxy.far.far.awayegt

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009 by A. U. Thor

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut

1;

use strict;
use warnings;

my $app = CatcherInTheRye-new();
$app-run();
[/code]


-Original Message-
From: cgiapp-boun...@lists.erlbaum.net 
[mailto:cgiapp-boun...@lists.erlbaum.net] On Behalf Of Michael Peters
Sent: Mittwoch, 5. August 2009 20:09
To: CGI Application
Subject: Re: [cgiapp] Re: error subclassing CGIApp

P Kishor wrote:
 So, I think I have discovered an answer to my problem -- seems like I 
 can't have setup in BaseCGIApp.

Sure you can. You just need to make sure that you call your base class's 
setup() method in your child classes if you're overridding it. This is pretty 
standard OO:

   sub setup {
 my $self = shift;
 $self-SUPER::setup();
 # now do my own stuff
   }

--
Michael Peters
Plus Three, LP


#  CGI::Application community mailing list  
####
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
####
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:  http://cgiapp.erlbaum.net