diff --git a/addons/document/document.py b/addons/document/document.py index eb51ad5ac57..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).db.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).db.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 dd95196ef4f..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.db.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) diff --git a/addons/procurement/schedulers.py b/addons/procurement/schedulers.py index d82fa94c132..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).db.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).db.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 afc8e2a721f..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.db.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 aab9657ae5d..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.db.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) 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):