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.

98 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # © initOS GmbH 2014
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp.osv import orm, fields
  5. import re
  6. def remove_html_tags(data):
  7. p = re.compile(r'<.*?>')
  8. return p.sub('', data)
  9. def remove_extra_spaces(data):
  10. p = re.compile(r'\s+')
  11. return p.sub(' ', data)
  12. def _type_selection(self, *args, **kwargs):
  13. """Redirect for easier overwriting."""
  14. return self.pool.get('web.note').type_selection(*args, **kwargs)
  15. class WebNote(orm.Model):
  16. _name = 'web.note'
  17. def name_get(self, cr, uid, ids, context=None):
  18. if context is None:
  19. context = {}
  20. if isinstance(ids, (int, long)):
  21. ids = [ids]
  22. res = []
  23. for record in self.browse(cr, uid, ids, context=context):
  24. name = remove_extra_spaces(remove_html_tags(record.message or ''))
  25. res.append((record.id, name))
  26. return res
  27. def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
  28. res = self.name_get(cr, uid, ids, context=context)
  29. return dict(res)
  30. def type_selection(self, cr, uid, context=None):
  31. return (('private', 'private'),
  32. ('internal', 'internal'),
  33. ('external', 'external'))
  34. def onchange_container_id(self, cr, uid, ids,
  35. container_id=False, message=None, context=None):
  36. result = {}
  37. if container_id:
  38. container = self.pool.get('web.note.container').\
  39. browse(cr, uid, container_id, context=context)
  40. result['value'] = {
  41. 'sequence':
  42. container.sequence or self._defaults.get('sequence', 0),
  43. }
  44. if container.pattern:
  45. result['value']['message'] = container.pattern
  46. return result
  47. _order = 'sequence,create_date'
  48. _columns = {
  49. 'type': fields.selection(_type_selection, 'Note type', required=True),
  50. 'message': fields.html('Message'),
  51. 'display_name':
  52. fields.function(_name_get_fnc,
  53. type="char", string='Note', store=False),
  54. 'container_id': fields.many2one('web.note.container', 'Note-Container',
  55. help='Containers include '
  56. 'templates for order and heading.'),
  57. 'sequence': fields.integer('Order in report'),
  58. 'create_uid': fields.many2one('res.users', 'User', readonly=True),
  59. }
  60. _defaults = {
  61. 'sequence': 10,
  62. }
  63. class WebNoteContainer(orm.Model):
  64. _name = 'web.note.container'
  65. _description = 'Note Container'
  66. _order = 'type, sequence'
  67. _columns = {
  68. 'name': fields.char('Name', required=True,),
  69. 'sequence': fields.integer('Order in report'),
  70. 'type': fields.selection(_type_selection, 'Report', required=True),
  71. 'pattern': fields.html('Template',
  72. help='If you select the container the '
  73. 'template is added to the note.')
  74. }
  75. _defaults = {
  76. 'sequence': 10,
  77. }