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.

43 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. from odoo import api, fields, models, _
  4. from odoo.exceptions import ValidationError
  5. import requests
  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. 'missing-input-response': _('The response parameter is missing.'),
  18. 'invalid-input-response':
  19. _('The response parameter is invalid or malformed.'),
  20. }
  21. return mapping.get(errorcode, _('There was a problem with '
  22. 'the captcha entry.'))
  23. def is_captcha_valid(self, response):
  24. get_res = {'secret': self.recaptcha_key_secret,
  25. 'response': response}
  26. res = requests.post(URL, data=get_res).json()
  27. error_msg = "\n".join(self._get_error_message(error)
  28. for error in res.get('error-codes', []))
  29. if error_msg:
  30. raise ValidationError(error_msg)
  31. if not res.get('success'):
  32. raise ValidationError(self._get_error_message())
  33. return True