How to do it...

Follow these steps to restrict books based on country:

  1. Add the restrict_country_ids m2m field in the library.book model, as follows:
class LibraryBook(models.Model):
_name = 'library.book'
_inherit = ['website.seo.metadata']

...
restrict_country_ids = fields.Many2many('res.country')
...
  1. Add a restrict_country_ids field in the form view of the library.books model, as follows:
...
<group>
<field name="date_release"/>
<field name="restrict_country_ids" widget="many2many_tags"/>
</group>
...
  1. Update the /books controller to restrict books based on country, as follows:
@http.route('/books', type='http', auth="user", website=True)
def library_books(self):
country_id = False
country_code = request.session.geoip and request.session.geoip.get('country_code') or False
if country_code:
country_ids = request.env['res.country'].sudo().search([('code', '=', country_code)])
if country_ids:
country_id = country_ids[0].id
domain = ['|', ('restrict_country_ids', '=', False), ('restrict_country_ids', 'not in', [country_id])]
return request.render(
'my_library.books', {
'books': request.env['library.book'].search(domain),
})

Update the module to apply the changes. Add your country in the restricted country field of the book, and access /book. This will not show restricted books in the list.

Warning: This recipe does not work with the local server. It will require a hosted server, because in the local machine, you will get the local IP, which is not related to any country.

You will also need to configure nginx properly.
..................Content has been hidden....................

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