Skip to content

Commit fbbf7be

Browse files
committed
reverse ordering
1 parent 70a135f commit fbbf7be

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

proxy_py/_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
BAD_PROXY_CHECKING_PERIOD = 2 * 60 * 60
2121
DEAD_PROXY_CHECKING_PERIOD = 24 * 60 * 60
2222
DEAD_PROXY_THRESHOLD = 6
23-
REMOVE_ON_N_BAD_CHECKS = DEAD_PROXY_THRESHOLD + 14
23+
REMOVE_ON_N_BAD_CHECKS = DEAD_PROXY_THRESHOLD + 21
2424
PROXY_CHECKING_TIMEOUT = 20
2525

2626
PROXY_PROVIDER_SERVER_ADDRESS = {

server/requests_to_models/request_executor.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from proxy_py import settings
12
from models import session
23
from server.requests_to_models.request import Request, GetRequest, CountRequest, FetchRequest
34
import importlib
@@ -20,9 +21,9 @@ def _fetch(self, request: FetchRequest):
2021
package = importlib.import_module(request.class_name[0])
2122
class_name = getattr(package, request.class_name[1])
2223

23-
# TODO: remove bad_proxy
24+
# TODO: remove checking number_of_bad_checks
2425

25-
queryset = session.query(class_name).filter(class_name.number_of_bad_checks == 0)
26+
queryset = session.query(class_name).filter(class_name.number_of_bad_checks < settings.DEAD_PROXY_THRESHOLD)
2627

2728
result = {
2829
'count': queryset.count(),
@@ -56,7 +57,16 @@ def _fetch(self, request: FetchRequest):
5657
def order_by_list_to_sqlalchemy(self, order_by_fields: list, class_name):
5758
result = []
5859
for field in order_by_fields:
59-
result.append(getattr(class_name, field))
60+
reverse = False
61+
if field[0] == '-':
62+
reverse = True
63+
field = field[1:]
64+
65+
attribute = getattr(class_name, field)
66+
if reverse:
67+
attribute = attribute.desc()
68+
69+
result.append(attribute)
6070

6171
return result
6272

server/requests_to_models/request_parser.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ def validate_value(self, key: str, value):
4747
self.validate_value(key, value_item)
4848
return
4949

50-
if key in {'model', 'method', 'fields', 'order_by'}:
50+
if key == 'order_by':
51+
self._validate_value_type(key, value, str)
52+
self._validate_value_regex(key, value, r'^-?[a-zA-Z][a-zA-Z0-9_]+$')
53+
elif key in {'model', 'method', 'fields'}:
5154
self._validate_value_type(key, value, str)
5255
self._validate_value_regex(key, value, r'^[a-zA-Z][a-zA-Z0-9_]+$')
5356
elif key in {'filter'}:
@@ -149,8 +152,12 @@ def parse_list(self, req_dict, config, request_key, config_key, default_value):
149152
result = []
150153

151154
for field in req_dict[request_key]:
152-
if field not in config[config_key]:
153-
raise ParseError("Field \"{}\" doesn't exist or isn't allowed".format(field))
155+
if config_key == 'order_by_fields':
156+
if (field[1:] if field[0] == '-' else field) not in config[config_key]:
157+
raise ParseError("Field \"{}\" doesn't exist or isn't allowed1".format(field))
158+
else:
159+
if field not in config[config_key]:
160+
raise ParseError("Field \"{}\" doesn't exist or isn't allowed".format(field))
154161

155162
result.append(field)
156163

0 commit comments

Comments
 (0)