How to setup environment to create a new module

Thanks @pokoli
I’m looking through the documentation and trying to find the trytond/modules folder as mentioned in the ‘Modules’ section

Module Structure

A module is a directory in trytond/modules which contains at least two files:

In my docker instance I can see /var/lib/trytond/www but no ‘module’ folder
Where do I copy my ‘cloned’ module?
Or does it get applied via a hg process?
An overview of the module creation workflow would certainly be appreciated.

I just have not had any luck getting a non-docker instance of Tryton installed. If that’s that’s the path I need to take I’m sure I’ll need coaching to get a development environment setup.

Basic setup

For creating the basic folder structure and setup.py you can use cookie-cutter (need to install cookiecutter first via pip/system packet manager)


then you can execute:
cookiecutter hg+https://hg.tryton.org/cookiecutter
cookiecutter will ask you some question (like the project name), then will create a folder with all the necessary thing to start developing with tryton

tryton.cfg

First you need to change the tryton.cfg; this file is responsible for:

  • tell setup.py witch version of tryton this module support
  • tell setup.py your tryton module dependecy
  • tell the xml file to include (the one that define the database record, not the views)

how to install your module

I strongly suggest to use a virtual-environment for development. The you can simply python setup.py install to install all your dependecy defined in the tryton.cfg (you can install the dependency manually if you want).
Every time you make a change you need to reinstall the module, unless you use a workaround (python setup.py develop doesn’t work unfortunally)

how install the module in editable mode (for developing it)

When developing, installing the module every time you modify it can be time consuming,
a sane way to solve this problem is to install it id editable mode:
python setup.py develop
This will not work if your module folder doesn’t match the module name!
This will create a simlink in the correct folder for you.

How to setup your playground/manual testing environment

Trytond needs a database, fortunately you can be lazy about it and use sqlite. For this you need create a config file, the minimum required is:

[database]
path=/path/where/sqlite/dbs/are/stored

