2019-11-16 18:45:23 +01:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
import arrow
|
2019-12-31 11:11:06 +01:00
|
|
|
from flask import render_template, flash, redirect, url_for, abort
|
2019-11-16 18:45:23 +01:00
|
|
|
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
|
|
|
|
|
2019-12-31 11:11:06 +01:00
|
|
|
LIMIT = 3
|
|
|
|
|
2019-11-16 18:45:23 +01:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class AliasLog:
|
|
|
|
website_email: str
|
2019-12-09 22:40:49 +01:00
|
|
|
website_from: str
|
2019-11-16 18:45:23 +01:00
|
|
|
alias: str
|
|
|
|
when: arrow.Arrow
|
|
|
|
is_reply: bool
|
|
|
|
blocked: bool
|
|
|
|
|
|
|
|
|
2019-12-31 11:11:06 +01:00
|
|
|
@dashboard_bp.route("/alias_log/<alias>", methods=["GET"], defaults={'page_id': 0})
|
|
|
|
@dashboard_bp.route("/alias_log/<alias>/<int:page_id>")
|
2019-11-16 18:45:23 +01:00
|
|
|
@login_required
|
2019-12-31 11:11:06 +01:00
|
|
|
def alias_log(alias, page_id):
|
2019-11-16 18:45:23 +01:00
|
|
|
gen_email = GenEmail.get_by(email=alias)
|
|
|
|
|
|
|
|
# sanity check
|
|
|
|
if not gen_email:
|
|
|
|
flash("You do not have access to this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
if gen_email.user_id != current_user.id:
|
|
|
|
flash("You do not have access to this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2019-12-31 11:11:06 +01:00
|
|
|
logs = get_alias_log(gen_email, page_id)
|
|
|
|
last_page = len(logs) < LIMIT # lightweight pagination without counting all objects
|
|
|
|
|
2019-11-16 18:45:23 +01:00
|
|
|
return render_template(
|
2019-12-31 11:11:06 +01:00
|
|
|
"dashboard/alias_log.html", **locals()
|
2019-11-16 18:45:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-12-31 11:11:06 +01:00
|
|
|
def get_alias_log(gen_email: GenEmail, page_id=0):
|
2019-11-16 18:45:23 +01:00
|
|
|
logs: [AliasLog] = []
|
|
|
|
|
|
|
|
q = (
|
|
|
|
db.session.query(ForwardEmail, ForwardEmailLog)
|
2019-12-31 11:11:06 +01:00
|
|
|
.filter(ForwardEmail.id == ForwardEmailLog.forward_id)
|
|
|
|
.filter(ForwardEmail.gen_email_id == gen_email.id)
|
|
|
|
.limit(LIMIT)
|
|
|
|
.offset(page_id * LIMIT)
|
2019-11-16 18:45:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
for fe, fel in q:
|
|
|
|
al = AliasLog(
|
|
|
|
website_email=fe.website_email,
|
2019-12-09 22:40:49 +01:00
|
|
|
website_from=fe.website_from,
|
2019-11-16 18:45:23 +01:00
|
|
|
alias=gen_email.email,
|
|
|
|
when=fel.created_at,
|
|
|
|
is_reply=fel.is_reply,
|
|
|
|
blocked=fel.blocked,
|
|
|
|
)
|
|
|
|
logs.append(al)
|
|
|
|
|
|
|
|
logs = sorted(logs, key=lambda l: l.when, reverse=True)
|
|
|
|
|
|
|
|
return logs
|