[IMP]google_drive: cleanup
bzr revid: dle@openerp.com-20130626145213-37to2q3q9jh4lnsg
This commit is contained in:
commit
afa6ea8e4f
|
@ -22,8 +22,8 @@
|
||||||
from openerp.osv import osv
|
from openerp.osv import osv
|
||||||
from openerp import SUPERUSER_ID
|
from openerp import SUPERUSER_ID
|
||||||
|
|
||||||
from httplib2 import Http
|
|
||||||
import urllib
|
import urllib
|
||||||
|
import urllib2
|
||||||
import simplejson
|
import simplejson
|
||||||
|
|
||||||
|
|
||||||
|
@ -31,7 +31,6 @@ class base_config_settings(osv.osv):
|
||||||
_inherit = "base.config.settings"
|
_inherit = "base.config.settings"
|
||||||
|
|
||||||
def onchange_google_authorization_code(self, cr, uid, ids, service, authorization_code, context=None):
|
def onchange_google_authorization_code(self, cr, uid, ids, service, authorization_code, context=None):
|
||||||
res = {}
|
|
||||||
if authorization_code:
|
if authorization_code:
|
||||||
ir_config = self.pool['ir.config_parameter']
|
ir_config = self.pool['ir.config_parameter']
|
||||||
client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_%s_client_id' % service)
|
client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_%s_client_id' % service)
|
||||||
|
@ -39,14 +38,18 @@ class base_config_settings(osv.osv):
|
||||||
redirect_uri = ir_config.get_param(cr, SUPERUSER_ID, 'google_redirect_uri')
|
redirect_uri = ir_config.get_param(cr, SUPERUSER_ID, 'google_redirect_uri')
|
||||||
|
|
||||||
#Get the Refresh Token From Google And store it in ir.config_parameter
|
#Get the Refresh Token From Google And store it in ir.config_parameter
|
||||||
headers = {"Content-type": "application/x-www-form-urlencoded"}
|
data = {
|
||||||
data = dict(code=authorization_code, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, grant_type="authorization_code")
|
'code': authorization_code,
|
||||||
data = urllib.urlencode(data)
|
'client_id': client_id,
|
||||||
resp, content = Http().request("https://accounts.google.com/o/oauth2/token", "POST", data, headers)
|
'client_secret': client_secret,
|
||||||
|
'redirect_uri': redirect_uri,
|
||||||
|
'grant_type': "authorization_code",
|
||||||
|
}
|
||||||
|
resp, content = urllib2.urlopen("https://accounts.google.com/o/oauth2/token", urllib.urlencode(data))
|
||||||
content = simplejson.loads(content)
|
content = simplejson.loads(content)
|
||||||
if 'refresh_token' in content.keys():
|
if 'refresh_token' in content.keys():
|
||||||
ir_config.set_param(cr, uid, 'google_%s_refresh_token' % service, content['refresh_token'])
|
ir_config.set_param(cr, uid, 'google_%s_refresh_token' % service, content['refresh_token'])
|
||||||
return res
|
return {}
|
||||||
|
|
||||||
def _get_google_token_uri(self, cr, uid, service, context=None):
|
def _get_google_token_uri(self, cr, uid, service, context=None):
|
||||||
ir_config = self.pool['ir.config_parameter']
|
ir_config = self.pool['ir.config_parameter']
|
||||||
|
|
|
@ -22,10 +22,9 @@ import logging
|
||||||
from openerp import SUPERUSER_ID
|
from openerp import SUPERUSER_ID
|
||||||
from openerp.osv import fields, osv
|
from openerp.osv import fields, osv
|
||||||
from openerp.tools.translate import _
|
from openerp.tools.translate import _
|
||||||
from urlparse import urlparse
|
|
||||||
|
|
||||||
from httplib2 import Http
|
|
||||||
import urllib
|
import urllib
|
||||||
|
import urllib2
|
||||||
import json
|
import json
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
@ -35,37 +34,29 @@ class config(osv.osv):
|
||||||
_name = 'google.drive.config'
|
_name = 'google.drive.config'
|
||||||
_description = "Google Drive templates config"
|
_description = "Google Drive templates config"
|
||||||
|
|
||||||
def get_google_doc_name(self, cr, uid, ids, res_id, tamplate_id, context=None):
|
def get_google_drive_url(self, cr, uid, config_id, res_id, template_id, context=None):
|
||||||
pool_model = self.pool.get("ir.model")
|
config = self.browse(cr, SUPERUSER_ID, config_id, context=context)
|
||||||
res = {}
|
model = config.model_id
|
||||||
for config in self.browse(cr, SUPERUSER_ID, ids, context=context):
|
filter_name = config.filter_id and config.filter_id.name or False
|
||||||
res_model = config.model_id
|
record = self.pool.get(model.model).read(cr, uid, res_id, [], context=context)
|
||||||
model_ids = pool_model.search(cr, uid, [('model', '=', res_model)])
|
record.update({'model': model.name, 'filter': filter_name})
|
||||||
if not model_ids:
|
name_gdocs = config.name_template or "%(name)s_%(model)s_%(filter)s_gdrive"
|
||||||
continue
|
try:
|
||||||
model = pool_model.browse(cr, uid, model_ids[0], context=context)
|
name_gdocs = name_gdocs % record
|
||||||
model_name = model.name
|
except:
|
||||||
filter_name = config.filter_id and config.filter_id.name or False
|
raise osv.except_osv(_('Key Error!'), _("At least one key cannot be found in your Google Drive name pattern"))
|
||||||
record = self.pool.get(res_model).read(cr, uid, res_id, [], context=context)
|
|
||||||
record.update({'model': model_name, 'filter': filter_name})
|
|
||||||
name_gdocs = config.name_template or "%(name)s_%(model)s_%(filter)s_gdrive"
|
|
||||||
try:
|
|
||||||
name_gdocs = name_gdocs % record
|
|
||||||
except:
|
|
||||||
raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object."))
|
|
||||||
|
|
||||||
attach_pool = self.pool.get("ir.attachment")
|
attach_pool = self.pool.get("ir.attachment")
|
||||||
attach_ids = attach_pool.search(cr, uid, [('res_model', '=', res_model), ('name', '=', name_gdocs), ('res_id', '=', res_id)])
|
attach_ids = attach_pool.search(cr, uid, [('res_model', '=', model.model), ('name', '=', name_gdocs), ('res_id', '=', res_id)])
|
||||||
url = False
|
url = False
|
||||||
if attach_ids:
|
if attach_ids:
|
||||||
attachment = attach_pool.browse(cr, uid, attach_ids[0], context)
|
attachment = attach_pool.browse(cr, uid, attach_ids[0], context)
|
||||||
url = attachment.url
|
url = attachment.url
|
||||||
else:
|
else:
|
||||||
url = self.copy_doc(cr, uid, ids, res_id, tamplate_id, name_gdocs, res_model, context)
|
url = self.copy_doc(cr, uid, res_id, template_id, name_gdocs, model.model, context)
|
||||||
res[config.id] = {'url': url}
|
return url
|
||||||
return res
|
|
||||||
|
|
||||||
def copy_doc(self, cr, uid, ids, res_id, tamplate_id, name_gdocs, res_model, context=None):
|
def copy_doc(self, cr, uid, res_id, template_id, name_gdocs, res_model, context=None):
|
||||||
ir_config = self.pool['ir.config_parameter']
|
ir_config = self.pool['ir.config_parameter']
|
||||||
google_drive_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_client_id')
|
google_drive_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_client_id')
|
||||||
google_drive_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_client_secret')
|
google_drive_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_client_secret')
|
||||||
|
@ -73,26 +64,26 @@ class config(osv.osv):
|
||||||
google_web_base_url = ir_config.get_param(cr, SUPERUSER_ID, 'web.base.url')
|
google_web_base_url = ir_config.get_param(cr, SUPERUSER_ID, 'web.base.url')
|
||||||
|
|
||||||
#For Getting New Access Token With help of old Refresh Token
|
#For Getting New Access Token With help of old Refresh Token
|
||||||
headers = {"Content-type": "application/x-www-form-urlencoded"}
|
|
||||||
data = dict(client_id=google_drive_client_id,
|
data = dict(client_id=google_drive_client_id,
|
||||||
refresh_token=google_drive_refresh_token,
|
refresh_token=google_drive_refresh_token,
|
||||||
client_secret=google_drive_client_secret,
|
client_secret=google_drive_client_secret,
|
||||||
grant_type="refresh_token")
|
grant_type="refresh_token")
|
||||||
data = urllib.urlencode(data)
|
data = urllib.urlencode(data)
|
||||||
resp, content = Http().request("https://accounts.google.com/o/oauth2/token", "POST", data, headers)
|
content = urllib2.urlopen("https://accounts.google.com/o/oauth2/token", data).read()
|
||||||
content = json.loads(content)
|
content = json.loads(content)
|
||||||
|
|
||||||
# Copy template in to drive with help of new access token
|
# Copy template in to drive with help of new access token
|
||||||
if 'access_token' in content:
|
if 'access_token' in content:
|
||||||
request_url = "https://www.googleapis.com/drive/v2/files/%s?fields=parents/id&access_token=%s" % (tamplate_id, content['access_token'])
|
request_url = "https://www.googleapis.com/drive/v2/files/%s?fields=parents/id&access_token=%s" % (template_id, content['access_token'])
|
||||||
resp, parents = Http().request(request_url, "GET")
|
parents = urllib2.urlopen(request_url).read()
|
||||||
parents_dict = json.loads(parents)
|
parents_dict = json.loads(parents)
|
||||||
|
|
||||||
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
|
|
||||||
record_url = "Click on link to open Record in OpenERP\n %s/?db=%s#id=%s&model=%s" % (google_web_base_url, cr.dbname, res_id, res_model)
|
record_url = "Click on link to open Record in OpenERP\n %s/?db=%s#id=%s&model=%s" % (google_web_base_url, cr.dbname, res_id, res_model)
|
||||||
data = {"title": name_gdocs, "description": record_url, "parents": parents_dict['parents']}
|
data = {"title": name_gdocs, "description": record_url, "parents": parents_dict['parents']}
|
||||||
request_url = "https://www.googleapis.com/drive/v2/files/%s/copy?access_token=%s" % (tamplate_id, content['access_token'])
|
request_url = "https://www.googleapis.com/drive/v2/files/%s/copy?access_token=%s" % (template_id, content['access_token'])
|
||||||
resp, content = Http().request(request_url, "POST", json.dumps(data), headers)
|
data_json = json.dumps(data)
|
||||||
|
req = urllib2.Request(request_url, data_json, {'Content-Type': 'application/json', 'Content-Length': len(data_json)})
|
||||||
|
content = urllib2.urlopen(req).read()
|
||||||
content = json.loads(content)
|
content = json.loads(content)
|
||||||
res = False
|
res = False
|
||||||
if 'alternateLink' in content.keys():
|
if 'alternateLink' in content.keys():
|
||||||
|
@ -121,61 +112,47 @@ class config(osv.osv):
|
||||||
# check if a model is configured with a template
|
# check if a model is configured with a template
|
||||||
config_ids = self.search(cr, uid, [('model_id', '=', res_model)], context=context)
|
config_ids = self.search(cr, uid, [('model_id', '=', res_model)], context=context)
|
||||||
configs = []
|
configs = []
|
||||||
for config in self.browse(cr, SUPERUSER_ID, config_ids, context=context):
|
for config in self.browse(cr, uid, config_ids, context=context):
|
||||||
if config.filter_id:
|
if config.filter_id:
|
||||||
if (config.filter_id.user_id and config.filter_id.user_id.id != uid):
|
if (config.filter_id.user_id and config.filter_id.user_id.id != uid):
|
||||||
#Private
|
#Private
|
||||||
continue
|
continue
|
||||||
google_doc_configs = self._filter(cr, uid, config, config.filter_id, res_id, context=context)
|
domain = [('id', 'in', [res_id])] + eval(config.filter_id.domain)
|
||||||
|
local_context = context and context.copy() or {}
|
||||||
|
local_context.update(eval(config.filter_id.context))
|
||||||
|
google_doc_configs = self.pool.get(config.filter_id.model_id).search(cr, uid, domain, context=local_context)
|
||||||
if google_doc_configs:
|
if google_doc_configs:
|
||||||
configs.append({'id': config.id, 'name': config.name})
|
configs.append({'id': config.id, 'name': config.name})
|
||||||
else:
|
else:
|
||||||
configs.append({'id': config.id, 'name': config.name})
|
configs.append({'id': config.id, 'name': config.name})
|
||||||
return configs
|
return configs
|
||||||
|
|
||||||
def _filter(self, cr, uid, action, action_filter, record_ids, context=None):
|
|
||||||
""" filter the list record_ids that satisfy the action filter """
|
|
||||||
if record_ids and action_filter:
|
|
||||||
if not action.model_id == action_filter.model_id:
|
|
||||||
raise osv.except_osv(_('Warning!'), _("Something went wrong with the configuration of attachments with google drive.Please contact your Administrator to fix the problem."))
|
|
||||||
model = self.pool.get(action_filter.model_id)
|
|
||||||
domain = [('id', 'in', [record_ids])] + eval(action_filter.domain)
|
|
||||||
ctx = dict(context or {})
|
|
||||||
ctx.update(eval(action_filter.context))
|
|
||||||
record_ids = model.search(cr, uid, domain, context=ctx)
|
|
||||||
return record_ids
|
|
||||||
|
|
||||||
def _list_all_models(self, cr, uid, context=None):
|
|
||||||
cr.execute("SELECT model, name from ir_model order by name")
|
|
||||||
return cr.fetchall()
|
|
||||||
|
|
||||||
def _resource_get(self, cr, uid, ids, name, arg, context=None):
|
def _resource_get(self, cr, uid, ids, name, arg, context=None):
|
||||||
result = {}
|
result = {}
|
||||||
for data in self.browse(cr, uid, ids, context):
|
for data in self.browse(cr, uid, ids, context):
|
||||||
template_url = data.google_drive_template_url
|
template_url = data.google_drive_template_url
|
||||||
try:
|
try:
|
||||||
url = urlparse(template_url)
|
if '/spreadsheet/' in template_url:
|
||||||
res = url.path.split('/')
|
key = template_url.split('key=')[1]
|
||||||
if res[1] == "spreadsheet":
|
|
||||||
key = url.query.split('=')[1]
|
|
||||||
else:
|
else:
|
||||||
key = res[3]
|
key = template_url.split('/d/')[1].split('/')[0]
|
||||||
result[data.id] = str(key)
|
result[data.id] = str(key)
|
||||||
except:
|
except IndexError:
|
||||||
raise osv.except_osv(_('Incorrect URL!'), _("Please enter a valid URL."))
|
raise osv.except_osv(_('Incorrect URL!'), _("Please enter a valid Google Document URL."))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _client_id_get(self, cr, uid, ids, name, arg, context=None):
|
def _client_id_get(self, cr, uid, ids, name, arg, context=None):
|
||||||
result = {}
|
result = {}
|
||||||
|
client_id = self.pool['ir.config_parameter'].get_param(cr, SUPERUSER_ID, 'google_drive_client_id')
|
||||||
for config_id in ids:
|
for config_id in ids:
|
||||||
config = self.pool['ir.config_parameter']
|
result[config_id] = client_id
|
||||||
result[config_id] = config.get_param(cr, SUPERUSER_ID, 'google_drive_client_id')
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
_columns = {
|
_columns = {
|
||||||
'name': fields.char('Template Name', required=True, size=1024),
|
'name': fields.char('Template Name', required=True, size=1024),
|
||||||
'model_id': fields.selection(_list_all_models, 'Model', required=True),
|
'model_id': fields.many2one('ir.model', 'Model', ondelete='set null', required=True),
|
||||||
'filter_id': fields.many2one('ir.filters', 'Filter'),
|
'model': fields.related('model_id', 'model', type='char', string='Model', readonly=True),
|
||||||
|
'filter_id': fields.many2one('ir.filters', 'Filter', domain="[('model_id', '=', model)]"),
|
||||||
'google_drive_template_url': fields.char('Template URL', required=True, size=1024),
|
'google_drive_template_url': fields.char('Template URL', required=True, size=1024),
|
||||||
'google_drive_resource_id': fields.function(_resource_get, type="char", string='Resource Id'),
|
'google_drive_resource_id': fields.function(_resource_get, type="char", string='Resource Id'),
|
||||||
'google_drive_client_id': fields.function(_client_id_get, type="char", string='Google Client '),
|
'google_drive_client_id': fields.function(_client_id_get, type="char", string='Google Client '),
|
||||||
|
@ -183,11 +160,12 @@ class config(osv.osv):
|
||||||
}
|
}
|
||||||
|
|
||||||
def onchange_model_id(self, cr, uid, ids, model_id, context=None):
|
def onchange_model_id(self, cr, uid, ids, model_id, context=None):
|
||||||
res = {'domain': {'filter_id': []}}
|
res = {}
|
||||||
if model_id:
|
if model_id:
|
||||||
res['domain'] = {'filter_id': [('model_id', '=', model_id)]}
|
model = self.pool['ir.model'].browse(cr, uid, model_id, context=context)
|
||||||
|
res['value'] = {'model': model.model}
|
||||||
else:
|
else:
|
||||||
res['value'] = {'filter_id': False}
|
res['value'] = {'filter_id': False, 'model': False}
|
||||||
return res
|
return res
|
||||||
|
|
||||||
_defaults = {
|
_defaults = {
|
||||||
|
@ -196,7 +174,7 @@ class config(osv.osv):
|
||||||
|
|
||||||
def _check_model_id(self, cr, uid, ids, context=None):
|
def _check_model_id(self, cr, uid, ids, context=None):
|
||||||
config_id = self.browse(cr, uid, ids[0], context=context)
|
config_id = self.browse(cr, uid, ids[0], context=context)
|
||||||
if config_id.filter_id.id and config_id.model_id != config_id.filter_id.model_id:
|
if config_id.filter_id and config_id.model_id.model != config_id.filter_id.model_id:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
<!-- template demo -->
|
<!-- template demo -->
|
||||||
<record id="template_partner" model="google.drive.config">
|
<record id="template_partner" model="google.drive.config">
|
||||||
<field name="name">Customer Doc</field>
|
<field name="name">Customer Doc</field>
|
||||||
<field name="model_id">res.partner</field>
|
<field name="model_id" ref="base.model_res_partner"/>
|
||||||
<field name="filter_id" ref="filter_partner"/>
|
<field name="filter_id" ref="filter_partner"/>
|
||||||
<field name="google_drive_template_url">https://docs.google.com/spreadsheet/ccc?key=0Ah2qnrLAoZmUdGRvdVdmS1VoSDctWk1kd18taGZ4ckE#gid=0</field>
|
<field name="google_drive_template_url">https://docs.google.com/spreadsheet/ccc?key=0Ah2qnrLAoZmUdGRvdVdmS1VoSDctWk1kd18taGZ4ckE#gid=0</field>
|
||||||
<field name="name_template">%(name)s_%(model)s_%(filter)s_gdrive</field>
|
<field name="name_template">%(name)s_%(model)s_%(filter)s_gdrive</field>
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
<field name="model">google.drive.config</field>
|
<field name="model">google.drive.config</field>
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<form string="Google Drive Configuration" version="7.0">
|
<form string="Google Drive Configuration" version="7.0">
|
||||||
|
<field name="model" invisible="1" />
|
||||||
<group>
|
<group>
|
||||||
<field name="name" />
|
<field name="name" />
|
||||||
<field name="model_id" on_change="onchange_model_id(model_id)" />
|
<field name="model_id" on_change="onchange_model_id(model_id)" />
|
||||||
|
|
|
@ -59,14 +59,9 @@ openerp.google_drive = function (instance, m) {
|
||||||
var loaded = self.fetch('google.drive.config', ['google_drive_resource_id', 'google_drive_client_id'], [['id', '=', doc_item.config_id]])
|
var loaded = self.fetch('google.drive.config', ['google_drive_resource_id', 'google_drive_client_id'], [['id', '=', doc_item.config_id]])
|
||||||
.then(function (configs) {
|
.then(function (configs) {
|
||||||
var ds = new instance.web.DataSet(self, 'google.drive.config');
|
var ds = new instance.web.DataSet(self, 'google.drive.config');
|
||||||
ds.call('get_google_doc_name', [[doc_item.config_id], doc_item.res_id,configs[0].google_drive_resource_id]).done(function (r) {
|
ds.call('get_google_drive_url', [doc_item.config_id, doc_item.res_id,configs[0].google_drive_resource_id]).done(function (url) {
|
||||||
if (!_.isEmpty(r)) {
|
if (url){
|
||||||
_.each(r, function (res) {
|
window.open(url, '_blank');
|
||||||
if(res.url)
|
|
||||||
{
|
|
||||||
window.open(res.url, '_blank');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue