[imp] working... the only thing missing is linking from the model web to the gdoc web

bzr revid: rlo@openerp.com-20120229122940-16h3bwa6j8zbz01s
This commit is contained in:
Roberto López López (OpenERP) 2012-02-29 13:29:40 +01:00
parent f8213e68f0
commit 4c04da28b7
2 changed files with 36 additions and 120 deletions

View File

@ -4,162 +4,80 @@ import gdata.docs.client
from gdata.client import RequestError from gdata.client import RequestError
from gdata.docs.service import DOCUMENT_LABEL from gdata.docs.service import DOCUMENT_LABEL
class google_docs(osv.osv): class google_docs_config(osv.osv):
_name = 'google.docs' _name = 'google.docs.config'
_inherit = 'ir.attachment'
_table = 'google_docs_templates'
_columns = { _columns = {
'id': fields.integer('ID', readonly=True),
'model': fields.many2one('ir.model', 'Model'), 'model': fields.many2one('ir.model', 'Model'),
'gdocs_res_id': fields.char('Google resource ID', size=64, translate=False), 'gdocs_resource_id': fields.char('Google resource ID', size=64),
'name_template': fields.char('GDoc name template', size=64, translate=False) 'name_template': fields.char('GDoc name template', size=64)
} }
print '''
######################################### _defaults = {
'name_template': 'Google Document'
}
GOOGLE DOCS
#########################################
'''
edit_url_template = 'https://docs.google.com/document/d/%s/edit' edit_url_template = 'https://docs.google.com/document/d/%s/edit'
prefix_gdoc_id_res = DOCUMENT_LABEL + ':' prefix_gdoc_id_res = DOCUMENT_LABEL + ':'
def copy_gdoc(self, cr, uid, model, context=None): def copy_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. '''Associate a copy of the gdoc identified by 'gdocs_res_id' to the current entity.
@param cr: the current row from the database cursor. @param cr: the current row from the database cursor.
@param uid: the current user ID, for security checks. @param uid: the current user ID, for security checks.
@param model: the current model name. @param model: the current model name.
@param context: a standard dictionary for contextual values. @param context: a standard dictionary for contextual values.
@return the url of the copy itself. @return the url of the copy itself.
@return -1 if the template hasn't been assigned yet.
@return -2 if the google_base_account hasn't been configured yet. @return -2 if the google_base_account hasn't been configured yet.
''' '''
print '''
#########################################
google_docs.copy_gdoc()
#########################################
'''
if context==None: if context==None:
context={} context={}
'''template_vars = {
'db' : cr.dbname,
'model' : model,
'id' : id,
'salt' : salt,
'name' : '',
}'''
name_template = 'Sales order %s' name_template = 'Sales order %s'
# check google_base_account # check google_base_account
users_obj = self.pool.get('res.users') users_obj = self.pool.get('res.users')
user = users_obj.browse(cr, uid, [uid])[0] user = users_obj.browse(cr, uid, [uid])[0]
print '%s' % user.__dict__
print '%s' % user.gmail_user
print '%s' % user.gmail_password
if not user.gmail_user or not user.gmail_password: if not user.gmail_user or not user.gmail_password:
return -2 return -2
# check template for the current model # create the document
# TODO check module logic
'''model_obj = self.pool.get(model[0])
res_gdocs_obj = self.pool.get('google.docs')
domain = [('model' , '=', model_obj)]
gdoc = res_gdocs_obj.search(cr,uid,domain,context=context)
if not gdoc:
return -1
'''
# copy the document
client = gdata.docs.client.DocsClient(source='openerp.com') client = gdata.docs.client.DocsClient(source='openerp.com')
client.ssl = True client.ssl = True
client.http_client.debug = False client.http_client.debug = False
client.ClientLogin(user.gmail_user, user.gmail_password, client.source, service='writely') client.ClientLogin(user.gmail_user, user.gmail_password, client.source, service='writely')
resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL) resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL)
new_resource = client.post(entry=resource, uri='https://docs.google.com/feeds/default/private/full/') gdocs_resource = client.post(entry=resource, uri='https://docs.google.com/feeds/default/private/full/')
print new_resource.__dict__ return gdocs_resource
print new_resource.resource_id.text
print new_resource.resource_id
return self.edit_url_template % (new_resource.resource_id.text,)
''' class google_docs(osv.osv):
def get_documents_list(self, cr, uid, context=None): _name = 'google.docs'
' ''Return the list of google documents available at the user's account.
@param cr: the current row from the database cursor.
@param uid: the current user ID, for security checks.
@param context: a standard dictionary for contextual values.
@return a list with information about the documents in form of tuples (document_name, document_resource_id).
@return -2 if the google_base_account hasn't been configured yet.
'' '
if context == None:
context = {}
# check google_base_account def doc_get(self, cr, uid, model, id, context=None):
users_obj = self.pool.get('res.users') google_docs_ref = self.pool.get('google.docs.config')
user = users_obj.browse(cr, uid, [uid])[0] gdocs_resource_id = google_docs_ref.search(cr, uid, [('model', '=', model)])
if not user.gmail_user or not user.gmail_password:
return -2
# get the documents list if gdocs_resource_id:
client = gdata.docs.client.DocsClient(source='openerp.com') return google_docs_ref.edit_url_template % (gdocs_resource_id, )
client.ssl = True else:
client.http_client.debug = False gdocs_resource = google_docs_ref.copy_gdoc(cr, uid, model, context)
client.ClientLogin(user.gmail_user, user.gmail_password, client.source, service='writely') print gdocs_resource
if gdocs_resource == -2:
return gdocs_resource
return map(lambda doc: (doc.title.text, doc.resource_id.text[len(prefix_gdoc_id_res):]), filter(lambda r: r.resource_id.text.startswith(prefix_gdoc_id_res), client.get_all_resources())) print gdocs_resource
'''
''' import pdb; pdb.set_trace()
def set_model_document_template(self, cr, uid, model, resource_id, context=None): # save the reference
'' 'Set the default document template for the specified model. This template doesn't have to be a google documents template itself, but just a document. gdocs_resource_id = gdocs_resource.resource_id.text[gdocs_resource.prefix_gdoc_id_res+1:]
@param cr: current row for the database cursor. google_docs_ref.create(cr, uid, {
@param uid: the current user ID, for security checks. 'model': model,
@param model: the current model name. 'google_resource_id': gdocs_resource_id,
@param resource_id: resource_id associated to the chosen document. 'name': gdocs_resource_title.text,
@param context: a standard dictionary for contextual values.
@return 0 on successful execution.
@return -2 if the google_base_account hasn't been configured yet.
@return -3 if the given resource_id doesn't exist in the user's google docs account.
' ''
if context == None:
context = {}
# 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
# check resource_id
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')
try:
client.get_resource_by_id(resource_id)
except RequestError:
return -3
# set the model document template
model_template_id = self.create(cr, uid,
{ 'model': self.pool.get(model),
'gdocs_res_id': resource_id
}) })
print gdocs_resource.resource_id.text
return 0 print gdocs_resource_id
''' return google_docs_ref.edit_url_template % (gdocs_resource_id,)
class google_docs_folder(osv.osv):
_name = 'google.docs.folder'
_columns = {
'res_id': fields.char('GDocs resource id', size=64, translate=False),
}

View File

@ -9,11 +9,9 @@ instance.web.form.SidebarAttachments = instance.web.form.SidebarAttachments.exte
var self = this; var self = this;
var $gdocbtn = this.$element.find('.oe_google_docs_button'); var $gdocbtn = this.$element.find('.oe_google_docs_button');
$gdocbtn.attr('disabled', 'true').find('img, span').toggle(); $gdocbtn.attr('disabled', 'true').find('img, span').toggle();
new openerp.sessions.session0.web.DataSet(this, 'google.docs').call_button('copy_gdoc', [[this.view.datarecord.id], this.view.dataset.get_context()], function(r) { new openerp.sessions.session0.web.DataSet(this, 'google.docs').call('doc_get', [this.view.datarecord.id, this.view.dataset.get_context()], function(r) {
console.log(r); console.log(r);
$gdocbtn.hide();
self.do_update(); self.do_update();
self.do_action(r.result);
}); });
} }
}); });