Accès rapide
Avoir un compte
Messagerie
Wifi 
12 : 12 [CriNET] FERME Probleme clime salle 123
2 : 12 [CriNET] OUVERT Probleme clime salle 123
5 : 11 [CriNET] INFO Probleme eduroam
4 : 11 [CriNET] INFO Probleme eduroam
S'abonner au flux RSS
Problème
Impossible de charger http://factuel.univ-lorraine.fr/feed/rss-breves.xml
Rechargez cette page
Tickets CRINet
2014120200FERME
Probleme clime salle 123
2014110400INFO
Probleme eduroam
2014110500INFO
Probleme eduroam
2014102300FERME
Intervention switch sale123

Tutorial

This test application aims to manage a meeting. Users can register by asking a login than they can choose a hotel.


# jifty app --name YvesTestApp
Can't guess application root from current path ...
Creating new application YvesTestApp
Creating directory lib
Creating directory lib/YvesTestApp
Creating directory bin
Creating directory etc
Creating directory doc
Creating directory log
Creating directory var
Creating directory var/mason
Creating directory share
Creating directory share/po
Creating directory share/web
Creating directory share/web/templates
Creating directory share/web/static
Creating directory lib/YvesTestApp/Model
Creating directory lib/YvesTestApp/Action
Creating directory t
Creating configuration file YvesTestApp/etc/config.yml

Models

We will write models for our data

# cd YvesTestApp
# jifty model --name User
Writing file  lib/YvesTestApp/Model/User.pm
Writing file  t/00-model-User.t
# jifty model --name Hotel
...
# jifty model --name Inscrit


We will use Login plugin for user
in lib/YvesTestApp/Model/User.pm
use strict;
use warnings;
package YvesTestApp::Model::User;
use base qw/Jifty::Plugin::Login::Model::User/;

# Your model-specific methods go here.

1;


in lib/YvesTestApp/Model/Hotel.pm
use YvesTestApp::Record schema {

}


add

       column name =>
           type is 'text',
           label is 'Nom',
           is mandatory;
       column address =>
           label is 'Adresse',
           render_as 'Textarea',
           type is 'text';
      column maxrooms =>
           type is 'integer',
           label is 'Nombre de chambres';


to allow all registred user to see hotels datas, we will override current_user_can function.
=head2 current_user_can ACTION

Let everybody read. 
Only allow logged-in users to create and edit. 
Otherwise, allow anyone.

=cut

sub current_user_can {
    my $self = shift;
    my $type = shift;

    if ($type eq 'create' || $type eq 'update') {
        return 0 if
           !$self->current_user->is_superuser
           && !$self->current_user->id;
        return 1;
    } elsif($type eq 'read') {
        return 1;
    }

    return $self->SUPER::current_user_can($type, @_);
}


in lib/YvesTestApp/Model/Inscrit.pm
we make foreign keys between User and Hotel models
use YvesTestApp::Model::User;
use YvesTestApp::Model::Hotel;


use YvesTestApp::Record schema {
       column user =>
               refers_to YvesTestApp::Model::User;
       column hotel =>
              refers_to YvesTestApp::Model::Hotel;

};

=head2 current_user_can ACTION

Let everybody read. 
Only allow logged-in users to create and edit. 
Otherwise, allow anyone.

=cut

sub current_user_can {
    my $self = shift;
    my $type = shift;

    if ($type eq 'create' || $type eq 'update') {
        return 0 if
           !$self->current_user->is_superuser
           && !$self->current_user->id;
        return 1;
    } elsif($type eq 'read') {
        return 1;
    }

    return $self->SUPER::current_user_can($type, @_);
}

1;




# jifty schema --setup
INFO - Generating SQL for application YvesTestApp...
INFO - Using YvesTestApp::Model::inscrit
INFO - Using YvesTestApp::Model::user
INFO - Using Jifty::Model::Metadata
INFO - Using Jifty::Model::Session
INFO - Using YvesTestApp::Model::hotel
INFO - Set up version v0.0.1, jifty version 0.609120


To use plugins:

# vi etc/config.yml

  Plugins: 
   - Login: {}
   - EditInPlace: {}



# jifty server        
INFO - You can connect to your server at http://localhost:8888/



Trouble :
This error message appears on some servers :
Application schema version in database (1e-06) doesn't match application schema version (0.0.1)
# sqlite3 yvestestapp
sqlite> update _jifty_metadata set value='v0.0.1' where id='1';
sqlite> .exit


View / Action

This view is only for registred users (Jifty->web->current_user->id)

create file share/web/templates/register
<‰init>
my $user= Jifty->web->current_user->id;

unless ($user) {
    Jifty->web->tangent(url => '/login/');
    $m->abort();
}

my $record = YvesTestApp::Model::Inscrit->new();
   $record->load($user);

my $action_class =  ($record->id)?'UpdateInscrit':'CreateInscrit';

my $action = Jifty->web->new_action(class=>$action_class, record => $record);

</%init>
<&|/_elements/wrapper, title => "Hôtel" &>
Choisissez votre hôtel :

<‰ Jifty->web->form->start() ‰>

<‰$action->hidden('user',$user)‰>
<‰$action->form_field('hotel')‰>

<‰ Jifty->web->form->submit(
    label    => ($record->id)?'Update':'Create') ‰>

<‰ Jifty->web->form->end() ‰>

</&>


Actions UpdateInscrit or CreateInscrit are automagically overload from model.
To protect a directory and all other sub pages or sub directory, we can use mason powerfull autohandler or work with controller/dispatcher.

Menu

Copy element/nav in your application
cp /usr/share/perl5/auto/Jifty/web/templates/_elements/nav share/web/templates/_elements/

Or use EditInPlace ('Page Info' in bottom of all pages)
to edit and change nav
Here my nav element with submenu subhome and subnav. See a bug in subhome where I need to force SERVER_NAME (??)
<‰init>
my $top = Jifty->web->navigation;
my $subhome=$top->child(Home       => url => "/", sort_order => 1);
   $subhome->child( Book  => url => ($ENV{SERVER_PORT} != 80)
       ? "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/register"
       : "http://$ENV{SERVER_NAME}/register", 
       label=>"Reserver un hotel", sort_order => 5) ;
my $subnav=$top->child(Identification       => url => "/login", sort_order => 2);
   $subnav->child(Quitter       => url => "/logout", sort_order => 3) if    
   $subnav->active();
$top->child(Admin       => url => "/admin/", sort_order => 998);

 if (Jifty->config->framework('AdminMode') ) {
    $top->child(Administration => url => "/__jifty/admin/", sort_order => 998);
    $top->child(OnlineDocs => url => "/__jifty/online_docs/", 
               label => 'Online docs',  sort_order => 999);
 }
return();
</%init>

Localisation

To choose your language
in etc/config.yml
 L10N: 
    PoDir: share/po
    Lang: fr


You can put your plugin po file in share/po
cp /usr/share/perl5/auto/Jifty/Plugin/Login/po/fr.po share/po/fr.po

Tests


in t/00-model-Hotel.t
change $o->create() with  
$o->create(name=>'h1'); 
....
$o->create(name=>'h2');

in t/00-model-Inscrit.t
change $o->create() with  
$o->create(user=>'agostini',hotel=>'h1'); 
....
$o->create(user=>'yves',hotel=>'h2');

# cd lib
# prove ../t
../t/00-model-User..........ok                                               
../t/00-model-Subscriber....ok                                               
../t/00-model-Hotel.........ok                                               
All tests successful.
Files=3, Tests=33,  5 wallclock secs ( 4.54 cusr +  0.20 csys =  4.74 CPU)

Deployement


use of fastCGI
in /etc/apache/conf.d/vhost.conf
NameVirtualHost 195.220.226.225:60

# not allow in virtualhost ! (but as many FastCgiServer as you need)
FastCgiServer /var/www/YvesTestApp/bin/jifty -initial-env JIFTY_COMMAND=fastcgi -processes 3

<VirtualHost test.univ-metz.fr>
  ServerName test.univ-metz.fr
  AddHandler fastcgi-script fcgi
  DocumentRoot /var/www/YvesTestApp/share/web/templates
  ScriptAlias /  /var/www/YvesTestApp/bin/jifty/

 <Directory /var/www/YvesTestApp/bin/>
    SetHandler fastcgi-script
    Options +ExecCGI
 </Directory>

CustomLog /var/www/YvesTestApp/log/access_log combined
ErrorLog /var/www/YvesTestApp/log/error_log
</VirtualHost>

FastCgiServer /var/www/YvesTestApp2/bin/jifty -initial-env JIFTY_COMMAND=fastcgi -processes 3 
<VirtualHost test2.univ-metz.fr>
  ServerName test2.univ-metz.fr
  AddHandler fastcgi-script fcgi
  DocumentRoot /var/www/YvesTestApp2/share/web/templates
  ScriptAlias /  /var/www/YvesTestApp/bin2/jifty/

....



Trouble with mysql and fastcgi :
$ FastCGI: server ".... TestApp/bin/jifty" stderr: DBD::mysql::st execute failed:
MySQL server has gone away at /usr/share/perl5/Jifty/DBI/Handle.pm line 485. $
Answer : see mysql_auto_reconnect in etc/config.yml
  Database:
    Database: 'yvestestapp'
    Driver: mysql
    mysql_auto_reconnect: 1
    Host: localhost


TODO:
mod_perl handler

OTHER


Sending mail
in etc/config.yml
  AdminEmail: 'agostini@univ-metz.fr'
  Mailer: 'SMTP'
  MailerArgs: []

this use Net::SMTP

There's a bug with actual Email::MIME 1.855
1.852 works fine