[IMP] google_docs: reviewed code. Overall cleaning of methods, better documentation, refactoring, fix of some glitches and so on...

bzr revid: qdp-launchpad@openerp.com-20120523095136-vj1n9ekukdxibzr2
This commit is contained in:
Quentin (OpenERP) 2012-05-23 11:51:36 +02:00
parent df52a73fc7
commit ab372e3dcf
4 changed files with 99 additions and 85 deletions

View File

@ -9,7 +9,7 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<xpath expr="//notebook[last()]" position="inside"> <xpath expr="//notebook[last()]" position="inside">
<page string=" Synchronization "> <page string=" Synchronization ">
<separator string="Google Account" colspan="4" /> <separator string="Google Account" colspan="4"/>
<field name="gmail_user"/> <field name="gmail_user"/>
<field name="gmail_password" password="True"/> <field name="gmail_password" password="True"/>
</page> </page>

View File

@ -39,7 +39,7 @@ class google_login(osv.osv_memory):
def google_login(self, user, password, type='', context=None): def google_login(self, user, password, type='', context=None):
if type == 'group': if type == 'group':
gd_client = gdata.contacts.client.ContactsClient(source='OpenERP') gd_client = gdata.contacts.client.ContactsClient(source='OpenERP')
if type == 'contact' : if type == 'contact':
gd_client = gdata.contacts.service.ContactsService() gd_client = gdata.contacts.service.ContactsService()
if type == 'calendar': if type == 'calendar':
gd_client = gdata.calendar.service.CalendarService() gd_client = gdata.calendar.service.CalendarService()
@ -48,7 +48,7 @@ class google_login(osv.osv_memory):
else: else:
gd_client = gdata.contacts.service.ContactsService() gd_client = gdata.contacts.service.ContactsService()
try: try:
gd_client.ClientLogin(user, password,gd_client.source) gd_client.ClientLogin(user, password, gd_client.source)
except Exception: except Exception:
return False return False
return gd_client return gd_client

View File

