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.

54 lines
1.8 KiB

  1. # Copyright 2018 Tecnativa - Jairo Llopis
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from datetime import datetime
  4. from werkzeug.exceptions import NotFound
  5. from odoo import SUPERUSER_ID
  6. from odoo.http import Controller, request, route
  7. from odoo.addons.web.controllers.main import ensure_db
  8. class ConsentController(Controller):
  9. @route(
  10. "/privacy/consent/<any(accept,reject):choice>/<int:consent_id>/<token>",
  11. type="http",
  12. auth="none",
  13. website=True,
  14. )
  15. def consent(self, choice, consent_id, token, *args, **kwargs):
  16. """Process user's consent acceptance or rejection."""
  17. ensure_db()
  18. try:
  19. # If there's a website, we need a user to render the template
  20. request.uid = request.website.user_id.id
  21. except AttributeError:
  22. # If there's no website, be root if there's no UID
  23. if not request.uid:
  24. request.uid = SUPERUSER_ID
  25. consent = (
  26. request.env["privacy.consent"]
  27. .with_context(subject_answering=True)
  28. .sudo()
  29. .browse(consent_id)
  30. )
  31. if not (consent.exists() and consent._token() == token):
  32. raise NotFound
  33. if consent.partner_id.lang:
  34. consent = consent.with_context(lang=consent.partner_id.lang)
  35. request.context = consent.env.context
  36. consent.action_answer(choice == "accept", self._metadata())
  37. return request.render("privacy_consent.form", {"consent": consent})
  38. def _metadata(self):
  39. return (
  40. u"User agent: {}\n"
  41. u"Remote IP: {}\n"
  42. u"Date and time: {:%Y-%m-%d %H:%M:%S}"
  43. ).format(
  44. request.httprequest.environ.get("HTTP_USER_AGENT"),
  45. request.httprequest.environ.get("REMOTE_ADDRESS"),
  46. datetime.now(),
  47. )