IEEE.org     |     IEEE Xplore Digital Library     |     IEEE Standards     |     IEEE Spectrum     |     More Sites

Verified Commit 8711f0f2 authored by Emi Simpson's avatar Emi Simpson
Browse files

Added pagination

parent 5e3f5ea3
......@@ -368,10 +368,17 @@ class Project:
return project
@staticmethod
def get_all_projects(c: Cursor, page: int = 0, page_size: int = 20) -> List['Project']:
def get_all_projects(
c: Cursor,
page: int = 0,
page_size: int = 9,
) -> Tuple[List['Project'], bool]:
"""
Produce an excerpt from the list of all projects
Returns two objects: A list of results, and a boolean indicating if there is a
page after this one, sort of like a `has_next`.
Results are paginated, so the provided value should be the page number. Generally
used for populating the Explore page. If you're looking for a specific project,
prefer to use a specific method.
......@@ -391,15 +398,15 @@ class Project:
WHERE draft_owner IS NULL
LIMIT %s
OFFSET %s;
''', (page_size, page * page_size))
''', (page_size + 1, page * page_size))
results: Optional[Tuple[Any, ...]] = c.fetchall()
assert results is not None
return [
return ([
Project.from_record(record)
for record in results
]
for record in results[:page_size]
], len(results) > page_size)
@staticmethod
def bulk_populate(c: Cursor, projects: List['Project']):
......
......@@ -67,6 +67,12 @@
.language.rss {
border-top-color: orangered;
}
#navbuttons {
display: grid;
grid-template-columns: 0fr 1fr 0fr;
width: calc(100% - 160px);
margin: 30px 80px;
}
</style>
{% endblock %}
{% block main %}
......@@ -106,4 +112,16 @@
</div>
{% endfor %}
</div>
<div id=navbuttons>
{% if page > 0 %}
<a href="{{page - 1}}">
<button class="big">Prev</button>
</a>
{% endif %}
{% if has_next_page %}
<a href="{{page + 1}}" style="grid-column-start: 3">
<button class="big">Next</button>
</a>
{% endif %}
</div>
{% endblock %}
......@@ -113,13 +113,14 @@ def explore_paginated(page: int = 0) -> str:
db = get_database()
with db.cursor() as cursor:
all_projects = Project.get_all_projects(cursor, page = page)
all_projects, has_next_page = Project.get_all_projects(cursor, page = page)
Project.bulk_populate(cursor, all_projects)
return render_template(
"explore.html",
projects=all_projects,
page=page,
has_next_page=has_next_page,
login=_login_or_profile(cursor),
cursor=cursor
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment