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.

77 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2015 ABF OSIELL (<http://osiell.com>).
  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. from openerp import models, fields, api
  22. from openerp.http import request
  23. class AuditlogHTTPRequest(models.Model):
  24. _name = 'auditlog.http.request'
  25. _description = u"Auditlog - HTTP request log"
  26. _order = "create_date DESC"
  27. _rec_name = 'display_name'
  28. display_name = fields.Char(u"Name", compute="_display_name")
  29. name = fields.Char(u"Path")
  30. root_url = fields.Char(u"Root URL")
  31. user_id = fields.Many2one(
  32. 'res.users', string=u"User")
  33. http_session_id = fields.Many2one(
  34. 'auditlog.http.session', string=u"Session")
  35. user_context = fields.Char(u"Context")
  36. log_ids = fields.One2many(
  37. 'auditlog.log', 'http_request_id', string=u"Logs")
  38. @api.multi
  39. def _display_name(self):
  40. for httprequest in self:
  41. create_date = fields.Datetime.from_string(httprequest.create_date)
  42. tz_create_date = fields.Datetime.context_timestamp(
  43. httprequest, create_date)
  44. httprequest.display_name = u"%s (%s)" % (
  45. httprequest.name or '?',
  46. fields.Datetime.to_string(tz_create_date))
  47. @api.model
  48. def current_http_request(self):
  49. """Create a log corresponding to the current HTTP request, and returns
  50. its ID. This method can be called several times during the
  51. HTTP query/response cycle, it will only log the request on the
  52. first call.
  53. If no HTTP request is available, returns `False`.
  54. """
  55. if not request:
  56. return False
  57. http_session_model = self.env['auditlog.http.session']
  58. httprequest = request.httprequest
  59. if httprequest:
  60. if hasattr(httprequest, 'auditlog_http_request_id'):
  61. return httprequest.auditlog_http_request_id
  62. vals = {
  63. 'name': httprequest.path,
  64. 'root_url': httprequest.url_root,
  65. 'user_id': request.uid,
  66. 'http_session_id': http_session_model.current_http_session(),
  67. 'user_context': request.context,
  68. }
  69. httprequest.auditlog_http_request_id = self.create(vals).id
  70. return httprequest.auditlog_http_request_id
  71. return False