[IMP] Improved Code

bzr revid: pga@tinyerp.com-20130510063501-szrjvvh4jhzy4x7j
This commit is contained in:
Parth Gajjar (Open ERP) 2013-05-10 12:05:01 +05:30
parent 32352d0695
commit ff3fded96e
6 changed files with 178 additions and 193 deletions

View File

@ -28,10 +28,9 @@
'installable': True,
'auto_install': False,
'js': [
'static/lib/gapi/client.js',
'static/src/js/gdocs.js',
],
# 'qweb': ['static/src/xml/gdocs.xml'],
'static/lib/gapi/client.js',
'static/src/js/gdocs.js',
],
'data': [
'security/ir.model.access.csv',
'res_config_user_view.xml'
@ -39,7 +38,7 @@
'demo': [
'google_docs_demo.xml'
],
'depends': ['crm'],
'depends': ['base_setup'],
'description': """
Integrate Google document with OpenERP.
=======================================

View File

@ -28,8 +28,37 @@ from urlparse import urlparse
_logger = logging.getLogger(__name__)
class google_docs_ir_attachment(osv.osv):
_inherit = 'ir.attachment'
class config(osv.osv):
_name = 'google.docs.config'
_description = "Google Drive templates config"
def get_google_doc_name(self, cr, uid, ids, res_id, context=None):
pool_model = self.pool.get("ir.model")
res = {}
for config in self.browse(cr, SUPERUSER_ID, ids, context=context):
res_model = config.model_id
model_ids = pool_model.search(cr, uid, [('model', '=', res_model)])
if not model_ids:
continue
model = pool_model.browse(cr, uid, model_ids[0], context=context)
model_name = model.name
filter_name = config.filter_id and config.filter_id.name or False
record = self.pool.get(res_model).read(cr, uid, res_id, [], context=context)
record.update({'model': model_name, 'filter':filter_name})
name_gdocs = config.name_template or "%(name)s_%(model)s_%(filter)s_gdrive"
try:
name_gdocs = name_gdocs % record
except:
raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object."))
attach_pool = self.pool.get("ir.attachment")
attach_ids = attach_pool.search(cr, uid, [('res_model', '=', res_model), ('name', '=', name_gdocs), ('res_id', '=', res_id)])
url = False
if attach_ids:
attachment = attach_pool.browse(cr, uid, attach_ids[0], context)
url = attachment.url
res[config.id] = {'name':name_gdocs, 'url': url}
return res
def get_google_docs_config(self, cr, uid, res_model, res_id, context=None):
'''
@ -45,11 +74,10 @@ class google_docs_ir_attachment(osv.osv):
'''
if not res_id:
raise osv.except_osv(_('Google Drive Error!'), _("Creating google drive may only be done by one at a time."))
pool_gdoc_config = self.pool.get('google.docs.config')
# check if a model is configured with a template
config_ids = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model)], context=context)
config_ids = self.search(cr, uid, [('model_id', '=', res_model)], context=context)
configs = []
for config in pool_gdoc_config.browse(cr, SUPERUSER_ID, config_ids, context=context):
for config in self.browse(cr, SUPERUSER_ID, config_ids, context=context):
if config.filter_id:
if (config.filter_id.user_id and config.filter_id.user_id.id != uid):
#Private
@ -74,38 +102,6 @@ class google_docs_ir_attachment(osv.osv):
record_ids = model.search(cr, uid, domain, context=ctx)
return record_ids
class config(osv.osv):
_name = 'google.docs.config'
_description = "Google Drive templates config"
def get_google_doc_name(self, cr, uid, ids, res_id, context=None):
pool_model = self.pool.get("ir.model")
res = {}
for config in self.browse(cr, SUPERUSER_ID, ids, context=context):
res_model = config.model_id
model_ids = pool_model.search(cr, uid, [('model','=',res_model)])
if not model_ids:
continue
model = pool_model.browse(cr, uid, model_ids[0], context=context)
model_name = model.name
filter_name = config.filter_id and config.filter_id.name or False
record = self.pool.get(res_model).read(cr, uid, res_id, [], context=context)
record.update({'model': model_name,'filter':filter_name})
name_gdocs = config.name_template or "%(name)s_%(model)s_%(filter)s_gdrive"
try:
name_gdocs = name_gdocs % record
except:
raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object."))
attach_pool = self.pool.get("ir.attachment")
attach_ids = attach_pool.search(cr, uid, [('res_model','=',res_model),('name','=',name_gdocs),('res_id','=',res_id)])
url = False
if attach_ids:
attachment = attach_pool.browse(cr, uid, attach_ids[0], context)
url = attachment.url
res[config.id] = {'name':name_gdocs, 'url': url}
return res
def _list_all_models(self, cr, uid, context=None):
cr.execute("SELECT model, name from ir_model order by name")
return cr.fetchall()
@ -118,7 +114,7 @@ class config(osv.osv):
url = urlparse(template_url)
res = url.path.split('/')
resource = res[1]
if res[1]== "spreadsheet":
if res[1] == "spreadsheet":
key = url.query.split('=')[1]
else:
key = res[3]
@ -139,8 +135,8 @@ class config(osv.osv):
'model_id': fields.selection(_list_all_models, 'Model', required=True),
'filter_id' : fields.many2one('ir.filters', 'Filter'),
'gdocs_template_url': fields.char('Template URL', required=True, size=1024),
'gdocs_resource_id' : fields.function(_resource_get,type="char" ,string='Resource Id'),
'google_client_id' : fields.function(_client_id_get,type="char" ,string='Google Client '),
'gdocs_resource_id' : fields.function(_resource_get, type="char" , string='Resource Id'),
'google_client_id' : fields.function(_client_id_get, type="char" , string='Google Client '),
'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),
}
@ -155,6 +151,7 @@ class config(osv.osv):
_defaults = {
'name_template': '%(name)s_%(model)s_%(filter)s_gdrive',
}
def _check_model_id(self, cr, uid, ids, context=None):
config_id = self.browse(cr, uid, ids[0], context=context)
if config_id.filter_id.id and config_id.model_id != config_id.filter_id.model_id:
@ -162,14 +159,13 @@ class config(osv.osv):
return True
_constraints = [
(_check_model_id, 'Model of selected filter is not matching with model of current template.', ['model_id','filter_id']),
(_check_model_id, 'Model of selected filter is not matching with model of current template.', ['model_id', 'filter_id']),
]
config()
class res_company(osv.osv):
_inherit = "res.company"
_columns = {
'google_client_id': fields.char('Google Client ID', size=200),
'google_client_id': fields.char('Google Client ID', size=200, help="Go to 'Google APIs console' and create 'OAuth 2.0 Client ID'."),
}

View File

@ -1,25 +1,25 @@
<?xml version="1.0"?>
<openerp>
<data noupdate="1">
<data noupdate="1">
<!-- filter demo -->
<record id="filter_crm" model="ir.filters">
<field name="name">Sales Department</field>
<field name="model_id">crm.lead</field>
<field name="domain">[['section_id', '=', 1]]</field>
<field name="context">{'lang': 'en_US', 'tz': 'Europe/Brussels'}</field>
<field name="user_id" eval="False"/>
</record>
<!-- filter demo -->
<record id="filter_partner" model="ir.filters">
<field name="name">Supplier</field>
<field name="model_id">res.partner</field>
<field name="domain">[['supplier', '=', 1]]</field>
<field name="context">{'lang': 'en_US', 'tz': 'Europe/Brussels'}</field>
<field name="user_id" eval="False" />
</record>
<!-- template demo -->
<record id="template_crm" model="google.docs.config">
<field name="name">Sales Department Opportunities</field>
<field name="model_id">crm.lead</field>
<field name="filter_id" ref="filter_crm"/>
<field name="gdocs_template_url">https://docs.google.com/spreadsheet/ccc?key=0Ah2qnrLAoZmUdGRvdVdmS1VoSDctWk1kd18taGZ4ckE#gid=0</field>
<field name="name_template">%(name)s_%(model)s_%(filter)s_gdrive</field>
</record>
</data>
<!-- template demo -->
<record id="template_partner" model="google.docs.config">
<field name="name">Supplier Docs</field>
<field name="model_id">res.partner</field>
<field name="filter_id" ref="filter_partner"/>
<field name="gdocs_template_url">https://docs.google.com/spreadsheet/ccc?key=0Ah2qnrLAoZmUdGRvdVdmS1VoSDctWk1kd18taGZ4ckE#gid=0</field>
<field name="name_template">%(name)s_%(model)s_%(filter)s_gdrive</field>
</record>
</data>
</openerp>

View File

@ -1,86 +1,85 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<data>
<!-- add google docs config field in user form -->
<!-- add google docs config field in user form -->
<record model="ir.ui.view" id="view_google_docs_config_tree">
<field name="name">google_docs.config.tree</field>
<field name="model">google.docs.config</field>
<field name="arch" type="xml">
<tree string="Google Drive Configuration">
<field name="name"/>
<field name="model_id"/>
</tree>
</field>
</record>
<record model="ir.ui.view" id="view_google_docs_config_tree">
<field name="name">google_docs.config.tree</field>
<field name="model">google.docs.config</field>
<field name="arch" type="xml">
<tree string="Google Drive Configuration">
<field name="name" />
<field name="model_id" />
</tree>
</field>
</record>
<record model="ir.ui.view" id="view_google_docs_config_form">
<field name="name">google_docs.config.form</field>
<field name="model">google.docs.config</field>
<field name="arch" type="xml">
<form string="Google Drive Configuration" version="7.0">
<group>
<field name="name"/>
<field name="model_id" on_change="onchange_model_id(model_id)"/>
<label for='filter_id'/>
<div>
<field name='filter_id'/>
<p class="oe_grey">
<b>To create a new filter:</b><br/>
- Go to the OpenERP document you want to filter. For instance, go to Opportunities and search on Sales Department.<br/>
- In this "Search" view, select the option "Save Current Filter", enter the name (Ex: Sales Department)<br/>
- If you select "Share with all users", link of google document in "More" options will appear for all users in opportunities of Sales Department.<br/>
- If you don't select "Share with all users", link of google document in "More" options will not appear for other users in opportunities of Sales Department.<br/>
- If filter is not specified, link of google document will appear in "More" option for all users for all opportunities.
</p>
</div>
<field name='gdocs_template_url' placeholder="https://docs.google.com/document/d/1vOtpJK9scIQz6taD9tJRIETWbEw3fSiaQHArsJYcua4/edit" required="1"/>
<field name='gdocs_resource_id' invisible="1"/>
<label for='name_template'/>
<div>
<field name='name_template'/>
<p class="oe_grey">
The name of the attached document can use fixed or variable data. To distinguish between documents in
Google Drive, use fixed words and fields. For instance, in the example above, if you wrote Agrolait_%%(name)s_Sales
in the Google Drive name field, the document in your Google Drive and in OpenERP attachment will be named
'Agrolait_SO0001_Sales'.
</p>
</div>
</group>
</form>
</field>
</record>
<record model="ir.ui.view" id="view_google_docs_config_form">
<field name="name">google_docs.config.form</field>
<field name="model">google.docs.config</field>
<field name="arch" type="xml">
<form string="Google Drive Configuration" version="7.0">
<group>
<field name="name" />
<field name="model_id" on_change="onchange_model_id(model_id)" />
<label for='filter_id' />
<div>
<field name='filter_id' />
<p class="oe_grey">
<b>To create a new filter:</b><br/>
- Go to the OpenERP document you want to filter. For instance, go to Opportunities and search on Sales Department.<br/>
- In this "Search" view, select the option "Save Current Filter", enter the name (Ex: Sales Department)<br/>
- If you select "Share with all users", link of google document in "More" options will appear for all users in opportunities of Sales Department.<br/>
- If you don't select "Share with all users", link of google document in "More" options will not appear for other users in opportunities of Sales Department.<br/>
- If filter is not specified, link of google document will appear in "More" option for all users for all opportunities.
</p>
</div>
<field name='gdocs_template_url' placeholder="https://docs.google.com/document/d/1vOtpJK9scIQz6taD9tJRIETWbEw3fSiaQHArsJYcua4/edit" required="1" />
<field name='gdocs_resource_id' invisible="1" />
<label for='name_template' />
<div>
<field name='name_template' />
<p class="oe_grey">
The name of the attached document can use fixed or variable data. To distinguish between documents in
Google Drive, use fixed words and fields. For instance, in the example above, if you wrote Agrolait_%%(name)s_Sales
in the Google Drive name field, the document in your Google Drive and in OpenERP attachment will be named
'Agrolait_SO0001_Sales'.
</p>
</div>
</group>
</form>
</field>
</record>
<record model='ir.actions.act_window' id='action_google_docs_users_config'>
<field name='name'>Google Drive Templates</field>
<field name='res_model'>google.docs.config</field>
<field name='type'>ir.actions.act_window</field>
<field name='view_type'>form</field>
<field name='view_id' ref='view_google_docs_config_tree'/>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to add a new template.
</p><p>
Link your own google drive templates to any record of OpenERP. If you have really specific documents you want your collaborator fill in, e.g. Use a spreadsheet to control the quality of your product or review the delivery checklist for each order in a foreign country, ... Its very easy to manage them, link them to OpenERP and use them to collaborate with your employees.
</p>
</field>
</record>
<record model='ir.actions.act_window' id='action_google_docs_users_config'>
<field name='name'>Google Drive Templates</field>
<field name='res_model'>google.docs.config</field>
<field name='type'>ir.actions.act_window</field>
<field name='view_type'>form</field>
<field name='view_id' ref='view_google_docs_config_tree' />
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to add a new template.
</p>
<p>
Link your own google drive templates to any record of OpenERP. If you have really specific documents you want your collaborator fill in, e.g. Use a spreadsheet to control the quality of your product or review the delivery checklist for each order in a foreign country, ... Its very easy to manage them, link them to OpenERP and use them to collaborate with your employees.
</p>
</field>
</record>
<record id="inherited_google_view_general_configuration" model="ir.ui.view">
<field name="name">General Settings</field>
<field name="model">base.config.settings</field>
<field name="inherit_id" ref="base_setup.view_general_configuration"/>
<field name="arch" type="xml">
<xpath expr="//div[@name='google_docs']" position="after">
<div attrs="{'invisible': [('module_google_docs','=',False)]}">
<button type="action"
name="%(google_docs.action_google_docs_users_config)d"
string="Configure Template" class="oe_link"/>
</div>
</xpath>
</field>
</record>
<record id="inherited_google_view_general_configuration" model="ir.ui.view">
<field name="name">General Settings</field>
<field name="model">base.config.settings</field>
<field name="inherit_id" ref="base_setup.view_general_configuration" />
<field name="arch" type="xml">
<xpath expr="//div[@name='google_docs']" position="after">
<div attrs="{'invisible': [('module_google_docs','=',False)]}">
<button type="action" name="%(google_docs.action_google_docs_users_config)d" string="Configure Template" class="oe_link" />
</div>
</xpath>
</field>
</record>
<record model="ir.ui.view" id="view_company_inherit_form2">
<field name="name">res.company.form.inherit</field>
@ -89,12 +88,13 @@
<field name="arch" type="xml">
<xpath expr="//group[@name='account_grp']" position="after">
<group string="Google Configuration">
<field name='google_client_id'/>
<field name='google_client_id' />
</group>
</xpath>
</field>
</record>
<menuitem name='Google Drive configuration' id='menu_gdocs_config' parent='base.menu_administration'/>
<menuitem id='menu_gdocs_model_config' parent='menu_gdocs_config' action='action_google_docs_users_config'/>
</data>
<menuitem name='Google Drive configuration' id='menu_gdocs_config' parent='base.menu_administration' />
<menuitem id='menu_gdocs_model_config' parent='menu_gdocs_config' action='action_google_docs_users_config' />
</data>
</openerp>

View File

@ -26,7 +26,7 @@ openerp.google_docs = function (instance, m) {
}
if (res_id) {
view.sidebar_eval_context().done(function (context) {
var ds = new instance.web.DataSet(this, 'ir.attachment', context);
var ds = new instance.web.DataSet(this, 'google.docs.config', context);
ds.call('get_google_docs_config', [view.dataset.model, res_id, context]).done(function (r) {
if (!_.isEmpty(r)) {
_.each(r, function (res) {
@ -49,30 +49,29 @@ openerp.google_docs = function (instance, m) {
});
}
},
fetch: function(model, fields, domain, ctx){
fetch: function (model, fields, domain, ctx) {
return new instance.web.Model(model).query(fields).filter(domain).context(ctx).all()
},
on_google_doc: function (doc_item) {
var self = this;
self.config = doc_item;
var loaded = self.fetch('google.docs.config',['gdocs_resource_id','google_client_id'],[['id','=',doc_item.config_id]])
.then(function(configs){
var ds = new instance.web.DataSet(self, 'google.docs.config');
var self = this;
self.config = doc_item;
var loaded = self.fetch('google.docs.config', ['gdocs_resource_id', 'google_client_id'], [['id', '=', doc_item.config_id]])
.then(function (configs) {
var ds = new instance.web.DataSet(self, 'google.docs.config');
ds.call('get_google_doc_name', [[doc_item.config_id], doc_item.res_id]).done(function (r) {
if (!_.isEmpty(r)) {
self.OAUTHURL = 'https://accounts.google.com/o/oauth2/auth?';
self.VALIDURL = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=';
self.SCOPES = 'https://www.googleapis.com/auth/drive';
self.CLIENT_ID = configs[0].google_client_id;//'39623646228-4de6gmcai1n3mj14h08bcir2u42ln07e.apps.googleusercontent.com';
//self.GDOCS_TEMPLATE_URL = configs[0].gdocs_template_url;//'https://docs.google.com/document/d/1FOqv2iDaRcjdDz577dECgVvqhYN7bgAg3vB9M7DiCdM/edit';
self.gdoc_name = r[doc_item.config_id]['name'];
//var pattern = /(\/d\/)(.[^/]*)/;
self.GDOCS_TEMPLATE_ID = configs[0].gdocs_resource_id;//self.GDOCS_TEMPLATE_URL.match(pattern)[2];
self.gdoc_url = r[doc_item.config_id]['url'];
self.handleClientLoad();
self.OAUTHURL = 'https://accounts.google.com/o/oauth2/auth?';
self.VALIDURL = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=';
self.SCOPES = 'https://www.googleapis.com/auth/drive';
self.CLIENT_ID = configs[0].google_client_id;
self.GDOC_NAME = r[doc_item.config_id]['name'];
self.GDOCS_TEMPLATE_ID = configs[0].gdocs_resource_id;
self.GDOC_URL = r[doc_item.config_id]['url'];
self.handleClientLoad();
}
});
});
});
},
@ -94,6 +93,7 @@ openerp.google_docs = function (instance, m) {
},
handleAuthResult: function (self, authResult) {
if (authResult && !authResult.error) {
self.clientLoad(self);
} else {
@ -101,19 +101,19 @@ openerp.google_docs = function (instance, m) {
'client_id': self.CLIENT_ID,
'scope': self.SCOPES,
'immediate': false
},
self.handleAuthResult);
},function (authResult) {
self.handleAuthResult(self, authResult)
});
}
},
clientLoad: function (self) {
gapi.client.load('drive', 'v2', function () {
if (self.gdoc_url == false){
self.copyFile(self.config, self.GDOCS_TEMPLATE_ID, self.gdoc_name);
}
else{
window.open(self.gdoc_url, '_newtab');
}
if (self.GDOC_URL == false) {
self.copyFile(self.config, self.GDOCS_TEMPLATE_ID, self.GDOC_NAME);
} else {
window.open(self.GDOC_URL, '_blank');
}
});
},
@ -131,16 +131,16 @@ openerp.google_docs = function (instance, m) {
'fileId': resp.id
});
get_new_file.execute(function (file) {
var ds = new instance.web.DataSet(self, 'ir.attachment');
vals = {
'res_model': config.res_model,
'res_id': config.res_id,
'type': 'url',
'name': copyTitle,
'url': file.alternateLink
}
var ds = new instance.web.DataSet(self, 'ir.attachment');
vals = {
'res_model': config.res_model,
'res_id': config.res_id,
'type': 'url',
'name': copyTitle,
'url': file.alternateLink
}
ds.call('create', [vals]).done(function (r) {
window.open(file.alternateLink, '_newtab');
window.open(file.alternateLink, '_blank');
});
});
});

View File

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