[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

@ -31,7 +31,6 @@
'static/lib/gapi/client.js',
'static/src/js/gdocs.js',
],
# 'qweb': ['static/src/xml/gdocs.xml'],
'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,52 +28,6 @@ from urlparse import urlparse
_logger = logging.getLogger(__name__)
class google_docs_ir_attachment(osv.osv):
_inherit = 'ir.attachment'
def get_google_docs_config(self, cr, uid, res_model, res_id, context=None):
'''
Function called by the js, when no google doc are yet associated with a record, with the aim to create one. It
will first seek for a google.docs.config associated with the model `res_model` to find out what's the template
of google doc to copy (this is usefull if you want to start with a non-empty document, a type or a name
different than the default values). If no config is associated with the `res_model`, then a blank text document
with a default name is created.
:param res_model: the object for which the google doc is created
:param ids: the list of ids of the objects for which the google doc is created. This list is supposed to have
a length of 1 element only (batch processing is not supported in the code, though nothing really prevent it)
:return: the config id and config name
'''
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)
configs = []
for config in pool_gdoc_config.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
continue
google_doc_configs = self._filter(cr, uid, config, config.filter_id, res_id, context=context)
if google_doc_configs:
configs.append({'id': config.id, 'name': config.name})
else:
configs.append({'id': config.id, 'name': config.name})
return configs
def _filter(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:
if not action.model_id == action_filter.model_id:
raise osv.except_osv(_('Warning!'), _("Something went wrong with the configuration of attachments with google drive.Please contact your Administrator to fix the problem."))
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
class config(osv.osv):
_name = 'google.docs.config'
_description = "Google Drive templates config"
@ -106,6 +60,48 @@ class config(osv.osv):
res[config.id] = {'name':name_gdocs, 'url': url}
return res
def get_google_docs_config(self, cr, uid, res_model, res_id, context=None):
'''
Function called by the js, when no google doc are yet associated with a record, with the aim to create one. It
will first seek for a google.docs.config associated with the model `res_model` to find out what's the template
of google doc to copy (this is usefull if you want to start with a non-empty document, a type or a name
different than the default values). If no config is associated with the `res_model`, then a blank text document
with a default name is created.
:param res_model: the object for which the google doc is created
:param ids: the list of ids of the objects for which the google doc is created. This list is supposed to have
a length of 1 element only (batch processing is not supported in the code, though nothing really prevent it)
:return: the config id and config name
'''
if not res_id:
raise osv.except_osv(_('Google Drive Error!'), _("Creating google drive may only be done by one at a time."))
# check if a model is configured with a template
config_ids = self.search(cr, uid, [('model_id', '=', res_model)], context=context)
configs = []
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
continue
google_doc_configs = self._filter(cr, uid, config, config.filter_id, res_id, context=context)
if google_doc_configs:
configs.append({'id': config.id, 'name': config.name})
else:
configs.append({'id': config.id, 'name': config.name})
return configs
def _filter(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:
if not action.model_id == action_filter.model_id:
raise osv.except_osv(_('Warning!'), _("Something went wrong with the configuration of attachments with google drive.Please contact your Administrator to fix the problem."))
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 _list_all_models(self, cr, uid, context=None):
cr.execute("SELECT model, name from ir_model order by name")
return cr.fetchall()
@ -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:
@ -167,9 +164,8 @@ class config(osv.osv):
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

@ -3,19 +3,19 @@
<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>
<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"/>
<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>

View File

@ -61,7 +61,8 @@
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to add a new template.
</p><p>
</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>
@ -74,9 +75,7 @@
<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"/>
<button type="action" name="%(google_docs.action_google_docs_users_config)d" string="Configure Template" class="oe_link" />
</div>
</xpath>
</field>
@ -94,6 +93,7 @@
</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>

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,9 +49,11 @@ openerp.google_docs = function (instance, m) {
});
}
},
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;
@ -63,14 +65,11 @@ openerp.google_docs = function (instance, m) {
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.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,18 +101,18 @@ 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');
}
});
},
@ -140,7 +140,7 @@ openerp.google_docs = function (instance, m) {
'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> -->