From e1f5158d7d6202a892ad5bdf9187d72817f74899 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Thu, 4 Nov 2010 18:05:23 +0100 Subject: [PATCH] [ADD] fields.binary: support 'bin_size_XXX' context flags to selectively enable returning size/contents of a binary field - pending API change after 6.0 bzr revid: odo@openerp.com-20101104170523-z1n4jop5e46t3wtu --- bin/osv/fields.py | 10 +++++++++- bin/osv/orm.py | 47 ++++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/bin/osv/fields.py b/bin/osv/fields.py index 664f51128ee..16fc24ee394 100644 --- a/bin/osv/fields.py +++ b/bin/osv/fields.py @@ -254,7 +254,15 @@ class binary(_column): if v['id'] == i: val = v[name] break - if context.get('bin_size', False) and val: + + # If client is requesting only the size of the field, we return it instead + # of the content. Presumably a separate request will be done to read the actual + # content if it's needed at some point. + # TODO: after 6.0 we should consider returning a dict with size and content instead of + # having an implicit convention for the value + import pdb + pdb.set_trace() + if val and context.get('bin_size_%s' % name, context.get('bin_size')): res[i] = tools.human_size(long(val)) else: res[i] = val diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 1dac64dc9a3..dbb15ca6194 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -1012,6 +1012,27 @@ class orm_template(object): return (done, 0, 0, 0) def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'): + """ + Read records with given ids with the given fields + + :param cr: database cursor + :param user: current user id + :param ids: id or list of the ids of the records to read + :param fields: optional list of field names to return (default: all fields would be returned) + :type fields: list (example ['field_name_1', ...]) + :param context: optional context dictionary - it may contains keys for specifying certain options + like ``context_lang``, ``context_tz`` to alter the results of the call. + A special ``bin_size`` boolean flag may also be passed in the context to request the + value of all fields.binary columns to be returned as the size of the binary instead of its + contents. This can also be selectively overriden by passing a field-specific flag + in the form ``bin_size_XXX: True/False`` where ``XXX`` is the name of the field. + Note: The ``bin_size_XXX`` form is new in OpenERP v6.0. + :return: list of dictionaries((dictionary per record asked)) with requested field values + :rtype: [{‘name_of_the_field’: value, ...}, ...] + :raise AccessError: * if user has no read rights on the requested object + * if user tries to bypass access rules for read on the requested object + + """ raise NotImplementedError(_('The read method is not implemented on this object !')) def get_invalid_fields(self, cr, uid): @@ -1049,8 +1070,15 @@ class orm_template(object): :param fields_list: list of fields to get the default values for (example ['field1', 'field2',]) :type fields_list: list - :param context: usual context dictionary - it may contains keys in the form ``default_XXX``, - where XXX is a field name to set or override a default value. + :param context: optional context dictionary - it may contains keys for specifying certain options + like ``context_lang`` (language) or ``context_tz`` (timezone) to alter the results of the call. + It may contain keys in the form ``default_XXX`` (where XXX is a field name), to set + or override a default value for a field. + A special ``bin_size`` boolean flag may also be passed in the context to request the + value of all fields.binary columns to be returned as the size of the binary instead of its + contents. This can also be selectively overriden by passing a field-specific flag + in the form ``bin_size_XXX: True/False`` where ``XXX`` is the name of the field. + Note: The ``bin_size_XXX`` form is new in OpenERP v6.0. :return: dictionary of the default values (set on the object model class, through user preferences, or in the context) """ # trigger view init hook @@ -2941,21 +2969,6 @@ class orm(orm_template): return super(orm, self).fields_get(cr, user, fields, context, write_access) def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'): - """ - Read records with given ids with the given fields - - :param cr: database cursor - :param user: current user id - :param ids: id or list of the ids of the records to read - :param fields: optional list of field names to return (default: all fields would be returned) - :type fields: list (example ['field_name_1', ...]) - :param context: (optional) context arguments, like lang, time zone - :return: list of dictionaries((dictionary per record asked)) with requested field values - :rtype: [{‘name_of_the_field’: value, ...}, ...] - :raise AccessError: * if user has no read rights on the requested object - * if user tries to bypass access rules for read on the requested object - - """ if not context: context = {} self.pool.get('ir.model.access').check(cr, user, self._name, 'read', context=context)