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