Using inheritance to add social network features

The social network module (technical name mail) provides the message board found at the bottom of many forms, also called Open Chatter, the followers are featured along with the logic regarding messages and notifications. This is something we will often want to add to our models, so let's learn how to do it.

The social network messaging features are provided by the mail.thread model of the mail module. To add it to a custom model we need to:

  • Have the module depend on mail.
  • Have the class inherit from mail.thread.
  • Have the Followers and Thread widgets added to the form view.
  • Optionally, set up record rules for followers.

Let's follow this checklist:

Regarding #1, since our extension module depends on todo_app, which in turn depends on mail, the dependency on mail is already implicit, so no action is needed.

Regarding #2, the inheritance on mail.thread is done using the _inherit attribute we used before. But our to-do task extension class is already using the _inherit attribute. Fortunately it can also accept a list of models to inherit from, so we can use that to make it also include the inheritance on mail.thread:

    _name = 'todo.task'
    _inherit = ['todo.task', 'mail.thread']

The mail.thread model is an abstract model. Abstract models are just like regular models except that they don't have a database representation; no actual tables are created for them. Abstract models are not meant to be used directly. Instead they are expected to be used in mixin classes, as we just did. We can think of them as templates with ready-to-use features. To create an abstract class we just need it to use models.AbstractModel instead of models.Model.

For #3, we want to add the social network widgets at the bottom of the form. We can reuse the inherited view we already created, view_form_todo_task_inherited, and add this into its arch data:

<sheet position="after">
  <div class="oe_chatter">
    <field name="message_follower_ids" widget="mail_followers" />
    <field name="message_ids" widget="mail_thread" />
  </div>
</sheet>

The two fields we add here haven't been explicitly declared by us, but they are provided by the mail.thread model.

The final step is to set up record rules for followers. This is only needed if our model has record rules implemented that limit other users' access to its records. In this case, we need to make sure that the followers for each record have at least read access to it.

We do have record rules on the to-do task model so we need to address this, and that's what we will do in the next section.

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

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