Skip to content

Commit 7f1e700

Browse files
committed
Add Pagination
1 parent 2e3a829 commit 7f1e700

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

audiophiler/__init__.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,62 +63,80 @@
6363
@app.route("/")
6464
@auth.oidc_auth('default')
6565
@audiophiler_auth
66-
def home(auth_dict=None):
66+
def home_def(auth_dict=None):
67+
return redirect("/1")
68+
69+
@app.route("/<int:page>")
70+
@auth.oidc_auth('default')
71+
@audiophiler_auth
72+
def home(page, auth_dict=None):
6773
# Retrieve list of files for templating
68-
db_files = File.query.all()
74+
rows = File.query.count()
75+
rows = int(rows // 10 + bool(rows % 10))
76+
db_files = File.query.offset((page-1) * 10).limit(10).all()
6977
harolds = get_harold_list(auth_dict["uid"])
7078
tour_harolds = get_harold_list("root")
7179
is_rtp = ldap_is_rtp(auth_dict["uid"])
7280
is_eboard = ldap_is_eboard(auth_dict["uid"])
7381
return render_template("main.html", db_files=db_files,
7482
get_date_modified=get_date_modified, s3_bucket=s3_bucket,
7583
auth_dict=auth_dict, harolds=harolds, tour_harolds=tour_harolds,
76-
is_rtp=is_rtp, is_eboard=is_eboard, is_tour_page=False)
84+
is_rtp=is_rtp, is_eboard=is_eboard, is_tour_page=False,
85+
current="", page=page, rows=rows)
7786

78-
@app.route("/mine")
87+
@app.route("/mine/<int:page>")
7988
@auth.oidc_auth('default')
8089
@audiophiler_auth
81-
def mine(auth_dict=None):
90+
def mine(page, auth_dict=None):
91+
rows = File.query.count()
92+
rows = int(rows // 10 + bool(rows % 10))
8293
is_rtp = ldap_is_rtp(auth_dict["uid"])
8394
is_eboard = ldap_is_eboard(auth_dict["uid"])
8495
# Retrieve list of files for templating
85-
db_files = File.query.filter_by(author=auth_dict["uid"]).all()
96+
db_files = File.query.filter_by(author=auth_dict["uid"]).offset((page-1) * 10).limit(10).all()
8697
harolds = get_harold_list(auth_dict["uid"])
8798
tour_harolds = get_harold_list("root")
8899
return render_template("main.html", db_files=db_files,
89100
get_file_s3=get_file_s3, get_date_modified=get_date_modified,
90101
s3_bucket=s3_bucket, auth_dict=auth_dict, harolds=harolds,
91-
tour_harolds=tour_harolds, is_rtp=is_rtp, is_eboard=is_eboard, is_tour_page=False)
102+
tour_harolds=tour_harolds, is_rtp=is_rtp, is_eboard=is_eboard, is_tour_page=False,
103+
current="/mine",page=page, rows=rows)
92104

93-
@app.route("/selected")
105+
@app.route("/selected/<int:page>")
94106
@auth.oidc_auth('default')
95107
@audiophiler_auth
96-
def selected(auth_dict=None):
108+
def selected(page, auth_dict=None):
109+
rows = File.query.count()
110+
rows = int(rows // 10 + bool(rows % 10))
97111
is_rtp = ldap_is_rtp(auth_dict["uid"])
98112
is_eboard = ldap_is_eboard(auth_dict["uid"])
99113
#Retrieve list of files for templating
100114
harolds = get_harold_list(auth_dict["uid"])
101115
tour_harolds = get_harold_list("root")
102-
db_files = File.query.filter(File.file_hash.in_(harolds)).all()
116+
db_files = File.query.filter(File.file_hash.in_(harolds)).offset((page-1) * 10).limit(10).all()
103117
return render_template("main.html", db_files=db_files,
104118
get_date_modified=get_date_modified, s3_bucket=s3_bucket,
105119
auth_dict=auth_dict, harolds=harolds, tour_harolds=tour_harolds,
106-
is_rtp=is_rtp, is_eboard=is_eboard, is_tour_page=False)
120+
is_rtp=is_rtp, is_eboard=is_eboard, is_tour_page=False,
121+
current="/selected",page=page, rows=rows)
107122

