From cde742506ee92500e9cd2e4c41b041613d225442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20L=C3=B3pez=20L=C3=B3pez=20=28OpenERP=29?= Date: Wed, 29 Feb 2012 19:45:20 +0100 Subject: [PATCH] [fix] logic... bzr revid: rlo@openerp.com-20120229184520-vs679o9drs1vls4r --- addons/google_docs/google_docs.py | 97 +++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 32 deletions(-) diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py index b60acd16197..689f5522cd3 100644 --- a/addons/google_docs/google_docs.py +++ b/addons/google_docs/google_docs.py @@ -4,24 +4,25 @@ import gdata.docs.client from gdata.client import RequestError from gdata.docs.service import DOCUMENT_LABEL -class google_docs_config(osv.osv): - _name = 'google.docs.config' +class google_docs_ir_attachment(osv.osv): _inherit = 'ir.attachment' - _columns = { - 'model': fields.many2one('ir.model', 'Model'), - 'gdocs_resource_id': fields.char('Google resource ID', size=64), - 'name_template': fields.char('GDoc name template', size=64) - } + def _auth(self, cr, uid): + # check google_base_account + users_obj = self.pool.get('res.users') + user = users_obj.browse(cr, uid, [uid])[0] + if not user.gmail_user or not user.gmail_password: + return -2 - _defaults = { - 'name_template': 'Google Document' - } + # login + client = gdata.docs.client.DocsClient(source='openerp.com') + client.ssl = True + client.http_client.debug = False + client.ClientLogin(user.gmail_user, user.gmail_password, client.source, service='writely') + + return client - edit_url_template = 'https://docs.google.com/document/d/%s/edit' - prefix_gdoc_id_res = DOCUMENT_LABEL + ':' - - def copy_gdoc(self, cr, uid, model, context=None): + def create_empty_gdoc(self, cr, uid, model, context=None): #import pdb; pdb.set_trace() '''Associate a copy of the gdoc identified by 'gdocs_res_id' to the current entity. @param cr: the current row from the database cursor. @@ -35,45 +36,77 @@ class google_docs_config(osv.osv): if context==None: context={} - name_template = 'Sales order %s' - - # check google_base_account - users_obj = self.pool.get('res.users') - user = users_obj.browse(cr, uid, [uid])[0] - if not user.gmail_user or not user.gmail_password: + client = _auth(cr, uid) + if client == -2: return -2 - # create the document - client = gdata.docs.client.DocsClient(source='openerp.com') - client.ssl = True - client.http_client.debug = False - client.ClientLogin(user.gmail_user, user.gmail_password, client.source, service='writely') resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL) gdocs_resource = client.post(entry=resource, uri='https://docs.google.com/feeds/default/private/full/') return gdocs_resource + def copy_gdoc(self, cr, uid, model, gdocs_resource_id, context=None): + if context==None: + context={} + + client = _auth(cr, uid) + if client == -2: + return -2 + + # fetch and copy the original document + original_resource = client.get_resource_by_id(gdocs_resource_id) + return client.copy_resource(entry=original_resource) + + def gdoc_get(self, cr, uid, model, context=None): + google_docs_config_ref = self.pool.get('google.docs.config') + google_template_ids = google_docs_config_ref.search(cr, uid, [('model', '=', model)]) + if not google_template_ids: + # there isn't any template. Create an empty doc. + return self.create_gdoc(cr, uid, model, context) + + # otherwise, copy document from existing template + return self.copy_gdoc(cr, uid, model, google_template_ids[0].gdocs_resource_id + +class google_docs_config(osv.osv): + _name = 'google.docs.config' + + _columns = { + 'model_id': fields.many2one('ir.model', 'Model'), + 'gdocs_resource_id': fields.char('Google resource ID', size=64), + 'name_template': fields.char('GDoc name template', size=64), + 'url': fields.char('url for the template', size=122), + } + + _defaults = { + 'name_template': 'Google Document' + } + + edit_url_template = 'https://docs.google.com/document/d/%s/edit' + prefix_gdoc_id_res = DOCUMENT_LABEL + ':' + + class google_docs(osv.osv): _name = 'google.docs' - def doc_get(self, cr, uid, model, ids, context=None): - google_docs_ref = self.pool.get('google.docs.config') + def doc_get(self, cr, uid, model, ids, context=None):# TODO fix logic here + google_docs_ref = self.pool.get('ir.attachment') gdocs_resource_id = google_docs_ref.search(cr, uid, [('model', '=', model), ('id', 'in', ids)]) #print gdocs_resource_id #print google_docs_ref.edit_url_template % (gdocs_resource_id, ) - #import pdb; pdb.set_trace() if gdocs_resource_id: return google_docs_ref.edit_url_template % (gdocs_resource_id, ) else: - gdocs_resource = google_docs_ref.copy_gdoc(cr, uid, model, context) + ir_attachment_res = self.pool.get('ir.attachment') + gdocs_resource = ir_attachment_res.create_empty_gdoc(cr, uid, model, context) if gdocs_resource == -2: return gdocs_resource # save the reference gdocs_resource_id = gdocs_resource.resource_id.text[len(google_docs_ref.prefix_gdoc_id_res):] + import pdb; pdb.set_trace() google_docs_ref.create(cr, uid, { - 'model': model, - 'google_resource_id': gdocs_resource_id, - 'name': gdocs_resource_title.text, + 'model_id': self.pool.get(model), + 'gdocs_resource_id': gdocs_resource_id, + 'name': gdocs_resource.title.text, }) return google_docs_ref.edit_url_template % (gdocs_resource_id,)