Follow these steps to add a mail alias for the library.book.rent model:
- Add mail alias data in the my_library/data/mail_template.xml file:
<record id="mail_alias_rent" model="mail.alias">
<field name="alias_name">rent</field>
<field name="alias_model_id" ref="model_library_book_rent"/>
<field name="alias_user_id" ref="base.user_admin"/>
<field name="alias_contact">partners</field>
</record>
- Add the following imports in the my_library/models/library_book_rent.py file:
import re
from odoo.tools import email_split, email_escape_char
- Override the message_new() method in the library.book.rent model:
@api.model
def message_new(self, msg_dict, custom_values=None):
self = self.with_context(default_user_id=False)
if custom_values is None:
custom_values = {}
regex = re.compile("^[(.*)]")
match = regex.match(msg_dict.get('subject')).group(1)
book_id = self.env['library.book'].search([
('name', '=', match),
('state', '=', 'available')], limit=1)
custom_values['book_id'] = book_id.id
email_from = email_escape_char(email_split(msg_dict.get('from'))[0])
custom_values['borrower_id'] = self._search_on_partner(email_from)
return super(LibraryBookRent, self).message_new(msg_dict, custom_values)
Update the my_library module to apply the changes. Then send an email to [email protected]. Make sure you have included the book's name in the email subject, for example, [Odoo 12 Development Cookbook] Request to borrow this book . This will create the new library.book.rent record and it will be displayed as follows:
Whenever you send an email to [email protected] with the book's name in the email subject, Odoo will generate a new borrowing record. Note that this will work only if the book is available in the library.