108-
@app.route("/tour_page")
123+
@app.route("/tour_page/<int:page>")
109124
@auth.oidc_auth('default')
110125
@audiophiler_auth
111-
def admin(auth_dict=None):
126+
def admin(page, auth_dict=None):
127+
rows = File.query.count()
128+
rows = int(rows // 10 + bool(rows % 10))
112129
is_rtp = ldap_is_rtp(auth_dict["uid"])
113130
is_eboard = ldap_is_eboard(auth_dict["uid"])
114131
if is_eboard or is_rtp:
115132
harolds = get_harold_list(auth_dict["uid"])
116133
tour_harolds = get_harold_list("root")
117-
db_files = File.query.filter(File.file_hash.in_(tour_harolds)).all()
134+
db_files = File.query.filter(File.file_hash.in_(tour_harolds)).offset((page-1) * 10).limit(10).all()
118135
return render_template("main.html", db_files=db_files,
119136
get_date_modified=get_date_modified, s3_bucket=s3_bucket,
120137
auth_dict=auth_dict, harolds=harolds, tour_harolds=tour_harolds,
121-
is_rtp=is_rtp, is_eboard=is_eboard, is_tour_page=True, is_tour_mode=get_tour_lock_status())
138+
is_rtp=is_rtp, is_eboard=is_eboard, is_tour_page=True, is_tour_mode=get_tour_lock_status(),
139+
current="/tour_page",page=page, rows=rows)
122140

123141
return "Permission Denied", 403
124142

audiophiler/templates/main.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,54 @@ <h6>Owner: {{ file.author }} uploaded {{ get_date_modified(s3_bucket, file.file_
8181
</div>
8282
{% endif %}
8383
{% endfor %}
84+
<div class="col" align="center">
85+
<nav aria-label="page selector">
86+
<ul class="pagination pagination-lg justify-content-center">
87+
{% if page == 1 %}
88+
<li class="page-item disabled">
89+
<a class="page-link" href="{{ current }}/{{ page - 1 }}" tabindex="-1">Previous</a>
90+
{% else %}
91+
<li class="page-item">
92+
<a class="page-link" href="{{ current }}/{{ page - 1 }}">Previous</a>
93+
{% endif %}
94+
</li>
95+
{% if rows <= 12 %}
96+
{% for num in range(1, rows + 1) %}
97+
<li class="page-item">
98+
<a class="page-link" href="{{ current }}{{ num }}">{{ num }}</a>
99+
</li>
100+
{% endfor %}
101+
{% elif page < 6 %}
102+
{% for num in range(1, 12) %}
103+
<li class="page-item">
104+
<a class="page-link" href="{{ current }}/{{ num }}">{{ num }}</a>
105+
</li>
106+
{% endfor %}
107+
{% elif rows - page <= 5 %}
108+
{% for num in range(rows - 10, rows + 1) %}
109+
<li class="page-item">
110+
<a class="page-link" href="{{ current }}/{{ num }}">{{ num }}</a>
111+
</li>
112+
{% endfor %}
113+
{% else %}
114+
{% for num in range(page - 5, page + 6) %}
115+
<li class="page-item">
116+
<a class="page-link" href="{{ current }}/{{ num }}">{{ num }}</a>
117+
</li>
118+
{% endfor %}
119+
{% endif %}
120+
{% if page == rows %}
121+
<li class="page-item disabled">
122+
<a class="page-link" href="{{ current }}/{{ page + 1 }}" tabindex="-1">Next</a>
123+
{% else %}
124+
<li class="page-item">
125+
<a class="page-link" href="{{ current }}/{{ page + 1 }}">Next</a>
126+
{% endif %}
127+
</li>
128+
129+
</li>
130+
</ul>
131+
</nav>
132+
</div>
84133
</div>
85134
{% endblock %}

0 commit comments

Comments
 (0)