How to do it...

Follow these steps to make the online website-multi website compatible:

  1. Add website.multi.mixin in the library.book model, as follows:
class LibraryBook(models.Model):
_name = 'library.book'
_inherit = ['website.seo.metadata', 'website.multi.mixin']
...
  1. Add website_id in the book form view, as follows:
...
<group>
<field name="author_ids" widget="many2many_tags"/>
<field name="website_id"/>
</group>
...
  1. Modify the domain in the /books controller, as follows:
@http.route('/books', type='http', auth="user", website=True)
def library_books(self, **post):
...
domain = ['|', ('restrict_country_ids', '=', False), ('restrict_country_ids', 'not in', [country_id])]
domain += request.website.website_domain()
return request.render(
'my_library.books', {
'books': request.env['library.book'].search(domain),
})
...
  1. Import werkzeug and modify a book details controller to restrict book access from another website, as follows:
import werkzeug
...
@http.route('/books/<model("library.book"):book>', type='http', auth="user", website=True, sitemap=sitemap_books)
def library_book_detail(self, book, **post):
if not book.can_access_from_current_website():
raise werkzeug.exceptions.NotFound()
return request.render(
'my_library.book_detail', {
'book': book,
'main_object': book
})
...

Update the module to apply the changes. To test this module, set different websites in some books. Now, open the /books URL and check the list of books. After this, change the website and check the list of books. For testing, you can change the website from the website switcher drop-down menu. Refer to the following screenshot to do that:

You can also try to access the book details directly from the URL, like /books/1. If a book is not from that website, it will show as 404.

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

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