From 0d019b5d902a345a6adf036fd70c810039b66d1f Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Wed, 9 Apr 2014 09:09:22 +0200 Subject: [PATCH 1/4] [IMP] document, edi, procurement: use new method Registry.get_cursor() bzr revid: rco@openerp.com-20140409070922-2lbc6b2llk46n8rg --- addons/document/document.py | 4 ++-- addons/edi/edi_service.py | 2 +- addons/procurement/schedulers.py | 4 ++-- addons/procurement/wizard/orderpoint_procurement.py | 2 +- addons/procurement/wizard/schedulers_all.py | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/addons/document/document.py b/addons/document/document.py index eb51ad5ac57..85c6f71ec4d 100644 --- a/addons/document/document.py +++ b/addons/document/document.py @@ -2005,7 +2005,7 @@ class nodefd_content(StringIO, node_descriptor): par = self._get_parent() uid = par.context.uid - cr = openerp.registry(par.context.dbname).db.cursor() + cr = openerp.registry(par.context.dbname).get_cursor() try: if self.mode in ('w', 'w+', 'r+'): data = self.getvalue() @@ -2058,7 +2058,7 @@ class nodefd_static(StringIO, node_descriptor): par = self._get_parent() # uid = par.context.uid - cr = openerp.registry(par.context.dbname).db.cursor() + cr = openerp.registry(par.context.dbname).get_cursor() try: if self.mode in ('w', 'w+', 'r+'): data = self.getvalue() diff --git a/addons/edi/edi_service.py b/addons/edi/edi_service.py index dd95196ef4f..75c87bec1fa 100644 --- a/addons/edi/edi_service.py +++ b/addons/edi/edi_service.py @@ -35,7 +35,7 @@ def _edi_dispatch(db_name, method_name, *method_args): registry = openerp.modules.registry.RegistryManager.get(db_name) assert registry, 'Unknown database %s' % db_name edi = registry['edi.edi'] - cr = registry.db.cursor() + cr = registry.get_cursor() res = None res = getattr(edi, method_name)(cr, *method_args) cr.commit() diff --git a/addons/procurement/schedulers.py b/addons/procurement/schedulers.py index d82fa94c132..e0676800fdd 100644 --- a/addons/procurement/schedulers.py +++ b/addons/procurement/schedulers.py @@ -59,7 +59,7 @@ class procurement_order(osv.osv): context = {} try: if use_new_cursor: - cr = openerp.registry(use_new_cursor).db.cursor() + cr = openerp.registry(use_new_cursor).get_cursor() procurement_obj = self.pool.get('procurement.order') if not skip_exception: @@ -199,7 +199,7 @@ class procurement_order(osv.osv): if context is None: context = {} if use_new_cursor: - cr = openerp.registry(use_new_cursor).db.cursor() + cr = openerp.registry(use_new_cursor).get_cursor() orderpoint_obj = self.pool.get('stock.warehouse.orderpoint') procurement_obj = self.pool.get('procurement.order') diff --git a/addons/procurement/wizard/orderpoint_procurement.py b/addons/procurement/wizard/orderpoint_procurement.py index afc8e2a721f..e026e4109eb 100644 --- a/addons/procurement/wizard/orderpoint_procurement.py +++ b/addons/procurement/wizard/orderpoint_procurement.py @@ -49,7 +49,7 @@ class procurement_compute(osv.osv_memory): """ proc_obj = self.pool.get('procurement.order') #As this function is in a new thread, I need to open a new cursor, because the old one may be closed - new_cr = self.pool.db.cursor() + new_cr = self.pool.get_cursor() for proc in self.browse(new_cr, uid, ids, context=context): proc_obj._procure_orderpoint_confirm(new_cr, uid, automatic=proc.automatic, use_new_cursor=new_cr.dbname, context=context) #close the new cursor diff --git a/addons/procurement/wizard/schedulers_all.py b/addons/procurement/wizard/schedulers_all.py index aab9657ae5d..c1d2d293662 100644 --- a/addons/procurement/wizard/schedulers_all.py +++ b/addons/procurement/wizard/schedulers_all.py @@ -45,7 +45,7 @@ class procurement_compute_all(osv.osv_memory): """ proc_obj = self.pool.get('procurement.order') #As this function is in a new thread, i need to open a new cursor, because the old one may be closed - new_cr = self.pool.db.cursor() + new_cr = self.pool.get_cursor() for proc in self.browse(new_cr, uid, ids, context=context): proc_obj.run_scheduler(new_cr, uid, automatic=proc.automatic, use_new_cursor=new_cr.dbname,\ context=context) From 560166d5a14306180a247d2012a06b5ba362f20d Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Wed, 9 Apr 2014 11:24:50 +0200 Subject: [PATCH 2/4] [FIX] website/tests: fix test_requests by manually switching registry to test mode bzr revid: rco@openerp.com-20140409092450-2r4gx0eggw2p7lch --- addons/website/tests/test_requests.py | 85 ++++++++++++++++----------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/addons/website/tests/test_requests.py b/addons/website/tests/test_requests.py index 2a22ae04445..127aea274d5 100644 --- a/addons/website/tests/test_requests.py +++ b/addons/website/tests/test_requests.py @@ -6,6 +6,7 @@ import werkzeug.urls import lxml.html +import openerp from openerp import tools import cases @@ -43,15 +44,23 @@ class CrawlSuite(unittest2.TestSuite): def __init__(self, user=None, password=None): super(CrawlSuite, self).__init__() - self.opener = urllib2.OpenerDirector() - self.opener.add_handler(urllib2.UnknownHandler()) - self.opener.add_handler(urllib2.HTTPHandler()) - self.opener.add_handler(urllib2.HTTPSHandler()) - self.opener.add_handler(urllib2.HTTPCookieProcessor()) - self.opener.add_handler(RedirectHandler()) + registry = openerp.registry(tools.config['db_name']) + try: + # switch registry to test mode, so that requests can be made + registry.enter_test_mode() - self._authenticate(user, password) - self.user = user + self.opener = urllib2.OpenerDirector() + self.opener.add_handler(urllib2.UnknownHandler()) + self.opener.add_handler(urllib2.HTTPHandler()) + self.opener.add_handler(urllib2.HTTPSHandler()) + self.opener.add_handler(urllib2.HTTPCookieProcessor()) + self.opener.add_handler(RedirectHandler()) + + self._authenticate(user, password) + self.user = user + + finally: + registry.leave_test_mode() def _request(self, path): return self.opener.open(urlparse.urlunsplit([ @@ -79,37 +88,45 @@ class CrawlSuite(unittest2.TestSuite): assert auth.getcode() < 400, "Auth failure %d" % auth.getcode() def _wrapped_run(self, result, debug=False): - paths = [URL('/'), URL('/sitemap')] - seen = set(paths) + registry = openerp.registry(tools.config['db_name']) + try: + # switch registry to test mode, so that requests can be made + registry.enter_test_mode() - while paths: - url = paths.pop(0) - r = self._request(url.url) - url.to_case(self.user, r).run(result) + paths = [URL('/'), URL('/sitemap')] + seen = set(paths) - if r.info().gettype() != 'text/html': - continue + while paths: + url = paths.pop(0) + r = self._request(url.url) + url.to_case(self.user, r).run(result) - doc = lxml.html.fromstring(r.read()) - for link in doc.xpath('//a[@href]'): - href = link.get('href') - - # avoid repeats, even for links we won't crawl no need to - # bother splitting them if we've already ignored them - # previously - if href in seen: continue - seen.add(href) - - parts = urlparse.urlsplit(href) - - if parts.netloc or \ - not parts.path.startswith('/') or \ - parts.path == '/web' or\ - parts.path.startswith('/web/') or \ - (parts.scheme and parts.scheme not in ('http', 'https')): + if r.info().gettype() != 'text/html': continue - paths.append(URL(href, url.url)) + doc = lxml.html.fromstring(r.read()) + for link in doc.xpath('//a[@href]'): + href = link.get('href') + + # avoid repeats, even for links we won't crawl no need to + # bother splitting them if we've already ignored them + # previously + if href in seen: continue + seen.add(href) + + parts = urlparse.urlsplit(href) + + if parts.netloc or \ + not parts.path.startswith('/') or \ + parts.path == '/web' or\ + parts.path.startswith('/web/') or \ + (parts.scheme and parts.scheme not in ('http', 'https')): + continue + + paths.append(URL(href, url.url)) + + finally: + registry.leave_test_mode() class URL(object): def __init__(self, url, source=None): From 89f5844b2ab3756b2934e2f03c7286c05b442fa4 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Wed, 9 Apr 2014 11:56:59 +0200 Subject: [PATCH 3/4] [IMP] document, edi, procurement: use method Registry.cursor() (formerly get_cursor) bzr revid: rco@openerp.com-20140409095659-sxcgsdbsxvfrzwg4 --- addons/document/document.py | 4 ++-- addons/edi/edi_service.py | 2 +- addons/procurement/schedulers.py | 4 ++-- addons/procurement/wizard/orderpoint_procurement.py | 2 +- addons/procurement/wizard/schedulers_all.py | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/addons/document/document.py b/addons/document/document.py index 85c6f71ec4d..9008f32cd20 100644 --- a/addons/document/document.py +++ b/addons/document/document.py @@ -2005,7 +2005,7 @@ class nodefd_content(StringIO, node_descriptor): par = self._get_parent() uid = par.context.uid - cr = openerp.registry(par.context.dbname).get_cursor() + cr = openerp.registry(par.context.dbname).cursor() try: if self.mode in ('w', 'w+', 'r+'): data = self.getvalue() @@ -2058,7 +2058,7 @@ class nodefd_static(StringIO, node_descriptor): par = self._get_parent() # uid = par.context.uid - cr = openerp.registry(par.context.dbname).get_cursor() + cr = openerp.registry(par.context.dbname).cursor() try: if self.mode in ('w', 'w+', 'r+'): data = self.getvalue() diff --git a/addons/edi/edi_service.py b/addons/edi/edi_service.py index 75c87bec1fa..f85d0be7767 100644 --- a/addons/edi/edi_service.py +++ b/addons/edi/edi_service.py @@ -35,7 +35,7 @@ def _edi_dispatch(db_name, method_name, *method_args): registry = openerp.modules.registry.RegistryManager.get(db_name) assert registry, 'Unknown database %s' % db_name edi = registry['edi.edi'] - cr = registry.get_cursor() + cr = registry.cursor() res = None res = getattr(edi, method_name)(cr, *method_args) cr.commit() diff --git a/addons/procurement/schedulers.py b/addons/procurement/schedulers.py index e0676800fdd..b86a799e19c 100644 --- a/addons/procurement/schedulers.py +++ b/addons/procurement/schedulers.py @@ -59,7 +59,7 @@ class procurement_order(osv.osv): context = {} try: if use_new_cursor: - cr = openerp.registry(use_new_cursor).get_cursor() + cr = openerp.registry(use_new_cursor).cursor() procurement_obj = self.pool.get('procurement.order') if not skip_exception: @@ -199,7 +199,7 @@ class procurement_order(osv.osv): if context is None: context = {} if use_new_cursor: - cr = openerp.registry(use_new_cursor).get_cursor() + cr = openerp.registry(use_new_cursor).cursor() orderpoint_obj = self.pool.get('stock.warehouse.orderpoint') procurement_obj = self.pool.get('procurement.order') diff --git a/addons/procurement/wizard/orderpoint_procurement.py b/addons/procurement/wizard/orderpoint_procurement.py index e026e4109eb..c8d41c5c655 100644 --- a/addons/procurement/wizard/orderpoint_procurement.py +++ b/addons/procurement/wizard/orderpoint_procurement.py @@ -49,7 +49,7 @@ class procurement_compute(osv.osv_memory): """ proc_obj = self.pool.get('procurement.order') #As this function is in a new thread, I need to open a new cursor, because the old one may be closed - new_cr = self.pool.get_cursor() + new_cr = self.pool.cursor() for proc in self.browse(new_cr, uid, ids, context=context): proc_obj._procure_orderpoint_confirm(new_cr, uid, automatic=proc.automatic, use_new_cursor=new_cr.dbname, context=context) #close the new cursor diff --git a/addons/procurement/wizard/schedulers_all.py b/addons/procurement/wizard/schedulers_all.py index c1d2d293662..c0f1cfcc2df 100644 --- a/addons/procurement/wizard/schedulers_all.py +++ b/addons/procurement/wizard/schedulers_all.py @@ -45,7 +45,7 @@ class procurement_compute_all(osv.osv_memory): """ proc_obj = self.pool.get('procurement.order') #As this function is in a new thread, i need to open a new cursor, because the old one may be closed - new_cr = self.pool.get_cursor() + new_cr = self.pool.cursor() for proc in self.browse(new_cr, uid, ids, context=context): proc_obj.run_scheduler(new_cr, uid, automatic=proc.automatic, use_new_cursor=new_cr.dbname,\ context=context) From cde05592cefe405b845eb377b99e094e2281f1b8 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Wed, 9 Apr 2014 15:44:40 +0200 Subject: [PATCH 4/4] [IMP] edi: use context manager for new cursor bzr revid: rco@openerp.com-20140409134440-ylhri8kl6mwv4g2k --- addons/edi/edi_service.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/addons/edi/edi_service.py b/addons/edi/edi_service.py index f85d0be7767..0d3edd451d1 100644 --- a/addons/edi/edi_service.py +++ b/addons/edi/edi_service.py @@ -34,18 +34,14 @@ def _edi_dispatch(db_name, method_name, *method_args): try: registry = openerp.modules.registry.RegistryManager.get(db_name) assert registry, 'Unknown database %s' % db_name - edi = registry['edi.edi'] - cr = registry.cursor() - res = None - res = getattr(edi, method_name)(cr, *method_args) - cr.commit() + with registry.cursor() as cr: + edi = registry['edi.edi'] + return getattr(edi, method_name)(cr, *method_args) + except Exception, e: _logger.exception('Failed to execute EDI method %s with args %r.', method_name, method_args) raise - finally: - cr.close() - return res def exp_import_edi_document(db_name, uid, passwd, edi_document, context=None): return _edi_dispatch(db_name, 'import_edi', uid, edi_document, None)