diff --git a/addons/base_setup/res_config.py b/addons/base_setup/res_config.py index 96ce669216a..1e5169fc2b3 100644 --- a/addons/base_setup/res_config.py +++ b/addons/base_setup/res_config.py @@ -37,7 +37,7 @@ class base_config_settings(osv.osv_memory): 'module_auth_oauth': fields.boolean('Use external authentication providers, sign in with google, facebook, ...'), 'module_base_import': fields.boolean("Allow users to import data from CSV files"), 'module_google_drive': fields.boolean('Attach a google document to any record', - help="""This installs the module google_docs."""), + help="""This installs the module google_docs."""), } def open_company(self, cr, uid, ids, context=None): diff --git a/addons/google_drive/__openerp__.py b/addons/google_drive/__openerp__.py index d12d442f27a..9e6c73d2649 100644 --- a/addons/google_drive/__openerp__.py +++ b/addons/google_drive/__openerp__.py @@ -44,7 +44,7 @@ Integrate google document to OpenERP record. ============================================ -This module allows you to integrate google documents to any of your OpenERP record quickly and easily using OAuth 2.0 for Installed Applications, +This module allows you to integrate google documents to any of your OpenERP record quickly and easily using OAuth 2.0 for Installed Applications, You can configure your google Authorization Code from Settings > Configuration > General Settings by clicking on "Generate Google Authorization Code" """ } diff --git a/addons/google_drive/google_drive.py b/addons/google_drive/google_drive.py index 02045b06c7c..61e64f011f5 100644 --- a/addons/google_drive/google_drive.py +++ b/addons/google_drive/google_drive.py @@ -18,9 +18,7 @@ # ############################################################################## import logging -from datetime import datetime -from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT from openerp import SUPERUSER_ID from openerp.osv import fields, osv from openerp.tools.translate import _ @@ -32,10 +30,11 @@ import json _logger = logging.getLogger(__name__) + class config(osv.osv): _name = 'google.drive.config' _description = "Google Drive templates config" - + def get_google_doc_name(self, cr, uid, ids, res_id, tamplate_id, context=None): pool_model = self.pool.get("ir.model") res = {} @@ -48,13 +47,13 @@ class config(osv.osv): 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}) + 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 @@ -65,51 +64,51 @@ class config(osv.osv): url = self.copy_doc(cr, uid, ids, res_id, tamplate_id, name_gdocs, res_model, context) res[config.id] = {'url': url} return res - + def copy_doc(self, cr, uid, ids, res_id, tamplate_id, name_gdocs, res_model, context=None): - ir_config = self.pool[ 'ir.config_parameter'] + ir_config = self.pool['ir.config_parameter'] google_drive_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_client_id') google_drive_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_client_secret') google_drive_refresh_token = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_refresh_token') google_web_base_url = ir_config.get_param(cr, SUPERUSER_ID, 'web.base.url') - - #For Getting New Access Token With help of old Refresh Token + + #For Getting New Access Token With help of old Refresh Token headers = {"Content-type": "application/x-www-form-urlencoded"} - data = dict(client_id = google_drive_client_id, - refresh_token = google_drive_refresh_token, - client_secret = google_drive_client_secret, - grant_type = "refresh_token") - data = urllib.urlencode(data) + data = dict(client_id=google_drive_client_id, + refresh_token=google_drive_refresh_token, + client_secret=google_drive_client_secret, + grant_type="refresh_token") + data = urllib.urlencode(data) resp, content = Http().request("https://accounts.google.com/o/oauth2/token", "POST", data, headers) content = json.loads(content) - + # Copy template in to drive with help of new access token - if content.has_key('access_token'): + if 'access_token' in content: request_url = "https://www.googleapis.com/drive/v2/files/%s?fields=parents/id&access_token=%s" % (tamplate_id, content['access_token']) resp, parents = Http().request(request_url, "GET") parents_dict = json.loads(parents) headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} - record_url = "Click on link to open Record in OpenERP\n %s/?db=%s#id=%s&model=%s" %(google_web_base_url, cr.dbname, res_id, res_model ) - data = {"title": name_gdocs, "description": record_url, "parents":parents_dict['parents']} + record_url = "Click on link to open Record in OpenERP\n %s/?db=%s#id=%s&model=%s" % (google_web_base_url, cr.dbname, res_id, res_model) + data = {"title": name_gdocs, "description": record_url, "parents": parents_dict['parents']} request_url = "https://www.googleapis.com/drive/v2/files/%s/copy?access_token=%s" % (tamplate_id, content['access_token']) resp, content = Http().request(request_url, "POST", json.dumps(data), headers) content = json.loads(content) res = False if 'alternateLink' in content.keys(): attach_pool = self.pool.get("ir.attachment") - attach_vals = {'res_model': res_model, 'name': name_gdocs, 'res_id': res_id, 'type': 'url', 'url':content['alternateLink']} + attach_vals = {'res_model': res_model, 'name': name_gdocs, 'res_id': res_id, 'type': 'url', 'url': content['alternateLink']} attach_pool.create(cr, uid, attach_vals) res = content['alternateLink'] else: raise self.pool.get('res.config.settings').get_config_warning(cr, _("You haven't configured 'Authorization Code' generated from google, Please generate and configure it in %(menu:base_setup.menu_general_configuration)s."), context=context) return res - + def get_google_drive_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 + 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 @@ -122,21 +121,20 @@ class config(osv.osv): # 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): + 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: + 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.")) @@ -146,11 +144,11 @@ class config(osv.osv): 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() - + def _resource_get(self, cr, uid, ids, name, arg, context=None): result = {} for data in self.browse(cr, uid, ids, context): @@ -158,7 +156,6 @@ class config(osv.osv): try: url = urlparse(template_url) res = url.path.split('/') - resource = res[1] if res[1] == "spreadsheet": key = url.query.split('=')[1] else: @@ -167,7 +164,7 @@ class config(osv.osv): except: raise osv.except_osv(_('Incorrect URL!'), _("Please enter a valid URL.")) return result - + def _client_id_get(self, cr, uid, ids, name, arg, context=None): result = {} for config_id in ids: @@ -176,27 +173,27 @@ class config(osv.osv): return result _columns = { - 'name' : fields.char('Template Name', required=True, size=1024), + 'name': fields.char('Template Name', required=True, size=1024), 'model_id': fields.selection(_list_all_models, 'Model', required=True), - 'filter_id' : fields.many2one('ir.filters', 'Filter'), + 'filter_id': fields.many2one('ir.filters', 'Filter'), 'google_drive_template_url': fields.char('Template URL', required=True, size=1024), - 'google_drive_resource_id' : fields.function(_resource_get, type="char" , string='Resource Id'), - 'google_drive_client_id' : fields.function(_client_id_get, type="char" , string='Google Client '), + 'google_drive_resource_id': fields.function(_resource_get, type="char", string='Resource Id'), + 'google_drive_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), } def onchange_model_id(self, cr, uid, ids, model_id, context=None): - res = {'domain':{'filter_id':[]}} - if model_id: + res = {'domain': {'filter_id': []}} + if model_id: res['domain'] = {'filter_id': [('model_id', '=', model_id)]} - else: - res['value'] = {'filter_id': False} - return res + else: + res['value'] = {'filter_id': False} + return res _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: