Browse Source

[MIG] website_form_recaptcha: Migrate to 11.0

- Remove groups on ir_config_paramater records (gone in 11.0)
- Move error mapping to private function to allow translation
pull/396/head
Damien Bouvy 2 years ago
parent
commit
665a9bd071
12 changed files with 48 additions and 66 deletions
  1. +2
    -2
      website_form_recaptcha/README.rst
  2. +0
    -1
      website_form_recaptcha/__init__.py
  3. +2
    -3
      website_form_recaptcha/__manifest__.py
  4. +0
    -1
      website_form_recaptcha/controllers/__init__.py
  5. +6
    -7
      website_form_recaptcha/controllers/main.py
  6. +10
    -15
      website_form_recaptcha/data/ir_config_parameter_data.xml
  7. +0
    -1
      website_form_recaptcha/models/__init__.py
  8. +1
    -2
      website_form_recaptcha/models/ir_model.py
  9. +18
    -20
      website_form_recaptcha/models/website_form_recaptcha.py
  10. +7
    -10
      website_form_recaptcha/static/src/js/field_recaptcha.js
  11. +0
    -1
      website_form_recaptcha/tests/__init__.py
  12. +2
    -3
      website_form_recaptcha/tests/test_captcha.py

+ 2
- 2
website_form_recaptcha/README.rst View File

@@ -6,7 +6,7 @@
Website Form - ReCaptcha
========================

Adds a ReCaptcha field widget for website forms
Adds a ReCaptcha field widget for website forms (extends the `website_form` module).


Configuration
@@ -30,7 +30,7 @@ Look at `website_crm_recaptcha` module for example implementation

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/186/10.0
:target: https://runbot.odoo-community.org/runbot/186/11.0


Known Issues / Road Map

+ 0
- 1
website_form_recaptcha/__init__.py View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


+ 2
- 3
website_form_recaptcha/__manifest__.py View File

@@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-
# Copyright 2016-2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
"name": "Website Form - ReCaptcha",
"summary": 'Provides a ReCaptcha field for Website Forms',
"version": "10.0.1.0.0",
"version": "11.0.1.0.0",
"category": "Website",
"website": "https://laslabs.com/",
"website": "https://github.com/OCA/website",
"author": "LasLabs, Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,

+ 0
- 1
website_form_recaptcha/controllers/__init__.py View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2016-2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


+ 6
- 7
website_form_recaptcha/controllers/main.py View File

@@ -1,12 +1,11 @@
# -*- coding: utf-8 -*-
# Copyright 2016-2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import http
from openerp.http import request
from openerp.exceptions import ValidationError
from odoo import http
from odoo.http import request
from odoo.exceptions import ValidationError

from openerp.addons.website_form.controllers.main import WebsiteForm
from odoo.addons.website_form.controllers.main import WebsiteForm

import json

@@ -17,13 +16,13 @@ class WebsiteForm(WebsiteForm):
'/website/recaptcha/',
type='http',
auth='public',
methods=['GET'],
methods=['POST'],
website=True,
multilang=False,
)
def recaptcha_public(self):
return json.dumps({
'site_key': request.env['ir.config_parameter'].get_param(
'site_key': request.env['ir.config_parameter'].sudo().get_param(
'recaptcha.key.site'
),
})

+ 10
- 15
website_form_recaptcha/data/ir_config_parameter_data.xml View File

@@ -6,18 +6,13 @@
-->


<openerp>
<data noupdate="1">
<record id="recaptcha_key_site" model="ir.config_parameter">
<field name="display_name">ReCaptcha Site Key</field>
<field name="key">recaptcha.key.site</field>
<field name="value">Obtain from Google</field>
</record>
<record id="recaptcha_key_secret" model="ir.config_parameter">
<field name="display_name">ReCaptcha Secret Key</field>
<field name="key">recaptcha.key.secret</field>
<field name="value">Obtain from Google</field>
<field name="group_ids" eval="[(4, ref('base.group_system'))]" />
</record>
</data>
</openerp>
<odoo noupdate="1">
<record id="recaptcha_key_site" model="ir.config_parameter">
<field name="key">recaptcha.key.site</field>
<field name="value">Obtain from Google</field>
</record>
<record id="recaptcha_key_secret" model="ir.config_parameter">
<field name="key">recaptcha.key.secret</field>
<field name="value">Obtain from Google</field>
</record>
</odoo>

+ 0
- 1
website_form_recaptcha/models/__init__.py View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


+ 1
- 2
website_form_recaptcha/models/ir_model.py View File

@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import models, fields
from odoo import models, fields


class IrModel(models.Model):

+ 18
- 20
website_form_recaptcha/models/website_form_recaptcha.py View File

@@ -1,9 +1,8 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import models, api
from openerp.exceptions import ValidationError
from odoo import models, api, _
from odoo.exceptions import ValidationError
import requests


@@ -16,15 +15,18 @@ class WebsiteFormRecaptcha(models.AbstractModel):
_description = 'Website Form Recaptcha Validations'
URL = 'https://www.google.com/recaptcha/api/siteverify'
RESPONSE_ATTR = 'g-recaptcha-response'
ERROR_MAP = {
'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.',
None: 'There was a problem with the captcha entry.',
}

@api.model
def _get_error_message(self, errorcode=None):
map = {
'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 map.get(errorcode, _('There was a problem with the captcha entry.'))

@api.model
def action_validate(self, response, remote_ip):
@@ -38,10 +40,8 @@ class WebsiteFormRecaptcha(models.AbstractModel):
True on success
"""

secret_key = self.env.ref(
'website_form_recaptcha.recaptcha_key_secret'
)
secret_key = secret_key.sudo().value
ICP = self.env['ir.config_parameter'].sudo()
secret_key = ICP.get_param('recaptcha.key.secret')

# @TODO: Domain validation
# domain_name = request.httprequest.environ.get(
@@ -57,12 +57,10 @@ class WebsiteFormRecaptcha(models.AbstractModel):

for error in res.get('error-codes', []):
raise ValidationError(
self.ERROR_MAP.get(
error, self.ERROR_MAP[None]
)
self._get_error_message(error)
)

if not res.get('success'):
raise ValidationError(self.ERROR_MAP[None])
raise ValidationError(self._get_error_message())

return True

+ 7
- 10
website_form_recaptcha/static/src/js/field_recaptcha.js View File

@@ -5,7 +5,8 @@
odoo.define('website_form_recaptcha.recaptcha', function(require){
"use strict";

var snippet_animation = require('web_editor.snippets.animation');
var ajax = require('web.ajax');
var snippet_animation = require('website.content.snippets.animation');

snippet_animation.registry.form_builder_send = snippet_animation.registry.form_builder_send.extend({

@@ -13,21 +14,17 @@ odoo.define('website_form_recaptcha.recaptcha', function(require){
var self = this;
this._super();
this.$captchas = self.$('.o_website_form_recaptcha');
$.ajax({
url: '/website/recaptcha/',
method: 'GET',
success: function(data){
data = JSON.parse(data);
ajax.post('/website/recaptcha/', {}).then(
function (result) {
var data = JSON.parse(result);
self.$captchas.append($(
'<div class="g-recaptcha" data-sitekey="' + data.site_key + '"></div>'
));
if (self.$captchas.length) {
$.getScript('https://www.google.com/recaptcha/api.js');
}
},
});
}
);
}

});

});

+ 0
- 1
website_form_recaptcha/tests/__init__.py View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2016-2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
from . import test_captcha

+ 2
- 3
website_form_recaptcha/tests/test_captcha.py View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

@@ -64,7 +63,7 @@ class TestCaptcha(TransactionCase):
self.model_obj.action_validate(*self.validate_vars)
except ValidationError as e:
self.assertEqual(
e[0], self.model_obj.ERROR_MAP[expect['error-codes'][0]]
e.name, self.model_obj._get_error_message([expect['error-codes'][0]])
)

@mock.patch(imp_requests)
@@ -77,7 +76,7 @@ class TestCaptcha(TransactionCase):
self.model_obj.action_validate(*self.validate_vars)
except ValidationError as e:
self.assertEqual(
e[0], self.model_obj.ERROR_MAP[None]
e.name, self.model_obj._get_error_message()
)

@mock.patch(imp_requests)

Loading…
Cancel
Save