Our goal here was to learn to write the interface between an external application and the Odoo server, and this was done in the previous section. But it would be a shame not to go the extra step and actually make it available to the end user.
To keep the setup as simple as possible, we will use Python's built-in features to implement the command-line application. Since it is part of the standard library, it does not require any additional installation.
Now, alongside the library_api.py file, create a new library.py file. It will first import Python's command-line argument parser and then the LibraryAPI class, as shown in the following code:
from argparse import ArgumentParser from library_api import LibraryAPI
Next, we describe the commands the argument parser will expect; there are four commands:
- Search and list books
- Add a book
- Set (change) a book title
- Delete a book
This is the code for adding them to the command-line parser:
parser = ArgumentParser()
parser.add_argument(
'command',
choices=['list', 'add', 'set', 'del'])
parser.add_argument('params', nargs='*') # optional args
args = parser.parse_args()
At this point, args is an object containing the arguments given to the script, args.command is the command provided, and args.params will optionally hold additional parameters to use for the command. If no or incorrect commands are given, the argument parser will handle that for us and will show the user what input is expected. For a complete reference on argparse, you can refer to the official documentation at https://docs.python.org/3/library/argparse.html.
The next step is to to perform the intended actions. We will first prepare the connection with the Odoo server:
srv, port, db = 'localhost', 8069, '12-library'
user, pwd = 'admin', 'admin'
api = LibraryAPI(srv, port, db, user, pwd)
The first line sets some fixed parameters for the server instance and database to connect to. In this example, we are connecting to an Odoo server in the same machine (localhost) listening on the 8069 default port, with an 12-library available database. If want to connect to a different server and database, you should adapt these parameters accordingly.
This has a hardcoded server address and plain text password, so it's far from being the best implementation. We should have a configuration step to collect these settings from the user and possibly store them in a secure way. But we need to keep in mind that our goal here is to learn to work with the Odoo RPC, so consider this as proof of concept code, and not a finished product.
Now we will write the code to handle each of the supported commands, which will also make use of the api object.
We can start with the list command, providing a list of the books:
if args.command == 'list':
books = api.search_read(args.text)
for book in books:
print('%(id)d %(name)s' % book)
Here, we use the LibraryAPI.search_read() method to retrieve the list of Book records from the server. We then iterate through each element in the list and print it out. We use Python's string formatting to present each book record, a key-value dictionary, to the user.
Next, we have the add command. This will make use of the additional parameters, for the book titles:
if args.command == 'add':
for title in args.params:
new_id = api.create(title)
print('Book added with ID %d.' % new_id)
Since the hard work was already done in the LibraryAPI object, here we just need to call the write() method and show the result to the end user.
The set command allows to change the title of an existing book. It should have two parameters, the new title and the ID of the book:
if args.command == 'set-title':
if len(params) != 2:
print("set command requires a Title and ID.")
return
book_id title = int(args.params[0]), args.params[1]
api.write(title, book_id)
print('Title set for Book ID %d.' % book_id)
Finally, we have the implementation for the del command, which should delete a Book record. At this point, this should be no challenge for us:
if args.command == 'del':
for param in params:
api.unlink(int(param))
print('Book with ID %s deleted.' % param)
At this point, the basic API CLI is finished, and the reader can try some commands on it the verify how it is working. For example, we should now be able to run the example commands shown at the beginning of this chapter, in the Learning project: a Client to catalogue books section. Accessing the data in the Library app using the regular web client would also be helpful to confirm that the CLI app is working as expected.
This is a quite basic application, and you probably could think of a few ways to improve it while going through the code. But remember that the point here is to make an example of interesting ways to leverage the Odoo RPC API.