[IMP]Adding virtual entry of gdoc template for a particular record to more based on filter

bzr revid: dhr@tinyerp.com-20130308133039-lme2p04ak2pviurt
This commit is contained in:
Dharti Ratani (Open ERP) 2013-03-08 19:00:39 +05:30
parent 5882be5747
commit f26fea2a09
5 changed files with 265 additions and 201 deletions

View File

@ -27,8 +27,8 @@
'category': 'Tools',
'installable': True,
'auto_install': False,
# 'js': ['static/src/js/gdocs.js'],
# 'qweb': ['static/src/xml/gdocs.xml'],
'js': ['static/src/js/gdocs.js'],
'qweb': ['static/src/xml/gdocs.xml'],
'data': [
'security/ir.model.access.csv',
'res_config_user_view.xml'

View File

@ -86,30 +86,30 @@ class google_docs_ir_attachment(osv.osv):
_logger.info('Logged into google docs as %s', user_config['user'])
return client
def create_empty_google_doc(self, cr, uid, res_model, res_id, context=None):
'''Create a new google document, empty and with a default type (txt)
:param res_model: the object for which the google doc is created
:param res_id: the Id of the object for which the google doc is created
:return: the ID of the google document object created
'''
#login with the base account google module
client = self._auth(cr, uid, context=context)
# create the document in google docs
title = "%s %s" % (context.get("name","Untitled Document."), datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT))
local_resource = gdata.docs.data.Resource(gdata.docs.data.SPREADSHEET_LABEL,title=title)
#create a new doc in Google Docs
gdocs_resource = client.post(entry=local_resource, uri='https://docs.google.com/feeds/default/private/full/')
# create an ir.attachment into the db
self.create(cr, uid, {
'res_model': res_model,
'res_id': res_id,
'type': 'url',
'name': title,
'url': gdocs_resource.get_alternate_link().href,
}, context=context)
return {'resource_id': gdocs_resource.resource_id.text,
'title': title,
'url': gdocs_resource.get_alternate_link().href}
# def create_empty_google_doc(self, cr, uid, res_model, res_id, context=None):
# '''Create a new google document, empty and with a default type (txt)
# :param res_model: the object for which the google doc is created
# :param res_id: the Id of the object for which the google doc is created
# :return: the ID of the google document object created
# '''
# #login with the base account google module
# client = self._auth(cr, uid, context=context)
# # create the document in google docs
# title = "%s %s" % (context.get("name","Untitled Document."), datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT))
# local_resource = gdata.docs.data.Resource(gdata.docs.data.SPREADSHEET_LABEL,title=title)
# #create a new doc in Google Docs
# gdocs_resource = client.post(entry=local_resource, uri='https://docs.google.com/feeds/default/private/full/')
# # create an ir.attachment into the db
# self.create(cr, uid, {
# 'res_model': res_model,
# 'res_id': res_id,
# 'type': 'url',
# 'name': title,
# 'url': gdocs_resource.get_alternate_link().href,
# }, context=context)
# return {'resource_id': gdocs_resource.resource_id.text,
# 'title': title,
# 'url': gdocs_resource.get_alternate_link().href}
def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None):
'''
@ -154,24 +154,50 @@ class google_docs_ir_attachment(osv.osv):
if len(ids) != 1:
raise osv.except_osv(_('Google Drive Error!'), _("Creating google drive may only be done by one at a time."))
res_id = ids[0]
pool_ir_attachment = self.pool.get('ir.attachment')
pool_gdoc_config = self.pool.get('google.docs.config')
name_gdocs = ''
model_fields_dic = self.pool.get(res_model).read(cr, uid, res_id, [], context=context)
# check if a model is configured with a template
google_docs_config = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model)], context=context)
if google_docs_config:
name_gdocs = pool_gdoc_config.browse(cr, uid, google_docs_config, context=context)[0].name_template
try:
name_gdocs = name_gdocs % model_fields_dic
except:
raise osv.except_osv(_('Key Error!'), _("Your Google Drive Name Pattern's key does not found in object."))
google_template_id = pool_gdoc_config.browse(cr, uid, google_docs_config[0], context=context).gdocs_resource_id
google_document = pool_ir_attachment.copy_gdoc(cr, uid, res_model, res_id, name_gdocs, google_template_id, context=context)
else:
google_document = pool_ir_attachment.create_empty_google_doc(cr, uid, res_model, res_id, context=context)
return google_document
config_ids = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model)], context=context)
config = []
for config_id in config_ids:
action = pool_gdoc_config.browse(cr, uid, config_id, context=context)
if action.filter_id:
google_doc_configs = self._filt(cr, uid, action, action.filter_id, res_id, context=context)
print google_doc_configs,'rrrrrrrrrrr',res_id
if google_doc_configs:
config.append(action.name_template)
else:
config.append(action.name_template)
return config
def _filt(self, cr, uid, action, action_filter, record_ids, context=None):
""" filter the list record_ids that satisfy the action filter """
records = {}
if record_ids and action_filter:
assert action.model_id.model == action_filter.model_id, "Filter model different from action rule model"
print 'aaction_filter',action_filter.model_id
model = self.pool.get(action_filter.model_id)
domain = [('id', 'in', [record_ids])] + eval(action_filter.domain)
ctx = dict(context or {})
ctx.update(eval(action_filter.context))
print 'domain---------',domain
record_ids = model.search(cr, uid, domain, context=ctx)
return record_ids
def get_attachment(self, cr, uid, res_model, rec_name, ids, context=None):
res_id = ids[0]
pool_gdoc_config = self.pool.get('google.docs.config')
action_dom = [('model_id', '=', res_model)]
config_ids = pool_gdoc_config.search(cr, uid, action_dom, context=context)[0]
action = pool_gdoc_config.browse(cr, uid, config_ids, context=context)
attachment = {}
attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',action.name_template),('res_id','=',res_id)])
if not attach_ids:
google_template_id = action.gdocs_resource_id
self.copy_gdoc(cr, uid, action.model_id.model, ids[0], action.name_template, google_template_id, context=context)
attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',action.name_template)])
attachments = self.browse(cr, uid, attach_ids, context)[0]
attachment['url'] = attachments.url
return attachment
class config(osv.osv):
_name = 'google.docs.config'
@ -200,138 +226,148 @@ class config(osv.osv):
'name_template': fields.char('Google Drive Name Pattern', size=64, help='Choose how the new google drive will be named, on google side. Eg. gdoc_%(field_name)s', required=True),
}
def onchange_model_id(self, cr, uid, ids, model_id):
def onchange_model_id(self, cr, uid, ids, model_id,name_template):
res = {'domain':{'filter_id':[]}}
if model_id:
model_name = self.pool.get('ir.model').read(cr, uid, model_id, ['model'])
if model_name:
mod_name = model_name['model']
res['domain'] = {'filter_id': [('model_id', '=', mod_name)]}
else:
res['value'] = {'filter_id': False}
model_name = self.pool.get('ir.model').read(cr, uid, model_id, ['model','name'])
mod_model = model_name['model']
mod_name = model_name['name']
res['domain'] = {'filter_id': [('model_id', '=', mod_model)]}
mod_name = model_name['name']
name = name_template.replace('model', mod_name)
res['value'] = {'filter_id': False,'name_template': name or False}
return res
def onchange_filter_id(self, cr, uid, ids,model_id,filter_id,name_template):
res = {}
if filter_id:
filter_name = self.pool.get('ir.filters').browse(cr, uid, filter_id)
name = name_template.replace('filter',filter_name.name)
res['value'] = {'name_template': name or False}
return res
_defaults = {
'name_template': '%(name)s_model_filter_gdoc',
}
def _wrap_create(self, old_create, model):
""" Return a wrapper around `old_create` calling both `old_create` and
`_process`, in that order.
"""
def wrapper(cr, uid, vals, context=None):
# avoid loops or cascading actions
if context and context.get('action'):
return old_create(cr, uid, vals, context=context)
context = dict(context or {}, action=True)
new_id = old_create(cr, uid, vals, context=context)
# as it is a new record, we do not consider the actions that have a prefilter
action_dom = [('model_id', '=', model)]
config_ids = self.search(cr, uid, action_dom, context=context)
# def _wrap_create(self, old_create, model):
# """ Return a wrapper around `old_create` calling both `old_create` and
# `_process`, in that order.
# """
# def wrapper(cr, uid, vals, context=None):
# # avoid loops or cascading actions
#
# if context and context.get('action'):
# return old_create(cr, uid, vals, context=context)
#
# context = dict(context or {}, action=True)
# new_id = old_create(cr, uid, vals, context=context)
#
# # as it is a new record, we do not consider the actions that have a prefilter
# action_dom = [('model_id', '=', model)]
# config_ids = self.search(cr, uid, action_dom, context=context)
## # check postconditions, and execute actions on the records that satisfy them
# for action in self.browse(cr, uid, config_ids, context=context):
# if self._filter(cr, uid, action, action.filter_id, [new_id], context=context):
# self._process(cr, uid, action, [new_id], context=context)
# return new_id
#
# return wrapper
#
# def _filter(self, cr, uid, action, action_filter, record_ids, context=None):
# """ filter the list record_ids that satisfy the action filter """
# if record_ids and action_filter:
# assert action.model_id.model == action_filter.model_id, "Filter model different from action rule model"
# model = self.pool.get(action_filter.model_id)
# domain = [('id', 'in', record_ids)] + eval(action_filter.domain)
# ctx = dict(context or {})
# ctx.update(eval(action_filter.context))
# record_ids = model.search(cr, uid, domain, context=ctx)
# return record_ids
#
# def _process(self, cr, uid, action, record_ids, context=None):
# """ process the given action on the records """
# # execute server actions
# model = self.pool.get(action.model_id.model)
# template_url = action.gdocs_template_url
# attach_obj = self.pool.get('ir.attachment')
# for record_id in record_ids:
# record_id = int(record_id)
# model_fields_dic = self.pool.get(action.model_id.model).read(cr, uid, record_id, [], context=context)
# name_gdocs = action.name_template
# name_gdocs = name_gdocs.replace('model',action.model_id.name)
# if action.filter_id:
# name_gdocs = name_gdocs.replace('filter',action.filter_id.name)
# try:
# name_gdocs = name_gdocs % model_fields_dic
# except:
# raise osv.except_osv(_('Key Error!'), _("Your Google Drive Name Pattern's key does not found in object."))
# attachments = attach_obj.search(cr, uid, [('res_id','=',record_id),('name','=',name_gdocs)])
# if not attachments:
# google_template_id = action.gdocs_resource_id
# attach_obj.copy_gdoc(cr, uid, action.model_id.model, record_id, name_gdocs, google_template_id, context=context)
# return True
#
# def _wrap_write(self, old_write, model):
# """ Return a wrapper around `old_write` calling both `old_write` and
# `_process`, in that order.
# """
# def wrapper(cr, uid, ids, vals, context=None):
# # avoid loops or cascading actions
# if context and context.get('action'):
# return old_write(cr, uid, ids, vals, context=context)
#
# context = dict(context or {}, action=True)
# ids = [ids] if isinstance(ids, (int, long, str)) else ids
#
# # retrieve the action rules to possibly execute
# action_dom = [('model_id', '=', model)]
# config_ids = self.search(cr, uid, action_dom, context=context)
# gconfigs = self.browse(cr, uid, config_ids, context=context)
#
# # check preconditions
# pre_ids = {}
# for gconfig in gconfigs:
# pre_ids[gconfig] = self._filter(cr, uid, gconfig, gconfig.filter_id, ids, context=context)
#
# # execute write
# old_write(cr, uid, ids, vals, context=context)
#
# # check postconditions, and execute actions on the records that satisfy them
for action in self.browse(cr, uid, config_ids, context=context):
if self._filter(cr, uid, action, action.filter_id, [new_id], context=context):
self._process(cr, uid, action, [new_id], context=context)
return new_id
return wrapper
def _filter(self, cr, uid, action, action_filter, record_ids, context=None):
""" filter the list record_ids that satisfy the action filter """
if record_ids and action_filter:
assert action.model_id.model == action_filter.model_id, "Filter model different from action rule model"
model = self.pool.get(action_filter.model_id)
domain = [('id', 'in', record_ids)] + eval(action_filter.domain)
ctx = dict(context or {})
ctx.update(eval(action_filter.context))
record_ids = model.search(cr, uid, domain, context=ctx)
return record_ids
def _process(self, cr, uid, action, record_ids, context=None):
""" process the given action on the records """
# execute server actions
model = self.pool.get(action.model_id.model)
template_url = action.gdocs_template_url
attach_obj = self.pool.get('ir.attachment')
for record_id in record_ids:
record_id = int(record_id)
model_fields_dic = self.pool.get(action.model_id.model).read(cr, uid, record_id, [], context=context)
name_gdocs = action.name_template
name_gdocs = name_gdocs.replace('model',action.model_id.name)
if action.filter_id:
name_gdocs = name_gdocs.replace('filter',action.filter_id.name)
try:
name_gdocs = name_gdocs % model_fields_dic
except:
raise osv.except_osv(_('Key Error!'), _("Your Google Drive Name Pattern's key does not found in object."))
attachments = attach_obj.search(cr, uid, [('res_id','=',record_id),('name','=',name_gdocs)])
if not attachments:
google_template_id = action.gdocs_resource_id
attach_obj.copy_gdoc(cr, uid, action.model_id.model, record_id, name_gdocs, google_template_id, context=context)
return True
def _wrap_write(self, old_write, model):
""" Return a wrapper around `old_write` calling both `old_write` and
`_process`, in that order.
"""
def wrapper(cr, uid, ids, vals, context=None):
# avoid loops or cascading actions
if context and context.get('action'):
return old_write(cr, uid, ids, vals, context=context)
context = dict(context or {}, action=True)
ids = [ids] if isinstance(ids, (int, long, str)) else ids
# retrieve the action rules to possibly execute
action_dom = [('model_id', '=', model)]
config_ids = self.search(cr, uid, action_dom, context=context)
gconfigs = self.browse(cr, uid, config_ids, context=context)
# check preconditions
pre_ids = {}
for gconfig in gconfigs:
pre_ids[gconfig] = self._filter(cr, uid, gconfig, gconfig.filter_id, ids, context=context)
# execute write
old_write(cr, uid, ids, vals, context=context)
# check postconditions, and execute actions on the records that satisfy them
for gconfig in gconfigs:
post_ids = self._filter(cr, uid, gconfig, gconfig.filter_id, pre_ids[gconfig], context=context)
print 'post_idsss',post_ids
if post_ids:
print 'post_idsss',post_ids
self._process(cr, uid, gconfig, post_ids, context=context)
return True
return wrapper
def _register_hook(self, cr, ids=None):
""" Wrap the methods `create` and `write` of the models specified by
the rules given by `ids` (or all existing rules if `ids` is `None`.)
"""
if ids is None:
ids = self.search(cr, SUPERUSER_ID, [])
for config in self.browse(cr, SUPERUSER_ID, ids):
model = config.model_id.model
model_obj = self.pool.get(model)
model_obj.create = self._wrap_create(model_obj.create, model)
model_obj.write = self._wrap_write(model_obj.write, model)
return True
def create(self, cr, uid, vals, context=None):
res_id = super(config, self).create(cr, uid, vals, context=context)
self._register_hook(cr, [res_id])
return res_id
def write(self, cr, uid, ids, vals, context=None):
if isinstance(ids, (int, long)):
ids = [ids]
super(config, self).write(cr, uid, ids, vals, context=context)
self._register_hook(cr, ids)
return True
# for gconfig in gconfigs:
# post_ids = self._filter(cr, uid, gconfig, gconfig.filter_id, pre_ids[gconfig], context=context)
# print 'post_idsss',post_ids
# if post_ids:
# print 'post_idsss',post_ids
# self._process(cr, uid, gconfig, post_ids, context=context)
# return True
#
# return wrapper
#
# def _register_hook(self, cr, ids=None):
# """ Wrap the methods `create` and `write` of the models specified by
# the rules given by `ids` (or all existing rules if `ids` is `None`.)
# """
# if ids is None:
# ids = self.search(cr, SUPERUSER_ID, [])
# for config in self.browse(cr, SUPERUSER_ID, ids):
# model = config.model_id.model
# model_obj = self.pool.get(model)
# model_obj.create = self._wrap_create(model_obj.create, model)
# model_obj.write = self._wrap_write(model_obj.write, model)
# return True
#
# def create(self, cr, uid, vals, context=None):
# res_id = super(config, self).create(cr, uid, vals, context=context)
# self._register_hook(cr, [res_id])
# return res_id
#
# def write(self, cr, uid, ids, vals, context=None):
# if isinstance(ids, (int, long)):
# ids = [ids]
# super(config, self).write(cr, uid, ids, vals, context=context)
# self._register_hook(cr, ids)
# return True
config()

View File

@ -21,10 +21,10 @@
<field name="arch" type="xml">
<form string="Google Drive Configuration" version="7.0">
<group>
<field name="model_id" on_change="onchange_model_id(model_id)"/>
<field name="model_id" on_change="onchange_model_id(model_id,name_template)"/>
<label for='filter_id'/>
<div>
<field name='filter_id'/>
<field name='filter_id' on_change="onchange_filter_id(model_id,filter_id,name_template)"/>
<p class="oe_grey">
To Create a new filter, go to the list of documents you want to attach a google document to and
save the filter.Then come back ti this view.The saved filter will be available in the filed above.

View File

@ -3,38 +3,64 @@ var _t = instance.web._t,
QWeb = instance.web.qweb;
instance.web.Sidebar.include({
redraw: function() {
start: function() {
var self = this;
var ids
this._super.apply(this, arguments);
self.$el.find('.oe_sidebar_add_attachment').after(QWeb.render('AddGoogleDocumentItem', {widget: self}))
self.$el.find('.oe_sidebar_add_google_doc').on('click', function (e) {
self.on_google_doc();
});
},
on_google_doc: function() {
var self = this;
var view = self.getParent();
var ids = ( view.fields_view.type != "form" )? view.groups.get_selection().ids : [ view.datarecord.id ];
if( !_.isEmpty(ids) ){
view.sidebar_eval_context().done(function (context) {
var ds = new instance.web.DataSet(this, 'ir.attachment', context);
ds.call('google_doc_get', [view.dataset.model, ids, context]).done(function(r) {
if (r == 'False') {
var params = {
error: response,
message: _t("The user google credentials are not set yet. Contact your administrator for help.")
}
$(openerp.web.qweb.render("DialogWarning", params)).dialog({
title: _t("User Google credentials are not yet set."),
modal: true,
});
}
}).done(function(r){
window.open(r.url,"_blank");
view.reload();
});
});
}
}
var result;
if(view.fields_view.type == "form"){
ids = []
view.on("load_record", self, function(r){
ids = [r.id]
self.add_gdoc_items(view,ids)
});
}
},
add_gdoc_items: function(view,ids){
$('.oe_share_gdoc').remove();
var self = this;
if( !_.isEmpty(ids) ){
view.sidebar_eval_context().done(function (context) {
var ds = new instance.web.DataSet(this, 'ir.attachment', context);
ds.call('google_doc_get', [view.dataset.model, ids, context]).done(function(r) {
console.log("thjoid ",self.items['other'])
if(!_.isEmpty(r)){
_.each(r,function(res){
_.each(self.items['other'],function(others){
if(others['label'] == res){
self.items['other'].pop()
}
})
self.add_items('other', [
{ label: res,
callback: self.on_google_doc,
classname: 'oe_share_gdoc' },
]);
})
}
});
});
}
},
on_google_doc: function(r) {
var self = this;
var view = self.getParent();
var ids = ( view.fields_view.type != "form" )? view.groups.get_selection().ids : [ view.datarecord.id ];
if( !_.isEmpty(ids) ){
view.sidebar_eval_context().done(function (context) {
var ds = new instance.web.DataSet(this, 'ir.attachment', context);
ds.call('get_attachment', [view.dataset.model, r, ids, context]).done(function(res) {
console.log('resssssss',res)
window.open(res.url,"_blank");
view.reload();
})
});
}
}
});
};

View File

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- vim:fdl=1:
-->
<templates id="template" xml:space="preserve">
<!-- <templates id="template" xml:space="preserve">
<t t-name="AddGoogleDocumentItem">
<li class="oe_sidebar_add_google_doc"><span><b>Add Google Drive...</b></span></li>
<t t-foreach="widget" t-as="item">
<li class="oe_gdoc_action_a"><span><t t-esc="item"/></span></li>
</t>
</t>
</templates>
</templates> -->