Changed the get_number_query() method to the get_query_number() function, so that it is shared by different view classes.

This commit is contained in:
依瑪貓 2020-07-01 00:52:38 +08:00
parent 967c51075c
commit 37cddf478c

View File

@ -133,25 +133,27 @@ ORDER BY
self.kwargs["subject_code"] + "%"])
pagination = Pagination(
len(records),
self.get_number_query("page"),
self.get_number_query("page-size"),
get_query_number(self.request, "page"),
get_query_number(self.request, "page-size"),
True)
start_no = pagination.page_size * (pagination.page_no - 1)
return records[start_no:start_no + pagination.page_size]
def get_number_query(self, name):
def get_query_number(request, name):
"""Returns a positive number query parameter.
Args:
request (HttpRequest): The HTTP request
name (str): The name of the query parameter
Returns:
The parameter value, or None if this parameter does not
exist or is not a positive number
"""
if name not in self.request.GET:
if name not in request.GET:
return None
elif not re.match("^[1-9][0-9]*$", self.request.GET[name]):
elif not re.match("^[1-9][0-9]*$", request.GET[name]):
return None
else:
return int(self.request.GET)
return int(request.GET)