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
|
|
|
|
|
|
|
class google_docs(osv.osv):
|
|
|
|
_name = 'google.docs'
|
|
|
|
|
|
|
|
_table = 'google_docs_templates'
|
|
|
|
_columns = {
|
|
|
|
'id': fields.integer('ID', readonly=True),
|
|
|
|
'model': fields.many2one('ir.model', 'Model'),
|
2012-02-24 15:29:20 +00:00
|
|
|
'gdocs_res_id': fields.char('Google resource ID', size=64, translate=False),
|
2012-02-24 13:53:15 +00:00
|
|
|
'name_template': fields.char('GDoc name template', size=64, translate=False)
|
2012-02-24 10:52:15 +00:00
|
|
|
}
|
2012-02-27 13:54:25 +00:00
|
|
|
print '''
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-02-27 13:54:25 +00:00
|
|
|
#########################################
|
|
|
|
|
|
|
|
GOOGLE DOCS
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
|
|
|
|
'''
|
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-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.
|
|
|
|
@return -1 if the template hasn't been assigned yet.
|
2012-02-24 14:01:19 +00:00
|
|
|
@return -2 if the google_base_account hasn't been configured yet.
|
|
|
|
'''
|
2012-02-27 13:54:25 +00:00
|
|
|
print '''
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
|
|
|
|
google_docs.copy_gdoc()
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
|
|
|
|
'''
|
2012-02-24 10:52:15 +00:00
|
|
|
|
|
|
|
if context==None:
|
|
|
|
context={}
|
|
|
|
|
2012-02-28 14:46:23 +00:00
|
|
|
'''template_vars = {
|
2012-02-24 13:52:28 +00:00
|
|
|
'db' : cr.dbname,
|
|
|
|
'model' : model,
|
|
|
|
'id' : id,
|
|
|
|
'salt' : salt,
|
|
|
|
'name' : '',
|
2012-02-28 14:46:23 +00:00
|
|
|
}'''
|
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]
|
2012-02-29 08:38:33 +00:00
|
|
|
print '%s' % user.__dict__
|
|
|
|
print '%s' % user.gmail_user
|
|
|
|
print '%s' % user.gmail_password
|
2012-02-24 10:52:15 +00:00
|
|
|
if not user.gmail_user or not user.gmail_password:
|
|
|
|
return -2
|
|
|
|
|
2012-02-28 14:46:23 +00:00
|
|
|
# check template for the current model
|
|
|
|
# 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
|
|
|
|
'''
|
2012-02-24 10:52:15 +00:00
|
|
|
# copy 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')
|
2012-02-28 14:46:23 +00:00
|
|
|
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/')
|
2012-02-29 08:38:33 +00:00
|
|
|
print new_resource.__dict__
|
|
|
|
print new_resource.resource_id.text
|
|
|
|
print new_resource.resource_id
|
2012-02-28 14:46:23 +00:00
|
|
|
return self.edit_url_template % (new_resource.resource_id.text,)
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-02-27 13:54:25 +00:00
|
|
|
'''
|
2012-02-24 10:52:15 +00:00
|
|
|
def get_documents_list(self, cr, uid, context=None):
|
2012-02-27 13:54:25 +00:00
|
|
|
' ''Return the list of google documents available at the user's account.
|
2012-02-24 10:52:15 +00:00
|
|
|
@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).
|
2012-02-24 14:01:19 +00:00
|
|
|
@return -2 if the google_base_account hasn't been configured yet.
|
2012-02-27 13:54:25 +00:00
|
|
|
'' '
|
2012-02-24 10:52:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# get the documents list
|
|
|
|
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-24 13:54:58 +00:00
|
|
|
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()))
|
2012-02-27 13:54:25 +00:00
|
|
|
'''
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-02-27 13:54:25 +00:00
|
|
|
'''
|
2012-02-24 10:52:15 +00:00
|
|
|
def set_model_document_template(self, cr, uid, model, resource_id, context=None):
|
2012-02-27 13:54:25 +00:00
|
|
|
'' '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.
|
2012-02-24 10:52:15 +00:00
|
|
|
@param cr: current row for the database cursor.
|
|
|
|
@param uid: the current user ID, for security checks.
|
|
|
|
@param model: the current model name.
|
|
|
|
@param resource_id: resource_id associated to the chosen document.
|
|
|
|
@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.
|
2012-02-24 11:03:09 +00:00
|
|
|
@return -3 if the given resource_id doesn't exist in the user's google docs account.
|
2012-02-27 13:54:25 +00:00
|
|
|
' ''
|
2012-02-24 10:52:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
return 0
|
2012-02-27 13:54:25 +00:00
|
|
|
'''
|
2012-02-24 13:30:15 +00:00
|
|
|
|
|
|
|
class google_docs_folder(osv.osv):
|
|
|
|
_name = 'google.docs.folder'
|
|
|
|
_columns = {
|
|
|
|
'res_id': fields.char('GDocs resource id', size=64, translate=False),
|
|
|
|
}
|