import base64
import datetime as DT
import logging
+import pytz
import re
-import string
-import sys
import xmlrpclib
from psycopg2 import Binary
import openerp
-import openerp.netsvc as netsvc
import openerp.tools as tools
from openerp.tools.translate import _
from openerp.tools import float_round, float_repr
import simplejson
+from openerp.tools.html_sanitize import html_sanitize
+from openerp import SUPERUSER_ID
_logger = logging.getLogger(__name__)
def _symbol_set(symb):
- if symb == None or symb == False:
+ if symb is None or symb == False:
return None
elif isinstance(symb, unicode):
return symb.encode('utf-8')
self.manual = manual
self.selectable = True
self.group_operator = args.get('group_operator', False)
+ self.groups = False # CSV list of ext IDs of groups that can access this field
+ self.deprecated = False # Optional deprecation warning
for a in args:
if args[a]:
setattr(self, a, args[a])
-
+
def restart(self):
pass
res = obj.read(cr, uid, ids, [name], context=context)
return [x[name] for x in res]
+ def as_display_name(self, cr, uid, obj, value, context=None):
+ """Converts a field value to a suitable string representation for a record,
+ e.g. when this field is used as ``rec_name``.
+
+ :param obj: the ``BaseModel`` instance this column belongs to
+ :param value: a proper value as returned by :py:meth:`~openerp.orm.osv.BaseModel.read`
+ for this column
+ """
+ # delegated to class method, so a column type A can delegate
+ # to a column type B.
+ return self._as_display_name(self, cr, uid, obj, value, context=None)
+
+ @classmethod
+ def _as_display_name(cls, field, cr, uid, obj, value, context=None):
+ # This needs to be a class method, in case a column type A as to delegate
+ # to a column type B.
+ return tools.ustr(value)
# ---------------------------------------------------------
# Simple fields
def __init__(self, string='unknown', required=False, **args):
super(boolean, self).__init__(string=string, required=required, **args)
if required:
- _logger.warning(
+ _logger.debug(
"required=True is deprecated: making a boolean field"
" `required` has no effect, as NULL values are "
"automatically turned into False.")
def __init__(self, string='unknown', required=False, **args):
super(integer, self).__init__(string=string, required=required, **args)
- if required:
- _logger.warning(
- "required=True is deprecated: making an integer field"
- " `required` has no effect, as NULL values are "
- "automatically turned into 0.")
-
-class integer_big(_column):
- """Experimental 64 bit integer column type, currently unused.
-
- TODO: this field should work fine for values up
- to 32 bits, but greater values will not fit
- in the XML-RPC int type, so a specific
- get() method is needed to pass them as floats,
- like what we do for integer functional fields.
- """
- _type = 'integer_big'
- # do not reference the _symbol_* of integer class, as that would possibly
- # unbind the lambda functions
- _symbol_c = '%s'
- _symbol_f = lambda x: int(x or 0)
- _symbol_set = (_symbol_c, _symbol_f)
- _symbol_get = lambda self,x: x or 0
- _deprecated = True
-
- def __init__(self, string='unknown', required=False, **args):
- super(integer_big, self).__init__(string=string, required=required, **args)
- if required:
- _logger.warning(
- "required=True is deprecated: making an integer_big field"
- " `required` has no effect, as NULL values are "
- "automatically turned into 0.")
class reference(_column):
_type = 'reference'
result[value['id']] = False
return result
+ @classmethod
+ def _as_display_name(cls, field, cr, uid, obj, value, context=None):
+ if value:
+ # reference fields have a 'model,id'-like value, that we need to convert
+ # to a real name
+ model_name, res_id = value.split(',')
+ model = obj.pool.get(model_name)
+ if model and res_id:
+ return model.name_get(cr, uid, [int(res_id)], context=context)[0][1]
+ return tools.ustr(value)
+
class char(_column):
_type = 'char'
- def __init__(self, string, size, **args):
- _column.__init__(self, string=string, size=size, **args)
+ def __init__(self, string="unknown", size=None, **args):
+ _column.__init__(self, string=string, size=size or None, **args)
self._symbol_set = (self._symbol_c, self._symbol_set_char)
# takes a string (encoded in utf8) and returns a string (encoded in utf8)
# * we need to remove the "symb==False" from the next line BUT
# for now too many things rely on this broken behavior
# * the symb==None test should be common to all data types
- if symb == None or symb == False:
+ if symb is None or symb == False:
return None
# we need to convert the string to a unicode object to be able
class text(_column):
_type = 'text'
+class html(text):
+ _type = 'html'
+ _symbol_c = '%s'
+ def _symbol_f(x):
+ return html_sanitize(x)
+
+ _symbol_set = (_symbol_c, _symbol_f)
+
import __builtin__
class float(_column):
self.digits = digits
# synopsis: digits_compute(cr) -> (precision, scale)
self.digits_compute = digits_compute
- if required:
- _logger.warning(
- "required=True is deprecated: making a float field"
- " `required` has no effect, as NULL values are "
- "automatically turned into 0.0.")
def digits_change(self, cr):
if self.digits_compute:
class date(_column):
_type = 'date'
+
@staticmethod
def today(*args):
""" Returns the current date in a format fit for being a
return DT.date.today().strftime(
tools.DEFAULT_SERVER_DATE_FORMAT)
+ @staticmethod
+ def context_today(model, cr, uid, context=None, timestamp=None):
+ """Returns the current date as seen in the client's timezone
+ in a format fit for date fields.
+ This method may be passed as value to initialize _defaults.
+
+ :param Model model: model (osv) for which the date value is being
+ computed - technical field, currently ignored,
+ automatically passed when used in _defaults.
+ :param datetime timestamp: optional datetime value to use instead of
+ the current date and time (must be a
+ datetime, regular dates can't be converted
+ between timezones.)
+ :param dict context: the 'tz' key in the context should give the
+ name of the User/Client timezone (otherwise
+ UTC is used)
+ :rtype: str
+ """
+ today = timestamp or DT.datetime.now()
+ context_today = None
+ if context and context.get('tz'):
+ try:
+ utc = pytz.timezone('UTC')
+ context_tz = pytz.timezone(context['tz'])
+ utc_today = utc.localize(today, is_dst=False) # UTC = no DST
+ context_today = utc_today.astimezone(context_tz)
+ except Exception:
+ _logger.debug("failed to compute context/client-specific today date, "
+ "using the UTC value for `today`",
+ exc_info=True)
+ return (context_today or today).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
+
class datetime(_column):
_type = 'datetime'
@staticmethod
return DT.datetime.now().strftime(
tools.DEFAULT_SERVER_DATETIME_FORMAT)
-class time(_column):
- _type = 'time'
- _deprecated = True
@staticmethod
- def now( *args):
- """ Returns the current time in a format fit for being a
- default value to a ``time`` field.
-
- This method should be proivided as is to the _defaults dict,
- it should not be called.
+ def context_timestamp(cr, uid, timestamp, context=None):
+ """Returns the given timestamp converted to the client's timezone.
+ This method is *not* meant for use as a _defaults initializer,
+ because datetime fields are automatically converted upon
+ display on client side. For _defaults you :meth:`fields.datetime.now`
+ should be used instead.
+
+ :param datetime timestamp: naive datetime value (expressed in UTC)
+ to be converted to the client timezone
+ :param dict context: the 'tz' key in the context should give the
+ name of the User/Client timezone (otherwise
+ UTC is used)
+ :rtype: datetime
+ :return: timestamp converted to timezone-aware datetime in context
+ timezone
"""
- return DT.datetime.now().strftime(
- tools.DEFAULT_SERVER_TIME_FORMAT)
+ assert isinstance(timestamp, DT.datetime), 'Datetime instance expected'
+ if context and context.get('tz'):
+ try:
+ utc = pytz.timezone('UTC')
+ context_tz = pytz.timezone(context['tz'])
+ utc_timestamp = utc.localize(timestamp, is_dst=False) # UTC = no DST
+ return utc_timestamp.astimezone(context_tz)
+ except Exception:
+ _logger.debug("failed to compute context/client-specific timestamp, "
+ "using the UTC value",
+ exc_info=True)
+ return timestamp
class binary(_column):
_type = 'binary'
_symbol_c = '%s'
- _symbol_f = lambda symb: symb and Binary(symb) or None
+
+ # Binary values may be byte strings (python 2.6 byte array), but
+ # the legacy OpenERP convention is to transfer and store binaries
+ # as base64-encoded strings. The base64 string may be provided as a
+ # unicode in some circumstances, hence the str() cast in symbol_f.
+ # This str coercion will only work for pure ASCII unicode strings,
+ # on purpose - non base64 data must be passed as a 8bit byte strings.
+ _symbol_f = lambda symb: symb and Binary(str(symb)) or None
+
_symbol_set = (_symbol_c, _symbol_f)
_symbol_get = lambda self, x: x and str(x)
# (4, ID) link
# (5) unlink all (only valid for one2many)
#
-#CHECKME: dans la pratique c'est quoi la syntaxe utilisee pour le 5? (5) ou (5, 0)?
-class one2one(_column):
- _classic_read = False
- _classic_write = True
- _type = 'one2one'
- _deprecated = True
-
- def __init__(self, obj, string='unknown', **args):
- _logger.warning("The one2one field is deprecated and doesn't work anymore.")
- _column.__init__(self, string=string, **args)
- self._obj = obj
-
- def set(self, cr, obj_src, id, field, act, user=None, context=None):
- if not context:
- context = {}
- obj = obj_src.pool.get(self._obj)
- self._table = obj_src.pool.get(self._obj)._table
- if act[0] == 0:
- id_new = obj.create(cr, user, act[1])
- cr.execute('update '+obj_src._table+' set '+field+'=%s where id=%s', (id_new, id))
- else:
- cr.execute('select '+field+' from '+obj_src._table+' where id=%s', (act[0],))
- id = cr.fetchone()[0]
- obj.write(cr, user, [id], act[1], context=context)
-
- def search(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, context=None):
- return obj.pool.get(self._obj).search(cr, uid, args+self._domain+[('name', 'like', value)], offset, limit, context=context)
-
class many2one(_column):
_classic_read = False
# build a dictionary of the form {'id_of_distant_resource': name_of_distant_resource}
# we use uid=1 because the visibility of a many2one field value (just id and name)
# must be the access right of the parent form and not the linked object itself.
- records = dict(obj.name_get(cr, 1,
+ records = dict(obj.name_get(cr, SUPERUSER_ID,
list(set([x for x in res.values() if isinstance(x, (int,long))])),
context=context))
for id in res:
def search(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, context=None):
return obj.pool.get(self._obj).search(cr, uid, args+self._domain+[('name', 'like', value)], offset, limit, context=context)
+
+ @classmethod
+ def _as_display_name(cls, field, cr, uid, obj, value, context=None):
+ return value[1] if isinstance(value, tuple) else tools.ustr(value)
+
class one2many(_column):
_classic_read = False
for id in ids:
res[id] = []
- ids2 = obj.pool.get(self._obj).search(cr, user, self._domain + [(self._fields_id, 'in', ids)], limit=self._limit, context=context)
+ domain = self._domain(obj) if callable(self._domain) else self._domain
+ ids2 = obj.pool.get(self._obj).search(cr, user, domain + [(self._fields_id, 'in', ids)], limit=self._limit, context=context)
for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'):
if r[self._fields_id] in res:
res[r[self._fields_id]].append(r['id'])
reverse_rel = obj._all_columns.get(self._fields_id)
assert reverse_rel, 'Trying to unlink the content of a o2m but the pointed model does not have a m2o'
# if the o2m has a static domain we must respect it when unlinking
- extra_domain = self._domain if isinstance(getattr(self, '_domain', None), list) else []
+ domain = self._domain(obj) if callable(self._domain) else self._domain
+ extra_domain = domain or []
ids_to_unlink = obj.search(cr, user, [(self._fields_id,'=',id)] + extra_domain, context=context)
# If the model has cascade deletion, we delete the rows because it is the intended behavior,
# otherwise we only nullify the reverse foreign key column.
return result
def search(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, operator='like', context=None):
- return obj.pool.get(self._obj).name_search(cr, uid, value, self._domain, operator, context=context,limit=limit)
+ domain = self._domain(obj) if callable(self._domain) else self._domain
+ return obj.pool.get(self._obj).name_search(cr, uid, value, domain, operator, context=context,limit=limit)
+
+ @classmethod
+ def _as_display_name(cls, field, cr, uid, obj, value, context=None):
+ raise NotImplementedError('One2Many columns should not be used as record name (_rec_name)')
#
# Values: (0, 0, { fields }) create
col2 = '%s_id' % dest_model._table
return (tbl, col1, col2)
+ def _get_query_and_where_params(self, cr, model, ids, values, where_params):
+ """ Extracted from ``get`` to facilitate fine-tuning of the generated
+ query. """
+ query = 'SELECT %(rel)s.%(id2)s, %(rel)s.%(id1)s \
+ FROM %(rel)s, %(from_c)s \
+ WHERE %(rel)s.%(id1)s IN %%s \
+ AND %(rel)s.%(id2)s = %(tbl)s.id \
+ %(where_c)s \
+ %(order_by)s \
+ %(limit)s \
+ OFFSET %(offset)d' \
+ % values
+ return query, where_params
+
def get(self, cr, model, ids, name, user=None, offset=0, context=None, values=None):
if not context:
context = {}
if self._limit is not None:
limit_str = ' LIMIT %d' % self._limit
- query = 'SELECT %(rel)s.%(id2)s, %(rel)s.%(id1)s \
- FROM %(rel)s, %(from_c)s \
- WHERE %(rel)s.%(id1)s IN %%s \
- AND %(rel)s.%(id2)s = %(tbl)s.id \
- %(where_c)s \
- %(order_by)s \
- %(limit)s \
- OFFSET %(offset)d' \
- % {'rel': rel,
+ query, where_params = self._get_query_and_where_params(cr, model, ids, {'rel': rel,
'from_c': from_c,
'tbl': obj._table,
'id1': id1,
'limit': limit_str,
'order_by': order_by,
'offset': offset,
- }
+ }, where_params)
+
cr.execute(query, [tuple(ids),] + where_params)
for r in cr.fetchall():
res[r[1]].append(r[0])
def search(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, operator='like', context=None):
return obj.pool.get(self._obj).search(cr, uid, args+self._domain+[('name', operator, value)], offset, limit, context=context)
+ @classmethod
+ def _as_display_name(cls, field, cr, uid, obj, value, context=None):
+ raise NotImplementedError('Many2Many columns should not be used as record name (_rec_name)')
+
def get_nice_size(value):
size = 0
self._symbol_f = boolean._symbol_f
self._symbol_set = boolean._symbol_set
- if type in ['integer','integer_big']:
+ if type == 'integer':
self._symbol_c = integer._symbol_c
self._symbol_f = integer._symbol_f
self._symbol_set = integer._symbol_set
elif not context.get('bin_raw'):
result = sanitize_binary_value(value)
- if field_type in ("integer","integer_big") and value > xmlrpclib.MAXINT:
+ if field_type == "integer" and value > xmlrpclib.MAXINT:
# integer/long values greater than 2^31-1 are not supported
# in pure XMLRPC, so we have to pass them as floats :-(
# This is not needed for stored fields and non-functional integer
# fields, as their values are constrained by the database backend
# to the same 32bits signed int limit.
- result = float(value)
+ result = __builtin__.float(value)
return result
def get(self, cr, obj, ids, name, uid=False, context=None, values=None):
if self._fnct_inv:
self._fnct_inv(obj, cr, user, id, name, value, self._fnct_inv_arg, context)
+ @classmethod
+ def _as_display_name(cls, field, cr, uid, obj, value, context=None):
+ # Function fields are supposed to emulate a basic field type,
+ # so they can delegate to the basic type for record name rendering
+ return globals()[field._type]._as_display_name(field, cr, uid, obj, value, context=context)
+
# ---------------------------------------------------------
# Related fields
# ---------------------------------------------------------
else:
res = {}.fromkeys(ids, False)
- objlst = obj.browse(cr, 1, ids, context=context)
+ objlst = obj.browse(cr, SUPERUSER_ID, ids, context=context)
for data in objlst:
if not data:
continue
# name_get as root, as seeing the name of a related
# object depends on access right of source document,
# not target, so user may not have access.
- ng = dict(obj.pool.get(self._obj).name_get(cr, 1, ids, context=context))
+ ng = dict(obj.pool.get(self._obj).name_get(cr, SUPERUSER_ID, ids, context=context))
for r in res:
if res[r]:
res[r] = (res[r], ng[res[r]])
result[-1]['relation'] = f['relation']
self._relations = result
-
class sparse(function):
def convert_value(self, obj, cr, uid, record, value, read_value, context=None):
value = value or []
if value:
# filter out deleted records as superuser
- relation_obj = obj.pool.get(self.relation)
+ relation_obj = obj.pool.get(obj._columns[field_name].relation)
value = relation_obj.exists(cr, openerp.SUPERUSER_ID, value)
if type(value) in (int,long) and field_type == 'many2one':
- relation_obj = obj.pool.get(self.relation)
+ relation_obj = obj.pool.get(obj._columns[field_name].relation)
# check for deleted record as superuser
if not relation_obj.exists(cr, openerp.SUPERUSER_ID, [value]):
value = False
# not target, so user may not have access) in order to avoid
# pointing on an unexisting record.
if property_destination_obj:
- if res[id][prop_name] and obj.pool.get(property_destination_obj).exists(cr, 1, res[id][prop_name].id):
+ if res[id][prop_name] and obj.pool.get(property_destination_obj).exists(cr, SUPERUSER_ID, res[id][prop_name].id):
name_get_ids[id] = res[id][prop_name].id
else:
res[id][prop_name] = False
# name_get as root (as seeing the name of a related
# object depends on access right of source document,
# not target, so user may not have access.)
- name_get_values = dict(obj.pool.get(property_destination_obj).name_get(cr, 1, name_get_ids.values(), context=context))
+ name_get_values = dict(obj.pool.get(property_destination_obj).name_get(cr, SUPERUSER_ID, name_get_ids.values(), context=context))
# the property field is a m2o, we need to return a tuple with (id, name)
for k, v in name_get_ids.iteritems():
if res[k][prop_name]:
"""
res = {'type': field._type}
- # This additional attributes for M2M and function field is added
- # because we need to display tooltip with this additional information
- # when client is started in debug mode.
+ # some attributes for m2m/function field are added as debug info only
if isinstance(field, function):
res['function'] = field._fnct and field._fnct.func_name or False
res['store'] = field.store
res['related_columns'] = [col1, col2]
res['third_table'] = table
for arg in ('string', 'readonly', 'states', 'size', 'required', 'group_operator',
- 'change_default', 'translate', 'help', 'select', 'selectable'):
+ 'change_default', 'translate', 'help', 'select', 'selectable', 'groups'):
if getattr(field, arg):
res[arg] = getattr(field, arg)
for arg in ('digits', 'invisible', 'filters'):
else:
# call the 'dynamic selection' function
res['selection'] = field.selection(model, cr, user, context)
- if res['type'] in ('one2many', 'many2many', 'many2one', 'one2one'):
+ if res['type'] in ('one2many', 'many2many', 'many2one'):
res['relation'] = field._obj
- res['domain'] = field._domain
+ res['domain'] = field._domain(model) if callable(field._domain) else field._domain
res['context'] = field._context
if isinstance(field, one2many):
class column_info(object):
- """Struct containing details about an osv column, either one local to
- its model, or one inherited via _inherits.
-
- :attr name: name of the column
- :attr column: column instance, subclass of osv.fields._column
- :attr parent_model: if the column is inherited, name of the model
- that contains it, None for local columns.
- :attr parent_column: the name of the column containing the m2o
- relationship to the parent model that contains
- this column, None for local columns.
- :attr original_parent: if the column is inherited, name of the original
- parent model that contains it i.e in case of multilevel
- inheritence, None for local columns.
+ """ Struct containing details about an osv column, either one local to
+ its model, or one inherited via _inherits.
+
+ .. attribute:: name
+
+ name of the column
+
+ .. attribute:: column
+
+ column instance, subclass of :class:`_column`
+
+ .. attribute:: parent_model
+
+ if the column is inherited, name of the model that contains it,
+ ``None`` for local columns.
+
+ .. attribute:: parent_column
+
+ the name of the column containing the m2o relationship to the
+ parent model that contains this column, ``None`` for local columns.
+
+ .. attribute:: original_parent
+
+ if the column is inherited, name of the original parent model that
+ contains it i.e in case of multilevel inheritance, ``None`` for
+ local columns.
"""
def __init__(self, name, column, parent_model=None, parent_column=None, original_parent=None):
self.name = name
self.parent_column = parent_column
self.original_parent = original_parent
+ def __str__(self):
+ return '%s(%s, %s, %s, %s, %s)' % (
+ self.__name__, self.name, self.column,
+ self.parent_model, self.parent_column, self.original_parent)
+
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: