Professional Documents
Culture Documents
N. vrard
B2 CK
N. vrard (B2 CK )
1 / 56
Outline
Tryton Overview
Prerequisites
Initializing tryton
2 A basic module
3 Some more advanced features
4 Additional topics
N. vrard (B2 CK )
2 / 56
Tryton Overview
Global overview
A general purpose application platform
Web shop
Insurance software
GNU Health
Traditional ERP
Free Software
GPL-3
Tryton foundation
TU*
N. vrard (B2 CK )
3 / 56
Tryton Overview
Technical Overview
Written in Python
Three tiers architecture
PyGTK client (tryton) and a JavaScript client (sao) in its infancy
Application Server (trytond)
dbms, usually PostgreSQL but you can use SQLite or even MySQL
N. vrard (B2 CK )
4 / 56
Prerequisites
N. vrard (B2 CK )
5 / 56
Prerequisites
Configuring mercurial
In order to activate both the hgnested and MQ extensions of Mercurial we will need to
add those lines to your .hgrc
Example
[extensions]
hgext.mq =
hgnested =
N. vrard (B2 CK )
6 / 56
Initializing tryton
Installing trytond
Example
$
$
$
$
N. vrard (B2 CK )
7 / 56
Initializing tryton
N. vrard (B2 CK )
8 / 56
Initializing tryton
Example
$ touch ~/databases/test.sqlite
$ ./bin/trytond -d test -u ir res
trytond will ask you for the admin password at the end of the installation process.
N. vrard (B2 CK )
9 / 56
Initializing tryton
cd
hg
cd
hg
trytond/modules
init training
training/.hg
clone http://hg.tryton.org/training -b 3.4 patches
N. vrard (B2 CK )
10 / 56
A basic module
Outline
1 Overview and Installation
2 A basic module
A minimal module
Defining some object / tables
Different fields
Defining a tree and a form view
Default values and on_change calls
N. vrard (B2 CK )
11 / 56
A basic module
A minimal module
N. vrard (B2 CK )
12 / 56
A basic module
A minimal module
Example
[tryton]
version=0.0.1
depends:
ir
res
N. vrard (B2 CK )
13 / 56
A basic module
Creating a model
A trytond model is a python class inheriting from Model. To enable the SQL
persistence the model must inherit of ModelSQL.
Example
from t r y t o n d . model import ModelSQL
__all__ = [ Opportunity ]
c l a s s O p p o r t u n i t y ( ModelSQL ) :
Opportunity
__name__ = t r a i n i n g . o p p o r t u n i t y
N. vrard (B2 CK )
14 / 56
A basic module
In order for your object to be "known" by trytond they must be registered into the pool.
Example
from t r y t o n d . p o o l import P o o l
from . o p p o r t u n i t y import
def r e g i s t e r ( ) :
Pool . r e g i s t e r (
Opportunity ,
module = t r a i n i n g , t y p e _ = model )
N. vrard (B2 CK )
15 / 56
A basic module
Example
c l a s s O p p o r t u n i t y ( ModelSQL ) :
Opportunity
__name__ = t r a i n i n g . o p p o r t u n i t y
_rec_name = d e s c r i p t i o n
d e s c r i p t i o n = f i e l d s . Char ( D e s c r i p t i o n ,
r e q u i r e d =True )
s t a r t _ d a t e = f i e l d s . Date ( S t a r t Date ,
r e q u i r e d =True )
e n d _ d a t e = f i e l d s . D a t e ( End D a t e )
p a r t y = f i e l d s . Many2One ( p a r t y . p a r t y ,
P a r t y , r e q u i r e d =True )
comment = f i e l d s . T e x t ( Comment )
N. vrard (B2 CK )
16 / 56
A basic module
fields arguments
N. vrard (B2 CK )
17 / 56
A basic module
Different fields
fields.Char
In a tryton module:
Example
d e s c r i p t i o n = f i e l d s . Char ( D e s c r i p t i o n , r e q u i r e d = T r u e )
In the interface:
Example
In SQL:
Example
d e s c r i p t i o n VARCHAR NOT NULL
N. vrard (B2 CK )
18 / 56
A basic module
Different fields
fields.Date
In a tryton module:
Example
s t a r t _ d a t e = f i e l d s . Date ( S t a r t Date , r e q u i r e d =True )
In the interface:
Example
In SQL:
Example
s t a r t _ d a t e DATE NOT NULL
N. vrard (B2 CK )
19 / 56
A basic module
Different fields
fields.Text
In a tryton module:
Example
comment = f i e l d s . T e x t ( Comment )
In the interface:
Example
In SQL:
Example
comment TEXT
N. vrard (B2 CK )
20 / 56
A basic module
Different fields
fields.Many2One
In a tryton module:
Example
p a r t y = f i e l d s . Many2One ( p a r t y . p a r t y , P a r t y , r e q u i r e d = T r u e )
In the interface:
Example
In SQL:
Example
p a r t y i n t e g e r NOT NULL,
FOREIGN KEY( p a r t y ) REFERENCES p a r t y _ p a r t y ( i d )
N. vrard (B2 CK )
21 / 56
A basic module
Different fields
fields.One2Many
fields.Many2Many
fields.One2One
N. vrard (B2 CK )
22 / 56
A basic module
Displaying data
To use the presentation layer your model must inherit from ModelView
Example
c l a s s O p p o r t u n i t y ( ModelSQL , ModelView ) :
You must also add the xml presentation file in the tryton.cfg configuration file
Example
xml:
opportunity.xml
N. vrard (B2 CK )
23 / 56
A basic module
Defining a view
N. vrard (B2 CK )
24 / 56
A basic module
A tree view
Example
< r e c o r d model = " i r . u i . view " i d = " o p p o r t u n i t y _ v i e w _ l i s t " >
< f i e l d name= " model " > t r a i n i n g . o p p o r t u n i t y < / f i e l d >
< f i e l d name= " t y p e " > t r e e < / f i e l d >
< f i e l d name= " name " > o p p o r t u n i t y _ l i s t < / f i e l d >
</ record>
Example
< t r e e s t r i n g =" O p p o r t u n i t i e s ">
< f i e l d name= " p a r t y " / >
< f i e l d name= " d e s c r i p t i o n " / >
< f i e l d name= " s t a r t _ d a t e " / >
< f i e l d name= " e n d _ d a t e " / >
</ tree>
N. vrard (B2 CK )
25 / 56
A basic module
A form view
Example
< r e c o r d model = " i r . u i . view " i d = " o p p o r t u n i t y _ v i e w _ f o r m " >
< f i e l d name= " model " > t r a i n i n g . o p p o r t u n i t y < / f i e l d >
< f i e l d name= " t y p e " > form < / f i e l d >
< f i e l d name= " name " > o p p o r t u n i t y _ f o r m < / f i e l d >
</ record>
Example
< form s t r i n g = " O p p o r t u n i t y " >
< l a b e l name= " p a r t y " / >
< f i e l d name= " p a r t y " / >
< l a b e l name= " d e s c r i p t i o n " / >
< f i e l d name= " d e s c r i p t i o n " / >
< l a b e l name= " s t a r t _ d a t e " / >
< f i e l d name= " s t a r t _ d a t e " / >
< l a b e l name= " e n d _ d a t e " / >
< f i e l d name= " e n d _ d a t e " / >
< s e p a r a t o r name= " comment " c o l s p a n = " 4 " / >
< f i e l d name= " comment " c o l s p a n = " 4 " / >
< / form >
N. vrard (B2 CK )
26 / 56
A basic module
Example
< r e c o r d model = " i r . a c t i o n . a c t _ w i n d o w "
id =" a c t _ o p p o r t u n i t y _ f o r m ">
< f i e l d name= " name " > O p p o r t u n i t i e s < / f i e l d >
< f i e l d name= " r e s _ m o d e l " > t r a i n i n g . o p p o r t u n i t y < / f i e l d >
</ record>
< r e c o r d model = " i r . a c t i o n . a c t _ w i n d o w . view "
id =" act_opportunity_form_view1 ">
< f i e l d name= " s e q u e n c e " e v a l = " 10 " / >
< f i e l d name= " view " r e f = " o p p o r t u n i t y _ v i e w _ t r e e " / >
< f i e l d name= " a c t _ w i n d o w " r e f = " a c t _ o p p o r t u n i t y _ f o r m " / >
</ record>
< r e c o r d model = " i r . a c t i o n . a c t _ w i n d o w . view "
id =" act_opportunity_form_view2 ">
< f i e l d name= " s e q u e n c e " e v a l = " 20 " / >
< f i e l d name= " view " r e f = " o p p o r t u n i t y _ v i e w _ f o r m " / >
< f i e l d name= " a c t _ w i n d o w " r e f = " a c t _ o p p o r t u n i t y _ f o r m " / >
</ record>
N. vrard (B2 CK )
27 / 56
A basic module
Example
< menuitem name= " T r a i n i n g " i d = " m e n u _ t r a i n i n g " / >
< menuitem p a r e n t = " m e n u _ t r a i n i n g "
a c t i o n =" a c t _ o p p o r t u n i t y _ f o r m "
id =" menu_opportunity_form " / >
N. vrard (B2 CK )
28 / 56
A basic module
N. vrard (B2 CK )
29 / 56
A basic module
Tryton provides a way to react on the user input by changing the value of other fields.
This mechanism is call the on_changes.
They come in two flavours:
on_change_<field name> When a field is changed a list of value is sent to the server.
The server sends the new values of some fields
on_change_with_<field name> A field value is computed when any field in a list of
fields is modified. The computation occurs on the server.
In both case the list of fields sent to the server is specified thanks to the decorator
@fields.depends.
N. vrard (B2 CK )
30 / 56
A basic module
on_change
Example
@fields . depends ( p a r t y )
def on_change_party ( s e l f ) :
a d d r e s s = None
if self . party :
a d d r e s s = s e l f . p a r t y . a d d r e s s _ g e t ( type= i n v o i c e )
return {
address : address ,
}
N. vrard (B2 CK )
31 / 56
A basic module
on_change_with
Example
@fields . depends ( s t a r t _ d a t e )
def on_change_with_end_date ( s e l f ) :
if self . start_date :
return s e l f . s t a r t _ d a t e + d a t e t i m e . t i m e d e l t a ( days =7)
r e t u r n None
N. vrard (B2 CK )
32 / 56
Outline
1 Overview and Installation
2 A basic module
3 Some more advanced features
Workflows
More beautiful views
Function fields
Wizards
Extending pre-existing tryton objects
4 Additional topics
N. vrard (B2 CK )
33 / 56
Workflows
N. vrard (B2 CK )
34 / 56
Workflows
Defining a workflow
N. vrard (B2 CK )
, c l s ) . __setup__ ( )
\|= set ((
, converted ) ,
, lost ) ,
35 / 56
Workflows
N. vrard (B2 CK )
36 / 56
Workflows
N. vrard (B2 CK )
37 / 56
Lets add the state and the buttons to the opportunity view. There is also a way of
grouping widgets
Example
<group co l =" 2 " colspan =" 2 " id =" s t a t e ">
< l a b e l name= " s t a t e " / >
< f i e l d name= " s t a t e " / >
< / group>
<group co l =" 2 " colspan =" 2 " id =" b u t t o n s ">
< b u t t o n name= " l o s t " s t r i n g = " L o s t " i c o n = " t r y t o nc a n c e l " / >
< b u t t o n name= " c o n v e r t " s t r i n g = " C o n v e r t " i c o n = " t r y t o ngon e x t " / >
< / group>
N. vrard (B2 CK )
38 / 56
N. vrard (B2 CK )
39 / 56
N. vrard (B2 CK )
: ~Eval ( s t a t e ) \
opportunity ]) ,
: ~Eval ( s t a t e ) \
opportunity ]) ,
40 / 56
Function fields
Any tryton type can be used. Note also that since this signature is the same as the one
of an on_change_with then the same function can be used for both.
Of course a setter and a searcher can also be defined in order to modify or search
corresponding data.
N. vrard (B2 CK )
41 / 56
Function fields
N. vrard (B2 CK )
42 / 56
Wizards
Sometime you want to add functionalities to a model that do not suite the use of a
button. For this kind of use case the wizard is your solution.
A wizard is composed of two things:
A set of views that represent the form to gather the user input
A "state machine" that define what should be done
N. vrard (B2 CK )
43 / 56
Wizards
Wizard views
Those are standard ModelView, you can define on_change, on_change_with and
default values on them.
Example
c l a s s C o n v e r t O p p o r t u n i t i e s S t a r t ( ModelView ) :
Convert O p p o r t u n i t i e s
__name__ = t r a i n i n g . o p p o r t u n i t y . c o n v e r t . s t a r t
N. vrard (B2 CK )
44 / 56
Wizards
N. vrard (B2 CK )
45 / 56
Wizards
Example
< r e c o r d model = " i r . a c t i o n . w i z a r d " i d = " a c t _ c o n v e r t _ o p p o r t u n i t i e s " >
< f i e l d name= " name " > C o n v e r t O p p o r t u n i t i e s < / f i e l d >
< f i e l d name= " wiz_name " > t r a i n i n g . o p p o r t u n i t y . c o n v e r t < / f i e l d >
< f i e l d name= " model " > t r a i n i n g . o p p o r t u n i t y < / f i e l d >
</ record>
< r e c o r d model = " i r . a c t i o n . keyword " i d = " a c t _ c o n v e r t _ o p p o r t u n i t i e s _ k e y w o r d " >
< f i e l d name= " keyword " > f o r m _ a c t i o n < / f i e l d >
< f i e l d name= " model " > t r a i n i n g . o p p o r t u n i t y ,1< / f i e l d >
< f i e l d name= " a c t i o n " r e f = " a c t _ c o n v e r t _ o p p o r t u n i t i e s " / >
</ record>
N. vrard (B2 CK )
46 / 56
Extending models
N. vrard (B2 CK )
47 / 56
Example
<data>
< x p a t h e x p r = " / form / n o t e b o o k / p a g e [ @id = a c c o u n t i n g ] "
p o s i t i o n =" a f t e r ">
< p a g e name= " o p p o r t u n i t i e s " c o l = " 1 " >
< s e p a r a t o r name= " o p p o r t u n i t i e s " / >
< f i e l d name= " o p p o r t u n i t i e s " / >
< / page>
< / xpath>
</ data>
N. vrard (B2 CK )
48 / 56
Additional topics
Outline
Reporting
proteus
Table Queries
N. vrard (B2 CK )
49 / 56
Additional topics
Reporting
Adding a report
To add a report you must create an odt template that is compatible with relatorio
(http://relatorio.openhex.org/)
This report engine allows you to create opendocument files from templates.
N. vrard (B2 CK )
50 / 56
Additional topics
Reporting
You must define a python class that will register a report object
Example
from t r y t o n d . r e p o r t import R e p o r t
c l a s s OpportunityReport ( Report ) :
pass
You might want to overload the parse method to define a specific behavior.
N. vrard (B2 CK )
51 / 56
Additional topics
Reporting
Example
< r e c o r d model = " i r . a c t i o n . r e p o r t " i d = " r e p o r t _ o p p o r t u n i t y " >
< f i e l d name= " name " > O p p o r t u n i t y < / f i e l d >
< f i e l d name= " model " > t r a i n i n g . o p p o r t u n i t y < / f i e l d >
< f i e l d name= " r e p o r t _ n a m e " > t r a i n i n g . o p p o r t u n i t y < / f i e l d >
< f i e l d name= " r e p o r t " > t r a i n i n g / o p p o r t u n i t y . o d t < / f i e l d >
</ record>
< r e c o r d model = " i r . a c t i o n . keyword "
id =" r e p o r t _ o p p o r t u n i t y _ k e y w o r d ">
< f i e l d name= " keyword " > f o r m _ p r i n t < / f i e l d >
< f i e l d name= " model " > t r a i n i n g . o p p o r t u n i t y ,1< / f i e l d >
< f i e l d name= " a c t i o n " r e f = " r e p o r t _ o p p o r t u n i t y " / >
</ record>
N. vrard (B2 CK )
52 / 56
Additional topics
proteus
You might want to script your tryton server. For this there is the proteus library. It will
allow you to interact with it in a python way.
With models you will be able to use the find, edit, delete and create objects
You will also have the possiblity to use the wizards.
N. vrard (B2 CK )
53 / 56
Additional topics
proteus
Setting up proteus
N. vrard (B2 CK )
54 / 56
Additional topics
proteus
An example of proteus
Example
import csv
import datetime
import sys
from p r o t e u s i m p o r t c o n f i g , Model
d e f main ( a r g s ) :
P a r t y = Model . g e t ( p a r t y . p a r t y )
O p p o r t u n i t y = Model . g e t ( t r a i n i n g . o p p o r t u n i t y )
c s v _ f i l e = c s v . D i c t R e a d e r ( open ( a r g s [ 1 ] , r ) )
for line in c s v _ f i l e :
p a r t i e s = P a r t y . f i n d ( [ ( name , = , l i n e [ P a r t y ] ) ] )
i f not p a r t i e s :
p a r t y = P a r t y ( name= l i n e [ P a r t y ] )
party . save ( )
else:
party = parties [0]
l i n e _ d a t e = d a t e t i m e . d a t e t i m e . s t r p t i m e ( l i n e [ Date ] ,
%d/%m/%y ) . d a t e ( )
new_opportunity = Opportunity ( party =party ,
description=line [ Description ] , start_date =line_date )
new_opportunity . save ( )
i f __name__ == __main__ :
c o n f i g . s e t _ t r y t o n d ( p a s s w o r d = admin , d a t a b a s e _ n a m e = t r a i n i n g )
main ( s y s . a r g v )
N. vrard (B2 CK )
55 / 56
Additional topics
Table Queries
N. vrard (B2 CK )
56 / 56