How to do it...

Follow these steps to generate a basic service module:

  1. Create a new iap_isbn_service module and add __init__.py:
from . import models
from . import controllers
  1. Add __manifest__.py, with the following content:
{
'name': "IAP ISBN service",
'summary': "Get books information by ISBN number",
'website': "http://www.example.com",
'category': 'Uncategorized',
'version': '12.0.1',
'depends': ['iap', 'web', 'base_setup'],
'data': [
'security/ir.model.access.csv',
'views/book_info_views.xml',
'data/books_data.xml',
]
}

  1. Add a book.info model at models/book_info.py, with a method to fetch the book data:
from odoo import models, fields, api

class BookInfo(models.Model):
_name = 'book.info'

name = fields.Char('Books Name', required=True)
isbn = fields.Char('ISBN', required=True)
date_release = fields.Date('Release Date')
cover_image = fields.Binary('BooksCover')
author_ids = fields.Many2many('res.partner', string='Authors')

@api.model
def _books_data_by_isbn(self, isbn):
book = self.search([('isbn', '=', isbn)], limit=1)
if book:
return {
'status': 'found',
'data': {
'name': book.name,
'isbn': book.isbn,
'date_release': book.date_release,
'cover_image': book.cover_image,
'authors': [a.name for a in book.author_ids]
}
}
else:
return {
'status': 'not found',
}
  1. Add an http controller in the controller/main.py file (don't forget to add the controllers/__init__.py file):
from odoo import http
from odoo.http import request

class Main(http.Controller):
@http.route('/get_book_data', type='json', auth="public")
def get_book_data(self):
# We will capture credit here
return {
'test': 'data'
}

  1. Add access rules to security/ir.model.access.csv:
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
acl_book_backend_user,book_info,model_book_info,base.group_user,1,1,1,1
  1. Add views, menus, and actions to views/book_info_views.xml:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Form View -->
<record id="book_info_view_form" model="ir.ui.view">
<field name="name">Book Info Form</field>
<field name="model">book.info</field>
<field name="arch" type="xml">
<form>
<sheet>
<field name="cover_image" widget='image' class="oe_avatar"/>
<div class="oe_title">
<label for="name" class="oe_edit_only"/>
<h1>
<field name="name" class="oe_inline"/>
</h1>
</div>
<group>
<group>
<field name="isbn"/>
<field name="author_ids" widget="many2many_tags"/>
</group>
<group>
<field name="date_release"/>
</group>
</group>
</sheet>
</form>
</field>
</record>

<!-- Tree(list) View -->
<record id="books_info_view_tree" model="ir.ui.view">
<field name="name">Book Info List</field>
<field name="model">book.info</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
<field name="date_release"/>
</tree>
</field>
</record>

<!-- action and menus -->
<record id='book_info_action' model='ir.actions.act_window'>
<field name="name">Book info</field>
<field name="res_model">book.info</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="Books Data" id="books_info_base_menu" />
<menuitem name="Books" id="book_info_menu" parent="books_info_base_menu" action="book_info_action"/>
</odoo>
  1. Add some sample book data to data/books_data.xml (don't forget to add cover images to the given directory):
<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">
<record id="book_info_data_1" model="book.info">
<field name="name">Learning PostgreSQL 10</field>
<field name="isbn">1788392019</field>
<field name="author_ids" eval="[(0, 0, {'name': 'Salahaldin Juba'}), (0, 0, {'name': 'Andrey Volkov'})]"/>
<field name="date_release">2017/12/01</field>
<field name="cover_image" type="base64" file="iap_isbn_service/static/img/postgres.jpg"/>
</record>

<record id="book_info_data_2" model="book.info">
<field name="name">Odoo 10 Development Essentials</field>
<field name="isbn">9781785884887</field>
<field name="author_ids" eval="[(0, 0, {'name': 'Daniel Reis'})]"/>
<field name="date_release">2016/09/25</field>
<field name="cover_image" type="base64" file="iap_isbn_service/static/img/odoo.jpg"/>
</record>

</odoo>

After installing the module, you will see a new menu with book data, like the following:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset