Skip to content

Instantly share code, notes, and snippets.

@nseinlet
Created December 1, 2016 14:44
Show Gist options
  • Save nseinlet/55e28ae0cfbe25ff182bbc26fdecb959 to your computer and use it in GitHub Desktop.
Save nseinlet/55e28ae0cfbe25ff182bbc26fdecb959 to your computer and use it in GitHub Desktop.
# -*- encoding: utf-8 -*-
##############################################################################
#
# Odoo, Open Source Management Solution
# Copyright (C) 2004-TODAY Odoo S.A. <http://www.odoo.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import argparse
import sys
import traceback
import os
import re
import openerplib
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='add titles in custom qweb website views.')
parser.add_argument('--host', dest='host', help='name or ip of the server', default="localhost")
parser.add_argument('--port', dest='port', help='ip port of the server', default="8069")
parser.add_argument('--protocol', dest='protocol', help='protocol to use (xmlrpc, jsonrpc, xmlrpcs,...)', default="jsonrpc")
parser.add_argument('--db', dest='db', help='Database to use')
parser.add_argument('--user', dest='login', default="admin", help='Login (default=admin)')
parser.add_argument('--userid', dest='userid', default="0", help='User ID (default=0)')
parser.add_argument('--password', dest='password', default="admin", help='Password (default=admin)')
if len(sys.argv) == 1:
sys.exit(parser.print_help())
args = parser.parse_args()
try:
#Connect by xml-rpc
user_id = None
if args.userid and int(args.userid)>0:
user_id = int(args.userid)
connection = openerplib.get_connection(hostname=args.host,
port=int(args.port),
database=args.db,
login=args.login,
password=args.password,
protocol=args.protocol,
user_id=user_id)
connection.check_login(force=False)
pattern = r'src="/web/image/([0-9]+)" alt="([^"]+)"'
# arch_base, arch_db
View = connection.get_model('ir.ui.view')
Attachment = connection.get_model('ir.attachment')
view_ids = View.search([('key', 'like', 'website.'), ('type', '=', 'qweb'), ('arch_db', 'like', 'src="/web/image/')])
print "Views to alter : %s" % len(view_ids)
for view in View.read(view_ids, ['arch_db']):
arch = view['arch_db']
def replace (m):
id, dom_filename = m.groups(0)
filename = Attachment.read([int(id),], ['datas_fname',])[0]['datas_fname']
if filename: filename = filename.replace('"', '')
else: filename =dom_filename
return u'src="/web/image/%s" alt="%s" title="%s"' % (id, filename, filename)
new_arch = re.sub(pattern, replace, arch)
View.write(view['id'], {"arch_db": new_arch})
print view['id']
except Exception, e:
print e
tb = traceback.format_exc()
print tb
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment