From 6cc272049cead346d963e52e2437b54e39fa06e0 Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Mon, 25 Feb 2013 14:46:39 +0530 Subject: [IMP]Renaming google docs to google drive bzr revid: dhr@tinyerp.com-20130225091639-s0m5xd110yozak0q --- addons/google_docs/google_docs.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index cdb14de1de3..d2e746c33e7 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -126,7 +126,7 @@ class google_docs_ir_attachment(osv.osv): #copy the document you choose in the configuration copy_resource = client.copy_resource(doc, name_gdocs) except: - raise osv.except_osv(_('Google Docs Error!'), _("Your resource id is not correct. You can find the id in the google docs URL.")) + raise osv.except_osv(_('Google Drive Error!'), _("Your resource id is not correct. You can find the id in the google docs URL.")) # create an ir.attachment self.create(cr, uid, { 'res_model': res_model, @@ -150,7 +150,7 @@ class google_docs_ir_attachment(osv.osv): :return: the google document object created ''' if len(ids) != 1: - raise osv.except_osv(_('Google Docs Error!'), _("Creating google docs may only be done by one at a time.")) + 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') @@ -164,7 +164,7 @@ class google_docs_ir_attachment(osv.osv): try: name_gdocs = name_gdocs % model_fields_dic except: - raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object.")) + 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: @@ -173,7 +173,7 @@ class google_docs_ir_attachment(osv.osv): class config(osv.osv): _name = 'google.docs.config' - _description = "Google Docs templates config" + _description = "Google Drive templates config" _columns = { 'model_id': fields.many2one('ir.model', 'Model', required=True), @@ -185,7 +185,7 @@ This is the id of the template document, on google side. You can find it thanks *for a drawing document with url like `https://docs.google.com/a/openerp.com/drawings/d/123456789/edit`, the ID is `drawings:123456789` ... ''', required=True), - 'name_template': fields.char('Google Doc Name Pattern', size=64, help='Choose how the new google docs will be named, on google side. Eg. gdoc_%(field_name)s', required=True), + '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), } _defaults = { -- cgit v1.2.3 From f06b26f4d591cab554a8939ed3a54928c0ba66e3 Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Tue, 26 Feb 2013 11:25:25 +0530 Subject: [IMP]Creating a tempalate for automatically creating a doc bzr revid: dhr@tinyerp.com-20130226055525-dvrlc9siy0cb8knl --- addons/google_docs/google_docs.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index d2e746c33e7..9aaa5b41b19 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -94,7 +94,7 @@ class google_docs_ir_attachment(osv.osv): 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.DOCUMENT_LABEL,title=title) + 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 @@ -177,14 +177,18 @@ class config(osv.osv): _columns = { 'model_id': fields.many2one('ir.model', 'Model', required=True), - 'gdocs_resource_id': fields.char('Google Resource ID to Use as Template', size=64, help=''' -This is the id of the template document, on google side. You can find it thanks to its URL: -*for a text document with url like `https://docs.google.com/a/openerp.com/document/d/123456789/edit`, the ID is `document:123456789` -*for a spreadsheet document with url like `https://docs.google.com/a/openerp.com/spreadsheet/ccc?key=123456789#gid=0`, the ID is `spreadsheet:123456789` -*for a presentation (slide show) document with url like `https://docs.google.com/a/openerp.com/presentation/d/123456789/edit#slide=id.p`, the ID is `presentation:123456789` -*for a drawing document with url like `https://docs.google.com/a/openerp.com/drawings/d/123456789/edit`, the ID is `drawings:123456789` -... -''', required=True), +# 'gdocs_resource_id': fields.char('Google Resource ID to Use as Template', size=64, help=''' +#This is the id of the template document, on google side. You can find it thanks to its URL: +#*for a text document with url like `https://docs.google.com/a/openerp.com/document/d/123456789/edit`, the ID is `document:123456789` +#*for a spreadsheet document with url like `https://docs.google.com/a/openerp.com/spreadsheet/ccc?key=123456789#gid=0`, the ID is `spreadsheet:123456789` +#*for a presentation (slide show) document with url like `https://docs.google.com/a/openerp.com/presentation/d/123456789/edit#slide=id.p`, the ID is `presentation:123456789` +#*for a drawing document with url like `https://docs.google.com/a/openerp.com/drawings/d/123456789/edit`, the ID is `drawings:123456789` +#... +#''', required=True), + 'gdocs_resource_id': fields.selection([('document', 'Document'), + ('spreadsheet', 'Spreadsheet'), + ('presentation', 'Presentation')], 'Type of Drive as template', + required=True, help="""Select here the type of drive you want as a template."""), '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), } -- cgit v1.2.3 From 2154cdbd066596cb1b3f555317331433e7e2b277 Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Thu, 28 Feb 2013 12:22:50 +0530 Subject: [IMP]Reverted google doc configuration from documents menu as per new spec bzr revid: dhr@tinyerp.com-20130228065250-4v5n75cit1afhabd --- addons/google_docs/google_docs.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 9aaa5b41b19..82cd80635ee 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -177,18 +177,14 @@ class config(osv.osv): _columns = { 'model_id': fields.many2one('ir.model', 'Model', required=True), -# 'gdocs_resource_id': fields.char('Google Resource ID to Use as Template', size=64, help=''' -#This is the id of the template document, on google side. You can find it thanks to its URL: -#*for a text document with url like `https://docs.google.com/a/openerp.com/document/d/123456789/edit`, the ID is `document:123456789` -#*for a spreadsheet document with url like `https://docs.google.com/a/openerp.com/spreadsheet/ccc?key=123456789#gid=0`, the ID is `spreadsheet:123456789` -#*for a presentation (slide show) document with url like `https://docs.google.com/a/openerp.com/presentation/d/123456789/edit#slide=id.p`, the ID is `presentation:123456789` -#*for a drawing document with url like `https://docs.google.com/a/openerp.com/drawings/d/123456789/edit`, the ID is `drawings:123456789` -#... -#''', required=True), - 'gdocs_resource_id': fields.selection([('document', 'Document'), - ('spreadsheet', 'Spreadsheet'), - ('presentation', 'Presentation')], 'Type of Drive as template', - required=True, help="""Select here the type of drive you want as a template."""), + 'gdocs_resource_id': fields.char('Google Resource ID to Use as Template', size=64, help=''' +This is the id of the template document, on google side. You can find it thanks to its URL: +*for a text document with url like `https://docs.google.com/a/openerp.com/document/d/123456789/edit`, the ID is `document:123456789` +*for a spreadsheet document with url like `https://docs.google.com/a/openerp.com/spreadsheet/ccc?key=123456789#gid=0`, the ID is `spreadsheet:123456789` +*for a presentation (slide show) document with url like `https://docs.google.com/a/openerp.com/presentation/d/123456789/edit#slide=id.p`, the ID is `presentation:123456789` +*for a drawing document with url like `https://docs.google.com/a/openerp.com/drawings/d/123456789/edit`, the ID is `drawings:123456789` +... +''', required=True), '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), } -- cgit v1.2.3 From 5b68aa7d83c25e635f5c2c44be7334e9c6dd22b1 Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Thu, 28 Feb 2013 12:58:22 +0530 Subject: [IMP]Changed the model template bzr revid: dhr@tinyerp.com-20130228072822-dxbuk1jjljcofp82 --- addons/google_docs/google_docs.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 82cd80635ee..e507ce33b16 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -177,14 +177,8 @@ class config(osv.osv): _columns = { 'model_id': fields.many2one('ir.model', 'Model', required=True), - 'gdocs_resource_id': fields.char('Google Resource ID to Use as Template', size=64, help=''' -This is the id of the template document, on google side. You can find it thanks to its URL: -*for a text document with url like `https://docs.google.com/a/openerp.com/document/d/123456789/edit`, the ID is `document:123456789` -*for a spreadsheet document with url like `https://docs.google.com/a/openerp.com/spreadsheet/ccc?key=123456789#gid=0`, the ID is `spreadsheet:123456789` -*for a presentation (slide show) document with url like `https://docs.google.com/a/openerp.com/presentation/d/123456789/edit#slide=id.p`, the ID is `presentation:123456789` -*for a drawing document with url like `https://docs.google.com/a/openerp.com/drawings/d/123456789/edit`, the ID is `drawings:123456789` -... -''', required=True), + 'filter_id' : fields.many2one('ir.filters', 'Filter'), + 'template_url': fields.char('Template Url', required=True, size=1024), '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), } -- cgit v1.2.3 From 62fc309e0c6a91cf97956ff00bf423d930b81840 Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Mon, 4 Mar 2013 10:50:58 +0530 Subject: [IMP]Putting a domain of filter to see only filters of the choosen model. bzr revid: dhr@tinyerp.com-20130304052058-fzs7a6ct9d1xb5so --- addons/google_docs/google_docs.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index e507ce33b16..73f4bca3664 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -182,6 +182,17 @@ 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): + 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} + return res + _defaults = { 'name_template': 'gdoc_%(name)s', } -- cgit v1.2.3 From 7db017fb82963d5309f8aa86b2f48ae0a6190e2c Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Tue, 5 Mar 2013 18:47:57 +0530 Subject: [IMP]Making gdoc_resource_id as a function field and Checking for the google doc template and creating while creating and editing any record matching the filter criteria bzr revid: dhr@tinyerp.com-20130305131757-sseqparoc0j2m9ax --- addons/google_docs/google_docs.py | 142 +++++++++++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 2 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 73f4bca3664..099890665f8 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -21,8 +21,10 @@ 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 _ +from urlparse import urlparse _logger = logging.getLogger(__name__) @@ -174,11 +176,27 @@ class google_docs_ir_attachment(osv.osv): class config(osv.osv): _name = 'google.docs.config' _description = "Google Drive templates config" + + def _resource_get(self, cr, uid, ids, name, arg, context=None): + result = {} + for data in self.browse(cr, uid, ids, context): + template_url = data.gdocs_template_url + url = urlparse(template_url) + res = url.path.split('/') + resource = res[1] + if res[1]== "spreadsheet": + key = url.query.split('=')[1] + else: + key = res[3] + res_id = resource + ":" + key + result[data.id] = str(res_id) + return result _columns = { 'model_id': fields.many2one('ir.model', 'Model', required=True), 'filter_id' : fields.many2one('ir.filters', 'Filter'), - 'template_url': fields.char('Template Url', required=True, size=1024), + 'gdocs_template_url': fields.char('Template Url', required=True, size=1024), + 'gdocs_resource_id' : fields.function(_resource_get,type="char" ,string='Resource Id',store=True), '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), } @@ -194,6 +212,126 @@ class config(osv.osv): return res _defaults = { - 'name_template': 'gdoc_%(name)s', + '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) +# +# # 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) + 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 + config() -- cgit v1.2.3 From bb517b020f922048ab3eef8345018e9959543e4a Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Wed, 6 Mar 2013 11:01:38 +0530 Subject: [IMP]Improved demo data bzr revid: dhr@tinyerp.com-20130306053138-nf7hfjxb8ujqyic3 --- addons/google_docs/google_docs.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 099890665f8..c41f1b5e82a 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -262,7 +262,8 @@ class config(osv.osv): 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) - name_gdocs = name_gdocs.replace('filter',action.filter_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: -- cgit v1.2.3 From 5882be5747dc1cb68c2b1b73ed224a36d815dee1 Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Wed, 6 Mar 2013 15:17:20 +0530 Subject: [IMP]Changed demo data,Changed the dependencies,removed knowledge from dependency, and changed access rights bzr revid: dhr@tinyerp.com-20130306094720-7r5awynvi799yvjk --- addons/google_docs/google_docs.py | 1 - 1 file changed, 1 deletion(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index c41f1b5e82a..20a88e0823a 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -231,7 +231,6 @@ class config(osv.osv): # 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): -- cgit v1.2.3 From f26fea2a09ab5841a22c0413e6d51c932cf9cac3 Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Fri, 8 Mar 2013 19:00:39 +0530 Subject: [IMP]Adding virtual entry of gdoc template for a particular record to more based on filter bzr revid: dhr@tinyerp.com-20130308133039-lme2p04ak2pviurt --- addons/google_docs/google_docs.py | 364 +++++++++++++++++++++----------------- 1 file changed, 200 insertions(+), 164 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 20a88e0823a..dd5b7f0ecc6 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -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() -- cgit v1.2.3 From a0585bf07c1a7b5995a6fe0b5ad6b5a7964c3048 Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Mon, 11 Mar 2013 18:59:29 +0530 Subject: [IMP]Adding name field and displaying message for empty list bzr revid: dhr@tinyerp.com-20130311132929-0pw8rtaxyj8ke9tx --- addons/google_docs/google_docs.py | 134 +++----------------------------------- 1 file changed, 8 insertions(+), 126 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index dd5b7f0ecc6..71d3e84a21d 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -111,7 +111,7 @@ class google_docs_ir_attachment(osv.osv): # '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): + def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs,name, gdoc_template_id, context=None): ''' copy an existing document in google docs :param res_model: the object for which the google doc is created @@ -134,7 +134,7 @@ class google_docs_ir_attachment(osv.osv): 'res_model': res_model, 'res_id': res_id, 'type': 'url', - 'name': name_gdocs, + 'name': name, 'url': copy_resource.get_alternate_link().href }, context=context) return copy_resource.resource_id.text @@ -164,9 +164,9 @@ class google_docs_ir_attachment(osv.osv): 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) + config.append(action.name) else: - config.append(action.name_template) + config.append(action.name) return config def _filt(self, cr, uid, action, action_filter, record_ids, context=None): @@ -190,11 +190,11 @@ class google_docs_ir_attachment(osv.osv): 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)]) + attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',action.name),('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)]) + self.copy_gdoc(cr, uid, action.model_id.model, ids[0], action.name_template, action.name, google_template_id, context=context) + attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',action.name)]) attachments = self.browse(cr, uid, attach_ids, context)[0] attachment['url'] = attachments.url return attachment @@ -219,6 +219,7 @@ class config(osv.osv): return result _columns = { + 'name' : fields.char('Name', required=True, size=1024), 'model_id': fields.many2one('ir.model', 'Model', required=True), 'filter_id' : fields.many2one('ir.filters', 'Filter'), 'gdocs_template_url': fields.char('Template Url', required=True, size=1024), @@ -251,123 +252,4 @@ class config(osv.osv): '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) -## # 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 - config() -- cgit v1.2.3 From 83767b0f3eb4883195b250f81c7c68516fc16201 Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Tue, 12 Mar 2013 15:02:39 +0530 Subject: [IMP]Changed code for name template bzr revid: dhr@tinyerp.com-20130312093239-l012shts2q80bd66 --- addons/google_docs/google_docs.py | 54 +++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 71d3e84a21d..b72943c750d 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -162,7 +162,6 @@ class google_docs_ir_attachment(osv.osv): 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) else: @@ -174,27 +173,38 @@ class google_docs_ir_attachment(osv.osv): 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] + config_ids = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model)], context=context)[0] action = pool_gdoc_config.browse(cr, uid, config_ids, context=context) attachment = {} + model_fields_dic = self.pool.get(res_model).read(cr, uid, res_id, [], context=context) + # check if a model is configured with a template + if config_ids: + name_gdocs = action.name_template + print 'name_gdocss',name_gdocs + try: + if name_gdocs.find('model')!=-1: + name_gdocs = name_gdocs.replace('model',action.model_id.name) + if name_gdocs.find('filter')!=-1: + name_gdocs = name_gdocs.replace('filter',action.filter_id.name) + name_gdocs = name_gdocs % model_fields_dic + except: + raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object.")) + attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',action.name),('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, action.name, google_template_id, context=context) - attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',action.name)]) + self.copy_gdoc(cr, uid, action.model_id.model, res_id, name_gdocs, action.name, google_template_id, context=context) + attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',action.name),('res_id','=',res_id)]) attachments = self.browse(cr, uid, attach_ids, context)[0] attachment['url'] = attachments.url return attachment @@ -227,26 +237,16 @@ 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,name_template): - res = {'domain':{'filter_id':[]}} - if model_id: - 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 - + def onchange_model_id(self, cr, uid, ids, model_id): + 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} + return res _defaults = { 'name_template': '%(name)s_model_filter_gdoc', -- cgit v1.2.3 From 7ba1c001491333c0ee5c6cc36728cb2ec95d9c63 Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Mon, 18 Mar 2013 17:14:03 +0530 Subject: [IMP]Changed the demo data for filter and changed the name of the newly created attachment bzr revid: dhr@tinyerp.com-20130318114403-uifxi4p9nlkbac9r --- addons/google_docs/google_docs.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index b72943c750d..945daa6c119 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -111,7 +111,7 @@ class google_docs_ir_attachment(osv.osv): # 'title': title, # 'url': gdocs_resource.get_alternate_link().href} - def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs,name, gdoc_template_id, context=None): + def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None): ''' copy an existing document in google docs :param res_model: the object for which the google doc is created @@ -134,7 +134,7 @@ class google_docs_ir_attachment(osv.osv): 'res_model': res_model, 'res_id': res_id, 'type': 'url', - 'name': name, + 'name': name_gdocs, 'url': copy_resource.get_alternate_link().href }, context=context) return copy_resource.resource_id.text @@ -190,21 +190,23 @@ class google_docs_ir_attachment(osv.osv): # check if a model is configured with a template if config_ids: name_gdocs = action.name_template - print 'name_gdocss',name_gdocs try: + name_gdocs = name_gdocs % model_fields_dic if name_gdocs.find('model')!=-1: name_gdocs = name_gdocs.replace('model',action.model_id.name) if name_gdocs.find('filter')!=-1: - name_gdocs = name_gdocs.replace('filter',action.filter_id.name) - name_gdocs = name_gdocs % model_fields_dic + if action.filter_id: + name_gdocs = name_gdocs.replace('filter',action.filter_id.name) + else: + name_gdocs = name_gdocs.replace('_filter',"") except: raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object.")) - attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',action.name),('res_id','=',res_id)]) + attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',name_gdocs),('res_id','=',res_id)]) if not attach_ids: google_template_id = action.gdocs_resource_id - self.copy_gdoc(cr, uid, action.model_id.model, res_id, name_gdocs, action.name, google_template_id, context=context) - attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',action.name),('res_id','=',res_id)]) + self.copy_gdoc(cr, uid, action.model_id.model, res_id, name_gdocs, google_template_id, context=context) + attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',name_gdocs),('res_id','=',res_id)]) attachments = self.browse(cr, uid, attach_ids, context)[0] attachment['url'] = attachments.url return attachment -- cgit v1.2.3 From 0bc08f0e361566656319347e352f3f99f8b5002a Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Tue, 19 Mar 2013 18:58:27 +0530 Subject: [IMP]Improved model configuration menu name,template field name,tooltip of module, and added field in name list view bzr revid: dhr@tinyerp.com-20130319132827-boj6dlv2oxkrut2n --- addons/google_docs/google_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 945daa6c119..020e78a90ae 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -234,7 +234,7 @@ class config(osv.osv): 'name' : fields.char('Name', required=True, size=1024), 'model_id': fields.many2one('ir.model', 'Model', required=True), 'filter_id' : fields.many2one('ir.filters', 'Filter'), - 'gdocs_template_url': fields.char('Template Url', required=True, size=1024), + 'gdocs_template_url': fields.char('Template URL', required=True, size=1024), 'gdocs_resource_id' : fields.function(_resource_get,type="char" ,string='Resource Id',store=True), '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), } -- cgit v1.2.3 From 1c251ffcf12007434f7c378916654ea14a04723e Mon Sep 17 00:00:00 2001 From: "Dharti Ratani (Open ERP)" Date: Wed, 20 Mar 2013 11:09:10 +0530 Subject: [IMP]Made changes as per the review of RPA on 19th march bzr revid: dhr@tinyerp.com-20130320053910-ywjxwwqvxjgrygia --- addons/google_docs/google_docs.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 020e78a90ae..2eabcb1f693 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -172,7 +172,8 @@ class google_docs_ir_attachment(osv.osv): """ 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" + if not action.model_id.model == 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 {}) @@ -183,7 +184,7 @@ class google_docs_ir_attachment(osv.osv): 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') - config_ids = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model)], context=context)[0] + config_ids = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model),('name','=',rec_name['label'])], context=context)[0] action = pool_gdoc_config.browse(cr, uid, config_ids, context=context) attachment = {} model_fields_dic = self.pool.get(res_model).read(cr, uid, res_id, [], context=context) @@ -219,15 +220,18 @@ class config(osv.osv): result = {} for data in self.browse(cr, uid, ids, context): template_url = data.gdocs_template_url - url = urlparse(template_url) - res = url.path.split('/') - resource = res[1] - if res[1]== "spreadsheet": - key = url.query.split('=')[1] - else: - key = res[3] - res_id = resource + ":" + key - result[data.id] = str(res_id) + try: + url = urlparse(template_url) + res = url.path.split('/') + resource = res[1] + if res[1]== "spreadsheet": + key = url.query.split('=')[1] + else: + key = res[3] + res_id = resource + ":" + key + result[data.id] = str(res_id) + except: + raise osv.except_osv(_('Incorrect URL!'), _("Please enter a valid URL.")) return result _columns = { @@ -239,7 +243,8 @@ 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,filter_id): + print 'fffffffff',filter_id res = {'domain':{'filter_id':[]}} if model_id: model_name = self.pool.get('ir.model').read(cr, uid, model_id, ['model']) @@ -248,6 +253,7 @@ class config(osv.osv): res['domain'] = {'filter_id': [('model_id', '=', mod_name)]} else: res['value'] = {'filter_id': False} + print 'rrrrrrr',res return res _defaults = { -- cgit v1.2.3 From c575ebfc4d181bbc9c2bc0cfa87f5fc6b36bd8ab Mon Sep 17 00:00:00 2001 From: "Rajesh Prajapati (OpenERP)" Date: Wed, 20 Mar 2013 13:59:56 +0530 Subject: [IMP] google_docs : constraint added bzr revid: rpr@tinyerp.com-20130320082956-yuwftz4vp6xq11au --- addons/google_docs/google_docs.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 2eabcb1f693..d487d31c0d6 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -243,17 +243,19 @@ 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,filter_id): - print 'fffffffff',filter_id + def onchange_model_id(self, cr, uid, ids, model_id, filter_id, context=None): res = {'domain':{'filter_id':[]}} if model_id: model_name = self.pool.get('ir.model').read(cr, uid, model_id, ['model']) + if filter_id: + filter = self.pool.get('ir.filters').read(cr, uid, filter_id, ['model_id']) + if not model_name['model'] == filter['model_id']: + raise osv.except_osv(_('Warning!'), _('Filter does not match with selected Model!')) if model_name: mod_name = model_name['model'] res['domain'] = {'filter_id': [('model_id', '=', mod_name)]} else: res['value'] = {'filter_id': False} - print 'rrrrrrr',res return res _defaults = { -- cgit v1.2.3 From f449ab66d9224a7f6aa9e11ded9959191e0bd218 Mon Sep 17 00:00:00 2001 From: "Rajesh Prajapati (OpenERP)" Date: Fri, 22 Mar 2013 17:58:59 +0530 Subject: [IMP] google_docs : Made changes as per the review of RPA on 21th march bzr revid: rpr@tinyerp.com-20130322122859-2s1wd5tba9u58g5f --- addons/google_docs/google_docs.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index d487d31c0d6..f88d408b253 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -235,7 +235,7 @@ class config(osv.osv): return result _columns = { - 'name' : fields.char('Name', required=True, size=1024), + 'name' : fields.char('Template Name', required=True, size=1024), 'model_id': fields.many2one('ir.model', 'Model', required=True), 'filter_id' : fields.many2one('ir.filters', 'Filter'), 'gdocs_template_url': fields.char('Template URL', required=True, size=1024), @@ -243,14 +243,10 @@ 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, filter_id, context=None): + def onchange_model_id(self, cr, uid, ids, model_id, context=None): res = {'domain':{'filter_id':[]}} if model_id: model_name = self.pool.get('ir.model').read(cr, uid, model_id, ['model']) - if filter_id: - filter = self.pool.get('ir.filters').read(cr, uid, filter_id, ['model_id']) - if not model_name['model'] == filter['model_id']: - raise osv.except_osv(_('Warning!'), _('Filter does not match with selected Model!')) if model_name: mod_name = model_name['model'] res['domain'] = {'filter_id': [('model_id', '=', mod_name)]} @@ -262,4 +258,20 @@ class config(osv.osv): 'name_template': '%(name)s_model_filter_gdoc', } + def _check_model_id(self, cr, uid, ids, context=None): + pool_id = self.browse(cr, uid, ids[0], context=context) + model_id = pool_id.model_id.id + filter_id = pool_id.filter_id.id + if model_id: + model_name = self.pool.get('ir.model').read(cr, uid, model_id, ['model']) + if filter_id: + filter = self.pool.get('ir.filters').read(cr, uid, filter_id, ['model_id']) + if model_name['model'] != filter['model_id']: + return False + return True + + _constraints = [ + (_check_model_id, 'The model or filter chosen have not matched.', ['model_id','filter_id']), + ] + config() -- cgit v1.2.3 From a6fb77926c6eb971176e8789a47eba63f80d1d5d Mon Sep 17 00:00:00 2001 From: "Rajesh Prajapati (OpenERP)" Date: Tue, 26 Mar 2013 14:56:30 +0530 Subject: [IMP] google_docs : changes for name_remplate in google template bzr revid: rpr@tinyerp.com-20130326092630-36b35ywehzttp697 --- addons/google_docs/google_docs.py | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index f88d408b253..dd2d9062080 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -188,18 +188,13 @@ class google_docs_ir_attachment(osv.osv): action = pool_gdoc_config.browse(cr, uid, config_ids, context=context) attachment = {} model_fields_dic = self.pool.get(res_model).read(cr, uid, res_id, [], context=context) + if model_fields_dic['name']: + model_fields_dic['name'] = model_fields_dic['name'].replace(' ','-') # check if a model is configured with a template if config_ids: name_gdocs = action.name_template try: name_gdocs = name_gdocs % model_fields_dic - if name_gdocs.find('model')!=-1: - name_gdocs = name_gdocs.replace('model',action.model_id.name) - if name_gdocs.find('filter')!=-1: - if action.filter_id: - name_gdocs = name_gdocs.replace('filter',action.filter_id.name) - else: - name_gdocs = name_gdocs.replace('_filter',"") except: raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object.")) @@ -234,14 +229,37 @@ class config(osv.osv): raise osv.except_osv(_('Incorrect URL!'), _("Please enter a valid URL.")) return result + def _name_google_template_get(self, cr, uid, ids, name, arg, context=None): + res = {} + res_id = ids[0] + doc_pool = self.browse(cr, uid, ids[0], context) + label = doc_pool.name + res_model = doc_pool.model_id + res_fields_dic = self.pool.get(res_model.model).read(cr, uid, res_id, [], context=context) + doc_ids = self.search(cr, uid, [('model_id', '=', res_model.model),('name','=',label)], context=context)[0] + act = self.browse(cr, uid, doc_ids, context=context) + if doc_ids: + name_temp = act.name_template + if name_temp.find('model')!=-1: + name_temp = name_temp.replace('model',act.model_id.name) + res[doc_pool.id] = name_temp + if name_temp.find('filter')!=-1: + if act.filter_id: + name_temp = name_temp.replace('filter',act.filter_id.name) + res[doc_pool.id] = name_temp + else: + name_temp = name_temp.replace('_filter',"") + res[doc_pool.id] = name_temp + return res + _columns = { 'name' : fields.char('Template Name', required=True, size=1024), 'model_id': fields.many2one('ir.model', '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',store=True), - '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), - } + 'name_template': fields.function(_name_google_template_get,type="char", string='Google Drive Name Pattern', help='Choose how the new google drive will be named, on google side. Eg. gdoc_%(field_name)s',store=True), + } def onchange_model_id(self, cr, uid, ids, model_id, context=None): res = {'domain':{'filter_id':[]}} -- cgit v1.2.3 From f3d3de20de66a798d928ee31afd462266a79ef46 Mon Sep 17 00:00:00 2001 From: "Rucha (Open ERP)" Date: Thu, 28 Mar 2013 12:42:12 +0530 Subject: [REF]: google_docs: Improved code for constraint bzr revid: rpa@tinyerp.com-20130328071212-kaim6d0oblhhq1a5 --- addons/google_docs/google_docs.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index dd2d9062080..c33fbbde616 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -277,19 +277,13 @@ class config(osv.osv): } def _check_model_id(self, cr, uid, ids, context=None): - pool_id = self.browse(cr, uid, ids[0], context=context) - model_id = pool_id.model_id.id - filter_id = pool_id.filter_id.id - if model_id: - model_name = self.pool.get('ir.model').read(cr, uid, model_id, ['model']) - if filter_id: - filter = self.pool.get('ir.filters').read(cr, uid, filter_id, ['model_id']) - if model_name['model'] != filter['model_id']: - return False + config_id = self.browse(cr, uid, ids[0], context=context) + if config_id.filter_id.id and config_id.model_id.model != config_id.filter_id.model_id: + return False return True _constraints = [ - (_check_model_id, 'The model or filter chosen have not matched.', ['model_id','filter_id']), + (_check_model_id, 'Model of selected filter is not matching with model of current template.', ['model_id','filter_id']), ] config() -- cgit v1.2.3 From deb23a62b75e332e0b6a6f24df8b8c5d704ffc98 Mon Sep 17 00:00:00 2001 From: "Rajesh Prajapati (OpenERP)" Date: Mon, 1 Apr 2013 13:33:23 +0530 Subject: [IMP] google_docs : changes for code and rpa review 28 march bzr revid: rpr@tinyerp.com-20130401080323-vdw9rgdvzi0qyqrf --- addons/google_docs/google_docs.py | 138 +++++++++++++------------------------- 1 file changed, 46 insertions(+), 92 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index c33fbbde616..3894eb7f318 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -86,31 +86,6 @@ 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 copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None): ''' copy an existing document in google docs @@ -139,7 +114,7 @@ class google_docs_ir_attachment(osv.osv): }, context=context) return copy_resource.resource_id.text - def google_doc_get(self, cr, uid, res_model, ids, context=None): + 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 @@ -149,30 +124,28 @@ class google_docs_ir_attachment(osv.osv): :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 google document object created + :return: the config id and config name ''' - if len(ids) != 1: + if not res_id: raise osv.except_osv(_('Google Drive Error!'), _("Creating google drive may only be done by one at a time.")) - res_id = ids[0] 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 = [] - 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) + configs = [] + for config in pool_gdoc_config.browse(cr, uid, config_ids, context=context): + if config.filter_id: + google_doc_configs = self._filter(cr, uid, config, config.filter_id, res_id, context=context) if google_doc_configs: - config.append(action.name) + configs.append({'id': config.id, 'name': config.name}) else: - config.append(action.name) - return config + configs.append({'id': config.id, 'name': config.name}) + return configs - def _filt(self, cr, uid, action, action_filter, record_ids, context=None): + 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.model == action_filter.model_id: + 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) @@ -181,36 +154,43 @@ class google_docs_ir_attachment(osv.osv): 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] + def get_google_attachment(self, cr, uid, config_id, res_id, context=None): pool_gdoc_config = self.pool.get('google.docs.config') - config_ids = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model),('name','=',rec_name['label'])], context=context)[0] - action = pool_gdoc_config.browse(cr, uid, config_ids, context=context) - attachment = {} - model_fields_dic = self.pool.get(res_model).read(cr, uid, res_id, [], context=context) - if model_fields_dic['name']: - model_fields_dic['name'] = model_fields_dic['name'].replace(' ','-') - # check if a model is configured with a template - if config_ids: - name_gdocs = action.name_template + pool_model = self.pool.get("ir.model") + attachment = {'url': False} + config = pool_gdoc_config.browse(cr, uid, config_id, context=context) + if config: + res_model = config.model_id + model_ids = pool_model.search(cr, uid, [('model','=',res_model)]) + if not model_ids: + return attachment + model = pool_model.browse(cr, uid, model_ids[0], context=context) + record = self.pool.get(res_model).browse(cr, uid, res_id, context=context) + filter_name = config.filter_id and config.filter_id.name or False + name_gdocs = config.name_template or "%(name)s_%(model)s_%(filter)s_gdrive" try: - name_gdocs = name_gdocs % model_fields_dic + name_gdocs = name_gdocs % {'name': record.name, 'model': model.name, 'filter': filter_name} except: raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object.")) - - attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',name_gdocs),('res_id','=',res_id)]) - if not attach_ids: - google_template_id = action.gdocs_resource_id - self.copy_gdoc(cr, uid, action.model_id.model, res_id, name_gdocs, google_template_id, context=context) + attach_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',name_gdocs),('res_id','=',res_id)]) - attachments = self.browse(cr, uid, attach_ids, context)[0] - attachment['url'] = attachments.url + if not attach_ids: + google_template_id = config.gdocs_resource_id + attach_id = self.copy_gdoc(cr, uid, config.model_id, res_id, name_gdocs, google_template_id, context=context) + else: + attach_id = attach_ids[0] + attachments = self.browse(cr, uid, attach_id, context) + attachment['url'] = attachments.url return attachment class config(osv.osv): _name = 'google.docs.config' _description = "Google Drive templates config" + def _list_all_models(self, cr, uid, context=None): + cr.execute("SELECT model, name from ir_model") + return cr.fetchall() + def _resource_get(self, cr, uid, ids, name, arg, context=None): result = {} for data in self.browse(cr, uid, ids, context): @@ -229,56 +209,30 @@ class config(osv.osv): raise osv.except_osv(_('Incorrect URL!'), _("Please enter a valid URL.")) return result - def _name_google_template_get(self, cr, uid, ids, name, arg, context=None): - res = {} - res_id = ids[0] - doc_pool = self.browse(cr, uid, ids[0], context) - label = doc_pool.name - res_model = doc_pool.model_id - res_fields_dic = self.pool.get(res_model.model).read(cr, uid, res_id, [], context=context) - doc_ids = self.search(cr, uid, [('model_id', '=', res_model.model),('name','=',label)], context=context)[0] - act = self.browse(cr, uid, doc_ids, context=context) - if doc_ids: - name_temp = act.name_template - if name_temp.find('model')!=-1: - name_temp = name_temp.replace('model',act.model_id.name) - res[doc_pool.id] = name_temp - if name_temp.find('filter')!=-1: - if act.filter_id: - name_temp = name_temp.replace('filter',act.filter_id.name) - res[doc_pool.id] = name_temp - else: - name_temp = name_temp.replace('_filter',"") - res[doc_pool.id] = name_temp - return res - _columns = { 'name' : fields.char('Template Name', required=True, size=1024), - 'model_id': fields.many2one('ir.model', 'Model', required=True), + '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',store=True), - 'name_template': fields.function(_name_google_template_get,type="char", string='Google Drive Name Pattern', help='Choose how the new google drive will be named, on google side. Eg. gdoc_%(field_name)s',store=True), + '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':[]}} + res = {} 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)]} + res['domain'] = {'filter_id': [('model_id', '=', model_id), ('user_id','=',False)]} else: - res['value'] = {'filter_id': False} + res['value'] = {'filter_id': [('user_id','=',False)]} return res _defaults = { - 'name_template': '%(name)s_model_filter_gdoc', + '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.model != config_id.filter_id.model_id: + if config_id.filter_id.id and config_id.model_id != config_id.filter_id.model_id: return False return True -- cgit v1.2.3 From d241e853ca60cdfe3758113cd6019eab541c4955 Mon Sep 17 00:00:00 2001 From: "Rajesh Prajapati (OpenERP)" Date: Mon, 1 Apr 2013 15:01:52 +0530 Subject: [IMP] google_docs : changes for user field in filters bzr revid: rpr@tinyerp.com-20130401093152-02ux70o02dxp5a2a --- addons/google_docs/google_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 3894eb7f318..999e4b29a29 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -188,7 +188,7 @@ class config(osv.osv): _description = "Google Drive templates config" def _list_all_models(self, cr, uid, context=None): - cr.execute("SELECT model, name from ir_model") + 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): -- cgit v1.2.3 From cc69f7e2e2de7f33a4848a9ef1c3a58dad18437e Mon Sep 17 00:00:00 2001 From: "Rajesh Prajapati (OpenERP)" Date: Wed, 3 Apr 2013 14:34:04 +0530 Subject: [IMP] google_docs : changes for tooltip and name_template bzr revid: rpr@tinyerp.com-20130403090404-skf3m78u0ervtg6y --- addons/google_docs/google_docs.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 999e4b29a29..5a72febee8e 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -164,12 +164,13 @@ class google_docs_ir_attachment(osv.osv): model_ids = pool_model.search(cr, uid, [('model','=',res_model)]) if not model_ids: return attachment - model = pool_model.browse(cr, uid, model_ids[0], context=context) - record = self.pool.get(res_model).browse(cr, uid, res_id, context=context) + model = pool_model.browse(cr, uid, model_ids[0], context=context).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,'filter':filter_name}) name_gdocs = config.name_template or "%(name)s_%(model)s_%(filter)s_gdrive" try: - name_gdocs = name_gdocs % {'name': record.name, 'model': model.name, 'filter': filter_name} + name_gdocs = name_gdocs % record except: raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object.")) -- cgit v1.2.3 From b25b6cb314e1db8958958e2b88cea3c35643cfa6 Mon Sep 17 00:00:00 2001 From: "Rajesh Prajapati (OpenERP)" Date: Wed, 3 Apr 2013 18:30:54 +0530 Subject: [IMP] google_docs : changes for warning if google account not configured bzr revid: rpr@tinyerp.com-20130403130054-4s77waguv2vp8ih0 --- addons/google_docs/google_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 5a72febee8e..63b78cf1f75 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -82,7 +82,7 @@ class google_docs_ir_attachment(osv.osv): #login gmail account client = google_pool.google_login(user_config['user'], user_config['password'], type='docs_client', context=context) if not client: - raise osv.except_osv(_('Google Docs Error!'), _("Check your google configuration in Users/Users/Synchronization tab.")) + raise osv.except_osv(_('Google Drive Error!'), _("Check your google account configuration in Users/Users/Google Account.")) _logger.info('Logged into google docs as %s', user_config['user']) return client -- cgit v1.2.3 From 811e51f362228c89bac8d7f438a3dbd66e82941c Mon Sep 17 00:00:00 2001 From: "Rajesh Prajapati (OpenERP)" Date: Thu, 4 Apr 2013 12:35:23 +0530 Subject: [IMP] google_docs : changes for google template creation for user bzr revid: rpr@tinyerp.com-20130404070523-3wt0hcot93jw6fop --- addons/google_docs/google_docs.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 63b78cf1f75..824a9ddf06b 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -132,8 +132,11 @@ class google_docs_ir_attachment(osv.osv): # 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, uid, config_ids, context=context): + 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}) @@ -158,7 +161,7 @@ class google_docs_ir_attachment(osv.osv): pool_gdoc_config = self.pool.get('google.docs.config') pool_model = self.pool.get("ir.model") attachment = {'url': False} - config = pool_gdoc_config.browse(cr, uid, config_id, context=context) + config = pool_gdoc_config.browse(cr, SUPERUSER_ID, config_id, context=context) if config: res_model = config.model_id model_ids = pool_model.search(cr, uid, [('model','=',res_model)]) @@ -220,11 +223,11 @@ class config(osv.osv): } def onchange_model_id(self, cr, uid, ids, model_id, context=None): - res = {} + res = {'domain':{'filter_id':[]}} if model_id: - res['domain'] = {'filter_id': [('model_id', '=', model_id), ('user_id','=',False)]} + res['domain'] = {'filter_id': [('model_id', '=', model_id)]} else: - res['value'] = {'filter_id': [('user_id','=',False)]} + res['value'] = {'filter_id': False} return res _defaults = { -- cgit v1.2.3 From bedf4120883af69c0b1b16c5e3ed0cd1b102aa81 Mon Sep 17 00:00:00 2001 From: "Rajesh Prajapati (OpenERP)" Date: Thu, 4 Apr 2013 15:26:19 +0530 Subject: [IMP] google_docs : changes for tooltip of filter bzr revid: rpr@tinyerp.com-20130404095619-lrvwxdc3w42pdaki --- addons/google_docs/google_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 824a9ddf06b..b81b69b7e44 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -161,7 +161,7 @@ class google_docs_ir_attachment(osv.osv): pool_gdoc_config = self.pool.get('google.docs.config') pool_model = self.pool.get("ir.model") attachment = {'url': False} - config = pool_gdoc_config.browse(cr, SUPERUSER_ID, config_id, context=context) + config = pool_gdoc_config.browse(cr, uid, config_id, context=context) if config: res_model = config.model_id model_ids = pool_model.search(cr, uid, [('model','=',res_model)]) -- cgit v1.2.3 From 4ed6734b82f199952f672a812478b283342125ed Mon Sep 17 00:00:00 2001 From: "Rajesh Prajapati (OpenERP)" Date: Thu, 4 Apr 2013 15:37:59 +0530 Subject: [IMP] google_docs :change for user bzr revid: rpr@tinyerp.com-20130404100759-5h0olj38rpkxhj6g --- addons/google_docs/google_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index b81b69b7e44..824a9ddf06b 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -161,7 +161,7 @@ class google_docs_ir_attachment(osv.osv): pool_gdoc_config = self.pool.get('google.docs.config') pool_model = self.pool.get("ir.model") attachment = {'url': False} - config = pool_gdoc_config.browse(cr, uid, config_id, context=context) + config = pool_gdoc_config.browse(cr, SUPERUSER_ID, config_id, context=context) if config: res_model = config.model_id model_ids = pool_model.search(cr, uid, [('model','=',res_model)]) -- cgit v1.2.3 From 32352d06953c5b64c2aa91002262ae5efcb5ecbf Mon Sep 17 00:00:00 2001 From: "Parth Gajjar (Open ERP)" Date: Thu, 9 May 2013 19:40:01 +0530 Subject: [FiX] added OAuth , google account login through OAuth bzr revid: pga@tinyerp.com-20130509141001-s9gtsdfnwxlgvsls --- addons/google_docs/google_docs.py | 149 ++++++++++---------------------------- 1 file changed, 39 insertions(+), 110 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 138d7afe44f..69188fe8c85 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -28,92 +28,9 @@ from urlparse import urlparse _logger = logging.getLogger(__name__) -try: - import gdata.docs.data - import gdata.docs.client - - # API breakage madness in the gdata API - those guys are insane. - try: - # gdata 2.0.15+ - gdata.docs.client.DocsClient.copy_resource - except AttributeError: - # gdata 2.0.14- : copy_resource() was copy() - gdata.docs.client.DocsClient.copy_resource = gdata.docs.client.DocsClient.copy - - try: - # gdata 2.0.16+ - gdata.docs.client.DocsClient.get_resource_by_id - except AttributeError: - try: - # gdata 2.0.15+ - gdata.docs.client.DocsClient.get_resource_by_self_link - def get_resource_by_id_2_0_16(self, resource_id, **kwargs): - return self.GetResourceBySelfLink( - gdata.docs.client.RESOURCE_FEED_URI + ('/%s' % resource_id), **kwargs) - gdata.docs.client.DocsClient.get_resource_by_id = get_resource_by_id_2_0_16 - except AttributeError: - # gdata 2.0.14- : alias get_resource_by_id() - gdata.docs.client.DocsClient.get_resource_by_id = gdata.docs.client.DocsClient.get_doc - - try: - import atom.http_interface - _logger.info('GData lib version `%s` detected' % atom.http_interface.USER_AGENT) - except (ImportError, AttributeError): - _logger.debug('GData lib version could not be detected', exc_info=True) - -except ImportError: - _logger.warning("Please install latest gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list") - - class google_docs_ir_attachment(osv.osv): _inherit = 'ir.attachment' - - def _auth(self, cr, uid, context=None): - ''' - Connexion with google base account - @return client object for connexion - ''' - #pool the google.login in google_base_account - google_pool = self.pool.get('google.login') - #get gmail password and login. We use default_get() instead of a create() followed by a read() on the - # google.login object, because it is easier. The keys 'user' and 'password' ahve to be passed in the dict - # but the values will be replaced by the user gmail password and login. - user_config = google_pool.default_get(cr, uid, {'user' : '' , 'password' : ''}, context=context) - #login gmail account - client = google_pool.google_login(user_config['user'], user_config['password'], type='docs_client', context=context) - if not client: - raise osv.except_osv(_('Google Drive Error!'), _("Check your google account configuration in Users/Users/Google Account.")) - _logger.info('Logged into google docs as %s', user_config['user']) - return client - - def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None): - ''' - copy an existing document in google docs - :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 - :param name_gdocs: the name of the future ir.attachment that will be created. Based on the google doc template foun. - :param gdoc_template_id: the id of the google doc document to copy - :return: the ID of the google document object created - ''' - #login with the base account google module - client = self._auth(cr, uid) - # fetch and copy the original document - try: - doc = client.get_resource_by_id(gdoc_template_id) - #copy the document you choose in the configuration - copy_resource = client.copy_resource(doc, name_gdocs) - except: - raise osv.except_osv(_('Google Drive Error!'), _("Your resource id is not correct. You can find the id in the google docs URL.")) - # create an ir.attachment - self.create(cr, uid, { - 'res_model': res_model, - 'res_id': res_id, - 'type': 'url', - 'name': name_gdocs, - 'url': copy_resource.get_alternate_link().href - }, context=context) - return copy_resource.resource_id.text - + 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 @@ -157,39 +74,37 @@ class google_docs_ir_attachment(osv.osv): record_ids = model.search(cr, uid, domain, context=ctx) return record_ids - def get_google_attachment(self, cr, uid, config_id, res_id, context=None): - pool_gdoc_config = self.pool.get('google.docs.config') +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") - attachment = {'url': False} - config = pool_gdoc_config.browse(cr, SUPERUSER_ID, config_id, context=context) - if config: + 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: - return attachment - model = pool_model.browse(cr, uid, model_ids[0], context=context).name + 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,'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_ids = self.search(cr, uid, [('res_model','=',res_model),('name','=',name_gdocs),('res_id','=',res_id)]) - if not attach_ids: - google_template_id = config.gdocs_resource_id - attach_id = self.copy_gdoc(cr, uid, config.model_id, res_id, name_gdocs, google_template_id, context=context) - else: - attach_id = attach_ids[0] - attachments = self.browse(cr, uid, attach_id, context) - attachment['url'] = attachments.url - return attachment - -class config(osv.osv): - _name = 'google.docs.config' - _description = "Google Drive templates config" + + 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") @@ -207,20 +122,27 @@ class config(osv.osv): key = url.query.split('=')[1] else: key = res[3] - res_id = resource + ":" + key - result[data.id] = str(res_id) + result[data.id] = str(key) 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: + user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid) + result[config_id] = user.company_id.google_client_id + return result _columns = { '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'), 'gdocs_template_url': fields.char('Template URL', required=True, size=1024), - 'gdocs_resource_id' : fields.function(_resource_get,type="char" ,string='Resource Id',store=True), + '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), - } + } def onchange_model_id(self, cr, uid, ids, model_id, context=None): res = {'domain':{'filter_id':[]}} @@ -244,3 +166,10 @@ class config(osv.osv): ] config() + + +class res_company(osv.osv): + _inherit = "res.company" + _columns = { + 'google_client_id': fields.char('Google Client ID', size=200), + } \ No newline at end of file -- cgit v1.2.3 From ff3fded96e208f989ee35cfc714610adc0fbc6a4 Mon Sep 17 00:00:00 2001 From: "Parth Gajjar (Open ERP)" Date: Fri, 10 May 2013 12:05:01 +0530 Subject: [IMP] Improved Code bzr revid: pga@tinyerp.com-20130510063501-szrjvvh4jhzy4x7j --- addons/google_docs/google_docs.py | 82 +++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 43 deletions(-) (limited to 'addons/google_docs/google_docs.py') 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 -- cgit v1.2.3 From b4e6c2109c715c66e14e6f99446fda6da7e5b433 Mon Sep 17 00:00:00 2001 From: "Parth Gajjar (Open ERP)" Date: Wed, 22 May 2013 15:58:57 +0530 Subject: [IMP]Google client id in ir.config_parameter bzr revid: pga@tinyerp.com-20130522102857-fehfsdks1fq3zc1i --- addons/google_docs/google_docs.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index f3d3ad72198..aea5ec35037 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -126,8 +126,8 @@ class config(osv.osv): def _client_id_get(self, cr, uid, ids, name, arg, context=None): result = {} for config_id in ids: - user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid) - result[config_id] = user.company_id.google_client_id + config = self.pool['ir.config_parameter'] + result[config_id] = config.get_param(cr, SUPERUSER_ID, 'google_client_id') return result _columns = { @@ -163,9 +163,3 @@ class config(osv.osv): ] config() - -class res_company(osv.osv): - _inherit = "res.company" - _columns = { - '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 -- cgit v1.2.3 From 5b9b00631de5020bd5560fe788eab7a703815253 Mon Sep 17 00:00:00 2001 From: "Parth Gajjar (Open ERP)" Date: Thu, 23 May 2013 16:11:55 +0530 Subject: [ADD] Added OAuth integration through python, Added Documentation bzr revid: pga@tinyerp.com-20130523104155-83gmp2gucvackppv --- addons/google_docs/google_docs.py | 67 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index aea5ec35037..4059ed9117f 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -26,13 +26,17 @@ from openerp.osv import fields, osv from openerp.tools.translate import _ from urlparse import urlparse +from httplib2 import Http +import urllib +import json + _logger = logging.getLogger(__name__) 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): + def get_google_doc_name(self, cr, uid, ids, res_id, tamplate_id, context=None): pool_model = self.pool.get("ir.model") res = {} for config in self.browse(cr, SUPERUSER_ID, ids, context=context): @@ -57,7 +61,41 @@ class config(osv.osv): if attach_ids: attachment = attach_pool.browse(cr, uid, attach_ids[0], context) url = attachment.url - res[config.id] = {'name':name_gdocs, 'url': url} + else: + 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' ] + google_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_client_id') + google_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_client_secret') + google_refresh_token = ir_config.get_param(cr, SUPERUSER_ID, 'google_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 + headers = {"Content-type": "application/x-www-form-urlencoded"} + data = dict(client_id = google_client_id, + refresh_token = google_refresh_token, + client_secret = google_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 + 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} + 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_pool.create(cr, uid, attach_vals) + res = content['alternateLink'] return res def get_google_docs_config(self, cr, uid, res_model, res_id, context=None): @@ -163,3 +201,28 @@ class config(osv.osv): ] config() + +class res_users(osv.osv): + _inherit = "res.users" + + def onchange_authorization_code(self, cr, uid, ids, authorization_code, context=None): + res = {} + if authorization_code: + ir_config = self.pool['ir.config_parameter'] + google_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_client_id') + google_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_client_secret') + google_redirect_uri = ir_config.get_param(cr, SUPERUSER_ID, 'google_redirect_uri') + + #Get the Refresh Token From Google And store it in ir.config_parameter + headers = {"Content-type": "application/x-www-form-urlencoded"} + data = dict(code=authorization_code, client_id=google_client_id, client_secret=google_client_secret,redirect_uri=google_redirect_uri,grant_type="authorization_code") + data = urllib.urlencode(data) + resp, content = Http().request("https://accounts.google.com/o/oauth2/token", "POST", data,headers) + content=json.loads(content) + if 'refresh_token' in content.keys(): + ir_config.set_param(cr, uid, 'google_refresh_token', content['refresh_token']) + return res + + _columns = { + 'authorization_code': fields.char('Authorization Code', size=124), + } \ No newline at end of file -- cgit v1.2.3 From ba97e0cdc2131cdbcc9bb2739ec1670cd209be74 Mon Sep 17 00:00:00 2001 From: "Parth Gajjar (Open ERP)" Date: Thu, 23 May 2013 19:08:48 +0530 Subject: [IMP] Moved Google Authorization Code to General Setting bzr revid: pga@tinyerp.com-20130523133848-0i3j6h3rzj5a66pd --- addons/google_docs/google_docs.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 4059ed9117f..c570ac5dd33 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -202,8 +202,8 @@ class config(osv.osv): config() -class res_users(osv.osv): - _inherit = "res.users" +class base_config_settings(osv.osv): + _inherit = "base.config.settings" def onchange_authorization_code(self, cr, uid, ids, authorization_code, context=None): res = {} @@ -222,7 +222,16 @@ class res_users(osv.osv): if 'refresh_token' in content.keys(): ir_config.set_param(cr, uid, 'google_refresh_token', content['refresh_token']) return res + + def get_default_authorization_code(self, cr, uid, ids, context=None): + authorization_code = self.pool.get("ir.config_parameter").get_param(cr, uid, "authorization_code", context=context) + return {'authorization_code': authorization_code} + def set_authorization_code(self, cr, uid, ids, context=None): + config_parameters = self.pool.get("ir.config_parameter") + for record in self.browse(cr, uid, ids, context=context): + config_parameters.set_param(cr, uid, "authorization_code", record.authorization_code or '', context=context) + _columns = { - 'authorization_code': fields.char('Authorization Code', size=124), + 'authorization_code': fields.char('Google Authorization Code', size=124), } \ No newline at end of file -- cgit v1.2.3 From c1d366480df962960da227c1c6114bd68a4d90cb Mon Sep 17 00:00:00 2001 From: "Parth Gajjar (Open ERP)" Date: Fri, 24 May 2013 11:35:44 +0530 Subject: [ADD] Added extra explanation in General Setting bzr revid: pga@tinyerp.com-20130524060544-2v2q47t4gds4p6uz --- addons/google_docs/google_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index c570ac5dd33..932f8658627 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -233,5 +233,5 @@ class base_config_settings(osv.osv): config_parameters.set_param(cr, uid, "authorization_code", record.authorization_code or '', context=context) _columns = { - 'authorization_code': fields.char('Google Authorization Code', size=124), + 'authorization_code': fields.char('Paste Generated "Authorization Code" from google to here', size=124), } \ No newline at end of file -- cgit v1.2.3 From 0fec1053f3ef27da8e09d54d6ba5817ab716eb32 Mon Sep 17 00:00:00 2001 From: "Parth Gajjar (Open ERP)" Date: Wed, 29 May 2013 12:37:23 +0530 Subject: [IMP] Added alert, Changed logo bzr revid: pga@tinyerp.com-20130529070723-ruz42rqh798h4w0a --- addons/google_docs/google_docs.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 932f8658627..e97f0d929f1 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -84,18 +84,23 @@ class config(osv.osv): content = json.loads(content) # Copy template in to drive with help of new access token - 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} - 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_pool.create(cr, uid, attach_vals) - res = content['alternateLink'] + if content.has_key('access_token'): + 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} + 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_pool.create(cr, uid, attach_vals) + res = content['alternateLink'] + elif uid==1: + 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) + else: + raise osv.except_osv(_('Google Drive Configuration Error!'), _("'Authorization Code' is not configured yet, Please Contact Administrator")) return res def get_google_docs_config(self, cr, uid, res_model, res_id, context=None): -- cgit v1.2.3 From d68d2e44eb30b70266e6f11656e7bf4705b3824d Mon Sep 17 00:00:00 2001 From: "Parth Gajjar (Open ERP)" Date: Wed, 29 May 2013 13:16:29 +0530 Subject: [IMP] Improved Code bzr revid: pga@tinyerp.com-20130529074629-4x7dooxgbq5g6hdc --- addons/google_docs/google_docs.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index e97f0d929f1..98ca35e20cc 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -97,10 +97,8 @@ class config(osv.osv): 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'] - elif uid==1: - 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) else: - raise osv.except_osv(_('Google Drive Configuration Error!'), _("'Authorization Code' is not configured yet, Please Contact Administrator")) + 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_docs_config(self, cr, uid, res_model, res_id, context=None): -- cgit v1.2.3 From 0784c33c381fd7c718c1209b9759e2611fc40af3 Mon Sep 17 00:00:00 2001 From: "Parth Gajjar (Open ERP)" Date: Thu, 30 May 2013 15:16:19 +0530 Subject: [ADD] Managing Folders in Google Drive bzr revid: pga@tinyerp.com-20130530094619-nvhpyvid4sqzur9l --- addons/google_docs/google_docs.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 98ca35e20cc..93274ba6d24 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -85,9 +85,13 @@ class config(osv.osv): # Copy template in to drive with help of new access token if content.has_key('access_token'): + 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} + 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) -- cgit v1.2.3 From ed3509568d0212a976344a626892bc56b666fd57 Mon Sep 17 00:00:00 2001 From: "Turkesh Patel (Open ERP)" Date: Tue, 4 Jun 2013 11:59:48 +0530 Subject: [IMP] improve configuration warning and disription of the module bzr revid: tpa@tinyerp.com-20130604062948-vi0j5pzi5s2agjp9 --- addons/google_docs/google_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 93274ba6d24..63a3a423a34 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -102,7 +102,7 @@ class config(osv.osv): 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) + 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_docs_config(self, cr, uid, res_model, res_id, context=None): -- cgit v1.2.3 From 0bca00c42c87c093e7ff54d7542c0f7fd083253c Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Tue, 25 Jun 2013 12:58:19 +0200 Subject: [FIX]google_docs: generic function moved in base account bzr revid: dle@openerp.com-20130625105819-l8d2zu0vf4tzne2t --- addons/google_docs/google_docs.py | 48 +++++++++------------------------------ 1 file changed, 11 insertions(+), 37 deletions(-) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index 63a3a423a34..d6cfb3dfe4a 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -68,16 +68,16 @@ class config(osv.osv): def copy_doc(self, cr, uid, ids, res_id, tamplate_id, name_gdocs, res_model, context=None): ir_config = self.pool[ 'ir.config_parameter' ] - google_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_client_id') - google_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_client_secret') - google_refresh_token = ir_config.get_param(cr, SUPERUSER_ID, 'google_refresh_token') + google_docs_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_docs_client_id') + google_docs_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_docs_client_secret') + google_docs_refresh_token = ir_config.get_param(cr, SUPERUSER_ID, 'google_docs_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 headers = {"Content-type": "application/x-www-form-urlencoded"} - data = dict(client_id = google_client_id, - refresh_token = google_refresh_token, - client_secret = google_client_secret, + data = dict(client_id = google_docs_client_id, + refresh_token = google_docs_refresh_token, + client_secret = google_docs_client_secret, grant_type = "refresh_token") data = urllib.urlencode(data) resp, content = Http().request("https://accounts.google.com/o/oauth2/token", "POST", data, headers) @@ -172,7 +172,7 @@ class config(osv.osv): result = {} for config_id in ids: config = self.pool['ir.config_parameter'] - result[config_id] = config.get_param(cr, SUPERUSER_ID, 'google_client_id') + result[config_id] = config.get_param(cr, SUPERUSER_ID, 'google_docs_client_id') return result _columns = { @@ -181,7 +181,7 @@ class config(osv.osv): '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 '), + 'google_docs_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), } @@ -209,36 +209,10 @@ class config(osv.osv): config() + class base_config_settings(osv.osv): _inherit = "base.config.settings" - - def onchange_authorization_code(self, cr, uid, ids, authorization_code, context=None): - res = {} - if authorization_code: - ir_config = self.pool['ir.config_parameter'] - google_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_client_id') - google_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_client_secret') - google_redirect_uri = ir_config.get_param(cr, SUPERUSER_ID, 'google_redirect_uri') - - #Get the Refresh Token From Google And store it in ir.config_parameter - headers = {"Content-type": "application/x-www-form-urlencoded"} - data = dict(code=authorization_code, client_id=google_client_id, client_secret=google_client_secret,redirect_uri=google_redirect_uri,grant_type="authorization_code") - data = urllib.urlencode(data) - resp, content = Http().request("https://accounts.google.com/o/oauth2/token", "POST", data,headers) - content=json.loads(content) - if 'refresh_token' in content.keys(): - ir_config.set_param(cr, uid, 'google_refresh_token', content['refresh_token']) - return res - - def get_default_authorization_code(self, cr, uid, ids, context=None): - authorization_code = self.pool.get("ir.config_parameter").get_param(cr, uid, "authorization_code", context=context) - return {'authorization_code': authorization_code} - def set_authorization_code(self, cr, uid, ids, context=None): - config_parameters = self.pool.get("ir.config_parameter") - for record in self.browse(cr, uid, ids, context=context): - config_parameters.set_param(cr, uid, "authorization_code", record.authorization_code or '', context=context) - _columns = { - 'authorization_code': fields.char('Paste Generated "Authorization Code" from google to here', size=124), - } \ No newline at end of file + 'google_docs_authorization_code': fields.char('Paste Generated "Authorization Code" from google to here', size=124), + } -- cgit v1.2.3 From 3afc97f451d8550f18b7b54a08456f7d242888e8 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Tue, 25 Jun 2013 18:19:22 +0200 Subject: [FIX&DEL]google_*: remove of now useless views and code from google_base_account, and move generic function from google_docs to google_base_account bzr revid: dle@openerp.com-20130625161922-3u1m5qeqcj84rmq5 --- addons/google_docs/google_docs.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index d6cfb3dfe4a..8b0d8f53b2e 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -215,4 +215,8 @@ class base_config_settings(osv.osv): _columns = { 'google_docs_authorization_code': fields.char('Paste Generated "Authorization Code" from google to here', size=124), + 'google_docs_uri': fields.char('URI', readonly=True, help="The URL to generate the authorization code from Google"), + } + _defaults = { + 'google_docs_uri': lambda s, cr, uid, c: s._get_google_token_uri(cr, uid, 'docs', context=c), } -- cgit v1.2.3 From c74517990f1bec61e52e455be4ec12b6135015a6 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Wed, 26 Jun 2013 11:30:19 +0200 Subject: [RENAME]google_docs => google_drive bzr revid: dle@openerp.com-20130626093019-krwt5eamagmd5amu --- addons/google_docs/google_docs.py | 222 -------------------------------------- 1 file changed, 222 deletions(-) delete mode 100644 addons/google_docs/google_docs.py (limited to 'addons/google_docs/google_docs.py') diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py deleted file mode 100644 index 8b0d8f53b2e..00000000000 --- a/addons/google_docs/google_docs.py +++ /dev/null @@ -1,222 +0,0 @@ -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2012 OpenERP SA (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## -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 _ -from urlparse import urlparse - -from httplib2 import Http -import urllib -import json - -_logger = logging.getLogger(__name__) - -class config(osv.osv): - _name = 'google.docs.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 = {} - 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 - else: - 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' ] - google_docs_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_docs_client_id') - google_docs_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_docs_client_secret') - google_docs_refresh_token = ir_config.get_param(cr, SUPERUSER_ID, 'google_docs_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 - headers = {"Content-type": "application/x-www-form-urlencoded"} - data = dict(client_id = google_docs_client_id, - refresh_token = google_docs_refresh_token, - client_secret = google_docs_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'): - 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']} - 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_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_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() - - def _resource_get(self, cr, uid, ids, name, arg, context=None): - result = {} - for data in self.browse(cr, uid, ids, context): - template_url = data.gdocs_template_url - try: - url = urlparse(template_url) - res = url.path.split('/') - resource = res[1] - if res[1] == "spreadsheet": - key = url.query.split('=')[1] - else: - key = res[3] - result[data.id] = str(key) - 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: - config = self.pool['ir.config_parameter'] - result[config_id] = config.get_param(cr, SUPERUSER_ID, 'google_docs_client_id') - return result - - _columns = { - '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'), - 'gdocs_template_url': fields.char('Template URL', required=True, size=1024), - 'gdocs_resource_id' : fields.function(_resource_get, type="char" , string='Resource Id'), - 'google_docs_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': [('model_id', '=', model_id)]} - 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: - return False - return True - - _constraints = [ - (_check_model_id, 'Model of selected filter is not matching with model of current template.', ['model_id', 'filter_id']), - ] - -config() - - -class base_config_settings(osv.osv): - _inherit = "base.config.settings" - - _columns = { - 'google_docs_authorization_code': fields.char('Paste Generated "Authorization Code" from google to here', size=124), - 'google_docs_uri': fields.char('URI', readonly=True, help="The URL to generate the authorization code from Google"), - } - _defaults = { - 'google_docs_uri': lambda s, cr, uid, c: s._get_google_token_uri(cr, uid, 'docs', context=c), - } -- cgit v1.2.3