Creating a New Application in Odoo 12Packt_PubBlockedUnblockFollowFollowingMar 26In this article, you will create a new library application in Odoo.
Some Odoo modules create new applications, and others add features or modify existing applications.
While the technical components involved are about the same, an app is expected to include a few characteristic elements.
Since we are creating a new library app, we should include them in our module.
An app should have:An icon, to be presented in the app list.
A top-level menu item, under which all the app’s menu items will be placed.
Security groups for the app, so that it can be enabled only for users that need it, and where access security will be set.
To add an icon, we just need to place an icon.
png file in the module's static/description/ subdirectory.
Next, we will take care of the app’s top-level menu.
Adding the app’s top menu itemSince we are creating a new app, it should have a main menu item.
On the CE, this is shown as a new entry in the top-left drop-down menu.
On the EE, it is shown as an additional icon in the App Switcher main menu.
Menu items are view components added using XML data files.
Create the views/library_menu.
xml file to define a menu item:<?xml version="1.
0"?> <odoo> <!– Library App Menu –> <menuitem id="menu_library" name="Library" /> </odoo>The user interface, including menu options and actions, is stored in database tables.
The preceding code is an Odoo data file, describing a record to load into the Odoo database.
The <menuitem> element is an instruction to write a record on the ir.
The id attribute is also called XML ID, and is used to uniquely identify each data element, providing a way for other elements to reference it.
For example, when adding library submenu items, we will need to reference the XML ID of their parent menu item, which should be menu_library.
The menu item added here is very simple, and the only attribute used is name.
Other frequently-used attributes are not used here.
We didn't set a parent menu item, because this is a top-level menu, and we didn't set action, because this menu item won't do anything—it is only the top element where submenu items will later be placed.
Our module does not yet know about this new XML data file.
For this to happen, it needs to be declared in the __manifest__.
py file, using the data attribute.
It is a list of the data files to be loaded by the module upon installation or upgrade.
Add this attribute to the manifest's dictionary:'data': [ 'views/library_menu.
xml',],To load these menu configurations into our Odoo database, we need to upgrade the module.
Doing that at this point won’t have any visible effects.
This menu item has no actionable submenu yet, and so won’t be shown.
It will be visible later, once we add one, and add the appropriate access-control permissions.
NoteItems in the menu tree are only shown if there are any visible submenu items.
The lower-level menu items, with Window Actions opening Views, are only visible if the user has access to the underlying Model.
Adding security groupsBefore features can be used by regular users, access must be granted for them.
In Odoo, this is done using security groups — access privileges are granted to groups, and users are assigned security groups.
Odoo apps typically provide two groups, a user level, for use by regular users, and a manager level, with additional access to app configuration.
So, we will add these two security groups now.
Access-security related files are usually kept in a /security module subdirectory, so we should create the security/library_security.
xml file for these definitions.
Security groups use categories to better organize related app.
So, we will start by creating a category for our library app, in the ir.
category model:<?xml version="1.
0" ?><odoo> <record id="module_library_category" model="ir.
category"> <field name="name">Library</field> </record></odoo>Next, we will add the two security groups, starting with the user group.
Add the following XML block inside the <odoo> element, just before the </odoo> closing tag:<!– Library User Group –> <record id="library_group_user" model="res.
groups"> <field name="name">User</field> <field name="category_id" ref="module_library_category"/> <field name="implied_ids" eval="[(4, ref('base.
group_user'))]"/> </record>The record is created in the res.
groups model, and values are given for three fields:name is the group title.
category_id is the related app.
It is a relational field, so the ref attribute is used with an XML ID linking it to the category we've already created.
implied_ids is a one-to-many relational field, and contains a list of groups that will also apply to users belonging to this group.
In this case, we are using code 4 to add a link to base.
group_user, the basic internal user group.
Next, we will create the manager group.
It should give us all the privileges granted to the user group, plus some additional access reserved for the app manager:<!– Library Manager Group –> <record id="library_group_manager" model="res.
groups"> <field name="name">Manager</field> <field name="category_id" ref="module_library_category"/> <field name="implied_ids" eval="[(4, ref('library_group_user'))]"/> <field name="users" eval="[(4, ref('base.
user_root')), (4, ref('base.
user_admin'))]"/> </record>Here, we also see the name, category_id, and implied_ids fields, as before.
implied_ids is set with a link to the library user group, to inherit its privileges.
We also set the value for the users field, so that the administrator and the internal root users are automatically app managers.
NoteIn previous Odoo versions, the admin administrator user was also the internal root user.
In Odoo 12, we have a system root user, which is not shown in the user list, and is used internally by the framework when privilege-elevation is needed (sudo).
The admin user can be used to log into the server and should have full access to all features, but can no longer bypass access security, as the system root user can.
We also need to add this new XML file to the module’s manifest file:'data': [ 'security/library_security.
xml',],Notice that the library_security.
xml file was added before library_menu.
The order in which data files are loaded is important, since you can only use identifier references that have already been defined.
It is common for menu items to reference security groups, and so it is good practice to add security definitions before menu and view definitions.
Hope you enjoyed reading this article.
If you’d want to learn more in detail about Odoo 12, you must check out Odoo 12 Development Essentials.
Odoo 12 Development Essentials is a fast-paced guide, exploring all the new features that Odoo 12 offers to build and customize business applications.
This will help you to develop your skills to build highly complex, more performant business applications in every domain you choose.
Coding News for Developers | gitconnectedThe developer homepage – join the programming community from gitconnected.
Discover and share coding news, with the…gitconnected.