Skip to content

Commit dbc6b2f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 2e8e9df commit dbc6b2f

File tree

8 files changed

+57
-19
lines changed

8 files changed

+57
-19
lines changed

openlibrary/i18n/messages.pot

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5820,6 +5820,26 @@ msgstr ""
58205820
msgid "%s does not exist."
58215821
msgstr ""
58225822

5823+
#: lib/challenge.html
5824+
msgid "Human Verification"
5825+
msgstr ""
5826+
5827+
#: lib/challenge.html
5828+
msgid "Please verify you are human to continue."
5829+
msgstr ""
5830+
5831+
#: lib/challenge.html
5832+
msgid "Verify you are human"
5833+
msgstr ""
5834+
5835+
#: lib/challenge.html
5836+
msgid "Verifying..."
5837+
msgstr ""
5838+
5839+
#: lib/challenge.html
5840+
msgid "Verification failed. Please try again."
5841+
msgstr ""
5842+
58235843
#: lib/edit_head.html lib/view_head.html
58245844
msgid "This doc was last edited by"
58255845
msgstr ""

openlibrary/plugins/openlibrary/code.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
from time import time
1515
from urllib.parse import parse_qs, urlencode
1616

17-
import infogami
1817
import requests
1918
import web
2019
import yaml
2120

21+
import infogami
2222
from openlibrary.core import db
2323
from openlibrary.core.batch_imports import (
2424
batch_import,
@@ -35,6 +35,7 @@
3535
infogami.config.features = [] # type: ignore[attr-defined]
3636

3737

38+
import openlibrary.core.stats
3839
from infogami.core.db import ValidationException
3940
from infogami.infobase import client
4041
from infogami.utils import delegate, features
@@ -46,8 +47,6 @@
4647
render_template,
4748
safeint,
4849
)
49-
50-
import openlibrary.core.stats
5150
from openlibrary.core.lending import get_availability
5251
from openlibrary.core.models import Edition
5352
from openlibrary.plugins.openlibrary import processors
@@ -1229,9 +1228,11 @@ def is_bot():
12291228
"""Check if the current request is from a bot."""
12301229
return req_context.get().is_bot
12311230

1231+
12321232
def is_recognized_bot():
12331233
return req_context.get().is_recognized_bot
12341234

1235+
12351236
def is_suspicious_visitor():
12361237
"""Check if the current visitor is suspicious and needs human verification.
12371238
@@ -1251,7 +1252,7 @@ def is_suspicious_visitor():
12511252
# Check if there's a referer header
12521253
if web.ctx.env.get('HTTP_REFERER'):
12531254
return False
1254-
1255+
12551256
# Check if visitor has already been verified (has vf=1 cookie)
12561257
if web.cookies().get('vf') == '1':
12571258
return False
@@ -1264,6 +1265,7 @@ def is_suspicious_visitor():
12641265
pass
12651266
return True
12661267

1268+
12671269
def require_human_verification():
12681270
"""Show the human verification challenge page.
12691271

openlibrary/plugins/openlibrary/tests/test_verification.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Tests for human verification challenge functionality."""
22

3-
import pytest
43
import web
54

65
from openlibrary.mocks.mock_infobase import MockSite
@@ -35,6 +34,7 @@ def test_is_suspicious_visitor_when_logged_in(self, monkeypatch):
3534
# Mock no verification cookie
3635
def mock_cookies():
3736
return {}
37+
3838
monkeypatch.setattr(web, 'cookies', mock_cookies)
3939

4040
assert code.is_suspicious_visitor() is False
@@ -50,6 +50,7 @@ def test_is_suspicious_visitor_when_bot(self, monkeypatch):
5050
# Mock no verification cookie
5151
def mock_cookies():
5252
return {}
53+
5354
monkeypatch.setattr(web, 'cookies', mock_cookies)
5455

5556
assert code.is_suspicious_visitor() is False
@@ -65,6 +66,7 @@ def test_is_suspicious_visitor_with_cookie(self, monkeypatch):
6566
# Mock verification cookie present
6667
def mock_cookies():
6768
return {'vf': '1'}
69+
6870
monkeypatch.setattr(web, 'cookies', mock_cookies)
6971

7072
assert code.is_suspicious_visitor() is False
@@ -80,6 +82,7 @@ def test_is_suspicious_visitor_when_all_checks_fail(self, monkeypatch):
8082
# Mock no verification cookie
8183
def mock_cookies():
8284
return {}
85+
8386
monkeypatch.setattr(web, 'cookies', mock_cookies)
8487

8588
assert code.is_suspicious_visitor() is True
@@ -95,6 +98,7 @@ def test_is_suspicious_visitor_with_wrong_cookie_value(self, monkeypatch):
9598
# Mock verification cookie with wrong value
9699
def mock_cookies():
97100
return {'vf': '0'}
101+
98102
monkeypatch.setattr(web, 'cookies', mock_cookies)
99103

100104
assert code.is_suspicious_visitor() is True

openlibrary/plugins/upstream/account.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
from typing import TYPE_CHECKING, Any, Final
1010
from urllib.parse import urlparse
1111

12-
import infogami.core.code as core # noqa: F401 side effects may be needed
1312
import requests
1413
import web
14+
15+
import infogami.core.code as core # noqa: F401 side effects may be needed
1516
from infogami import config
1617
from infogami.utils import delegate
1718
from infogami.utils.view import (
@@ -20,7 +21,6 @@
2021
render_template,
2122
require_login,
2223
)
23-
2424
from openlibrary import accounts
2525
from openlibrary.accounts import (
2626
InternetArchiveAccount,
@@ -1309,8 +1309,9 @@ class account_verify_human(delegate.page):
13091309
13101310
Sets the vf=1 cookie and redirects back to the original URL.
13111311
"""
1312+
13121313
path = "/account/verify_human"
1313-
1314+
13141315
# Cookie expires in 30 days
13151316
VERIFICATION_COOKIE_EXPIRES_SECONDS = 30 * 24 * 60 * 60
13161317

