Browse Source

[MIG] migrate module to v12 and taking code from website_form_recaptcha

pull/12/head
houssine 5 years ago
parent
commit
1bde6631ae
  1. 26
      website_recaptcha_reloaded/__init__.py
  2. 26
      website_recaptcha_reloaded/__manifest__.py
  3. 45
      website_recaptcha_reloaded/__openerp__.py
  4. 24
      website_recaptcha_reloaded/models/__init__.py
  5. 58
      website_recaptcha_reloaded/models/res_config.py
  6. 49
      website_recaptcha_reloaded/views/res_config.xml
  7. 8
      website_recaptcha_reloaded/views/website_view.xml
  8. 84
      website_recaptcha_reloaded/website.py

26
website_recaptcha_reloaded/__init__.py

@ -1,22 +1,4 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Tech-Receptives Solutions Pvt. Ltd.
# Copyright (C)2004-TODAY Tech Receptives(<https://www.techreceptives.com>)
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
import website
import models
# 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

26
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_<module>_recaptcha modules
to use it in your various pages.
""",
'data': [
'views/website_view.xml',
'views/res_config.xml',
],
'installable': True,
}

45
website_recaptcha_reloaded/__openerp__.py

@ -1,45 +0,0 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Tech-Receptives Solutions Pvt. Ltd.
# Copyright (C)2004-TODAY Tech Receptives(<https://www.techreceptives.com>)
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
{
'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_<module>_recaptcha modules
to use it in your various pages.
""",
'data': [
'views/website_view.xml',
'views/res_config.xml',
],
'installable': False,
'auto_install': False
}

24
website_recaptcha_reloaded/models/__init__.py

@ -1,21 +1,3 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Tech-Receptives Solutions Pvt. Ltd.
# Copyright (C)2004-TODAY Tech Receptives(<https://www.techreceptives.com>)
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
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

58
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(<https://www.techreceptives.com>)
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
# 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

49
website_recaptcha_reloaded/views/res_config.xml

@ -4,25 +4,38 @@
<record id="view_website_config_settings" model="ir.ui.view"> <record id="view_website_config_settings" model="ir.ui.view">
<field name="name">Website settings</field> <field name="name">Website settings</field>
<field name="model">website.config.settings</field>
<field name="inherit_id" ref="website.view_website_config_settings" />
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="website.res_config_settings_view_form" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<group name="social" position="after">
<group string="Google reCAPTCHA Reloaded">
<label for="recaptcha_site_key"/>
<div name="recaptcha_site_key">
<div class="oe_inline">
<field name="recaptcha_site_key" placeholder="Your reCAPTCHA site key"/>
</div>
</div>
<label for="recaptcha_private_key"/>
<div name="recaptcha_private_key">
<div class="oe_inline">
<field name="recaptcha_private_key" placeholder="Your reCAPTCHA private key"/>
</div>
</div>
</group>
</group>
<div id="google_maps_setting" position="after">
<div class="col-xs-12 col-md-6 o_setting_box" id="reCaptcha_setting">
<div class="o_setting_left_pane">
<field name="has_google_recaptcha"/>
</div>
<div class="o_setting_right_pane">
<img width="20px" alt="captcha" class="mr-2" src="website_form_recaptcha/static/description/recaptcha_ico.png"/>
<label for="has_google_recaptcha" string="reCaptcha"/>
<span class="fa fa-lg fa-globe" title="Values set here are website-specific." groups="website.group_multi_website"/>
<div class="text-muted mt8">
Protect your forms using reCaptcha control.
</div>
<div class="content-group" attrs="{'invisible': [('has_google_recaptcha', '=', False)]}">
<div class="row mt16">
<label class="col-lg-5 o_light_label" for="recaptcha_key_site"/>
<field name="recaptcha_key_site" placeholder="XXXXX"/>
<label class="col-lg-5 o_light_label" for="recaptcha_key_secret"/>
<field name="recaptcha_key_secret" placeholder="XXXXX"/>
</div>
</div>
<div attrs="{'invisible': [('has_google_recaptcha', '=', False)]}" class="mt8">
<a role="button" class="btn-link" target="_blank" href="http://www.google.com/recaptcha/admin"><i class="fa fa-arrow-right"></i>
How to get my reCaptcha Key
</a>
</div>
</div>
</div>
</div>
</field> </field>
</record> </record>
</data> </data>

8
website_recaptcha_reloaded/views/website_view.xml

@ -3,19 +3,19 @@
<data> <data>
<template id="assets_editor_recaptcha" name="GoogleRecaptcha" inherit_id="website.layout"> <template id="assets_editor_recaptcha" name="GoogleRecaptcha" inherit_id="website.layout">
<xpath expr="//body" position="inside">
<xpath expr="//main" position="inside">
<script src="https://www.google.com/recaptcha/api.js"></script> <script src="https://www.google.com/recaptcha/api.js"></script>
</xpath> </xpath>
</template> </template>
<record id="view_website_form" model="ir.ui.view">
<!--<record id="view_website_form" model="ir.ui.view">
<field name="name">website.form</field> <field name="name">website.form</field>
<field name="model">website</field> <field name="model">website</field>
<field name="inherit_id" ref="website.view_website_form" /> <field name="inherit_id" ref="website.view_website_form" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<div name="social_media" position="after"> <div name="social_media" position="after">
<div name="goole_recaptcha">
<div name="google_recaptcha">
<separator string="Google reCAPTCHA Reloaded"/> <separator string="Google reCAPTCHA Reloaded"/>
<group name="social_media"> <group name="social_media">
<field name="recaptcha_site_key" placeholder="Your reCAPTCHA site key"/> <field name="recaptcha_site_key" placeholder="Your reCAPTCHA site key"/>
@ -24,6 +24,6 @@
</div> </div>
</div> </div>
</field> </field>
</record>
</record>-->
</data> </data>
</openerp> </openerp>

84
website_recaptcha_reloaded/website.py

@ -1,49 +1,43 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Tech-Receptives Solutions Pvt. Ltd.
# Copyright (C)2004-TODAY Tech Receptives(<https://www.techreceptives.com>)
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
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 requests
import json
class website(osv.osv):
URL = 'https://www.google.com/recaptcha/api/siteverify'
class Website(models.Model):
_inherit = 'website' _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"
}
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
Loading…
Cancel
Save