How to do it...

In order to complete the service module, we will add a configuration option to the store service key. Follow these steps to add a new field to set the isbn_service_key in the general settings:

  1. Add an isbn_service_key field in res.config.settings:
from odoo import models, fields

class ConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

isbn_service_key = fields.Char("ISBN service key",
config_parameter='iap.isbn_service_key')
  1. Add an isbn_service_key field in the general settings view:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_general_config_isbn_service" model="ir.ui.view">
<field name="name">Configuration: IAP service ksy</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="base_setup.res_config_settings_view_form" />
<field name="arch" type="xml">
<div id="business_documents" position="before">
<h2>IAP Books ISBN service</h2>
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<span class="o_form_label">IAP service key</span>
<div class="text-muted">
Generate service in odoo IAP and add service key here
</div>
<div class="content-group">
<div class="mt16 row">
<label for="isbn_service_key"
class="col-3 col-lg-3 o_light_label"/>
<field name="isbn_service_key"
class="oe_inline" required="1"/>
</div>
</div>
</div>
</div>
</div>
</div>
</field>
</record>
</odoo>

This will add a field in the General Settings to store a service key, as shown in the following screenshot. If you remember, we generated the service key in the Registering an IAP service in Odoo recipe of this chapter. Add the generated service key in this field. See the following screenshot for more information:

Now, we will update the /get_book_data controller to capture the customer credit. Update the main.py file, as follows:

from odoo import http
from odoo.http import request
from odoo.addons.iap.models import iap

class Main(http.Controller):
@http.route('/get_book_data', type='json', auth="public")
def get_book_data(self, account_token, isbn_number):
service_key = request.env['ir.config_parameter'].sudo()
.get_param('iap.isbn_service_key', False)
if not service_key:
return {
'status': 'service is not active'
}
credits_to_reserve = 1
data = {}
with iap.charge(request.env, service_key,
account_token, credits_to_reserve):

data = request.env['book.info'].sudo()._books_data_by_isbn(isbn_number)
if data['status'] == 'not found':
raise Exception('Book not found')
return data

Update the module to apply these changes.

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

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