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.

48 lines
1.5 KiB

  1. # Copyright 2019 Simone Orsi - Camptocamp SA
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. import requests
  4. from odoo import _, api, fields, models
  5. from odoo.exceptions import ValidationError
  6. URL = "https://www.google.com/recaptcha/api/siteverify"
  7. class Website(models.Model):
  8. _inherit = "website"
  9. recaptcha_key_site = fields.Char()
  10. recaptcha_key_secret = fields.Char()
  11. @api.model
  12. def _get_error_message(self, errorcode=None):
  13. mapping = {
  14. "missing-input-secret": _("The secret parameter is missing."),
  15. "invalid-input-secret": _(
  16. "The secret parameter is invalid or malformed."
  17. ),
  18. "missing-input-response": _("The response parameter is missing."),
  19. "invalid-input-response": _(
  20. "The response parameter is invalid or malformed."
  21. ),
  22. }
  23. return mapping.get(
  24. errorcode, _("There was a problem with " "the captcha entry.")
  25. )
  26. def is_captcha_valid(self, response):
  27. get_res = {"secret": self.recaptcha_key_secret, "response": response}
  28. res = requests.post(URL, data=get_res).json()
  29. error_msg = "\n".join(
  30. self._get_error_message(error)
  31. for error in res.get("error-codes", [])
  32. )
  33. if error_msg:
  34. raise ValidationError(error_msg)
  35. if not res.get("success"):
  36. raise ValidationError(self._get_error_message())
  37. return True