Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module A minimal module A tryton module creation crash course Defining some object / tables Defining a tree and a form view Default values N. Évrard Some more advanced features
Workflows B2CK Buttons Function fields Wizards
Extending Tryton Unconference Leipzig 2014 pre-existing tryton objects
Extending models Extending views Outline
Tryton Crash Course 1 Installing tryton N. Évrard Prerequisites Installing tryton Initializing tryton Prerequisites Initializing tryton 2 A basic module A basic module
A minimal module A minimal module Defining some object / tables Defining some object / tables Defining a tree and a form view Defining a tree and a form view Default values Default values Some more advanced features 3 Some more advanced features Workflows Buttons Workflows Function fields Wizards Buttons
Extending Function fields pre-existing tryton objects Wizards Extending models Extending views 4 Extending pre-existing tryton objects Extending models Extending views Python good practices (pip & virtualenv)
Tryton Crash Course
N. Évrard
Installing tryton Prerequisites pip is THE tool for installing and managing Python packages. Initializing tryton virtualenv is THE tool used to create isolated Python environment. A basic module A minimal module create isolated Python environments Defining some object / tables Do not mix different version of your libraries / applications Defining a tree and a form view Installation of packages in the user $HOME Default values
Some more pip install virtualenv (or your distro package manager) advanced features Workflows virtualenv --system-site-packages .venv/trytond Buttons Function fields source .venv/trytond/bin/activate Wizards Extending hgnested is a mercurial extension. The tryton project use it to easily pre-existing tryton objects apply the same command on nested repositories. Extending models Extending views Installing trytond
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module Example A minimal module Defining some object / tables Defining a tree and a $ hg nclone http://hg.tryton.org/trytond -b 3.4 form view Default values $ cd trytond Some more $ pip install -e . advanced features Workflows $ pip install vobject Buttons Function fields Wizards
Extending pre-existing tryton objects
Extending models Extending views Setting up a trytond config file
Tryton Crash Course
N. Évrard Here is a minimal example of a configuration file. You should save it in $HOME/.trytond.conf Installing tryton
Prerequisites Initializing tryton Example A basic module
A minimal module Defining some object / tables [database] Defining a tree and a form view path = /home/training/databases Default values
Some more advanced features
Workflows Buttons We will set the TRYTOND_CONFIG environment variable Function fields Wizards Example Extending pre-existing tryton objects Extending models $ export TRYTOND_CONFIG=$HOME/.trytond.conf Extending views Initializing a minimal trytond database
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module Example A minimal module Defining some object / tables Defining a tree and a $ touch ~/databases/test.sqlite form view Default values $ ./bin/trytond -d test -u ir res
Some more advanced features
Workflows Buttons Function fields trytond will ask you for the admin password at the end of the installation Wizards process. Extending pre-existing tryton objects
Extending models Extending views Adding a new modules
Tryton Crash Course
N. Évrard
Installing tryton Prerequisites In this tutorial we will use a MQ repository in order to progress step by Initializing tryton step. A basic module
A minimal module Defining some object / Example tables Defining a tree and a form view Default values $ cd trytond/modules Some more advanced features $ hg init training Workflows $ cd training/.hg Buttons Function fields $ hg clone http://hg.tryton.org/training -b 3.4 patches Wizards
Extending pre-existing tryton objects
Extending models Extending views A minimal trytond modules
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module
A minimal module Defining some object / A minimal trytond modules needs two files: tables Defining a tree and a form view __init__.py the usual file needed by all python modules Default values tryton.cfg the file that helps tryton glue together model and view Some more advanced features definitions Workflows Buttons Function fields Wizards
Extending pre-existing tryton objects
Extending models Extending views The content of tryton.cfg
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton Example A basic module
A minimal module Defining some object / tables [tryton] Defining a tree and a form view version=0.0.1 Default values depends: Some more advanced features ir Workflows Buttons res Function fields Wizards
Extending pre-existing tryton objects
Extending models Extending views Creating a model
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module A minimal module Example Defining some object / tables Defining a tree and a from trytond .model import ModelSQL form view Default values __all__ = [ ’Opportunity’]
Some more class Opportunity (ModelSQL): advanced features ’Opportunity ’ Workflows __name__ = ’training.opportunity ’ Buttons Function fields Wizards
Extending pre-existing tryton objects
Extending models Extending views Register the model
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module A minimal module Example Defining some object / tables Defining a tree and a from trytond.pool import Pool form view from .opportunity import ∗ Default values def register (): Some more Pool.register( advanced features Opportunity , Workflows module=’training ’ , type_=’model’) Buttons Function fields Wizards
Extending pre-existing tryton objects
Extending models Extending views Adding fields to a model
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module Example
A minimal module Defining some object / class Opportunity (ModelSQL): tables ’Opportunity ’ Defining a tree and a __name__ = ’training.opportunity ’ form view _rec_name = ’description ’ Default values description = fields.Char( ’Description ’, required=True) Some more start_date = fields.Date( ’Start Date’, advanced features required=True) Workflows end_date = fields.Date( ’End Date’) Buttons party = fields.Many2One( ’party.party ’, ’Party ’ , required=True) Function fields comment = fields .Text( ’Comment’) Wizards
Extending pre-existing tryton objects
Extending models Extending views Displaying data
Tryton Crash Course
N. Évrard To use the presentation layer your model must inherit from ModelView Installing tryton
Prerequisites Initializing tryton Example
A basic module class Opportunity(ModelSQL, ModelView): A minimal module Defining some object / tables Defining a tree and a form view You must also add the xml presentation file in the tryton.cfg Default values configuration file Some more advanced features Workflows Example Buttons Function fields Wizards xml: Extending pre-existing tryton opportunity.xml objects
Extending models Extending views Defining a view
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module
A minimal module Defining some object / trytond/ir/ui/view.py tables View objects are normal tryton objects ( ) Defining a tree and a form view Two kind of view: Default values
Some more Tree view a list of record advanced features Form view a view for editing/creating one record Workflows Buttons Function fields Wizards
Extending pre-existing tryton objects
Extending models Extending views A tree view
Tryton Crash Course
N. Évrard
Installing tryton Prerequisites Example Initializing tryton
A basic module
A minimal module
Some more advanced features Example Workflows
Buttons
Extending models Extending views A form view
Tryton Crash Course
N. Évrard Example Installing tryton
Prerequisites
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton A basic module Create a method in the object with the name default_
Extending pre-existing tryton objects
Extending models Extending views Adding workflow to object
Tryton Crash Course
N. Évrard
Installing tryton Your object should inherit from Workflow Prerequisites Initializing tryton Example A basic module
A minimal module Defining some object / class Opportunity(Workflow , ModelSQL, ModelView): tables Defining a tree and a form view Default values It must have a state field: Some more advanced features Example Workflows Buttons state = fields.Selection([ Function fields ( ’opportunity ’, ’Opportunity’), Wizards ( ’converted ’, ’Converted’), (’lost’, ’Lost’), Extending ], ’State’, required=True, pre-existing tryton readonly=True, sort=False) objects
Extending models Extending views Defining a workflow
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module A workflow is composed of transitions:
A minimal module Defining some object / tables Example Defining a tree and a form view @classmethod Default values def __setup__(cls ): super (Opportunity , cls ).__setup__() Some more cls._transitions \|= set (( advanced features ( ’opportunity ’, ’converted’), Workflows (’opportunity’, ’lost ’), Buttons )) Function fields Wizards
Extending pre-existing tryton objects
Extending models Extending views Defining transition method
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module Each transition must have a class method:
A minimal module Defining some object / Example tables Defining a tree and a form view @classmethod Default values @Workflow. transition ( ’converted ’) def convert(cls , opportunities): Some more pool = Pool() advanced features Date = pool.get(’ir.date’)
Workflows cls.write(opportunities , { ’end_date’: Date.today() , Buttons }) Function fields Wizards
Extending pre-existing tryton objects
Extending models Extending views Adding buttons in view
Tryton Crash Course N. Évrard Now we need to add buttons in the view Installing tryton Example Prerequisites Initializing tryton
A minimal module Defining some object / tables The method must be "button decorated" to be callable and defined. Defining a tree and a form view Default values Example
Some more advanced features @classmethod Workflows def __setup__(cls ): ... Buttons cls ._buttons.update({ Function fields ’convert’: {}, Wizards ’ l o s t ’ : { } , }) Extending pre-existing tryton @classmethod objects @ModelView. button Extending models @Workflow. transition ( ’converted ’) Extending views def convert(cls , opportunities): ... Defining a simple function field
Tryton Crash Course
N. Évrard
Installing tryton A function field is a field that is computed into python its data is not Prerequisites persistently store into the database. Initializing tryton A basic module Example A minimal module Defining some object / tables duration = fields.Function(fields.Integer( ’Duration’), ’get_duration ’) Defining a tree and a form view def get_duration(self , name=None): Default values i f not self .start_date or not self .end_date: return None Some more return ( self .end_date − self .start_date ).days advanced features
Workflows Buttons Function fields Any tryton type can be used. Note also that since this signature is the Wizards same as the one of an on_change_with then the same function can be Extending pre-existing tryton used for both. objects setter searcher Extending models Of course a and a can also be defined in order to Extending views modify or search corresponding data. Defining a function field operating by batch
Tryton Crash Course
N. Évrard The previous example makes one call per record. Installing tryton Prerequisites Tryton provides a way to compute the values by batch. In order to do so Initializing tryton the getter must be a classmethod and it must return a dictionary mapping A basic module A minimal module the id to the function value. Defining some object / tables Defining a tree and a Example form view
Default values description_length = fields.Function(fields.Integer( ’Description Length’), ’get_description_length ’) Some more advanced features @classmethod Workflows def get_description_length(cls , opportunities , name): Buttons cursor = Transaction.cursor() Function fields Wizards opportunity = cls.__table__() query = opportunity.select( Extending opportunity. id , CharLength(opportunity.description )) pre-existing tryton cursor.execute(∗ query ) objects
Extending models return dict (cursor.fetchall ()) Extending views Adding actions to model
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton
A basic module A minimal module Sometime you want to add functionalities to a model that do not suite the Defining some object / tables use of a button. For this kind of use case the wizard is your solution. Defining a tree and a form view A wizard is composed of two things: Default values
Some more A set of views that represent the form to gather the user input advanced features Workflows A "state machine" that define what should be done Buttons Function fields Wizards
Extending pre-existing tryton objects
Extending models Extending views Wizard views
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton A basic module Those are standard ModelView, you can define on_change, A minimal module Defining some object / on_change_with and default values on them. tables Defining a tree and a form view Example Default values
Some more class ConvertOpportunitiesStart(ModelView): advanced features ’Convert Opportunities ’ Workflows __name__ = ’training.opportunity.convert.start ’ Buttons Function fields Wizards
Extending pre-existing tryton objects
Extending models Extending views Wizard "state machine"
Tryton Crash Course N. Évrard This is a class that inherits from Wizard. You’ll be able to define different Installing tryton states on it. Prerequisites Initializing tryton Example A basic module
A minimal module from trytond .wizard import Wizard, StateView, StateTransition , Button Defining some object / tables class ConvertOpportunities(Wizard): Defining a tree and a ’Convert Opportunities ’ form view __name__ = ’training.opportunity.convert ’ Default values start = StateView( ’training.opportunity.convert.start ’, Some more ’training .opportunity_convert_start_view_form ’ , [ advanced features Button( ’Cancel’, ’end’, ’tryton−cancel ’ ) , Workflows Button( ’Convert’, ’convert’, ’tryton−ok’, default=True), Buttons ]) Function fields convert = StateTransition() Wizards def transition_convert(self ): Extending pool = Pool() pre-existing tryton Opportunity = pool.get( ’training.opportunity ’) objects opportunities = Opportunity.browse( Transaction(). context[ ’active_ids ’]) Extending models Opportunity.convert(opportunities) Extending views return ’ end ’ Activating the wizard
Tryton Crash Course
N. Évrard
Installing tryton
Prerequisites Initializing tryton A basic module Example A minimal module Defining some object / tables
Workflows
Extending pre-existing tryton objects
Extending models Extending views Extending existing objects
Tryton Crash Course
N. Évrard
Installing tryton Prerequisites Sometimes you want to extend existing objects to add miscellaneous Initializing tryton information. Doing so is just a matter of (tryton) inheritance: A basic module
A minimal module Defining some object / Example tables Defining a tree and a from import form view trytond .model f i e l d s from trytond.pool import PoolMeta Default values
Some more __all__ = [’Party’] advanced features __metaclass__ = PoolMeta
Workflows
Buttons class Party : Function fields __name__ = ’party.party ’ Wizards opportunities = fields .One2Many( ’training.opportunity ’, ’party’, Extending ’Opportunities ’) pre-existing tryton objects
Extending models Extending views Extending existing views
Tryton Crash Course
N. Évrard Modifing existing view is done by adding record in the XML files that
Installing tryton specify an xpath and what to do with the resulting node.
Prerequisites Initializing tryton Example A basic module A minimal module
Some more advanced features Workflows Example Buttons Function fields Wizards