Wednesday, December 13, 2017

Odoo XMLRPC: create ir.attachment with non-ascii datas

Consider the following Python 3 code:

import xmlrpc.client
import functools
import base64

url = 'http://myodoo:8069'
db = 'a_database'
username = 'admin'
password = 'admin'

common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
uid = common.authenticate(db, username, password, {})

models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))
call = functools.partial(models.execute_kw, db, uid, password)

vals = {'res_model': 'crm.lead', 'company_id': 1, 'res_name': 'A Name', 
        'res_id': 1, 'datas_fname': 'TEST.txt', 'name': 'TEST.txt'}
  
vals.update({
  'datas': 'Hi!'
})
call('ir.attachment', 'create', [vals])


This should work fine. However, if the data contains non-ASCII characters, the server side complains:

ValueError: string argument should contain only ASCII characters


So we must encode the data, but it must stay ASCII. To do that, use this:

'datas': base64.b64encode("allĂ´!".encode()).decode('ascii')


Thursday, June 1, 2017

Odoo restore DB: No space left on device

If you encounter this error while restoring a database using Odoo's web database manager, it might be because the device where your temporary folder is, is out of free space. In my case, it was /tmp. So I couldn't just free up space, so I wanted to change the temporary folder Odoo uses. Turns out Odoo uses the Python module tempfiles and it can use environment variables.

So, to set a different temporary folder, set the TMPDIR environment variable, run Odoo, and then proceed to the database restore.

$ export TMPDIR=/home/jerther
$ ./odoo-bin

Wednesday, March 22, 2017

Odoo 8 Many2many or One2many users field domain based on group

Here's an example of a collection of users that I wanted to be filtered by some group:

def _get_users_domain(self):
    return [('groups_id', 'in', self.env.ref('topo.users').id)]

user_ids = fields.Many2many('res.users', domain=_get_users_domain, string="Users", help="Who is allowed to access this board")

When you expose the field in a view, the selection widget should only show users that are members of the given group.

This should also work for a One2many field.

Thursday, January 26, 2017

UnitTest odoo 8 modules with coverage

At the time of this writing, help is a bit sparse on testing with odoo, let alone testing inside eclipse. Although I've been able to achieve it, it's quite the journey so I've decided to write this article.

Before we start, if you've not done so already, take a look at Python UnitTest Framework doc and then Testing Odoo Modules . I now assume you know how to write and organize unit tests for odoo. Also, in the latter link, you'll read that testing outside odoo is not supported. Which might be true for the odoo team, but lately I came accross a really neat plugin. But enough of this, let's dig in!

We're gonna be using pytest and the plugin won't work with older versions so make sure it is up to date:
sudo pip install -U pytest
Then install pytest-odoo plugin:
sudo pip install pytest-odoo
Then you need to export a variable that points to an odoo config file with some database information:
export OPENERP_SERVER=/etc/odoo/openerp-server.conf
If you're already running odoo, this file should already be configured but could miss this important parameter so make sure it is there:
db_name = testjerther
And last, go to the root of your module and run:
pytest -v
Bingo!


I'd like to run this inside Eclipse with its pytest runner, but I gave up after (too) many hours.

Now for coverage, install pytest-cov
sudo pip install pytest-cov
You can then add coverage to your tests with:
pytest -v --cov
But that gives a LOT, and I found that using --cov=moduleName does not work, but we can apply a filter in a config file. So go ahead and create one in the root folder of your project and put these lines in:
[run]
include = */moduleName/*
There are many other parameters that can go in there. Now you can run:
pytest -v --cov --cov-config=coverage.config

You can also export the coverage in html by adding --cov-report html:cov_html where cov_html is a folder. check out the doc for more options!

Tuesday, April 19, 2016

TFS 2015: Build failed but no issues and WI images not showing

Some people had this problem and it was due to some permissions but it got fixed with Update 1. I still had it with the recently released Update 2. The build would fail with no issue, no log, no nothing.

Also, attached images in work items would not show!

The problem was that we upgraded from TFS 2010, and the upgrade did not update all the Application Tier URLs: the Notification URL was still set to an old URL. Correcting this fixed the problem!


Wednesday, September 23, 2015

Visual Paradigm: Draw a table

Just a quick tip. Here's how to create a table in Visual Paradigm.

First, create a Text box, then use this associated tool:

Result:
This may seem quite obvious but at first I had to look around for quite a bit. Hope this help save some time.

Thursday, August 13, 2015

Vino VNC asks for keyring password

When you try to connect with VNC to your Ubuntu desktop which has Vino configured and running, it may trigger a gnome keyring password prompt on the local display, and the login process hangs on the remote end. There are many solutions around, and most of them leave the system somewhat dirty. Here's a procedure I've gathered which fixes the problem, even remotely, and leave the system clean.

First of all, we need to shut down the gnome keyring daemon. But I've found that all application trying to gain access to the keyring will cause it to launch. So I found the only way to stop it without removing it is to rename it. Temporarily, don't worry:

sudo mv /usr/bin/gnome-keyring-daemon /usr/bin/gnome-keyring-daemon.bak
sudo reboot

Then you should be able to connect to the remote machine with VNC. But let's not stop here and do things proper. First, reenable gnome keyring daemon and launch it so it is available to the next step:

sudo mv /usr/bin/gnome-keyring-daemon.bak /usr/bin/gnome-keyring-daemon
gnome-keyring-daemon

Make sure seahorse is installed and launch it from the VNC session. It's in System / Preferences / Passwords and Keys.

Now there's this "Login" folder. Unlock it by right clicking and choosing Unlock.

In the login folder, you will find an item named Vino. Right click on it and delete it.

And that's it. You can make sure everything is fixed by rebooting and trying to connect with VNC again.

I believe you have to leave the VNC password blank. Otherwise it'll create this item in the keyring. Feel free to confirm this in the comments. I've pulled too much hair out for this already ;)