2012-02-24 10:52:15 +00:00
|
|
|
from osv import osv, fields
|
|
|
|
import gdata.docs.data
|
|
|
|
import gdata.docs.client
|
|
|
|
from gdata.client import RequestError
|
2012-02-24 13:30:15 +00:00
|
|
|
from gdata.docs.service import DOCUMENT_LABEL
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-02-29 12:29:40 +00:00
|
|
|
class google_docs_config(osv.osv):
|
|
|
|
_name = 'google.docs.config'
|
|
|
|
_inherit = 'ir.attachment'
|
2012-02-24 10:52:15 +00:00
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'model': fields.many2one('ir.model', 'Model'),
|
2012-02-29 12:29:40 +00:00
|
|
|
'gdocs_resource_id': fields.char('Google resource ID', size=64),
|
|
|
|
'name_template': fields.char('GDoc name template', size=64)
|
2012-02-24 10:52:15 +00:00
|
|
|
}
|
2012-02-27 13:54:25 +00:00
|
|
|
|
2012-02-29 12:29:40 +00:00
|
|
|
_defaults = {
|
|
|
|
'name_template': 'Google Document'
|
|
|
|
}
|
2012-02-27 13:54:25 +00:00
|
|
|
|
2012-02-29 08:38:33 +00:00
|
|
|
edit_url_template = 'https://docs.google.com/document/d/%s/edit'
|
2012-02-24 13:30:15 +00:00
|
|
|
prefix_gdoc_id_res = DOCUMENT_LABEL + ':'
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-02-27 12:38:34 +00:00
|
|
|
def copy_gdoc(self, cr, uid, model, context=None):
|
2012-02-29 12:29:40 +00:00
|
|
|
#import pdb; pdb.set_trace()
|
2012-02-24 10:52:15 +00:00
|
|
|
'''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 uid: the current user ID, for security checks.
|
|
|
|
@param model: the current model name.
|
|
|
|
@param context: a standard dictionary for contextual values.
|
|
|
|
@return the url of the copy itself.
|
2012-02-24 14:01:19 +00:00
|
|
|
@return -2 if the google_base_account hasn't been configured yet.
|
|
|
|
'''
|
2012-02-24 10:52:15 +00:00
|
|
|
|
|
|
|
if context==None:
|
|
|
|
context={}
|
|
|
|
|
2012-02-24 15:29:20 +00:00
|
|
|
name_template = 'Sales order %s'
|
2012-02-24 13:52:28 +00:00
|
|
|
|
2012-02-24 10:52:15 +00:00
|
|
|
# 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
|
|
|
|
|
2012-02-29 12:29:40 +00:00
|
|
|
# create the document
|
2012-02-24 10:52:15 +00:00
|
|
|
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')
|
2012-02-28 14:46:23 +00:00
|
|
|
resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL)
|
2012-02-29 12:29:40 +00:00
|
|
|
gdocs_resource = client.post(entry=resource, uri='https://docs.google.com/feeds/default/private/full/')
|
|
|
|
return gdocs_resource
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-02-29 12:29:40 +00:00
|
|
|
class google_docs(osv.osv):
|
|
|
|
_name = 'google.docs'
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-02-29 17:23:19 +00:00
|
|
|
def doc_get(self, cr, uid, model, ids, context=None):
|
2012-02-29 12:29:40 +00:00
|
|
|
google_docs_ref = self.pool.get('google.docs.config')
|
2012-02-29 17:23:19 +00:00
|
|
|
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()
|
2012-02-29 12:29:40 +00:00
|
|
|
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)
|
|
|
|
if gdocs_resource == -2:
|
|
|
|
return gdocs_resource
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-02-29 12:29:40 +00:00
|
|
|
# save the reference
|
2012-02-29 12:37:58 +00:00
|
|
|
gdocs_resource_id = gdocs_resource.resource_id.text[len(google_docs_ref.prefix_gdoc_id_res):]
|
2012-02-29 12:29:40 +00:00
|
|
|
google_docs_ref.create(cr, uid, {
|
|
|
|
'model': model,
|
|
|
|
'google_resource_id': gdocs_resource_id,
|
|
|
|
'name': gdocs_resource_title.text,
|
2012-02-24 10:52:15 +00:00
|
|
|
})
|
2012-02-29 12:39:42 +00:00
|
|
|
|
2012-02-29 12:29:40 +00:00
|
|
|
return google_docs_ref.edit_url_template % (gdocs_resource_id,)
|