or optionally set the environment variable
export TRYTOND_DATABASE__PATH=/path/where/sqlite/dbs/are/stored
[explained in https://trytond.readthedocs.io/en/latest/topics/configuration.html]

The only thing missing is to create a empty file in the /path/where/sqlite/dbs/are/stored folder, don’t forget to ad the .sqlite extention or tryton will not recognize it. You can have multiple sqlite db in the same folder.
example:

touch /path/where/sqlite/dbs/are/stored/testplzignore.sqlite

Prime the database

You need to prime your database, this can be accomplished with the trytond-admin command.
This will create all the necessary table for tryton to work, and set the admin password.
The detail are explained in the documentation

# with the configfile
trytond-admin -c configfile.cfg -d dbfilename --all

#with the enviroment variable
#export TRYTOND_DATABASE__PATH=/path/where/sqlite/dbs/are/stored
trytond-admin -d dbfilename --all

where dbfilename is the filename of the db without the .sqlite extention.

Activate your module

After “priming” you need to activate your module, this can be done via the client gui or via command line:

#with the enviroment variable
#export TRYTOND_DATABASE__PATH=/path/where/sqlite/dbs/are/stored
trytond-admin -d dbfilename -u moduletoactivate --activate-dependecies

Update your module

if you modify some xml file, you need to update the db and usually relaunch the client. this can be archived with:

#with the enviroment variable
#export TRYTOND_DATABASE__PATH=/path/where/sqlite/dbs/are/stored
trytond-admin -d dbfilename -u moduletoupdate

This isn’t necessary it you modify just your python code.

Execute the tryton server (trytond)

You can execute the tryton server with:

# with the configfile
trytond -c configfile.cfg

#with the enviroment variable
#export TRYTOND_DATABASE__PATH=/path/where/sqlite/dbs/are/stored
trytond

If you don’t want to reload the tryton server on every change, you can use the --dev flag.
This flag will auto-reload your module every time tryton see a file-change in the trytond folder. If you created the simlink like above mentioned, trytond will reload changes every time you save your module. The command

#with the enviroment variable
#export TRYTOND_DATABASE__PATH=/path/where/sqlite/dbs/are/stored
trytond --dev

the tryton client

You can install the tryton clinet via pip, the only caviat is that you need to install pygobject first…
Thsi can be a problem inside a virtual enviroment, but just remember to install pygobject globally and make sure to create the virtualenviroment with the system-site-packages flag.

If you executed the tryton server locally, by default it will listen on 127.0.0.1:8000 (you can change it in the config file)
When starting the client, you need to specify:

  • host (127.0.0.1:8000 if the server is executed locally)
  • database name (The databse nabe is the filename of the sqlite db you primed before)
  • user (the only one is admin for the moment, the password is the one you inputted in the db priming stage)

unit-testing

I suggest you read the guide here: https://trytond.readthedocs.io/en/latest/topics/testing.html

3 Likes

Thank you Luca.
I’m attempting to setup a virtual environment by following this - is it the best set of steps to follow?

When I try this command

trytond/bin/trytond-admin -d test_db -u ir res -v

I get (among other errors), this
UserWarning: Unable to create index with parameters
warnings.warn(‘Unable to create index with parameters’)

I was able to get the steps in
Create a virtual environment:
Install dependencies:
and
Test trytond server:
completed without error, however

That guide is a work in progress. I hope to get more time soon to improve the patch submitted to review, however I’ve good news for you:

You’re doing well so that warning messages are due to nature of sqlite database, if you would set up the environment on postgres database you will not see those warnings.

Hi Jose.
I think I’m getting close.
In your guide, when I get to the Test tryton client stage I get an error
Namespace Gtk not available
I’m assuming because this Tryton client is a desktop GUI and I’m trying to set this up on a console server

Do I need to skip this part and install sao instead?
I have cloned GitHub - tryton/sao: Mirror of Tryton web client - Sao but I’m not really confidant about it’s needed configuration

This from your guide

sao

In case you want to develop with web client enter directory where sao was unpackaged and install it:

$ npm install
$ grunt

Create symlink to sao:

$ ln -s ~/path/to/directory/where/sao/is/placed www

Finally, test it setting localhost:8000 as the url in your browser.

Do I need an absolute path for the www in the ln command?
How do I set the listener from localhost to 0.0.0.0 so it’s available to browse from my network?

Answered my own question about the listener.

trytond.conf

[database]
path=~/db/test_db.sqlite

[web]
listen=0.0.0.0:8000
root=~/tryton_hg/sao

But I now get this from the browser
What am I missing?

Method Not Allowed

The method is not allowed for the requested URL.

OK, I’ve gotten a workable system and can browse and log into the Tryton WebUI
Here’s what I diid:
I installed (apt) postgres & (pip) psycopg2,
created a postgres tryton role and DB,
‘primed’ the new DB,
and updated my trytond.conf as follows

[database]
uri = postgresql://tryton:tryton@localhost/
path = /var/lib/trytond
#path=~/db/test_db.sqlite

[web]
listen=0.0.0.0:8000
root=/home/matt/tryton_hg/sao

I’ll start putting together the blue print for my proposed EDI module, next.
Thanks for the help!

I suspect you where using the sqlite backend and which did not support all features so we raise a warning to notice that the feature is not supported on sqlite.

After setting up and running and testing startup of trytond, I’ve cd’d into the tryton-env/modules/sale folder and ran
python setup.py install

everything appears to have completed without error but I still only see the ir and res modules in the WebGUI

Anybody have any tips on how to add the basic modules for sales from a source install?

This appears to have fixed it

trytond/bin/trytond-admin -c trytond.conf -d tryton -u ir res account_invoice_stock party account account_invoice currency company product stock account_product country sale -v

I’m missing something here. How is this done? In what context? After activating the virtual environment? From what folder?

Cookie cutter is a python package/script for create a folder with file based on a template. You need to install cookiecutter first via pip (pip install cookiecutter)
The hg+https://hg.tryton.org/cookiecutter Is the template that cookiecutter will download.
The result is a folder with your project name, and all the basic file pre-populated.

This is not necessary, but it helps speed the module, and eliminate the need to write the boilerplate manually.

Don’t need to be in a virtual environment. This will just setup the folder structure.

Thanks. I see now.
I needed to do this prior - obvious mistake on my part
pip install cookiecutter
then the cookiecutter command works and prompts for the template values

After developing the module created by the above command, when I’m ready to deploy it, I would
python setup.py install
and then use the trytond-admin with the -u to activate it in the WebGUI?

Yes, but when developing I recommend using python setup.py develop in order to install the module in development module so you don’t need to reinstall the package for every change you make on it.

1 Like

Doesn’t work for me. This is because develop mode does not respect src structure · Issue #230 · pypa/setuptools · GitHub
So you need to create the simlink manually like I explained

yes, you will activate/update the module with the -u, but not forget to add all the dependecies. the better option is to add the --activate-dependecies:

#with the enviroment variable
#export TRYTOND_DATABASE__PATH=/path/where/sqlite/dbs/are/stored
trytond-admin -d dbfilename -u moduletoactivate --activate-dependecies

Then the module will show up (in theory).

The folder should have the same name as the module name. Otherwise it does not work.

All this years… and now i understand why editable/develop mode is not working. Modified my developer guide accordingly.

1 Like

Also, do not forget to install Mercurial package on the server before creating a module template:

sudo apt-get install mercurial

That’s the part responsible for hg+ option in cookiecutter source link/protocol. On Ubuntu it is not available by default and you will get an error.

Constantine.

I’m using the following script to automate this process. I’m also using this script to quickly set up some testing environment (just adjust which modules get installed and updated).

My directory-layout is:

project-root
+-- __init__.py
+-- … module files
+-- _venv  # python virtual env
    +-- <module-name>.sqlite  # the database
    +-- tryton.conf
    +-- trytond.conf
    +-- … other file created by virutalenv

As you can see, I’m storing the database and the config files in the virtual environment, too. This keeps all files at on place, while still keeping the project root tidy and eases clean-up when done.

The script creates all of these files if not existing, updates the module (even if not stricktly required) and starts the client as well a the server. This allows me to jsut re-run the script without taking care wich task is required exactly.
To restart with a clean database or a clean virtualenv, I just delete what should be rebuild and rerun the script.

Major steps are:

  • If not existing, create a virtualenv and install the requirement and the module to be developed in it
  • if not existing, create a client config file
  • if not existing, create a server config file
  • if not existing, create sqlite database and initialize it using trytond-admin
  • If not already running, start the tryton client
  • Start trytond --dev
#!/bin/sh

MODULE_NAME=country_order
DB_NAME=country_order

SRV_CFG=_venv/trytond.conf
CNT_CFG=_venv/tryton.conf

if [ ! -d _venv/bin ] ; then
    python3 -m venv _venv --system-site-packages
    . _venv/bin/activate
    pip install -r requirements.txt tryton
    pip install -e .
else
    . _venv/bin/activate
fi

# Client config file
if [ ! -f $CNT_CFG ] ; then
    echo > $CNT_CFG "[login]"
    echo >>$CNT_CFG "profile ="
    echo >>$CNT_CFG "host = localhost"
    echo >>$CNT_CFG "db = $DB_NAME"
    echo >>$CNT_CFG "expanded = True"
    echo >>$CNT_CFG "login = admin"
fi

# server config file
if [ ! -f $SRV_CFG ] ; then
    echo > $SRV_CFG "[database]"
    echo >>$SRV_CFG "uri = sqlite://"
    echo >>$SRV_CFG "path = _venv"
    echo >>$SRV_CFG "default_name = $DB_NAME"
    #echo >>$SRV_CFG "language = de"
fi

# database
if [ ! -f _venv/${DB_NAME}.sqlite ] ; then
    touch _venv/${DB_NAME}.sqlite
    trytond-admin -c $SRV_CFG -d $DB_NAME --all --email ""
    trytond-admin -c $SRV_CFG -d $DB_NAME -u ir --activate-dependencies
    trytond-admin -c $SRV_CFG -d $DB_NAME -l de
    #trytond_import_countries -c $SRV_CFG -d $DB_NAME
fi

trytond-admin -c $SRV_CFG -d $DB_NAME -u ${MODULE_NAME} --activate-dependencies

# start
if [ $(pgrep --count --uid $(id --user) tryton\$) = "0" ] ; then
    _venv/bin/tryton -c $CNT_CFG &
    :
fi
_venv/bin/trytond -c $SRV_CFG --dev