From 1bde6631aee1ca86aee5aa7e048ae8379f18626b Mon Sep 17 00:00:00 2001 From: houssine Date: Sat, 27 Jul 2019 12:27:07 +0200 Subject: [PATCH] [MIG] migrate module to v12 and taking code from website_form_recaptcha --- website_recaptcha_reloaded/__init__.py | 26 +----- website_recaptcha_reloaded/__manifest__.py | 26 ++++++ website_recaptcha_reloaded/__openerp__.py | 45 ---------- website_recaptcha_reloaded/models/__init__.py | 24 +----- .../models/res_config.py | 58 ++++++------- .../views/res_config.xml | 49 +++++++---- .../views/website_view.xml | 8 +- website_recaptcha_reloaded/website.py | 84 +++++++++---------- 8 files changed, 134 insertions(+), 186 deletions(-) create mode 100644 website_recaptcha_reloaded/__manifest__.py delete mode 100644 website_recaptcha_reloaded/__openerp__.py diff --git a/website_recaptcha_reloaded/__init__.py b/website_recaptcha_reloaded/__init__.py index 38706fe..c0f9011 100644 --- a/website_recaptcha_reloaded/__init__.py +++ b/website_recaptcha_reloaded/__init__.py @@ -1,22 +1,4 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Tech-Receptives Solutions Pvt. Ltd. -# Copyright (C)2004-TODAY Tech Receptives() -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## -import website -import models \ No newline at end of file +# Copyright 2004 Tech-Receptives Solutions Pvt. Ltd. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import website +from . import models diff --git a/website_recaptcha_reloaded/__manifest__.py b/website_recaptcha_reloaded/__manifest__.py new file mode 100644 index 0000000..a0f5ea9 --- /dev/null +++ b/website_recaptcha_reloaded/__manifest__.py @@ -0,0 +1,26 @@ +# Copyright 2004 Tech-Receptives Solutions Pvt. Ltd. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + 'name': 'Website reCAPTCHA Reloaded', + 'version': '12.0.0.1', + 'category': 'Website', + 'depends': ['website'], + 'author': 'Tech Receptives', + 'license': 'AGPL-3', + 'website': 'https://www.techreceptives.com', + 'description': """ + Odoo Website reCAPTCHA Reloaded + ================================ + This modules allows you to integrate Google reCAPTCHA v2.0 to your website + forms. You can configure your Google reCAPTCHA site and public keys + in "Settings" -> "Website Settings" + + You will need to install various website__recaptcha modules + to use it in your various pages. + """, + 'data': [ + 'views/website_view.xml', + 'views/res_config.xml', + ], + 'installable': True, +} diff --git a/website_recaptcha_reloaded/__openerp__.py b/website_recaptcha_reloaded/__openerp__.py deleted file mode 100644 index 41e716b..0000000 --- a/website_recaptcha_reloaded/__openerp__.py +++ /dev/null @@ -1,45 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Tech-Receptives Solutions Pvt. Ltd. -# Copyright (C)2004-TODAY Tech Receptives() -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## -{ - 'name': 'Website reCAPTCHA Reloaded', - 'version': '1.0', - 'category': 'Website', - 'depends': ['website'], - 'author': 'Tech Receptives', - 'license': 'AGPL-3', - 'website': 'https://www.techreceptives.com', - 'description': """ - Odoo Website reCAPTCHA Reloaded - ================================ - This modules allows you to integrate Google reCAPTCHA v2.0 to your website - forms. You can configure your Google reCAPTCHA site and public keys - in "Settings" -> "Website Settings" - - You will need to install various website__recaptcha modules - to use it in your various pages. - """, - 'data': [ - 'views/website_view.xml', - 'views/res_config.xml', - ], - 'installable': False, - 'auto_install': False -} diff --git a/website_recaptcha_reloaded/models/__init__.py b/website_recaptcha_reloaded/models/__init__.py index a199d14..fa388d3 100644 --- a/website_recaptcha_reloaded/models/__init__.py +++ b/website_recaptcha_reloaded/models/__init__.py @@ -1,21 +1,3 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Tech-Receptives Solutions Pvt. Ltd. -# Copyright (C)2004-TODAY Tech Receptives() -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## -import res_config +# Copyright 2004 Tech-Receptives Solutions Pvt. Ltd. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import res_config diff --git a/website_recaptcha_reloaded/models/res_config.py b/website_recaptcha_reloaded/models/res_config.py index 71e6833..4f38a4f 100644 --- a/website_recaptcha_reloaded/models/res_config.py +++ b/website_recaptcha_reloaded/models/res_config.py @@ -1,36 +1,32 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Tech-Receptives Solutions Pvt. Ltd. -# Copyright (C)2004-TODAY Tech Receptives() -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# Copyright 2019 Simone Orsi - Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) -from openerp.osv import fields, osv +from odoo import api, fields, models -class website_config_settings(osv.osv_memory): - _inherit = 'website.config.settings' +class website_config_settings(models.TransientModel): + _inherit = 'res.config.settings' - _columns = { - 'recaptcha_site_key': fields.related( - 'website_id', 'recaptcha_site_key', type="char", - string='reCAPTCHA site Key'), - 'recaptcha_private_key': fields.related( - 'website_id', 'recaptcha_private_key', type="char", - string='reCAPTCHA Private Key'), - } + recaptcha_key_site = fields.Char( + related='website_id.recaptcha_key_site', + readonly=False, + ) + recaptcha_key_secret = fields.Char( + related='website_id.recaptcha_key_secret', + readonly=False, + ) + has_google_recaptcha = fields.Boolean( + 'Google reCaptcha', + compute='_compute_has_google_recaptcha', + inverse='_inverse_has_google_recaptcha', + readonly=False, + ) + @api.depends('website_id') + def _compute_has_google_recaptcha(self): + self.has_google_recaptcha = bool(self.recaptcha_key_site) + + def _inverse_has_google_recaptcha(self): + if not self.has_google_recaptcha: + self.recaptcha_key_site = False + self.recaptcha_key_secret = False diff --git a/website_recaptcha_reloaded/views/res_config.xml b/website_recaptcha_reloaded/views/res_config.xml index 2381123..24d84e4 100644 --- a/website_recaptcha_reloaded/views/res_config.xml +++ b/website_recaptcha_reloaded/views/res_config.xml @@ -4,25 +4,38 @@ Website settings - website.config.settings - + res.config.settings + - - - - +
+
+
+ +
+
+ captcha +
+
+
diff --git a/website_recaptcha_reloaded/views/website_view.xml b/website_recaptcha_reloaded/views/website_view.xml index 1e03868..dd2a077 100644 --- a/website_recaptcha_reloaded/views/website_view.xml +++ b/website_recaptcha_reloaded/views/website_view.xml @@ -3,19 +3,19 @@ - + diff --git a/website_recaptcha_reloaded/website.py b/website_recaptcha_reloaded/website.py index da23c39..8ba7217 100644 --- a/website_recaptcha_reloaded/website.py +++ b/website_recaptcha_reloaded/website.py @@ -1,49 +1,43 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Tech-Receptives Solutions Pvt. Ltd. -# Copyright (C)2004-TODAY Tech Receptives() -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## -from openerp.osv import osv, fields +# Copyright 2019 Simone Orsi - Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models, _ +from odoo.exceptions import ValidationError import requests -import json -class website(osv.osv): +URL = 'https://www.google.com/recaptcha/api/siteverify' + + +class Website(models.Model): _inherit = 'website' - _columns = { - 'recaptcha_site_key': fields.char('reCAPTCHA Site Key'), - 'recaptcha_private_key': fields.char('reCAPTCHA Private Key'), - } - - - def is_captcha_valid(self, cr, uid, ids, response, context={}): - for website in self.browse(cr, uid, ids, context=context): - get_res = {'secret': website.recaptcha_private_key,'response': response} - try: - response = requests.get('https://www.google.com/recaptcha/api/siteverify', params=get_res) - except Exception,e: - raise osv.except_osv(('Invalid Data!'),("%s.")%(e)) - res_con = json.loads(response.content) - if res_con.has_key('success') and res_con['success']: - return True - return False - - _defaults = { - 'recaptcha_site_key': "6LchkgATAAAAAAdTJ_RCvTRL7_TTcN3Zm_YXB39s", - 'recaptcha_private_key': "6LchkgATAAAAADbGqMvbRxHbTnTEkavjw1gSwCng" - } \ No newline at end of file + recaptcha_key_site = fields.Char() + recaptcha_key_secret = fields.Char() + + @api.model + def _get_error_message(self, errorcode=None): + mapping = { + 'missing-input-secret': _('The secret parameter is missing.'), + 'invalid-input-secret': + _('The secret parameter is invalid or malformed.'), + 'missing-input-response': _('The response parameter is missing.'), + 'invalid-input-response': + _('The response parameter is invalid or malformed.'), + } + return mapping.get(errorcode, _('There was a problem with ' + 'the captcha entry.')) + + def is_captcha_valid(self, response): + get_res = {'secret': self.recaptcha_key_secret, + 'response': response} + + res = requests.post(URL, data=get_res).json() + + error_msg = "\n".join(self._get_error_message(error) + for error in res.get('error-codes', [])) + if error_msg: + raise ValidationError(error_msg) + + if not res.get('success'): + raise ValidationError(self._get_error_message()) + return True