Skip to content

Commit aa2e859

Browse files
committed
[MIG] fastapi_captcha
1 parent 4e32999 commit aa2e859

7 files changed

Lines changed: 33 additions & 40 deletions

File tree

fastapi_captcha/README.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ Fastapi Captcha
1717
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1818
:alt: License: AGPL-3
1919
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github
20-
:target: https://github.com/OCA/rest-framework/tree/16.0/fastapi_captcha
20+
:target: https://github.com/OCA/rest-framework/tree/18.0/fastapi_captcha
2121
:alt: OCA/rest-framework
2222
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23-
:target: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-fastapi_captcha
23+
:target: https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-fastapi_captcha
2424
:alt: Translate me on Weblate
2525
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26-
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=16.0
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=18.0
2727
:alt: Try me on Runboat
2828

2929
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -58,7 +58,7 @@ Bug Tracker
5858
Bugs are tracked on `GitHub Issues <https://github.com/OCA/rest-framework/issues>`_.
5959
In case of trouble, please check there if your issue has already been reported.
6060
If you spotted it first, help us to smash it by providing a detailed and welcomed
61-
`feedback <https://github.com/OCA/rest-framework/issues/new?body=module:%20fastapi_captcha%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
61+
`feedback <https://github.com/OCA/rest-framework/issues/new?body=module:%20fastapi_captcha%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
6262

6363
Do not contact contributors directly about support or help with technical issues.
6464

@@ -96,6 +96,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
9696

9797
|maintainer-paradoxxxzero|
9898

99-
This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/16.0/fastapi_captcha>`_ project on GitHub.
99+
This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/18.0/fastapi_captcha>`_ project on GitHub.
100100

101101
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

fastapi_captcha/__manifest__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{
66
"name": "Fastapi Captcha",
7-
"version": "16.0.1.0.0",
7+
"version": "18.0.1.0.0",
88
"author": "Akretion, Odoo Community Association (OCA)",
99
"summary": "Add a captcha to your FastAPI routes",
1010
"category": "Tools",
@@ -14,7 +14,6 @@
1414
"views/fastapi_endpoint_views.xml",
1515
],
1616
"maintainers": ["paradoxxxzero"],
17-
"demo": [],
1817
"installable": True,
1918
"license": "AGPL-3",
2019
}

fastapi_captcha/captcha_middleware.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from starlette.middleware.base import BaseHTTPMiddleware
66

7-
from odoo import _
87
from odoo.exceptions import AccessError
98

109
from odoo.addons.fastapi.context import odoo_env_ctx
@@ -29,15 +28,15 @@ async def dispatch(self, request, call_next):
2928
token = request.headers.get("X-Captcha-Token")
3029
if not token:
3130
raise AccessError(
32-
_("Captcha token not found in headers"),
31+
env._("Captcha token not found in headers"),
3332
)
3433
try:
3534
endpoint.validate_captcha(token)
3635
except AccessError as e:
3736
raise e
38-
except IOError as e:
37+
except OSError as e:
3938
raise AccessError(
40-
_("Captcha validation failed: %s") % str(e),
39+
env._("Captcha validation failed: %s") % str(e),
4140
) from e
4241
response = await call_next(request)
4342
return response

fastapi_captcha/models/fastapi_endpoint.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import requests
99
from starlette.middleware import Middleware
1010

11-
from odoo import _, api, fields, models
11+
from odoo import api, fields, models
1212
from odoo.exceptions import AccessError, UserError, ValidationError
1313

