How to use OpenChatter in my addon
from:https://doc.openerp.com/trunk/mail/mail_openchatter_howto/
A small my_task model will be used as example to explain how to use the OpenChatter feature. Being simple, it has only the following fields :
a name
a task responsible
a related project
class my_task(osv.osv):
_name = "my.task"
_description = "My Task"
_columns = {
'name': fields.char('Name', required=True, size=64),
'user_id':fields.many2one('res.users', string='Responsible',
ondelete='cascade', required=True, select=1),
'project_id':fields.many2one('project.project', string='Related project',
ondelete='cascade', required=True, select=1),
}
Two-lines feature integration
Make your module inheriting from the mail.thread class.
class my_task(osv.osv):
_name = "my.task"
_description = "My Task"
# inherit from mail.thread allows the use of OpenChatter
_inherit = ['mail.thread']
Use the thread viewer widget inside your form view by using the mail_thread widget on the message_ids field inherited from mail.thread.
<record model="ir.ui.view" id="my_task_form_view">
<field name="name">My Task</field>
<field name="model">my.task</field>
<field name="priority">1</field>
<field name="arch" type="xml">
<form>
[...]
<field name="message_ids" colspan="4" widget="mail_thread" nolabel="1"/>
</form>
</field>
</record>
Send notifications
When sending a notification is required in your workflow or business logic, use the message_post method. This method is a shortcut to the message_append method that takes all mail.message fields as arguments. This latter method calls message_create that
creates the message
parses the body to find users you want to push the message to (finding and parsing @login in the message body)
pushes a notification to users following the document and requested users of the latetr step
You should therefore not worry about subscriptions or anything else than sending the notification. Here is a small example of sending a notification when the do_something method is called :
def do_something(self, cr, uid, ids, context=None):
[...]
self.do_something_send_note(cr, uid, ids, context=context)
[...]
return res def do_something_send_note(self, cr, uid, ids, context=None):
self.message_post(cr, uid, ids, _('My subject'),
_("has received a <b>notification</b> and is happy for it."), context=context)
Notifications guidelines
Here are a few guidelines that you should keep in mind during the addition of system notifications :
avoid unnecessary content; if a message has no interest, do not implement it
use short sentences
do not include the document name, as it is managed by the thread widget
use a simple and clean style
html tags are supported: use <b> or <em> mainly
put main word(s) in bold
avoid fancy styles that will break the OpenERP look and feel
create a separate method for sending your notification
use a method name like original_method_name_send_note, that allow to easily spot notification methods in the code
Subscription management
There are a few default subscription tricks that you should know before playing with subscription:
users that click on 'follow' follow the document. An entry in mail.subscription is created.
users that click on 'unfollow' are no longer followers to the document. The related entry in mail.subscription is created.
users that create or update a document automatically follow it. An entry in mail.subscription is created.
If you want to override this default behavior, you should avoid doing it manualle. You should instead override the message_get_subscribers method from mail.thread. The default implementation looks in the mail.suscription table for entries matching user_id=uid, res_model=self._name, res_id=current_record_id. You can add subscribers by overriding the message_get_subscribers and adding user ids to the returned list. This means that they will be considered as followers even if they do not have an entry in the mail.subscription table.
As an exemple, let us say that you want to automatically add the my_task responsible along with the project manager to the list of followers. The method could look like:
def message_get_subscribers(self, cr, uid, ids, context=None):
# get the followers from the mail.subscription table
sub_ids = self.message_get_subscribers_ids(cr, uid, ids, context=context);
# add the employee and its manager if specified to the subscribed users
for obj in self.browse(cr, uid, ids, context=context):
if obj.user_id:
sub_ids.append(obj.user_id)
if obj.project_id and obj.project_id.user_id:
sub_ids.append(obj.project_id.user_id)
return self.pool.get('res.users').read(cr, uid, sub_ids, context=context)
This method has the advantage of being able to implement a particular behavior with as few code addition as possible. Moreover, when changing the task responsible of the project manager, the subscribers are always correct. This allows to avoid to implement complex corner cases that could obfuscate the code.
The drawback of this method is that it is no longer possible to those subscribers to unfollow a document. Indeed, as user ids are added directly in a list in message_get_subscribers, it is not possible to unsubscribe to a document. However, this drawback is mitigated by
only important users shoudl be added using this method. Important users should not unsubscribe from their documents.
users can hide the notifications on their Wall
Messages display management
By default, the mail_thread widget shows all messages related to the current document beside the document, in the History and comments section. However, you may want to display other messages in the widget. For example, the OpenChatter on res.users model shows
messages related to the user, as usual (messages with model = res.users, res_id = current_document_id)
messages directly pushed to this user (containing @login)
The best way to direct the messages that will be displayed in the OpenChatter widget is to override the message_load method. For example, the following method fetches messages as usual, but also fetches messages linked to the task project that contain the task name. Please refer to the API for more details about the arguments.
def message_load(self, cr, uid, ids, limit=100, offset=0, domain=[], ascent=False, root_ids=[False], context=None):
msg_obj = self.pool.get('mail.message')
for my_task in self.browse(cr, uid, ids, context=context):
# search as usual messages related to the current document
msg_ids += msg_obj.search(cr, uid, ['|', '&', ('res_id', '=', my_task.id), ('model', '=', self._name),
# add: search in the current task project messages
'&', '&', ('res_id', '=', my_task.project_id.id), ('model', '=', 'project.project'),
# ... containing the task name
'|', ('body', 'like', '%s' % (my_task.name)), ('body_html', 'like', '%s' % (my_task.name))
] + domain, limit=limit, offset=offset, context=context)
# if asked: add ancestor ids to have complete threads
if (ascent): msg_ids = self._message_add_ancestor_ids(cr, uid, ids, msg_ids, root_ids, context=context)
return msg_obj.read(cr, uid, msg_ids, context=context)
How to use OpenChatter in my addon的更多相关文章
- NodeJS Addon 多线程
Mac版本客户端准备使用electron实现,需要对现有的C API的IM SDK 做NodeJS封装,提供Javascript接口. 使用Nan,遇到的问题主要是NodeJS是libuv defal ...
- nginx安装过程,报错处理:make[1]: *** [objs/addon/src/bson.o] Error 1
nginx安装过程中,经常会有各种错误: 具体安装步骤这里不做说明,网上一搜大把: 主要分析安装过程中遇到的问题 在make编译的时候,若报如下错误: cc1: warnings being trea ...
- SAP B1 ADDON 开发
承接各类SAP B1 ADDON 开发. 有需要,请联系.
- Ogre Addon之Paged Geometry
还是OGRE好啊,无尽的Addon,无尽的宝藏.既有SkyX,Hydrx这样的天空水体渲染库可供学习,还有Paged Geometry这样的“大规模海量geometry管理系统”.它通过batch,s ...
- 使用 ADD-ON SDK 开发 基于 Html JQuery 和 CSS 的 firefox 插件入门教程1: 创建一个简单的 Add-on
[本文转载自http://sixpoint.me/942/implementing-simple-addon/] 实现一个简单的插件 教程的这个部分带你使用 SDK 来实现, 运行并打包一个插件. 这 ...
- 如何使用Add-on SDK开发一个自己的火狐扩展
黄聪:如何使用Add-on SDK开发一个自己的火狐扩展 火狐开放了扩展的开发权限给程序员们,相信很多人都会希望自己做一些扩展来方便一些使用. 我最近做一些项目也需要开发一个火狐扩展,方便收集自己需要 ...
- odoo10 addon开发流程
odoo addon开发流程 创建一个addon(插件) 命令如下 python odoo-bin scaffold 插件名 路径 # 例如 python odoo-bin scaffold hh_t ...
- NodeJS Addon 多线程通信
某个产品的Mac客户端计划基于electron实现,因为现有SDK有C API,原理上用NodeJS Addon来封装成JS API就可使用了.但涉及到与Addon多线程交互,翻找资料没能找到到底该怎 ...
- cPanel中添加设置附加域(Addon domain)
本文介绍cPanel设置附加域(addon domain)来实现一个空间做多个网站的方法. 附加域(addon domain) 作用:通过它可以实现添加 新的顶级域名绑定到主机,从而创建新的站点.例如 ...
随机推荐
- kNN(K-Nearest Neighbor)最邻近规则分类
KNN最邻近规则,主要应用领域是对未知事物的识别,即推断未知事物属于哪一类,推断思想是,基于欧几里得定理,推断未知事物的特征和哪一类已知事物的的特征最接近: K近期邻(k-Nearest Neighb ...
- Delphi 调用SQL Server 2008存储过程
1.表结构如下(预算数据明细表): CREATE TABLE [dbo].[BA_FeeDetail]( [ID] [int] IDENTITY(1,1) NOT NULL, [FeeDeptID] ...
- 你得学会并且学得会的Socket编程基础知识(续)——Silverlight客户端
本文将在这个案例的基础上,加入一个特殊场景,利用Silverlight来实现客户端.有的朋友可能会说,其实是一样的吧.请不要急于下结论,有用过Silverlight的朋友都有这种体会,很多在标准.NE ...
- Error launching remote program: No such file or directory
iPhone真机调试报如下错误时,关掉Xcode,重新启动就可以了,注意是关掉Xcode,彻底关掉.Error launching remote program: No such file or di ...
- HTML5中的Web Storage(sessionStorage||localStorage)理解与简单实例
Web Storage是什么? Web Storage功能,顾名思义,就是在Web上针对client本地储存数据的功能,详细来说Web Storage分为两种: sessionStorage: 将数据 ...
- POP的Stroke动画
POP的Stroke动画 效果 源码 https://github.com/YouXianMing/Animations // // PopStrokeController.m // Animatio ...
- Unity3d-Particle System 5.x系统的学习(四)
Unity3d-Particle System 5.x系统的学习(四) 今天,我们来聊聊unity5.x的粒子系统和unity4.x粒子系统的区别. 我大致看了下,区别还是蛮多的,但是总体的粒子制作思 ...
- 图文详解 Android Binder跨进程通信机制 原理
图文详解 Android Binder跨进程通信机制 原理 目录 目录 1. Binder到底是什么? 中文即 粘合剂,意思为粘合了两个不同的进程 网上有很多对Binder的定义,但都说不清楚:Bin ...
- 按示例学python:使用python抓取网页正文
平时打开一个网页,除了文章的正文内容,通常会有一大堆的导航,广告和其他方面的信息.本博客的目的,在于说明如何从一个网页中提取出文章的正文内容,而过渡掉其他无关的的信息. 这里先看看 demo : ht ...
- 开源 java CMS - FreeCMS2.2 网站管理
项目地址:http://www.freeteam.cn/ 网站管理 FreeCMS支持站点群模式,并支持无限树级管理. 网站的相关文件在site文件夹下,每一个网站有自己的文件夹,源文件文件夹名就是自 ...