How to do it...

If you have tested the module, you will find that only users who have librarian access rights can mark a book as borrowed. Non-librarian users cannot borrow a book by themselves; they need to ask a librarian user. Suppose that we want to add a new feature so that non-librarian users can borrow books by themselves. We will do this without giving them the access rights for the library.book.rent model.

In order to let normal users borrow books, you need to perform the following steps:

  1. Add the book_rent() method in the library.book model:
class LibraryBook(models.Model):
_name = 'library.book'
...
def book_rent(self):
  1. In the method, ensure that we are acting on a single record:
self.ensure_one() 
  1. Raise a warning if a book is not available to borrow:
if self.state != 'available':
raise UserError(_('Book is not available for renting'))
  1. Get the empty recordset of library.book.rent as a superuser:
rent_as_superuser = self.env['library.book.rent'].sudo()
  1. Create a new book borrow record with the appropriate values:
rent_as_superuser.create({
'book_id': self.id,
'borrower_id': self.env.user.partner_id.id,
})
  1. To trigger this method from the user interface, add the button to the book's form view:
 <button name="book_rent" 
string="Rent this book"
type="object"
class="btn-primary"/>

Restart the server and update my_library to apply the given changes. After the update, you will see a Rent this book button on the book form view. When you click on that, a new rent record will be created. This will also work for non-librarian users. You can test this by accessing Odoo as a demo user.

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

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