diff --git a/addons/google_docs/__openerp__.py b/addons/google_docs/__openerp__.py index 657b30258c3..220b882b53b 100644 --- a/addons/google_docs/__openerp__.py +++ b/addons/google_docs/__openerp__.py @@ -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. ======================================= diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 69188fe8c85..f3d3ad72198 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -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'."), } \ No newline at end of file diff --git a/addons/google_docs/google_docs_demo.xml b/addons/google_docs/google_docs_demo.xml index b877e45aefa..d0f75e36ba6 100644 --- a/addons/google_docs/google_docs_demo.xml +++ b/addons/google_docs/google_docs_demo.xml @@ -1,25 +1,25 @@ - + - - - Sales Department - crm.lead - [['section_id', '=', 1]] - {'lang': 'en_US', 'tz': 'Europe/Brussels'} - - + + + Supplier + res.partner + [['supplier', '=', 1]] + {'lang': 'en_US', 'tz': 'Europe/Brussels'} + + - - - Sales Department Opportunities - crm.lead - - https://docs.google.com/spreadsheet/ccc?key=0Ah2qnrLAoZmUdGRvdVdmS1VoSDctWk1kd18taGZ4ckE#gid=0 - %(name)s_%(model)s_%(filter)s_gdrive - - - + + + Supplier Docs + res.partner + + https://docs.google.com/spreadsheet/ccc?key=0Ah2qnrLAoZmUdGRvdVdmS1VoSDctWk1kd18taGZ4ckE#gid=0 + %(name)s_%(model)s_%(filter)s_gdrive + + + diff --git a/addons/google_docs/res_config_user_view.xml b/addons/google_docs/res_config_user_view.xml index f4fa96b1a51..abf394f052a 100644 --- a/addons/google_docs/res_config_user_view.xml +++ b/addons/google_docs/res_config_user_view.xml @@ -1,86 +1,85 @@ - + - + - - google_docs.config.tree - google.docs.config - - - - - - - + + google_docs.config.tree + google.docs.config + + + + + + + - - google_docs.config.form - google.docs.config - -
- - - - -
-
-
+ + google_docs.config.form + google.docs.config + +
+ + + + +
+
+
- - Google Drive Templates - google.docs.config - ir.actions.act_window - form - - -

- Click to add a new template. -

- 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. -

-
-
+ + Google Drive Templates + google.docs.config + ir.actions.act_window + form + + +

+ Click to add a new template. +

+

+ 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. +

+
+
- - General Settings - base.config.settings - - - -
-
-
-
-
+ + General Settings + base.config.settings + + + +
+
+
+
+
res.company.form.inherit @@ -89,12 +88,13 @@ - + - - -
+ + + +
diff --git a/addons/google_docs/static/src/js/gdocs.js b/addons/google_docs/static/src/js/gdocs.js index 1d0fbd42be1..c4769bed4dd 100644 --- a/addons/google_docs/static/src/js/gdocs.js +++ b/addons/google_docs/static/src/js/gdocs.js @@ -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'); }); }); }); diff --git a/addons/google_docs/static/src/xml/gdocs.xml b/addons/google_docs/static/src/xml/gdocs.xml deleted file mode 100644 index a90f4034ea8..00000000000 --- a/addons/google_docs/static/src/xml/gdocs.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - \ No newline at end of file