1
+ import secrets
2
+
1
3
from django .conf import settings
2
4
from django .contrib import messages
3
5
from django .urls import reverse
21
23
from open_inwoner .utils .views import CommonPageMixin , LogMixin
22
24
23
25
26
+ def generate_question_answer_pair (
27
+ range_ : tuple [int , int ],
28
+ operators : list [str ],
29
+ ) -> tuple [str , int ]:
30
+ lower , upper = range_
31
+ num1 = secrets .choice (range (lower , upper ))
32
+ num2 = secrets .choice (range (lower , upper ))
33
+ operator = secrets .choice (operators )
34
+
35
+ # exclude negative results
36
+ num1 , num2 = max (num1 , num2 ), min (num1 , num2 )
37
+
38
+ question = _ ("What is {num1} {operator} {num2}?" ).format (
39
+ num1 = num1 , operator = operator , num2 = num2
40
+ )
41
+
42
+ match operator :
43
+ case "+" :
44
+ answer = num1 + num2
45
+ case "-" :
46
+ answer = num1 - num2
47
+
48
+ return question , answer
49
+
50
+
24
51
class ContactFormView (CommonPageMixin , LogMixin , BaseBreadcrumbMixin , FormView ):
25
52
form_class = ContactForm
26
- template_name = "pages/contactform/form_wrap.html" # inner ("structure") template rendered by CMS plugin
53
+ template_name = (
54
+ "pages/contactform/form_wrap.html" # inner ("structure") template rendered by CMS plugin
55
+ )
27
56
28
57
@cached_property
29
58
def crumbs (self ):
@@ -44,7 +73,18 @@ def get_success_url(self):
44
73
45
74
def get_form_kwargs (self ):
46
75
kwargs = super ().get_form_kwargs ()
76
+
77
+ captcha_question = self .request .session .get ("captcha_question" )
78
+ captcha_answer = self .request .session .get ("captcha_answer" )
79
+
80
+ if not captcha_question or not captcha_answer :
81
+ captcha_question , captcha_answer = generate_question_answer_pair ((1 , 10 ), ["+" , "-" ])
82
+
83
+ self .request .session ["captcha_question" ] = captcha_question
84
+ self .request .session ["captcha_answer" ] = captcha_answer
85
+
47
86
kwargs ["user" ] = self .request .user
87
+ kwargs ["request_session" ] = self .request .session
48
88
return kwargs
49
89
50
90
def get_initial (self ):
@@ -106,9 +146,7 @@ def form_valid(self, form):
106
146
user_email = api_user_email or user_email or form .cleaned_data .get ("email" )
107
147
108
148
if send_confirmation :
109
- send_contact_confirmation_mail (
110
- user_email , form .cleaned_data ["subject" ].subject
111
- )
149
+ send_contact_confirmation_mail (user_email , form .cleaned_data ["subject" ].subject )
112
150
113
151
self .set_result_message (email_success or api_success )
114
152
@@ -133,9 +171,7 @@ def register_by_email(self, form, recipient_email):
133
171
success = template .send_email ([recipient_email ], context )
134
172
135
173
if success :
136
- self .log_system_action (
137
- "registered contactmoment by email" , user = self .request .user
138
- )
174
+ self .log_system_action ("registered contactmoment by email" , user = self .request .user )
139
175
return True
140
176
else :
141
177
self .log_system_action (
@@ -156,9 +192,7 @@ def register_by_api(self, form, config: OpenKlantConfig) -> tuple[bool, str]:
156
192
if self .request .user .is_authenticated and (
157
193
self .request .user .bsn or self .request .user .kvk
158
194
):
159
- klant = klanten_client .retrieve_klant (
160
- ** get_fetch_parameters (self .request )
161
- )
195
+ klant = klanten_client .retrieve_klant (** get_fetch_parameters (self .request ))
162
196
if klant :
163
197
self .log_system_action (
164
198
"retrieved klant for BSN or KVK user" , user = self .request .user
@@ -216,9 +250,7 @@ def register_by_api(self, form, config: OpenKlantConfig) -> tuple[bool, str]:
216
250
parts = [form .cleaned_data [k ] for k in ("first_name" , "infix" , "last_name" )]
217
251
full_name = " " .join (p for p in parts if p )
218
252
219
- text = _ ("{text}\n \n Naam: {full_name}" ).format (
220
- text = text , full_name = full_name
221
- )
253
+ text = _ ("{text}\n \n Naam: {full_name}" ).format (text = text , full_name = full_name )
222
254
223
255
self .log_system_action (
224
256
"could not retrieve or create klant for user, appended info to message" ,
@@ -253,9 +285,7 @@ def register_by_api(self, form, config: OpenKlantConfig) -> tuple[bool, str]:
253
285
contactmoment = contactmoment_client .create_contactmoment (data , klant = klant )
254
286
255
287
if contactmoment :
256
- self .log_system_action (
257
- "registered contactmoment by API" , user = self .request .user
258
- )
288
+ self .log_system_action ("registered contactmoment by API" , user = self .request .user )
259
289
return True , user_email
260
290
else :
261
291
self .log_system_action (
0 commit comments