To define a method on Library Books to change the state of a selection of books, you need to add the following code to the model definition:
- Add a helper method to check whether a state transition is allowed:
@api.model def is_allowed_transition(self, old_state, new_state): allowed = [('draft', 'available'), ('available', 'borrowed'), ('borrowed', 'available'), ('available', 'lost'), ('borrowed', 'lost'), ('lost', 'available')] return (old_state, new_state) in allowed
- Add a method to change the state of some books to a new state that is passed as an argument:
@api.multi def change_state(self, new_state): for book in self: if book.is_allowed_transition(book.state, new_state): book.state = new_state else: continue
- Add a method to change the book state by calling the change_state method:
@api.model
def make_available(self):
self.change_state('available')
@api.model
def make_borrowed(self):
self.change_state('borrowed')
@api.model
def make_lost(self):
self.change_state('lost')
- Add buttons in the <form> view. This will help us trigger these methods from the user interface:
<form>
...
<button name="make_available" string="Make Available" type="object"/>
<button name="make_borrowed" string="Make Borrowed" type="object"/>
<button name="make_lost" string="Make Lost" type="object"/>
...
</form>
Update or install the module to make these changes available.