You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.2 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Guewen Baconnier
  5. # Copyright 2012-2013 Camptocamp SA
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. import openerp.osv.orm
  22. # add the method in the orm so we can use it from the TranslateDialog of the
  23. # webclient instead of the normal read
  24. def read_translations(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
  25. """ Read records with given ids with the given fields, if a field is not
  26. translated, its value will be False instead of the source language's value.
  27. :param fields: optional list of field names to return (default: all fields would be returned)
  28. :type fields: list (example ['field_name_1', ...])
  29. :return: list of dictionaries((dictionary per record asked)) with requested field values
  30. :rtype: [{name_of_the_field: value, ...}, ...]
  31. :raise AccessError: * if user has no read rights on the requested object
  32. * if user tries to bypass access rules for read on the requested object
  33. """
  34. if context is None:
  35. context = {}
  36. self.check_access_rights(cr, user, 'read')
  37. fields = self.check_field_access_rights(cr, user, 'read', fields)
  38. if isinstance(ids, (int, long)):
  39. select = [ids]
  40. else:
  41. select = ids
  42. select = map(lambda x: isinstance(x, dict) and x['id'] or x, select)
  43. result = self._read_flat(cr, user, select, fields, context, load)
  44. if context.get('lang') and context['lang'] != 'en_US':
  45. fields_pre = [f for f in fields if
  46. (f in self._columns and
  47. getattr(self._columns[f], '_classic_write'))] + \
  48. self._inherits.values()
  49. for f in fields_pre:
  50. if self._columns[f].translate:
  51. res_ids = [x['id'] for x in result]
  52. res_trans = self.pool.get('ir.translation')._get_ids(
  53. cr, user,
  54. self._name + ',' + f,
  55. 'model',
  56. context['lang'],
  57. res_ids)
  58. for r in result:
  59. if not res_trans.get(r['id']):
  60. r[f] = None
  61. for r in result:
  62. for key, v in r.iteritems():
  63. if v is None:
  64. r[key] = False
  65. if isinstance(ids, (int, long, dict)):
  66. return result and result[0] or False
  67. return result
  68. openerp.osv.orm.BaseModel.read_translations = read_translations