Created
December 1, 2016 13:01
-
-
Save nseinlet/dc8230a7b05533ef72f9a9988c1042c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 openerplib | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Remove unused currency rates.') | |
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) | |
#List models | |
currency_ids = [] | |
fields_model = connection.get_model('ir.model.fields') | |
model_model = connection.get_model('ir.model') | |
field_ids = fields_model.search(['&', '&', ('relation', '=', 'res.currency'), ('ttype', '=', 'many2one'), '!', ('model_id.model', '=', 'res.currency.rate')]) | |
fields = fields_model.read(field_ids, ['name', 'model_id']) | |
for field in fields: | |
model = model_model.read(field['model_id'][0], ['model']) | |
tmp_model = connection.get_model(model['model']) | |
try: | |
currency_groups = tmp_model.read_group(domain=[], fields=[field['name']], groupby=[field['name']]) | |
currency_ids += [rec[field['name']][0] for rec in currency_groups] | |
except Exception, e: | |
pass | |
rates_model = connection.get_model('res.currency.rate') | |
#Check number of rates | |
print "Number of rates before clean : %s" % len(rates_model.search([])) | |
#Remove rates for unused currencies | |
rates_ids = rates_model.search(['!', ('currency_id', 'in', list(set(currency_ids)))]) | |
rates_model.unlink(rates_ids) | |
#Clean same rates | |
company_model = connection.get_model('res.company') | |
company_ids = company_model.search([]) | |
for company_id in company_ids: | |
for currency_id in currency_ids: | |
rate_ids = rates_model.search([('currency_id', '=', currency_id), ('company_id', '=', company_id)], order='name') | |
rates = rates_model.read(rate_ids, ['id', 'rate']) | |
previous_rate = False | |
for rate in rates: | |
if rate['rate']==previous_rate: | |
rates_model.unlink([rate['id'], ]) | |
else: | |
previous_rate = rate['rate'] | |
#Check number of rates | |
print "Number of rates after clean : %s" % len(rates_model.search([])) | |
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