@ -19,6 +19,7 @@
############################################################################## ##############################################################################
from osv import osv, fields from osv import osv, fields
from tools.translate import _
try: try:
import gdata.docs.data import gdata.docs.data
import gdata.docs.client import gdata.docs.client
@ -31,95 +32,102 @@ except ImportError:
class google_docs_ir_attachment(osv.osv): class google_docs_ir_attachment(osv.osv):
_inherit = 'ir.attachment' _inherit = 'ir.attachment'
def _auth(self, cr, uid,context=None): def _auth(self, cr, uid, context=None):
''' '''
Connexion with google base account Connexion with google base account
@return client object for connexion @return client object for connexion
''' '''
#pool the google.login in google_base_account #pool the google.login in google_base_account
google_pool = self.pool.get('google.login') 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
#get gmail password and login # google.login object, because it is easier. The keys 'user' and 'password' ahve to be passed in the dict
user_config = google_pool.default_get( cr, uid, {'user' : '' , 'password' : ''},context=context) # 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 #login gmail account
client = google_pool.google_login( user_config['user'], user_config['password'], type='docs_client', context=context) client = google_pool.google_login( user_config['user'], user_config['password'], type='docs_client', context=context)
if not client: if not client:
raise osv.except_osv(('Google Docs Error!'),("Check your google configuration in users/synchronization") ) raise osv.except_osv( _('Google Docs Error!'), _("Check your google configuration in users/synchronization"))
return client return client
def create_empty_google_doc(self, cr, uid, name_gdocs, model, ids, type_doc,context=None): def create_empty_google_doc(self, cr, uid, res_model, res_id, context=None):
'''Associate a copy of the gdoc identified by 'gdocs_res_id' to the current entity. '''Create a new google document, empty and with a default type (txt)
@param cr: the current row from the database cursor. :param res_model: the object for which the google doc is created
@param uid: the current user ID, for security checks. :param res_id: the Id of the object for which the google doc is created
@param model: the current model name. :return: the ID of the google document object created
@param type_doc: text, spreadsheet or slide.
@return the document object.
@return False if the google_base_account hasn't been configured yet.
''' '''
#login with the base account google module #login with the base account google module
client = self._auth(cr, uid,context=context) client = self._auth(cr, uid, context=context)
# create the document in google docs # create the document in google docs
local_resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL) local_resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL)
#create a new doc in Google Docs #create a new doc in Google Docs
gdocs_resource = client.post(entry=local_resource, uri='https://docs.google.com/feeds/default/private/full/') gdocs_resource = client.post(entry=local_resource, uri='https://docs.google.com/feeds/default/private/full/')
# register into the db # create an ir.attachment into the db
self.create(cr, uid, { self.create(cr, uid, {
'res_model': model, 'res_model': res_model,
'res_id': ids[0], 'res_id': res_id,
'type': 'url', 'type': 'url',
'name': 'Google Doc', 'name': _('Google Doc'),
'url': gdocs_resource.get_alternate_link().href, 'url': gdocs_resource.get_alternate_link().href,
},context=context) }, context=context)
return gdocs_resource.resource_id.text
def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None):
return 1
def copy_gdoc(self, cr, uid, name_gdocs, model, google_res_id, ids,context=None):
''' '''
copy an existing document in google docs 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) client = self._auth(cr, uid)
# fetch and copy the original document # fetch and copy the original document
try: try:
original_resource = client.get_resource_by_id(google_res_id) original_resource = client.get_resource_by_id(gdoc_template_id)
#copy the document you choose in the configuration #copy the document you choose in the configuration
copy_resource = client.copy_resource(original_resource,'copy_%s' % original_resource.title.text) copy_resource = client.copy_resource(original_resource, 'copy_%s' % original_resource.title.text)
except: except:
raise osv.except_osv(('Google Docs Error!'),("Your ressource id is not correct. You can find the id in the google docs URL") ) raise osv.except_osv(_('Google Docs Error!'), _("Your resource id is not correct. You can find the id in the google docs URL"))
# register into the db # create an ir.attachment
self.create(cr, uid, { self.create(cr, uid, {
'res_model': model, 'res_model': res_model,
'res_id': ids[0], 'res_id': res_id,
'type': 'url', 'type': 'url',
'name': name_gdocs, 'name': name_gdocs,
'url': copy_resource.get_alternate_link().href 'url': copy_resource.get_alternate_link().href
},context=context) }, context=context)
return copy_resource.resource_id.text
return copy_resource def google_doc_get(self, cr, uid, res_model, ids, context=None):
'''
class google_docs(osv.osv): Function called by the js, when no google doc are yet associated with a record, with the aim to create one. It
_name = 'google.docs' 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
def doc_get(self, cr, uid, model, id, type_doc,context=None): different than the default values). If no config is associated with the `res_model`, then a blank text document
ir_attachment_ref = self.pool.get('ir.attachment') 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 google document object created
'''
assert len(ids) == 1, 'Creating google docs 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') pool_gdoc_config = self.pool.get('google.docs.config')
google_docs_config = pool_gdoc_config.search(cr, uid, [('model_id', '=', model)]) name_gdocs = ''
name_gdocs='' model_fields_dic = self.pool.get(res_model).read(cr, uid, res_id, [], context=context)
model_fields_dic = self.pool.get(model).read(cr,uid,id,[])
# 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: if google_docs_config:
name_gdocs = pool_gdoc_config.browse(cr,uid,google_docs_config,context=context)[0].name_template name_gdocs = pool_gdoc_config.browse(cr, uid, google_docs_config, context=context)[0].name_template
name_gdocs = name_gdocs % model_fields_dic[0] name_gdocs = name_gdocs % model_fields_dic
google_template_id = pool_gdoc_config.browse(cr, uid, google_docs_config[0], context=context).gdocs_resource_id
# check if a model is configurate with a template google_document = pool_ir_attachment.copy_gdoc(cr, uid, res_model, res_id, name_gdocs, google_template_id, context=context)
if google_docs_config:
for google_config in self.pool.get('google.docs.config').browse(cr,uid,google_docs_config,context=context):
google_res_id = google_config.gdocs_resource_id
google_document = ir_attachment_ref.copy_gdoc(cr, uid,name_gdocs ,model,google_res_id, id)
else: else:
google_document = ir_attachment_ref.create_empty_google_doc(cr, uid,name_gdocs, model, id, type_doc) google_document = pool_ir_attachment.create_empty_google_doc(cr, uid, res_model, res_id, context=context)
return -1 return google_document
class config(osv.osv): class config(osv.osv):
_name = 'google.docs.config' _name = 'google.docs.config'
@ -127,22 +135,18 @@ class config(osv.osv):
_columns = { _columns = {
'model_id': fields.many2one('ir.model', 'Model'), 'model_id': fields.many2one('ir.model', 'Model'),
'gdocs_resource_id': fields.char('Google resource ID', size=64,help='This is the id of the template document you kind find it in the URL'), 'gdocs_resource_id': fields.char('Google resource ID', size=64,help='''
This is the id of the template document, on google side. You can find 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`
...
'''),
'name_template': fields.char('GDoc name template ', size=64, help='This is the name which appears on google side'), 'name_template': fields.char('GDoc name template ', size=64, help='This is the name which appears on google side'),
} }
_defaults = { _defaults = {
'name_template': 'pr_%(name)s', 'name_template': 'gdoc_%(name)s',
} }
def get_config(self, cr, uid, ids, model,context=None):
'''
Method use with the js to hidde or show the add google doc button
@return : list of configuration ids or false
'''
if self.pool.get('ir.attachment').search_count(cr,uid,[('url','like','https://docs.google.com/%'),('res_model','=',model),('res_id','=',ids[0])]) !=0:
return False
else:
return True
config() config()

