Skip to content

Instantly share code, notes, and snippets.

@hbrunn
Created November 18, 2015 10:03
Show Gist options
  • Save hbrunn/174be7844b409666a281 to your computer and use it in GitHub Desktop.
Save hbrunn/174be7844b409666a281 to your computer and use it in GitHub Desktop.
cache_messup_demo.py
#!/usr/bin/env python2
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('odoo_basedir')
parser.add_argument('odoo_cfg')
parser.add_argument('odoo_db')
args = parser.parse_args()
import sys
sys.path.insert(0, args.odoo_basedir)
from openerp import SUPERUSER_ID, api, tools
from openerp.sql_db import db_connect
tools.config.parse_config(['-c', args.odoo_cfg])
cr = db_connect(args.odoo_db).cursor()
uid = SUPERUSER_ID
def print_context_cache():
print '-----'
for env in api.Environment.envs.envs:
if not env.context:
# this is the context introduced by env.ref()
continue
print 'context: %s\nargs: %s\ncache: %s' % (
env.context,
env.args[2],
env.cache[env['res.partner.category']._fields['display_name']]
)
print '-----'
with api.Environment.manage():
env = api.Environment(cr, uid, {'partner_category_display': 'short'})
print 'after first fetch %s' % (
env.ref('base.res_partner_category_5').display_name)
print_context_cache()
env.context = {'partner_category_display': 'long'}
print 'after changing context %s, %s' % (
env.ref('base.res_partner_category_5').display_name,
env.ref('base.res_partner_category_4').display_name)
print_context_cache()
env = api.Environment(cr, uid, {'partner_category_display': 'short'})
print 'after requesting same context %s, %s' % (
env.ref('base.res_partner_category_5').display_name,
env.ref('base.res_partner_category_4').display_name)
print_context_cache()
env = api.Environment(cr, uid, {'partner_category_display': 'long'})
print 'after requesting coerced context %s, %s' % (
env.ref('base.res_partner_category_5').display_name,
env.ref('base.res_partner_category_4').display_name)
print_context_cache()
env
cr.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment