[MERGE] forward port of branch saas-3 up to revid 5112 chs@openerp.com-20140407144238-3s38zadkh7yxpdy7

bzr revid: chs@openerp.com-20140407160548-ttuc877kknuycoqw
This commit is contained in:
Christophe Simonis 2014-04-07 18:05:48 +02:00
commit 0161666e8b
15 changed files with 255 additions and 140 deletions

View File

@ -945,6 +945,7 @@ class ir_actions_server(osv.osv):
'user': user, 'user': user,
'context': context, 'context': context,
'workflow': workflow 'workflow': workflow
'Warning': openerp.exceptions.Warning,
} }
def run(self, cr, uid, ids, context=None): def run(self, cr, uid, ids, context=None):

View File

@ -85,8 +85,8 @@ class ir_http(osv.AbstractModel):
return auth_method return auth_method
def _handle_exception(self, exception): def _handle_exception(self, exception):
# If handle exception return something different than None, it will be used as a response # If handle_exception returns something different than None, it will be used as a response
raise return request._handle_exception(exception)
def _dispatch(self): def _dispatch(self):
# locate the controller method # locate the controller method

View File

@ -603,7 +603,7 @@ class DateTimeConverter(osv.AbstractModel):
if isinstance(value, basestring): if isinstance(value, basestring):
value = datetime.datetime.strptime( value = datetime.datetime.strptime(
value, openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT) value, openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT)
value = column.context_timestamp( value = fields.datetime.context_timestamp(
cr, uid, timestamp=value, context=context) cr, uid, timestamp=value, context=context)
if options and 'format' in options: if options and 'format' in options:

View File

@ -73,12 +73,18 @@ class view_custom(osv.osv):
class view(osv.osv): class view(osv.osv):
_name = 'ir.ui.view' _name = 'ir.ui.view'
def _get_model_data(self, cr, uid, ids, *args, **kwargs): def _get_model_data(self, cr, uid, ids, fname, args, context=None):
ir_model_data = self.pool.get('ir.model.data') result = dict.fromkeys(ids, False)
data_ids = ir_model_data.search(cr, uid, [('model', '=', self._name), ('res_id', 'in', ids)]) IMD = self.pool['ir.model.data']
result = dict(zip(ids, data_ids)) data_ids = IMD.search_read(cr, uid, [('res_id', 'in', ids), ('model', '=', 'ir.ui.view')], ['res_id'], context=context)
result.update(map(itemgetter('res_id', 'id'), data_ids))
return result return result
def _views_from_model_data(self, cr, uid, ids, context=None):
IMD = self.pool['ir.model.data']
data_ids = IMD.search_read(cr, uid, [('id', 'in', ids), ('model', '=', 'ir.ui.view')], ['res_id'], context=context)
return map(itemgetter('res_id'), data_ids)
_columns = { _columns = {
'name': fields.char('View Name', required=True), 'name': fields.char('View Name', required=True),
'model': fields.char('Object', select=True), 'model': fields.char('Object', select=True),
@ -97,7 +103,11 @@ class view(osv.osv):
'inherit_id': fields.many2one('ir.ui.view', 'Inherited View', ondelete='cascade', select=True), 'inherit_id': fields.many2one('ir.ui.view', 'Inherited View', ondelete='cascade', select=True),
'inherit_children_ids': fields.one2many('ir.ui.view','inherit_id', 'Inherit Views'), 'inherit_children_ids': fields.one2many('ir.ui.view','inherit_id', 'Inherit Views'),
'field_parent': fields.char('Child Field'), 'field_parent': fields.char('Child Field'),
'model_data_id': fields.function(_get_model_data, type='many2one', relation='ir.model.data', string="Model Data", store=True), 'model_data_id': fields.function(_get_model_data, type='many2one', relation='ir.model.data', string="Model Data",
store={
_name: (lambda s, c, u, i, ctx=None: i, None, 10),
'ir.model.data': (_views_from_model_data, ['model', 'res_id'], 10),
}),
'xml_id': fields.function(osv.osv.get_xml_id, type='char', size=128, string="External ID", 'xml_id': fields.function(osv.osv.get_xml_id, type='char', size=128, string="External ID",
help="ID of the view defined in xml file"), help="ID of the view defined in xml file"),
'groups_id': fields.many2many('res.groups', 'ir_ui_view_group_rel', 'view_id', 'group_id', 'groups_id': fields.many2many('res.groups', 'ir_ui_view_group_rel', 'view_id', 'group_id',

View File

@ -37,6 +37,23 @@ and Ubuntu distros, we have to override the search path, too.
""" """
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
# Alternatives for the [broken] builtin PDF fonts. Default order chosen to match
# the pre-v8 mapping from openerp.report.render.rml2pdf.customfonts.CustomTTFonts.
# Format: [ (BuiltinFontFamily, mode, [AlternativeFontName, ...]), ...]
BUILTIN_ALTERNATIVES = [
('Helvetica', "normal", ["DejaVuSans", "LiberationSans"]),
('Helvetica', "bold", ["DejaVuSans-Bold", "LiberationSans-Bold"]),
('Helvetica', 'italic', ["DejaVuSans-Oblique", "LiberationSans-Italic"]),
('Helvetica', 'bolditalic', ["DejaVuSans-BoldOblique", "LiberationSans-BoldItalic"]),
('Times', 'normal', ["LiberationSerif", "DejaVuSerif"]),
('Times', 'bold', ["LiberationSerif-Bold", "DejaVuSerif-Bold"]),
('Times', 'italic', ["LiberationSerif-Italic", "DejaVuSerif-Italic"]),
('Times', 'bolditalic', ["LiberationSerif-BoldItalic", "DejaVuSerif-BoldItalic"]),
('Courier', 'normal', ["FreeMono", "DejaVuSansMono"]),
('Courier', 'bold', ["FreeMonoBold", "DejaVuSansMono-Bold"]),
('Courier', 'italic', ["FreeMonoOblique", "DejaVuSansMono-Oblique"]),
('Courier', 'bolditalic', ["FreeMonoBoldOblique", "DejaVuSansMono-BoldOblique"]),
]
class res_font(osv.Model): class res_font(osv.Model):
_name = "res.font" _name = "res.font"
@ -113,9 +130,32 @@ class res_font(osv.Model):
def _sync(self, cr, uid, context=None): def _sync(self, cr, uid, context=None):
"""Set the customfonts.CustomTTFonts list to the content of the database""" """Set the customfonts.CustomTTFonts list to the content of the database"""
customfonts.CustomTTFonts = [] customfonts.CustomTTFonts = []
local_family_modes = set()
local_font_paths = {}
found_fonts_ids = self.search(cr, uid, [('path', '!=', '/dev/null')], context=context) found_fonts_ids = self.search(cr, uid, [('path', '!=', '/dev/null')], context=context)
for font in self.browse(cr, uid, found_fonts_ids, context=None): for font in self.browse(cr, uid, found_fonts_ids, context=None):
local_family_modes.add((font.family, font.mode))
local_font_paths[font.name] = font.path
customfonts.CustomTTFonts.append((font.family, font.name, font.path, font.mode)) customfonts.CustomTTFonts.append((font.family, font.name, font.path, font.mode))
# Attempt to remap the builtin fonts (Helvetica, Times, Courier) to better alternatives
# if available, because they only support a very small subset of unicode
# (missing 'č' for example)
for builtin_font_family, mode, alts in BUILTIN_ALTERNATIVES:
if (builtin_font_family, mode) not in local_family_modes:
# No local font exists with that name, try alternatives
for altern_font in alts:
if local_font_paths.get(altern_font):
altern_def = (builtin_font_family, altern_font,
local_font_paths[altern_font], mode)
customfonts.CustomTTFonts.append(altern_def)
_logger.debug("Builtin remapping %r", altern_def)
break
else:
_logger.warning("No local alternative found for builtin font `%s` (%s mode)."
"Consider installing the DejaVu fonts if you have problems "
"with unicode characters in RML reports",
builtin_font_family, mode)
return True return True
def clear_caches(self): def clear_caches(self):

View File

@ -28,6 +28,7 @@ import openerp
from openerp import SUPERUSER_ID from openerp import SUPERUSER_ID
from openerp import tools from openerp import tools
from openerp.osv import osv, fields from openerp.osv import osv, fields
from openerp.osv.expression import get_unaccent_wrapper
from openerp.tools.translate import _ from openerp.tools.translate import _
class format_address(object): class format_address(object):
@ -228,10 +229,10 @@ class res_partner(osv.osv, format_address):
_order = "display_name" _order = "display_name"
_columns = { _columns = {
'name': fields.char('Name', size=128, required=True, select=True), 'name': fields.char('Name', size=128, required=True, select=True),
'display_name': fields.function(_display_name, type='char', string='Name', store=_display_name_store_triggers), 'display_name': fields.function(_display_name, type='char', string='Name', store=_display_name_store_triggers, select=True),
'date': fields.date('Date', select=1), 'date': fields.date('Date', select=1),
'title': fields.many2one('res.partner.title', 'Title'), 'title': fields.many2one('res.partner.title', 'Title'),
'parent_id': fields.many2one('res.partner', 'Related Company'), 'parent_id': fields.many2one('res.partner', 'Related Company', select=True),
'child_ids': fields.one2many('res.partner', 'parent_id', 'Contacts', domain=[('active','=',True)]), # force "active_test" domain to bypass _search() override 'child_ids': fields.one2many('res.partner', 'parent_id', 'Contacts', domain=[('active','=',True)]), # force "active_test" domain to bypass _search() override
'ref': fields.char('Reference', size=64, select=1), 'ref': fields.char('Reference', size=64, select=1),
'lang': fields.selection(_lang_get, 'Language', 'lang': fields.selection(_lang_get, 'Language',
@ -629,10 +630,17 @@ class res_partner(osv.osv, format_address):
if operator in ('=ilike', '=like'): if operator in ('=ilike', '=like'):
operator = operator[1:] operator = operator[1:]
query = ('SELECT id FROM res_partner ' + unaccent = get_unaccent_wrapper(cr)
where_str + '(email ' + operator + ''' %s
OR display_name ''' + operator + ''' %s) query = """SELECT id
ORDER BY display_name''') FROM res_partner
{where} ({email} {operator} {percent}
OR {display_name} {operator} {percent})
ORDER BY {display_name}
""".format(where=where_str, operator=operator,
email=unaccent('email'),
display_name=unaccent('display_name'),
percent=unaccent('%s'))
where_clause_params += [search_name, search_name] where_clause_params += [search_name, search_name]
if limit: if limit:

View File

@ -1,6 +1,7 @@
import unittest2 import unittest2
import openerp import openerp
from openerp.osv.expression import get_unaccent_wrapper
from openerp.osv.orm import BaseModel from openerp.osv.orm import BaseModel
import openerp.tests.common as common import openerp.tests.common as common
@ -124,6 +125,7 @@ class test_expression(common.TransactionCase):
def test_20_auto_join(self): def test_20_auto_join(self):
registry, cr, uid = self.registry, self.cr, self.uid registry, cr, uid = self.registry, self.cr, self.uid
unaccent = get_unaccent_wrapper(cr)
# Get models # Get models
partner_obj = registry('res.partner') partner_obj = registry('res.partner')
@ -180,8 +182,11 @@ class test_expression(common.TransactionCase):
sql_query = self.query_list[0].get_sql() sql_query = self.query_list[0].get_sql()
self.assertIn('res_partner_bank', sql_query[0], self.assertIn('res_partner_bank', sql_query[0],
"_auto_join off: ('bank_ids.name', 'like', '..') first query incorrect main table") "_auto_join off: ('bank_ids.name', 'like', '..') first query incorrect main table")
self.assertIn('"res_partner_bank"."name" like %s', sql_query[1],
expected = "%s like %s" % (unaccent('"res_partner_bank"."name"'), unaccent('%s'))
self.assertIn(expected, sql_query[1],
"_auto_join off: ('bank_ids.name', 'like', '..') first query incorrect where condition") "_auto_join off: ('bank_ids.name', 'like', '..') first query incorrect where condition")
self.assertEqual(set(['%' + name_test + '%']), set(sql_query[2]), self.assertEqual(set(['%' + name_test + '%']), set(sql_query[2]),
"_auto_join off: ('bank_ids.name', 'like', '..') first query incorrect parameter") "_auto_join off: ('bank_ids.name', 'like', '..') first query incorrect parameter")
sql_query = self.query_list[2].get_sql() sql_query = self.query_list[2].get_sql()
@ -217,8 +222,11 @@ class test_expression(common.TransactionCase):
"_auto_join on: ('bank_ids.name', 'like', '..') query incorrect main table") "_auto_join on: ('bank_ids.name', 'like', '..') query incorrect main table")
self.assertIn('"res_partner_bank" as "res_partner__bank_ids"', sql_query[0], self.assertIn('"res_partner_bank" as "res_partner__bank_ids"', sql_query[0],
"_auto_join on: ('bank_ids.name', 'like', '..') query incorrect join") "_auto_join on: ('bank_ids.name', 'like', '..') query incorrect join")
self.assertIn('"res_partner__bank_ids"."name" like %s', sql_query[1],
expected = "%s like %s" % (unaccent('"res_partner__bank_ids"."name"'), unaccent('%s'))
self.assertIn(expected, sql_query[1],
"_auto_join on: ('bank_ids.name', 'like', '..') query incorrect where condition") "_auto_join on: ('bank_ids.name', 'like', '..') query incorrect where condition")
self.assertIn('"res_partner"."id"="res_partner__bank_ids"."partner_id"', sql_query[1], self.assertIn('"res_partner"."id"="res_partner__bank_ids"."partner_id"', sql_query[1],
"_auto_join on: ('bank_ids.name', 'like', '..') query incorrect join condition") "_auto_join on: ('bank_ids.name', 'like', '..') query incorrect join condition")
self.assertEqual(set(['%' + name_test + '%']), set(sql_query[2]), self.assertEqual(set(['%' + name_test + '%']), set(sql_query[2]),
@ -296,8 +304,11 @@ class test_expression(common.TransactionCase):
sql_query = self.query_list[0].get_sql() sql_query = self.query_list[0].get_sql()
self.assertIn('"res_country"', sql_query[0], self.assertIn('"res_country"', sql_query[0],
"_auto_join on for state_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect main table") "_auto_join on for state_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect main table")
self.assertIn('"res_country"."code" like %s', sql_query[1],
expected = "%s like %s" % (unaccent('"res_country"."code"'), unaccent('%s'))
self.assertIn(expected, sql_query[1],
"_auto_join on for state_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect where condition") "_auto_join on for state_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect where condition")
self.assertEqual(['%' + name_test + '%'], sql_query[2], self.assertEqual(['%' + name_test + '%'], sql_query[2],
"_auto_join on for state_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect parameter") "_auto_join on for state_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect parameter")
sql_query = self.query_list[1].get_sql() sql_query = self.query_list[1].get_sql()
@ -327,8 +338,11 @@ class test_expression(common.TransactionCase):
"_auto_join on for country_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect main table") "_auto_join on for country_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect main table")
self.assertIn('"res_country" as "res_country_state__country_id"', sql_query[0], self.assertIn('"res_country" as "res_country_state__country_id"', sql_query[0],
"_auto_join on for country_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect join") "_auto_join on for country_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect join")
self.assertIn('"res_country_state__country_id"."code" like %s', sql_query[1],
expected = "%s like %s" % (unaccent('"res_country_state__country_id"."code"'), unaccent('%s'))
self.assertIn(expected, sql_query[1],
"_auto_join on for country_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect where condition") "_auto_join on for country_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect where condition")
self.assertIn('"res_country_state"."country_id"="res_country_state__country_id"."id"', sql_query[1], self.assertIn('"res_country_state"."country_id"="res_country_state__country_id"."id"', sql_query[1],
"_auto_join on for country_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect join condition") "_auto_join on for country_id: ('state_id.country_id.code', 'like', '..') query 1 incorrect join condition")
self.assertEqual(['%' + name_test + '%'], sql_query[2], self.assertEqual(['%' + name_test + '%'], sql_query[2],
@ -358,8 +372,11 @@ class test_expression(common.TransactionCase):
"_auto_join on: ('state_id.country_id.code', 'like', '..') query incorrect join") "_auto_join on: ('state_id.country_id.code', 'like', '..') query incorrect join")
self.assertIn('"res_country" as "res_partner__state_id__country_id"', sql_query[0], self.assertIn('"res_country" as "res_partner__state_id__country_id"', sql_query[0],
"_auto_join on: ('state_id.country_id.code', 'like', '..') query incorrect join") "_auto_join on: ('state_id.country_id.code', 'like', '..') query incorrect join")
self.assertIn('"res_partner__state_id__country_id"."code" like %s', sql_query[1],
expected = "%s like %s" % (unaccent('"res_partner__state_id__country_id"."code"'), unaccent('%s'))
self.assertIn(expected, sql_query[1],
"_auto_join on: ('state_id.country_id.code', 'like', '..') query incorrect where condition") "_auto_join on: ('state_id.country_id.code', 'like', '..') query incorrect where condition")
self.assertIn('"res_partner"."state_id"="res_partner__state_id"."id"', sql_query[1], self.assertIn('"res_partner"."state_id"="res_partner__state_id"."id"', sql_query[1],
"_auto_join on: ('state_id.country_id.code', 'like', '..') query incorrect join condition") "_auto_join on: ('state_id.country_id.code', 'like', '..') query incorrect join condition")
self.assertIn('"res_partner__state_id"."country_id"="res_partner__state_id__country_id"."id"', sql_query[1], self.assertIn('"res_partner__state_id"."country_id"="res_partner__state_id__country_id"."id"', sql_query[1],
@ -385,7 +402,9 @@ class test_expression(common.TransactionCase):
"_auto_join on one2many with domains incorrect result") "_auto_join on one2many with domains incorrect result")
# Test produced queries that domains effectively present # Test produced queries that domains effectively present
sql_query = self.query_list[0].get_sql() sql_query = self.query_list[0].get_sql()
self.assertIn('"res_partner__child_ids__bank_ids"."acc_number" like %s', sql_query[1],
expected = "%s like %s" % (unaccent('"res_partner__child_ids__bank_ids"."acc_number"'), unaccent('%s'))
self.assertIn(expected, sql_query[1],
"_auto_join on one2many with domains incorrect result") "_auto_join on one2many with domains incorrect result")
# TDE TODO: check first domain has a correct table name # TDE TODO: check first domain has a correct table name
self.assertIn('"res_partner__child_ids"."name" = %s', sql_query[1], self.assertIn('"res_partner__child_ids"."name" = %s', sql_query[1],
@ -447,6 +466,19 @@ class test_expression(common.TransactionCase):
domain = [('x', 'in', ['y', 'z']), ('a.v', '=', 'e'), '|', '|', ('a', '=', 'b'), '!', ('c', '>', 'd'), ('e', '!=', 'f'), ('g', '=', 'h')] domain = [('x', 'in', ['y', 'z']), ('a.v', '=', 'e'), '|', '|', ('a', '=', 'b'), '!', ('c', '>', 'd'), ('e', '!=', 'f'), ('g', '=', 'h')]
norm_domain = ['&', '&', '&'] + domain norm_domain = ['&', '&', '&'] + domain
assert norm_domain == expression.normalize_domain(domain), "Non-normalized domains should be properly normalized" assert norm_domain == expression.normalize_domain(domain), "Non-normalized domains should be properly normalized"
def test_translate_search(self):
Country = self.registry('res.country')
be = self.ref('base.be')
domains = [
[('name', '=', 'Belgium')],
[('name', 'ilike', 'Belgi')],
[('name', 'in', ['Belgium', 'Care Bears'])],
]
for domain in domains:
ids = Country.search(self.cr, self.uid, domain)
self.assertListEqual([be], ids)
if __name__ == '__main__': if __name__ == '__main__':
unittest2.main() unittest2.main()

View File

@ -29,6 +29,7 @@ GNU Public Licence.
(c) 2003-TODAY, Fabien Pinckaers - OpenERP SA (c) 2003-TODAY, Fabien Pinckaers - OpenERP SA
""" """
import atexit
import logging import logging
import os import os
import signal import signal
@ -78,16 +79,23 @@ def report_configuration():
('database user', config['db_user'])]: ('database user', config['db_user'])]:
_logger.info("%s: %s", name, value) _logger.info("%s: %s", name, value)
def rm_pid_file():
config = openerp.tools.config
if not openerp.evented and os.path.exists(config['pidfile']):
os.unlink(config['pidfile'])
def setup_pid_file(): def setup_pid_file():
""" Create a file with the process id written in it. """ Create a file with the process id written in it.
This function assumes the configuration has been initialized. This function assumes the configuration has been initialized.
""" """
config = openerp.tools.config config = openerp.tools.config
if config['pidfile']: if not openerp.evented and config['pidfile']:
with open(config['pidfile'], 'w') as fd: with open(config['pidfile'], 'w') as fd:
pidtext = "%d" % (os.getpid()) pidtext = "%d" % (os.getpid())
fd.write(pidtext) fd.write(pidtext)
atexit.register(rm_pid_file)
def export_translation(): def export_translation():
config = openerp.tools.config config = openerp.tools.config
@ -155,8 +163,6 @@ def main(args):
setup_pid_file() setup_pid_file()
rc = openerp.service.server.start(preload=preload, stop=stop) rc = openerp.service.server.start(preload=preload, stop=stop)
if config['pidfile']:
os.unlink(config['pidfile'])
sys.exit(rc) sys.exit(rc)
class Server(Command): class Server(Command):

View File

@ -270,6 +270,13 @@ class WebRequest(object):
self.endpoint = endpoint self.endpoint = endpoint
self.auth_method = auth self.auth_method = auth
def _handle_exception(self, exception):
"""Called within an except block to allow converting exceptions
to abitrary responses. Anything returned (except None) will
be used as response."""
raise
def _call_function(self, *args, **kwargs): def _call_function(self, *args, **kwargs):
request = self request = self
if self.endpoint.routing['type'] != self._request_type: if self.endpoint.routing['type'] != self._request_type:
@ -421,39 +428,15 @@ class JsonRequest(WebRequest):
self.params = dict(self.jsonrequest.get("params", {})) self.params = dict(self.jsonrequest.get("params", {}))
self.context = self.params.pop('context', dict(self.session.context)) self.context = self.params.pop('context', dict(self.session.context))
def dispatch(self): def _json_response(self, result=None, error=None):
""" Calls the method asked for by the JSON-RPC2 or JSONP request response = {
""" 'jsonrpc': '2.0',
if self.jsonp_handler: 'id': self.jsonrequest.get('id')
return self.jsonp_handler() }
response = {"jsonrpc": "2.0" } if error is not None:
error = None response['error'] = error
if result is not None:
try: response['result'] = result
response['id'] = self.jsonrequest.get('id')
response["result"] = self._call_function(**self.params)
except AuthenticationError, e:
_logger.exception("JSON-RPC AuthenticationError in %s.", self.httprequest.path)
se = serialize_exception(e)
error = {
'code': 100,
'message': "OpenERP Session Invalid",
'data': se
}
self._failed = e # prevent tx commit
except Exception, e:
# Mute test cursor error for runbot
if not (openerp.tools.config['test_enable'] and isinstance(e, psycopg2.OperationalError)):
_logger.exception("JSON-RPC Exception in %s.", self.httprequest.path)
se = serialize_exception(e)
error = {
'code': 200,
'message': "OpenERP Server Error",
'data': se
}
self._failed = e # prevent tx commit
if error:
response["error"] = error
if self.jsonp: if self.jsonp:
# If we use jsonp, that's mean we are called from another host # If we use jsonp, that's mean we are called from another host
@ -466,8 +449,36 @@ class JsonRequest(WebRequest):
mime = 'application/json' mime = 'application/json'
body = simplejson.dumps(response) body = simplejson.dumps(response)
r = Response(body, headers=[('Content-Type', mime), ('Content-Length', len(body))]) return Response(
return r body, headers=[('Content-Type', mime),
('Content-Length', len(body))])
def _handle_exception(self, exception):
"""Called within an except block to allow converting exceptions
to abitrary responses. Anything returned (except None) will
be used as response."""
_logger.exception("Exception during JSON request handling.")
self._failed = exception # prevent tx commit
error = {
'code': 200,
'message': "OpenERP Server Error",
'data': serialize_exception(exception)
}
if isinstance(exception, AuthenticationError):
error['code'] = 100
error['message'] = "OpenERP Session Invalid"
return self._json_response(error=error)
def dispatch(self):
""" Calls the method asked for by the JSON-RPC2 or JSONP request
"""
if self.jsonp_handler:
return self.jsonp_handler()
try:
result = self._call_function(**self.params)
return self._json_response(result)
except Exception, e:
return self._handle_exception(e)
def serialize_exception(e): def serialize_exception(e):
tmp = { tmp = {

View File

@ -429,6 +429,10 @@ def select_distinct_from_where_not_null(cr, select_field, from_table):
cr.execute('SELECT distinct("%s") FROM "%s" where "%s" is not null' % (select_field, from_table, select_field)) cr.execute('SELECT distinct("%s") FROM "%s" where "%s" is not null' % (select_field, from_table, select_field))
return [r[0] for r in cr.fetchall()] return [r[0] for r in cr.fetchall()]
def get_unaccent_wrapper(cr):
if openerp.modules.registry.RegistryManager.get(cr.dbname).has_unaccent:
return lambda x: "unaccent(%s)" % (x,)
return lambda x: x
# -------------------------------------------------- # --------------------------------------------------
# ExtendedLeaf class for managing leafs and contexts # ExtendedLeaf class for managing leafs and contexts
@ -630,7 +634,7 @@ class expression(object):
:attr list expression: the domain expression, that will be normalized :attr list expression: the domain expression, that will be normalized
and prepared and prepared
""" """
self.has_unaccent = openerp.modules.registry.RegistryManager.get(cr.dbname).has_unaccent self._unaccent = get_unaccent_wrapper(cr)
self.joins = [] self.joins = []
self.root_model = table self.root_model = table
@ -1012,10 +1016,10 @@ class expression(object):
else: else:
if field._type == 'datetime' and right and len(right) == 10: if field._type == 'datetime' and right and len(right) == 10:
if operator in ('>', '>=', '='): if operator in ('>', '<='):
right += ' 00:00:00'
elif operator in ('<', '<='):
right += ' 23:59:59' right += ' 23:59:59'
else:
right += ' 00:00:00'
push(create_substitution_leaf(leaf, (left, operator, right), working_model)) push(create_substitution_leaf(leaf, (left, operator, right), working_model))
elif field.translate and right: elif field.translate and right:
@ -1030,33 +1034,37 @@ class expression(object):
sql_operator = sql_operator[4:] if sql_operator[:3] == 'not' else '=' sql_operator = sql_operator[4:] if sql_operator[:3] == 'not' else '='
inselect_operator = 'not inselect' inselect_operator = 'not inselect'
subselect = '( SELECT res_id' \ unaccent = self._unaccent if sql_operator.endswith('like') else lambda x: x
' FROM ir_translation' \
' WHERE name = %s' \
' AND lang = %s' \
' AND type = %s'
instr = ' %s'
#Covering in,not in operators with operands (%s,%s) ,etc.
if sql_operator == 'in':
instr = ','.join(['%s'] * len(right))
subselect += ' AND value ' + sql_operator + ' ' + " (" + instr + ")" \
') UNION (' \
' SELECT id' \
' FROM "' + working_model._table + '"' \
' WHERE "' + left + '" ' + sql_operator + ' ' + " (" + instr + "))"
else:
subselect += ' AND value ' + sql_operator + instr + \
') UNION (' \
' SELECT id' \
' FROM "' + working_model._table + '"' \
' WHERE "' + left + '" ' + sql_operator + instr + ")"
params = [working_model._name + ',' + left, trans_left = unaccent('value')
context.get('lang', False) or 'en_US', quote_left = unaccent(_quote(left))
'model', instr = unaccent('%s')
right,
right, if sql_operator == 'in':
] # params will be flatten by to_sql() => expand the placeholders
instr = '(%s)' % ', '.join(['%s'] * len(right))
subselect = """(SELECT res_id
FROM ir_translation
WHERE name = %s
AND lang = %s
AND type = %s
AND {trans_left} {operator} {right}
) UNION (
SELECT id
FROM "{table}"
WHERE {left} {operator} {right}
)
""".format(trans_left=trans_left, operator=sql_operator,
right=instr, table=working_model._table, left=quote_left)
params = (
working_model._name + ',' + left,
context.get('lang') or 'en_US',
'model',
right,
right,
)
push(create_substitution_leaf(leaf, ('id', inselect_operator, (subselect, params)), working_model)) push(create_substitution_leaf(leaf, ('id', inselect_operator, (subselect, params)), working_model))
else: else:
@ -1177,10 +1185,9 @@ class expression(object):
if left in model._columns: if left in model._columns:
format = need_wildcard and '%s' or model._columns[left]._symbol_set[0] format = need_wildcard and '%s' or model._columns[left]._symbol_set[0]
if self.has_unaccent and sql_operator in ('ilike', 'not ilike'): unaccent = self._unaccent if sql_operator.endswith('like') else lambda x: x
query = '(unaccent(%s."%s") %s unaccent(%s))' % (table_alias, left, sql_operator, format) column = '%s.%s' % (table_alias, _quote(left))
else: query = '(%s %s %s)' % (unaccent(column), sql_operator, unaccent(format))
query = '(%s."%s" %s %s)' % (table_alias, left, sql_operator, format)
elif left in MAGIC_COLUMNS: elif left in MAGIC_COLUMNS:
query = "(%s.\"%s\" %s %%s)" % (table_alias, left, sql_operator) query = "(%s.\"%s\" %s %%s)" % (table_alias, left, sql_operator)
params = right params = right

View File

@ -648,7 +648,10 @@ class one2many(_column):
else: else:
cr.execute('update '+_table+' set '+self._fields_id+'=null where id=%s', (act[1],)) cr.execute('update '+_table+' set '+self._fields_id+'=null where id=%s', (act[1],))
elif act[0] == 4: elif act[0] == 4:
cr.execute("select 1 from {0} where id=%s and {1}=%s".format(_table, self._fields_id), (act[1], id)) # table of the field (parent_model in case of inherit)
field_model = self._fields_id in obj.pool[self._obj]._columns and self._obj or obj.pool[self._obj]._all_columns[self._fields_id].parent_model
field_table = obj.pool[field_model]._table
cr.execute("select 1 from {0} where id=%s and {1}=%s".format(field_table, self._fields_id), (act[1], id))
if not cr.fetchone(): if not cr.fetchone():
# Must use write() to recompute parent_store structure if needed and check access rules # Must use write() to recompute parent_store structure if needed and check access rules
obj.write(cr, user, [act[1]], {self._fields_id:id}, context=context or {}) obj.write(cr, user, [act[1]], {self._fields_id:id}, context=context or {})

View File

@ -469,7 +469,12 @@ class browse_record(object):
else: else:
new_data[field_name] = browse_null() new_data[field_name] = browse_null()
elif field_column._type in ('one2many', 'many2many') and len(result_line[field_name]): elif field_column._type in ('one2many', 'many2many') and len(result_line[field_name]):
new_data[field_name] = self._list_class([browse_record(self._cr, self._uid, id, self._table.pool[field_column._obj], self._cache, context=self._context, list_class=self._list_class, fields_process=self._fields_process) for id in result_line[field_name]], self._context) new_data[field_name] = self._list_class(
(browse_record(self._cr, self._uid, id, self._table.pool.get(field_column._obj),
self._cache, context=self._context, list_class=self._list_class,
fields_process=self._fields_process)
for id in result_line[field_name]),
context=self._context)
elif field_column._type == 'reference': elif field_column._type == 'reference':
if result_line[field_name]: if result_line[field_name]:
if isinstance(result_line[field_name], browse_record): if isinstance(result_line[field_name], browse_record):
@ -1911,7 +1916,7 @@ class BaseModel(object):
return result return result
def _view_look_dom_arch(self, cr, uid, node, view_id, context=None): def _view_look_dom_arch(self, cr, uid, node, view_id, context=None):
return self['ir.ui.view'].postprocess_and_fields( return self.pool['ir.ui.view'].postprocess_and_fields(
cr, uid, self._name, node, view_id, context=context) cr, uid, self._name, node, view_id, context=context)
def search_count(self, cr, user, args, context=None): def search_count(self, cr, user, args, context=None):
@ -4250,7 +4255,7 @@ class BaseModel(object):
if isinstance(select, (int, long)): if isinstance(select, (int, long)):
return browse_record(cr, uid, select, self, cache, context=context, list_class=self._list_class, fields_process=fields_process) return browse_record(cr, uid, select, self, cache, context=context, list_class=self._list_class, fields_process=fields_process)
elif isinstance(select, list): elif isinstance(select, list):
return self._list_class([browse_record(cr, uid, id, self, cache, context=context, list_class=self._list_class, fields_process=fields_process) for id in select], context=context) return self._list_class((browse_record(cr, uid, id, self, cache, context=context, list_class=self._list_class, fields_process=fields_process) for id in select), context=context)
else: else:
return browse_null() return browse_null()

View File

@ -88,50 +88,30 @@ def _open_image(filename, path=None):
class NumberedCanvas(canvas.Canvas): class NumberedCanvas(canvas.Canvas):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
canvas.Canvas.__init__(self, *args, **kwargs) canvas.Canvas.__init__(self, *args, **kwargs)
self._codes = [] self._saved_page_states = []
self._flag=False
self._pageCount=0
self._currentPage =0
self._pageCounter=0
self.pages={}
def showPage(self): def showPage(self):
self._currentPage +=1 self._saved_page_states.append(dict(self.__dict__))
if not self._flag:
self._pageCount += 1
else:
self.pages.update({self._currentPage:self._pageCount})
self._codes.append({'code': self._code, 'stack': self._codeStack})
self._startPage() self._startPage()
self._flag=False
def pageCount(self): def save(self):
if self.pages.get(self._pageCounter,False): """add page info to each page (page x of y)"""
self._pageNumber=0 for state in self._saved_page_states:
self._pageCounter +=1 self.__dict__.update(state)
key=self._pageCounter self.draw_page_number()
if not self.pages.get(key,False): canvas.Canvas.showPage(self)
while not self.pages.get(key,False): canvas.Canvas.save(self)
key += 1
def draw_page_number(self):
page_count = len(self._saved_page_states)
self.setFont("Helvetica", 8) self.setFont("Helvetica", 8)
self.drawRightString((self._pagesize[0]-30), (self._pagesize[1]-40), self.drawRightString((self._pagesize[0]-30), (self._pagesize[1]-40),
" %(this)i / %(total)i" % { " %(this)i / %(total)i" % {
'this': self._pageNumber+1, 'this': self._pageNumber+1,
'total': self.pages.get(key,False), 'total': page_count,
} }
) )
def save(self):
"""add page info to each page (page x of y)"""
# reset page counter
self._pageNumber = 0
for code in self._codes:
self._code = code['code']
self._codeStack = code['stack']
self.pageCount()
canvas.Canvas.showPage(self)
# self.restoreState()
self._doc.SaveToFile(self._filename, self)
class PageCount(platypus.Flowable): class PageCount(platypus.Flowable):
def __init__(self, story_count=0): def __init__(self, story_count=0):
@ -303,6 +283,9 @@ class _rml_doc(object):
from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont from reportlab.pdfbase.ttfonts import TTFont
if mode:
mode = mode.lower()
if fontname not in pdfmetrics._fonts: if fontname not in pdfmetrics._fonts:
pdfmetrics.registerFont(TTFont(fontname, filename)) pdfmetrics.registerFont(TTFont(fontname, filename))
if mode == 'all': if mode == 'all':
@ -310,14 +293,14 @@ class _rml_doc(object):
addMapping(face, 0, 1, fontname) #italic addMapping(face, 0, 1, fontname) #italic
addMapping(face, 1, 0, fontname) #bold addMapping(face, 1, 0, fontname) #bold
addMapping(face, 1, 1, fontname) #italic and bold addMapping(face, 1, 1, fontname) #italic and bold
elif (mode== 'normal') or (mode == 'regular') or (mode == 'book'): elif mode in ['italic', 'oblique']:
addMapping(face, 0, 0, fontname) #normal
elif mode == 'italic':
addMapping(face, 0, 1, fontname) #italic addMapping(face, 0, 1, fontname) #italic
elif mode == 'bold': elif mode == 'bold':
addMapping(face, 1, 0, fontname) #bold addMapping(face, 1, 0, fontname) #bold
elif mode == 'bolditalic': elif mode in ('bolditalic', 'bold italic','boldoblique', 'bold oblique'):
addMapping(face, 1, 1, fontname) #italic and bold addMapping(face, 1, 1, fontname) #italic and bold
else:
addMapping(face, 0, 0, fontname) #normal
def _textual_image(self, node): def _textual_image(self, node):
rc = '' rc = ''

View File

@ -440,7 +440,7 @@ class PreforkServer(CommonServer):
sys.exit(0) sys.exit(0)
def long_polling_spawn(self): def long_polling_spawn(self):
nargs = stripped_sys_argv('--pidfile', '--workers') nargs = stripped_sys_argv()
cmd = nargs[0] cmd = nargs[0]
cmd = os.path.join(os.path.dirname(cmd), "openerp-gevent") cmd = os.path.join(os.path.dirname(cmd), "openerp-gevent")
nargs[0] = cmd nargs[0] = cmd
@ -834,8 +834,13 @@ def _reexec(updated_modules=None):
def load_test_file_yml(registry, test_file): def load_test_file_yml(registry, test_file):
with registry.cursor() as cr: with registry.cursor() as cr:
openerp.tools.convert_yaml_import(cr, 'base', file(test_file), 'test', {}, 'test', True) openerp.tools.convert_yaml_import(cr, 'base', file(test_file), 'test', {}, 'init')
cr.rollback() if config['test_commit']:
_logger.info('test %s has been commited', test_file)
cr.commit()
else:
_logger.info('test %s has been rollbacked', test_file)
cr.rollback()
def load_test_file_py(registry, test_file): def load_test_file_py(registry, test_file):
# Locate python module based on its filename and run the tests # Locate python module based on its filename and run the tests
@ -887,10 +892,10 @@ def start(preload=None, stop=False):
""" """
global server global server
load_server_wide_modules() load_server_wide_modules()
if config['workers']: if openerp.evented:
server = PreforkServer(openerp.service.wsgi_server.application)
elif openerp.evented:
server = GeventServer(openerp.service.wsgi_server.application) server = GeventServer(openerp.service.wsgi_server.application)
elif config['workers']:
server = PreforkServer(openerp.service.wsgi_server.application)
else: else:
server = ThreadedServer(openerp.service.wsgi_server.application) server = ThreadedServer(openerp.service.wsgi_server.application)

View File

@ -70,7 +70,11 @@ def image_resize_image(base64_source, size=(1024, 1024), encoding='base64', file
image_stream = StringIO.StringIO(base64_source.decode(encoding)) image_stream = StringIO.StringIO(base64_source.decode(encoding))
image = Image.open(image_stream) image = Image.open(image_stream)
# store filetype here, as Image.new below will lose image.format # store filetype here, as Image.new below will lose image.format
filetype = filetype or image.format filetype = (filetype or image.format).upper()
filetype = {
'BMP': 'PNG',
}.get(filetype, filetype)
asked_width, asked_height = size asked_width, asked_height = size
if asked_width is None: if asked_width is None: