How to do it...

Define the handlers in controllers/main.py:

  1. Add a path that shows all the books, as shown in the following example:
    @http.route('/my_library/all-books', type='http', auth='none')
def all_books(self):
books = request.env['library.book'].sudo().search([])
html_result = '<html><body><ul>'
for book in books:
html_result += "<li> %s </li>" % book.name
html_result += '</ul></body></html>'
return html_result
  1. Add a path that shows all the books and indicates which was written by the current user, if any. This is shown in the following example:
    @http.route('/my_library/all-books/mark-mine', type='http', auth='public')
def all_books_mark_mine(self):
books = request.env['library.book'].sudo().search([])
html_result = '<html><body><ul>'
for book in books:
if request.env.user.partner_id.id in book.author_ids.ids:
html_result += "<li> <b>%s</b> </li>" % book.name
else:
html_result += "<li> %s </li>" % book.name
html_result += '</ul></body></html>'
return html_result
  1. Add a path that shows the current user's books, as follows:
    @http.route('/my_library/all-books/mine', type='http', auth='user')
def all_books_mine(self):
books = request.env['library.book'].search([
('author_ids', 'in', request.env.user.partner_id.ids),
])
html_result = '<html><body><ul>'
for book in books:
html_result += "<li> %s </li>" % book.name
html_result += '</ul></body></html>'
return html_result

With this code, the /my_library/all-books and /my_library/all-books/mark-mine paths look the same for unauthenticated users, while a logged in user sees their books in a bold font on the latter path. The /my_library/all-books/mine path is not accessible at all for unauthenticated users. If you try to access it without being authenticated, you'll be redirected to the login screen in order to do so.

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

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