View File

@ -1,31 +1,41 @@
openerp.google_docs = function(instance, m) { openerp.google_docs = function(instance, m) {
var _t = instance.web._t;
instance.web.Sidebar = instance.web.Sidebar.extend({ instance.web.Sidebar = instance.web.Sidebar.extend({
on_attachments_loaded: function(attachments) { on_attachments_loaded: function(attachments) {
self._super(attachements); self = this
self._super(attachments);
// if attachment contains a google doc url do nothing // if attachment contains a google doc url do nothing
// else // else display a button to create a google doc
this.sidebar.add_items('other', [ var flag = false;
_.each(attachments, function(i) {
if (i.url && i.url.match('/docs.google.com/')) { flag = true; }
});
if (! flag) {
this.add_items('files', [
{ label: _t('Google Doc'), callback: self.on_google_doc }, { label: _t('Google Doc'), callback: self.on_google_doc },
]); ]);
}
}, },
on_google_doc: function() { on_google_doc: function() {
var self = this; var self = this;
var form = self.getParent(); var form = self.getParent();
form.sidebar_context().then(function (context) { form.sidebar_context().then(function (context) {
var ds = new instance.web.DataSet(this, 'google.docs', context); var ds = new instance.web.DataSet(this, 'ir.attachment', context);
ds.call('doc_get', [form.view.dataset.model, [form.view.datarecord.id], 'text'], function(r) { ds.call('google_doc_get', [form.dataset.model, [form.datarecord.id], context], function(r) {
if (r == 'False') { if (r == 'False') {
var params = { var params = {
error: response, error: response,
message: "The user google credentials are not set yet. Contact your administrator for help." message: _t("The user google credentials are not set yet. Contact your administrator for help.")
} }
$(openerp.web.qweb.render("DialogWarning", params)).dialog({ $(openerp.web.qweb.render("DialogWarning", params)).dialog({
title: "User Google credentials are not yet set.", title: _t("User Google credentials are not yet set."),
modal: true, modal: true,
}); });
} }
view.reload(); form.reload();
}); });
})
} }
} })
}; }