1
+ import json
1
2
import logging
2
3
3
4
import requests
4
5
5
- from odoo import _ , models
6
- from odoo .exceptions import ValidationError
6
+ from odoo import _ , http , models
7
+ from odoo .exceptions import UserError
7
8
8
9
_logger = logging .getLogger (__name__ )
9
10
@@ -13,41 +14,36 @@ class LibreCaptcha(models.AbstractModel):
13
14
_description = "LibreCaptcha"
14
15
15
16
def captcha (self ):
16
- url = self .env ["ir.config_parameter" ].sudo ().get_param ("base_librecaptcha.url" )
17
- level = (
18
- self .env ["ir.config_parameter" ]
19
- .sudo ()
20
- .get_param ("base_librecaptcha.level" , "hard" )
21
- )
22
- media = (
23
- self .env ["ir.config_parameter" ]
24
- .sudo ()
25
- .get_param ("base_librecaptcha.media" , "image/gif" )
26
- )
17
+ if not self .is_enabled ():
18
+ return http .request .make_response (
19
+ data = json .dumps ({"error" : "Captcha is not enabled" }),
20
+ status = 400 ,
21
+ )
22
+
23
+ url , level , media , input_type = self ._get_config ().values ()
27
24
28
25
resp = requests .post (
29
- "%s /v2/captcha" % url ,
26
+ f" { url } /v2/captcha" ,
30
27
json = {
31
28
"level" : level ,
32
29
"media" : media ,
33
- "input_type" : "text" ,
30
+ "input_type" : input_type ,
34
31
"size" : "350x100" ,
35
32
},
36
33
)
37
34
38
35
if resp .ok :
39
36
return resp .json ()["id" ]
40
37
41
- error = "librecaptcha failed with code %s: %s" % (resp .status_code , resp .text )
38
+ error = "captcha failed with code %s: %s" % (resp .status_code , resp .text )
42
39
_logger .error (error )
43
-
44
40
raise Exception (error )
45
41
46
42
def media (self , captcha_id ):
47
- url = self .env [ "ir.config_parameter" ]. sudo ().get_param ( "base_librecaptcha. url" )
43
+ url = self ._get_config ().get ( " url" )
48
44
49
45
resp = requests .get (
50
- "%s /v2/media" % url ,
46
+ f" { url } /v2/media" ,
51
47
params = {
52
48
"id" : captcha_id ,
53
49
},
@@ -57,25 +53,45 @@ def media(self, captcha_id):
57
53
return resp .content
58
54
59
55
def answer (self , captcha_id , answer , raise_exception = False ):
60
- url = self .env [ "ir.config_parameter" ]. sudo ().get_param ( "base_librecaptcha. url" )
56
+ url = self ._get_config ().get ( " url" )
61
57
resp = requests .post (
62
- "%s/v2/answer" % url , json = {"id" : captcha_id , "answer" : answer }
58
+ f"{ url } /v2/answer" ,
59
+ json = {
60
+ "id" : captcha_id ,
61
+ "answer" : answer ,
62
+ },
63
63
)
64
64
65
65
if resp .ok :
66
66
result = resp .json ()["result" ]
67
67
if raise_exception :
68
68
if result == "False" :
69
- raise ValidationError (_ ("Captcha incorrect." ))
69
+ raise UserError (_ ("Captcha incorrect." ))
70
70
if result == "Expired" :
71
- raise ValidationError (_ ("Captcha Expired." ))
71
+ raise UserError (_ ("Captcha Expired." ))
72
72
return result
73
73
74
74
error = "librecaptcha failed with code %s: %s" % (resp .status_code , resp .text )
75
75
_logger .error (error )
76
76
raise Exception (error )
77
77
78
78
def is_enabled (self ):
79
- return bool (
80
- self .env ["ir.config_parameter" ].sudo ().get_param ("base_librecaptcha.url" )
81
- )
79
+ record = self ._get_config_record ()
80
+ return record .librecaptcha_enabled and record .librecaptcha_valid_server
81
+
82
+ def _get_config_record (self ):
83
+ """method to be inherit to change the config record"""
84
+ return self .env .company
85
+
86
+ def _get_config (self ):
87
+ if not self .is_enabled ():
88
+ return {}
89
+
90
+ record = self ._get_config_record ()
91
+
92
+ return {
93
+ "url" : record .librecaptcha_url ,
94
+ "level" : record .librecaptcha_level ,
95
+ "media" : f"image/{ record .librecaptcha_media } " ,
96
+ "input_type" : record .librecaptcha_type ,
97
+ }
0 commit comments