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.

63 lines
2.6 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. name = fields.Char(u"Path")
  28. root_url = fields.Char(u"Root URL")
  29. user_id = fields.Many2one(
  30. 'res.users', string=u"User")
  31. http_session_id = fields.Many2one(
  32. 'auditlog.http.session', string=u"Session")
  33. user_context = fields.Char(u"Context")
  34. log_ids = fields.One2many(
  35. 'auditlog.log', 'http_request_id', string=u"Logs")
  36. @api.model
  37. def current_http_request(self):
  38. """Create a log corresponding to the current HTTP request, and returns
  39. its ID. This method can be called several times during the
  40. HTTP query/response cycle, it will only log the request on the
  41. first call.
  42. If no HTTP request is available, returns `False`.
  43. """
  44. http_session_model = self.env['auditlog.http.session']
  45. httprequest = request.httprequest
  46. if httprequest:
  47. if hasattr(httprequest, 'auditlog_http_request_id'):
  48. return httprequest.auditlog_http_request_id
  49. vals = {
  50. 'name': httprequest.path,
  51. 'root_url': httprequest.url_root,
  52. 'user_id': request.uid,
  53. 'http_session_id': http_session_model.current_http_session(),
  54. 'user_context': request.context,
  55. }
  56. httprequest.auditlog_http_request_id = self.create(vals).id
  57. return httprequest.auditlog_http_request_id
  58. return False