2019-12-31 19:20:17 +00:00
|
|
|
import arrow
|
|
|
|
from flask import render_template, flash, redirect, url_for
|
2019-11-16 17:45:23 +00:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
2020-02-04 16:28:05 +00:00
|
|
|
from app.config import PAGE_LIMIT
|
2019-11-16 17:45:23 +00:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2021-10-12 12:36:47 +00:00
|
|
|
from app.db import Session
|
2020-03-17 10:51:40 +00:00
|
|
|
from app.models import Alias, EmailLog, Contact
|
2019-11-16 17:45:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AliasLog:
|
2019-12-31 19:20:17 +00:00
|
|
|
website_email: str
|
2020-04-04 17:18:07 +00:00
|
|
|
reverse_alias: str
|
2019-12-31 19:20:17 +00:00
|
|
|
alias: str
|
|
|
|
when: arrow.Arrow
|
|
|
|
is_reply: bool
|
|
|
|
blocked: bool
|
2020-02-22 06:27:22 +00:00
|
|
|
bounced: bool
|
2020-05-10 16:41:22 +00:00
|
|
|
email_log: EmailLog
|
2020-11-17 08:27:30 +00:00
|
|
|
contact: Contact
|
2019-12-31 10:22:18 +00:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
setattr(self, k, v)
|
2019-11-16 17:45:23 +00:00
|
|
|
|
|
|
|
|
2020-01-11 20:48:21 +00:00
|
|
|
@dashboard_bp.route(
|
|
|
|
"/alias_log/<int:alias_id>", methods=["GET"], defaults={"page_id": 0}
|
|
|
|
)
|
|
|
|
@dashboard_bp.route("/alias_log/<int:alias_id>/<int:page_id>")
|
2019-11-16 17:45:23 +00:00
|
|
|
@login_required
|
2020-01-11 20:48:21 +00:00
|
|
|
def alias_log(alias_id, page_id):
|
2020-03-17 10:51:40 +00:00
|
|
|
alias = Alias.get(alias_id)
|
2019-11-16 17:45:23 +00:00
|
|
|
|
|
|
|
# sanity check
|
2020-03-17 10:51:40 +00:00
|
|
|
if not alias:
|
2019-11-16 17:45:23 +00:00
|
|
|
flash("You do not have access to this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2020-03-17 10:51:40 +00:00
|
|
|
if alias.user_id != current_user.id:
|
2019-11-16 17:45:23 +00:00
|
|
|
flash("You do not have access to this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2020-03-17 10:51:40 +00:00
|
|
|
logs = get_alias_log(alias, page_id)
|
2020-01-05 21:49:48 +00:00
|
|
|
base = (
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.query(Contact, EmailLog)
|
2020-03-17 10:10:50 +00:00
|
|
|
.filter(Contact.id == EmailLog.contact_id)
|
2020-03-17 11:01:18 +00:00
|
|
|
.filter(Contact.alias_id == alias.id)
|
2020-01-05 21:49:48 +00:00
|
|
|
)
|
|
|
|
total = base.count()
|
2020-01-06 23:02:12 +00:00
|
|
|
email_forwarded = (
|
2020-12-06 21:00:01 +00:00
|
|
|
base.filter(EmailLog.is_reply.is_(False))
|
|
|
|
.filter(EmailLog.blocked.is_(False))
|
2020-01-06 23:02:12 +00:00
|
|
|
.count()
|
|
|
|
)
|
2020-12-06 21:00:01 +00:00
|
|
|
email_replied = base.filter(EmailLog.is_reply.is_(True)).count()
|
|
|
|
email_blocked = base.filter(EmailLog.blocked.is_(True)).count()
|
2019-12-31 19:08:59 +00:00
|
|
|
last_page = (
|
2020-02-04 16:28:05 +00:00
|
|
|
len(logs) < PAGE_LIMIT
|
2019-12-31 19:08:59 +00:00
|
|
|
) # lightweight pagination without counting all objects
|
2019-12-31 10:11:06 +00:00
|
|
|
|
2019-12-31 10:22:18 +00:00
|
|
|
return render_template("dashboard/alias_log.html", **locals())
|
2019-11-16 17:45:23 +00:00
|
|
|
|
|
|
|
|
2020-04-05 14:32:38 +00:00
|
|
|
def get_alias_log(alias: Alias, page_id=0) -> [AliasLog]:
|
2019-11-16 17:45:23 +00:00
|
|
|
logs: [AliasLog] = []
|
|
|
|
|
|
|
|
q = (
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.query(Contact, EmailLog)
|
2020-03-17 10:10:50 +00:00
|
|
|
.filter(Contact.id == EmailLog.contact_id)
|
2020-03-17 11:01:18 +00:00
|
|
|
.filter(Contact.alias_id == alias.id)
|
2020-03-17 10:10:50 +00:00
|
|
|
.order_by(EmailLog.id.desc())
|
2020-02-04 16:28:05 +00:00
|
|
|
.limit(PAGE_LIMIT)
|
|
|
|
.offset(page_id * PAGE_LIMIT)
|
2019-11-16 17:45:23 +00:00
|
|
|
)
|
|
|
|
|
2020-04-04 17:11:10 +00:00
|
|
|
for contact, email_log in q:
|
2019-11-16 17:45:23 +00:00
|
|
|
al = AliasLog(
|
2020-04-04 17:11:10 +00:00
|
|
|
website_email=contact.website_email,
|
2020-04-04 17:18:07 +00:00
|
|
|
reverse_alias=contact.website_send_to(),
|
2020-03-17 10:51:40 +00:00
|
|
|
alias=alias.email,
|
2020-04-04 17:11:10 +00:00
|
|
|
when=email_log.created_at,
|
|
|
|
is_reply=email_log.is_reply,
|
|
|
|
blocked=email_log.blocked,
|
|
|
|
bounced=email_log.bounced,
|
2020-05-10 16:41:22 +00:00
|
|
|
email_log=email_log,
|
2020-11-17 08:27:45 +00:00
|
|
|
contact=contact,
|
2019-11-16 17:45:23 +00:00
|
|
|
)
|
|
|
|
logs.append(al)
|
|
|
|
logs = sorted(logs, key=lambda l: l.when, reverse=True)
|
|
|
|
|
|
|
|
return logs
|