1414
from fastapi import Depends, Header
@@ -72,8 +72,9 @@ def _check_captcha_routes_regex(self):
7272
re.compile(rex)
7373
except re.error as e:
7474
raise ValidationError(
75-
_(
76-
"Invalid regex for captcha routes: %(regex)s (error: %(error)s)"
75+
self.env._(
76+
"Invalid regex for captcha routes: %(regex)s "
77+
"(error: %(error)s)"
7778
)
7879
% {
7980
"regex": rex,
@@ -111,7 +112,7 @@ def validate_captcha(self, captcha_response):
111112
"""Validate the captcha response."""
112113
secret_key = self.captcha_secret_key
113114
if not secret_key:
114-
raise UserError(_("No secret key found for this endpoint"))
115+
raise UserError(self.env._("No secret key found for this endpoint"))
115116

116117
if self.captcha_type == "recaptcha":
117118
return self._validate_recaptcha(captcha_response, secret_key)
@@ -136,12 +137,14 @@ def _validate_recaptcha(self, captcha_response, secret_key):
136137
if not success:
137138
error_codes = result.get("error-codes", ["?"])
138139
raise AccessError(
139-
_("Recaptcha validation failed: %s") % ", ".join(error_codes)
140+
self.env._("Recaptcha validation failed: %s") % ", ".join(error_codes)
140141
)
141142
score = result.get("score", 1)
142143
if score < self.captcha_minimum_score:
143144
raise AccessError(
144-
_("Recaptcha validation failed: score %(score)s < %(min_score)s")
145+
self.env._(
146+
"Recaptcha validation failed: score %(score)s < %(min_score)s"
147+
)
145148
% {
146149
"score": score,
147150
"min_score": self.captcha_minimum_score,
@@ -163,13 +166,14 @@ def _validate_hcaptcha(self, captcha_response, secret_key):
163166
if not success:
164167
error_codes = result.get("error-codes", ["?"])
165168
raise AccessError(
166-
_("Hcaptcha validation failed: %s") % ", ".join(error_codes)
169+
self.env._("Hcaptcha validation failed: %s") % ", ".join(error_codes)
167170
)
168171
score = result.get("score", 1)
169172
if score < self.captcha_minimum_score:
170173
raise AccessError(
171-
_(
172-
"Hcaptcha validation failed: score %(score)s < %(min_score)s (%(reason)s)"
174+
self.env._(
175+
"Hcaptcha validation failed: score %(score)s < %(min_score)s "
176+
"(%(reason)s)"
173177
)
174178
% {
175179
"score": score,
@@ -194,7 +198,7 @@ def _validate_altcha(self, captcha_response, secret_key):
194198
if not success:
195199
error = result.get("error", "?")
196200
raise AccessError(
197-
_("Altcha (%(url)s) validation failed: %(error)s")
201+
self.env._("Altcha (%(url)s) validation failed: %(error)s")
198202
% {"url": url, "error": error}
199203
)
200204

fastapi_captcha/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["whool"]
3+
build-backend = "whool.buildapi"

fastapi_captcha/static/description/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ <h1 class="title">Fastapi Captcha</h1>
369369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370370
!! source digest: sha256:a507efff5b29eb67557d6283c396db18daddc1e48115ede431daff7f686594b6
371371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/16.0/fastapi_captcha"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-fastapi_captcha"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
372+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/18.0/fastapi_captcha"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-fastapi_captcha"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373373
<p>This module provides a simple way to protect several fastapi endpoints
374374
routes with a captcha.</p>
375375
<p>It currently supports the following captcha providers:</p>
@@ -404,7 +404,7 @@ <h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
404404
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/rest-framework/issues">GitHub Issues</a>.
405405
In case of trouble, please check there if your issue has already been reported.
406406
If you spotted it first, help us to smash it by providing a detailed and welcomed
407-
<a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20fastapi_captcha%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
407+
<a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20fastapi_captcha%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
408408
<p>Do not contact contributors directly about support or help with technical issues.</p>
409409
</div>
410410
<div class="section" id="credits">
@@ -432,7 +432,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
432432
promote its widespread use.</p>
433433
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
434434
<p><a class="reference external image-reference" href="https://github.com/paradoxxxzero"><img alt="paradoxxxzero" src="https://github.com/paradoxxxzero.png?size=40px" /></a></p>
435-
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/rest-framework/tree/16.0/fastapi_captcha">OCA/rest-framework</a> project on GitHub.</p>
435+
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/rest-framework/tree/18.0/fastapi_captcha">OCA/rest-framework</a> project on GitHub.</p>
436436
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
437437
</div>
438438
</div>

fastapi_captcha/views/fastapi_endpoint_views.xml

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
66
-->
77
<odoo>
8-
9-
108
<record id="fastapi_endpoint_form_view" model="ir.ui.view">
119
<field name="model">fastapi.endpoint</field>
1210
<field name="inherit_id" ref="fastapi.fastapi_endpoint_form_view" />
@@ -15,27 +13,17 @@
1513
<field name="use_captcha" />
1614
</field>
1715
<group name="resoures" position="after">
18-
<group
19-
name="captcha"
20-
attrs="{'invisible': [('use_captcha', '=', False)]}"
21-
>
22-
<field
23-
name="captcha_type"
24-
attrs="{'required': [('use_captcha', '=', True)]}"
25-
/>
26-
<field
27-
name="captcha_secret_key"
28-
attrs="{'required': [('use_captcha', '=', True)]}"
29-
/>
16+
<group name="captcha" invisible="not use_captcha">
17+
<field name="captcha_type" required="use_captcha" />
18+
<field name="captcha_secret_key" required="use_captcha" />
3019
<field name="captcha_routes_regex" />
3120
<field
3221
name="captcha_custom_verify_url"
33-
attrs="{'invisible': [('captcha_type', '!=', 'altcha')]}"
22+
invisible="captcha_type != 'altcha'"
3423
/>
3524
<field name="captcha_minimum_score" />
3625
</group>
3726
</group>
3827
</field>
3928
</record>
40-
4129
</odoo>

0 commit comments

Comments
 (0)