working on paginate alias log page
This commit is contained in:
parent
8202be7bae
commit
37ca4eaf20
|
@ -7,7 +7,7 @@
|
|||
{% endblock %}
|
||||
|
||||
{% block default_content %}
|
||||
<div class="page-header row">
|
||||
<div class="page-header row ml-0">
|
||||
<h3 class="page-title col">
|
||||
{{ alias }}
|
||||
</h3>
|
||||
|
@ -16,7 +16,7 @@
|
|||
<div class="row">
|
||||
{% for log in logs %}
|
||||
<div class="col-12">
|
||||
<div class="my-2 p-2 card">
|
||||
<div class="my-2 p-2 card border-light">
|
||||
<div class="font-weight-bold">{{ log.when | dt }}</div>
|
||||
<div>
|
||||
<span class="mr-2">{{ log.website_from or log.website_email }}</span>
|
||||
|
@ -40,6 +40,12 @@
|
|||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<nav aria-label="Alias log navigation">
|
||||
<ul class="pagination">
|
||||
<li class="page-item {% if page_id == 0 %}disabled{% endif %}"><a class="page-link" href="{{ url_for('dashboard.alias_log', alias=alias, page_id=page_id-1) }}">Previous</a></li>
|
||||
<li class="page-item {% if last_page %}disabled{% endif %}"><a class="page-link" href="{{ url_for('dashboard.alias_log', alias=alias, page_id=page_id+1) }}">Next</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
{% endblock %}
|
||||
|
||||
{% block script %}
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
import arrow
|
||||
from flask import render_template, flash, redirect, url_for
|
||||
from flask import render_template, flash, redirect, url_for, abort
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
from app.dashboard.base import dashboard_bp
|
||||
from app.extensions import db
|
||||
from app.models import GenEmail, ForwardEmailLog, ForwardEmail
|
||||
|
||||
LIMIT = 3
|
||||
|
||||
|
||||
@dataclass
|
||||
class AliasLog:
|
||||
|
@ -19,9 +21,10 @@ class AliasLog:
|
|||
blocked: bool
|
||||
|
||||
|
||||
@dashboard_bp.route("/alias_log/<alias>", methods=["GET"])
|
||||
@dashboard_bp.route("/alias_log/<alias>", methods=["GET"], defaults={'page_id': 0})
|
||||
@dashboard_bp.route("/alias_log/<alias>/<int:page_id>")
|
||||
@login_required
|
||||
def alias_log(alias):
|
||||
def alias_log(alias, page_id):
|
||||
gen_email = GenEmail.get_by(email=alias)
|
||||
|
||||
# sanity check
|
||||
|
@ -33,19 +36,23 @@ def alias_log(alias):
|
|||
flash("You do not have access to this page", "warning")
|
||||
return redirect(url_for("dashboard.index"))
|
||||
|
||||
logs = get_alias_log(gen_email, page_id)
|
||||
last_page = len(logs) < LIMIT # lightweight pagination without counting all objects
|
||||
|
||||
return render_template(
|
||||
"dashboard/alias_log.html", logs=get_alias_log(gen_email), alias=alias
|
||||
"dashboard/alias_log.html", **locals()
|
||||
)
|
||||
|
||||
|
||||
def get_alias_log(gen_email: GenEmail):
|
||||
def get_alias_log(gen_email: GenEmail, page_id=0):
|
||||
logs: [AliasLog] = []
|
||||
|
||||
q = (
|
||||
db.session.query(ForwardEmail, ForwardEmailLog)
|
||||
.filter(ForwardEmail.id == ForwardEmailLog.forward_id)
|
||||
.filter(ForwardEmail.gen_email_id == gen_email.id)
|
||||
.all()
|
||||
.filter(ForwardEmail.id == ForwardEmailLog.forward_id)
|
||||
.filter(ForwardEmail.gen_email_id == gen_email.id)
|
||||
.limit(LIMIT)
|
||||
.offset(page_id * LIMIT)
|
||||
)
|
||||
|
||||
for fe, fel in q:
|
||||
|
|
Loading…
Reference in a new issue