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.

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