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.

81 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. from odoo import _, api, fields, models
  5. class Export(models.Model):
  6. _name = 'export.event'
  7. _description = 'Data Export Record'
  8. name = fields.Char()
  9. model_id = fields.Many2one(
  10. 'ir.model',
  11. string='Exported Model',
  12. readonly=True,
  13. )
  14. user_id = fields.Many2one(
  15. 'res.users',
  16. string='Exported by',
  17. readonly=True,
  18. )
  19. field_ids = fields.Many2many(
  20. 'ir.model.fields',
  21. string='Exported Fields',
  22. readonly=True,
  23. )
  24. record_ids = fields.Many2many(
  25. 'ir.model.data',
  26. string='Exported Records',
  27. readonly=True,
  28. )
  29. @api.model
  30. def log_export(self, recordset, field_names):
  31. date = fields.Datetime.now()
  32. model_name = recordset._name
  33. model = self.env['ir.model'].search([('model', '=', model_name)])
  34. user = self.env.user
  35. name_data = {'date': date, 'model': model.name, 'user': user.name}
  36. name = '%(date)s / %(model)s / %(user)s' % name_data
  37. exported_fields = self.env['ir.model.fields'].search([
  38. ('model', '=', model_name),
  39. ('name', 'in', field_names),
  40. ])
  41. records = self.env['ir.model.data'].search([
  42. ('model', '=', model_name),
  43. ('res_id', 'in', recordset.ids),
  44. ])
  45. export = self.create({
  46. 'name': name,
  47. 'model_id': model.id,
  48. 'field_ids': [(6, 0, exported_fields.ids)],
  49. 'record_ids': [(6, 0, records.ids)],
  50. 'user_id': user.id,
  51. })
  52. export.sudo().post_notification()
  53. return export
  54. @api.multi
  55. def post_notification(self):
  56. channel = self.env.ref('base_export_security.export_channel')
  57. field_labels = ', '.join(
  58. self.field_ids.mapped('field_description'),
  59. )
  60. message_data = {
  61. 'records': len(self.record_ids),
  62. 'model': self.model_id.name,
  63. 'user': self.user_id.name,
  64. 'fields': field_labels,
  65. }
  66. message_body = _(
  67. '%(records)d <b>%(model)s</b> records exported by <b>%(user)s'
  68. '</b>.<br><b>Fields exported:</b> %(fields)s'
  69. ) % message_data
  70. message = channel.message_post(
  71. body=message_body,
  72. message_type='notification',
  73. subtype='mail.mt_comment',
  74. )
  75. return message