openlibrary/plugins/worksearch/code.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
import httpx
1515
import web
16+
from requests import Response
17+
1618
from infogami import config
1719
from infogami.infobase.client import storify
1820
from infogami.utils import delegate
1921
from infogami.utils.view import public, render, render_template, safeint
20-
from requests import Response
21-
2222
from openlibrary.core import cache
2323
from openlibrary.core.lending import add_availability
2424
from openlibrary.core.models import Edition
@@ -717,7 +717,11 @@ def GET(self):
717717

718718
# Check if this is an expensive search that requires human verification
719719
if self._is_expensive_search(i):
720-
from openlibrary.plugins.openlibrary.code import is_suspicious_visitor, require_human_verification
720+
from openlibrary.plugins.openlibrary.code import (
721+
is_suspicious_visitor,
722+
require_human_verification,
723+
)
724+
721725
if is_suspicious_visitor():
722726
return require_human_verification()
723727

openlibrary/plugins/worksearch/subjects.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
from typing import cast
77

88
import web
9+
from typing_extensions import deprecated
10+
911
from infogami.plugins.api.code import jsonapi
1012
from infogami.utils import delegate
1113
from infogami.utils.view import render_template, safeint
12-
from typing_extensions import deprecated
13-
1414
from openlibrary.core.lending import add_availability
1515
from openlibrary.core.models import Subject, Tag
1616
from openlibrary.solr.query_utils import query_dict_to_str
@@ -30,7 +30,11 @@ class subjects(delegate.page):
3030

3131
def GET(self, key):
3232
# Check if human verification is needed for subject pages
33-
from openlibrary.plugins.openlibrary.code import is_suspicious_visitor, require_human_verification
33+
from openlibrary.plugins.openlibrary.code import (
34+
is_suspicious_visitor,
35+
require_human_verification,
36+
)
37+
3438
if is_suspicious_visitor():
3539
return require_human_verification()
3640

openlibrary/plugins/worksearch/tests/test_verification.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77

88
class TestSearchVerification:
99
"""Tests for search expensive query detection."""
10-
10+
1111
def setup_method(self):
1212
"""Setup test fixtures."""
1313
self.search_handler = search()
14-
14+
1515
def test_is_expensive_search_with_language_colon(self):
1616
"""Test detection of language: syntax."""
1717
i = web.storage(q='language:eng')
1818
assert self.search_handler._is_expensive_search(i) is True
19-
19+
2020
def test_is_expensive_search_empty_query(self):
2121
"""Test that empty queries are not marked as expensive."""
2222
i = web.storage(q='')
2323
assert self.search_handler._is_expensive_search(i) is False
24-
24+
2525
def test_is_expensive_search_no_query(self):
2626
"""Test handling when no query parameter exists."""
2727
i = web.storage()

openlibrary/utils/request_context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ class RequestContextVars:
8888
'icc-crawler',
8989
]
9090

91+
9192
def _compute_is_recognized_bot(user_agent: str) -> bool:
9293
my_ua = user_agent.lower()
9394
return any(ua in my_ua for ua in USER_AGENT_BOTS)
9495

96+
9597
def _compute_is_bot(user_agent: str | None, hhcl: str | None) -> bool:
9698
"""Determine if the request is from a bot.
9799
@@ -102,7 +104,7 @@ def _compute_is_bot(user_agent: str | None, hhcl: str | None) -> bool:
102104
Returns:
103105
True if the request appears to be from a bot, False otherwise
104106
"""
105-
107+
106108
# Check hhcl header first (set by nginx)
107109
if hhcl == '1':
108110
return True
@@ -113,6 +115,7 @@ def _compute_is_bot(user_agent: str | None, hhcl: str | None) -> bool:
113115

114116
return _compute_is_recognized_bot(user_agent)
115117

118+
116119
def _parse_solr_editions_from_web() -> bool:
117120
"""Parse solr_editions from web.py context."""
118121

0 commit comments

Comments
 (0)