Odoo Development Essentials - Sample Chapter
Odoo Development Essentials - Sample Chapter
$ 29.99 US
19.99 UK
P U B L I S H I N G
E x p e r i e n c e
D i s t i l l e d
Odoo Development
Essentials
Fast track your development skills to build powerful Odoo
business applications
Sa
m
pl
C o m m u n i t y
Daniel Reis
Odoo Development
Essentials
ee
Daniel Reis
[1]
Odoo can run on a variety of operating systems, so why pick Debian at the expense
of other operating systems? Because Odoo is developed primarily with the Debian/
Ubuntu platform in mind, it supports Odoo better. It will be easier to find help and
additional resources if working with Debian/Ubuntu.
It's also the platform the majority of developers work on, and where most
deployments are rolled out. So, inevitably, Odoo developers will be expected to be
comfortable with that platform. Even if you're from a Windows background it will
be important to have some knowledge about it.
In this chapter, you will learn how to set up and work with Odoo hosted in a Debian
system, using only the command line. For those more at home with a Windows
system, we will cover how to set up a virtual machine to host the Odoo server. As a
bonus, the techniques you will learn will also allow you to manage Odoo in cloud
servers where your only access will be through Secure Shell (SSH).
Keep in mind that these instructions are intended to set up a
new system for development. If you want to try some of them
in an existing system, always take a backup ahead of time to
be able to restore it in case something goes wrong.
If you are already running Ubuntu or another Debian-based distribution, you're set;
this machine can also be used as a host for Odoo.
For the Windows and Macintosh operating systems, it is possible to have
Python, PostgreSQL, and all the dependencies installed, and then run Odoo
from source natively.
[2]
Chapter 1
You can change odoo to whatever username you want. The -m option has its home
directory created. The -g sudo adds it to the sudoers list, so it can run commands as
root, and the -s /bin/bash sets the default shell to bash, which is nicer to use than
the default sh.
Now we can log in as the new user and set up Odoo.
[3]
Now we can use this script. It shows us how to install Odoo from source in
a Debian system:
$ sudo apt-get update && sudo apt-get upgrade
$ sudo apt-get install git
$ mkdir ~/odoo-dev
$ cd ~/odoo-dev
# Install Git
At the end, Odoo should be ready to be used. The ~ symbol is a shortcut for
your home directory (for example, /home/odoo). The git -b 8.0 option asks to
explicitly download the 8.0 branch of Odoo. At the time of writing this book, this is
redundant, since 8.0 is the default branch, but this may change, so it will make the
script time resilient.
To start an Odoo server instance, just run odoo.py:
$ ~/odoo-dev/odoo/odoo.py
[4]
Chapter 1
But we will learn how to initialize new databases from the command line, now so
press Ctrl + C to stop the server and get back to the command prompt.
To create a new database we use the command createdb. Let's create a v8dev
database:
$ createdb v8dev
To initialize this database with the Odoo data schema we should run Odoo on the
empty database by using the -d option:
$ ~/odoo-dev/odoo/odoo.py -d v8dev
[5]
This will take a couple of minutes to initialize a v8dev database, and will end with
an INFO log message Modules loaded. Then the server will be ready to listen to
client requests.
By default, this method will initialize the database with demonstration data,
which often is useful on development databases. To initialize a database without
demonstration data, add to the command the option: --without-demo-data=all.
Open http://<server-name>:8069 in your browser to be presented with the login
screen. If you don't know your server name, type the hostname command at the
terminal to find it, or the ifconfig command to find the IP address.
If you are hosting Odoo in a virtual machine you might need to do some network
configuration to be able to use it as a server. The simplest solution is to change the
VM network type from NAT to Bridged. With this, instead of sharing the host IP
address, the guest VM will have its own IP address. It's also possible to use NAT,
but that requires you to configure port forwarding, so your system knows that some
ports, such as 8069, should be handled by the VM. In case you're having trouble,
hopefully these details can help you find help in the documentation for your chosen
virtualization software.
The default administrator account is admin with password admin. Upon login you
are presented with the Settings menu, displaying the installed modules. Remove the
Installed filter and you will be able to see and install any of the official modules.
Whenever you want to stop the Odoo server instance and return to the command
line, press Ctrl + C. At the bash prompt, pressing the Up arrow key will bring you
the previous shell command, so it's a quick way to start Odoo again with the same
options. You will see the Ctrl + C followed by Up arrow and Enter is a frequently
used combination to restart the Odoo server during development.
[6]
Chapter 1
Running it we should see listed the two databases we created so far: v8dev and
v8test. The list will also display the encoding used in each database. The default is
UTF8, which is the encoding needed for Odoo databases.
To remove a database you no longer need (or want to recreate), use the
dropdb command:
$ dropdb v8test
Now you know the basics to work with several databases. To learn more on
PostgresSQL, the official documentation can be found at http://www.postgresql.
org/docs/
WARNING: The drop database will irrevocably destroy your
data. Be careful when using it and always keep backups of
your important databases before using it.
And be warned that the version in the master branch will result in the next major
stable version, but until then it's not "API stable" and you should not use it to build
custom modules. Doing so is like moving on quicksand: you can't be sure when
some changes will be introduced that will make you custom module break.
# save configuration
Here we also used the --stop-after-init option, to have the server stop after it
finishes its actions. This option is often used when running tests or asking to run a
module upgrade to check if it installs correctly.
Now we can inspect what was saved in this default configuration file:
$ more ~/.openerp_serverrc
This will show all configuration options available with the default values for them.
Editing them will be effective the next time you start an Odoo instance. Type q to
quit and go back to the prompt.
We can also choose to use a specific configuration file, using the --conf=<filepath>
option. Configuration files don't need to have all those the options you've just seen.
Only the ones that actually change a default value need to be there.
Chapter 1
Let's try that. Open two terminal windows. On the first one run:
$ ~/odoo-dev/odoo.py --xmlrpc-port=8070
And there you go: two Odoo instances on the same server listening on different
ports. The two instances can use the same or different databases. And the two could
be running the same or different versions of Odoo.
Logging
The --log-level option allows us to set the log verbosity. This can be very useful to
understand what is going on in the server. For example, to enable the debug log level
use: --log-level=debug
The following log levels can be particularly interesting:
debug_sql to inspect SQL generated by the server
debug_rpc to detail the requests received by the server
debug_rpc_answer to detail the responses sent by the server
By default the log output is directed to standard output (your console screen), but it
can be directed to a log file with the option --logfile=<filepath>.
Finally, the --debug option will bring up the Python debugger (pdb) when an
exception is raised. It's useful to do a post-mortem analysis of a server error. Note
that it doesn't have any effect on the logger verbosity. More details on the Python
debugger commands can be found here: https://docs.python.org/2/library/
pdb.html#debugger-commands.
You may be running Odoo with a Debian/Ubuntu system, either in a local virtual
machine or in a server over the network. But you may prefer to do the development
work in your personal workstation, using your favorite text editor or IDE.
This may frequently be the case for developers working from Windows
workstations. But it also may be the case for Linux users that need to work
on an Odoo server over the local network.
[9]
A solution for this is to enable file sharing in the Odoo host, so that files are easy to
edit from our workstation. For Odoo server operations, such as a server restart, we
can use an SSH shell (such as PuTTY on Windows) alongside our favorite editor.
In the following sections we will assume nano as the preferred editor. If you prefer
any other editor, feel free to adapt the commands accordingly.
The samba package installs the file sharing services and the samba-common-bin
package is needed for the smbpasswd tool. By default users allowed to access shared
files need to be registered with it. We need to register our user odoo and set a
password for its file share access:
$ sudo smbpasswd -a odoo
After this the odoo user will be able to access a fileshare for its home directory,
but it will be read only. We want to have write access, so we need to edit Sambas,
configuration file to change that:
$ sudo nano /etc/samba/smb.conf
In the configuration file, look for the [homes] section. Edit its configuration lines so
that they match the settings below:
[homes]
comment = Home Directories
browseable = yes
read only = no
create mask = 0640
directory mask = 0750
[ 10 ]
Chapter 1
To access the files from Windows, we can map a network drive for the path
\\<my-server-name>\odoo using the specific user and password defined
with smbpasswd. When trying to log in with the odoo user, you might find
trouble with Windows adding the computer's domain to the user name (for
example MYPC\odoo). To avoid this, use an empty domain by prepending a \
to the login (for example \odoo).
[ 11 ]
If we now open the mapped drive with Windows Explorer, we will be able to access
and edit the contents of the odoo user home directory.
[ 12 ]
Chapter 1
Now we need to reload the page in our web browser. Then we should see in
the Settings menu a new Technical menu section giving access to many Odoo
server internals.
The Technical menu option allows us to inspect and edit all Odoo configurations
stored in the database, from user interface to security and other system parameters.
You will be learning more about many of these throughout the book.
[ 13 ]
To enable it, open the drop-down menu from the top-right corner of the browser
window, next to the username, and select the About Odoo option. In the About
dialog, click on the Activate the developer mode button at the top-right corner.
After this, we will see a Debug View combo box at the top left of the current
form area.
Chapter 1
To add a module to an Odoo installation we could just copy it into the addons
directory, alongside the official modules. In our case, the addons directory is at
~/odoo-dev/odoo/addons/. This might not be the best option for us, since our Odoo
installation is based on a version controlled code repository, and we will want to
keep it synchronized with the GitHub repository.
Fortunately, we can use additional locations for modules, so we can keep our custom
modules in a different directory, without having them mixed with the official addons.
As an example, we will download the OCA project department and make its modules
available in our Odoo installation. This project is a set of very simple modules adding a
Department field on several forms, such as Projects or CRM Opportunities.
To get the source code from GitHub:
$ cd ~/odoo-dev
$ git clone https://github.com/OCA/department.git -b 8.0
We used the optional -b option to make sure we are downloading the modules for
the 8.0 version. Since at the moment of writing 8.0 is the projects default branch we
could have omitted it.
After this, we will have a new /department directory alongside the /odoo
directory, containing the modules. Now we need to let Odoo know about this
new module directory.
If you look closer at the server log you will notice a line reporting the addons
path in use: INFO ? openerp: addons paths: (...). Confirm that it contains our
department directory.
[ 15 ]
Summary
In this chapter, you learned how to set up a Debian system to host Odoo and to
install it from GitHub sources. We also learned how to create Odoo databases and
run Odoo instances. To allow developers to use their favorite tools in their personal
workstation, we also explained how to configure file sharing in the Odoo host.
We should now have a functioning Odoo environment to work with and be
comfortable managing databases and instances.
With this in place, we're ready to go straight into the action. In the next chapter we
will create from scratch our first Odoo module and understand the main elements
it involves.
So let's get started!
[ 16 ]
www.PacktPub.com
Stay Connected: