app-MAIL-temp/app/dashboard/views/alias_log.py

92 lines
2.6 KiB
Python
Raw Normal View History

import arrow
from flask import render_template, flash, redirect, url_for
2019-11-16 18:45:23 +01:00
from flask_login import login_required, current_user
2020-02-04 17:28:05 +01:00
from app.config import PAGE_LIMIT
2019-11-16 18:45:23 +01:00
from app.dashboard.base import dashboard_bp
from app.extensions import db
2020-03-17 11:10:50 +01:00
from app.models import GenEmail, EmailLog, Contact
2019-11-16 18:45:23 +01:00
class AliasLog:
website_email: str
website_from: str
alias: str
when: arrow.Arrow
is_reply: bool
blocked: bool
2020-02-22 07:27:22 +01:00
bounced: bool
2020-02-22 15:09:07 +01:00
mailbox: str
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
2019-11-16 18:45:23 +01: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 18:45:23 +01:00
@login_required
def alias_log(alias_id, page_id):
gen_email = GenEmail.get(alias_id)
2019-11-16 18:45:23 +01:00
# 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)
2020-01-05 22:49:48 +01:00
base = (
2020-03-17 11:10:50 +01:00
db.session.query(Contact, EmailLog)
.filter(Contact.id == EmailLog.contact_id)
2020-03-17 10:56:59 +01:00
.filter(Contact.gen_email_id == gen_email.id)
2020-01-05 22:49:48 +01:00
)
total = base.count()
2020-01-07 00:02:12 +01:00
email_forwarded = (
2020-03-17 11:10:50 +01:00
base.filter(EmailLog.is_reply == False)
.filter(EmailLog.blocked == False)
2020-01-07 00:02:12 +01:00
.count()
)
2020-03-17 11:10:50 +01:00
email_replied = base.filter(EmailLog.is_reply == True).count()
email_blocked = base.filter(EmailLog.blocked == True).count()
2019-12-31 20:08:59 +01:00
last_page = (
2020-02-04 17:28:05 +01:00
len(logs) < PAGE_LIMIT
2019-12-31 20:08:59 +01:00
) # lightweight pagination without counting all objects
2019-12-31 11:11:06 +01:00
return render_template("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] = []
2020-02-22 15:09:07 +01:00
mailbox = gen_email.mailbox_email()
2019-11-16 18:45:23 +01:00
q = (
2020-03-17 11:10:50 +01:00
db.session.query(Contact, EmailLog)
.filter(Contact.id == EmailLog.contact_id)
2020-03-17 10:56:59 +01:00
.filter(Contact.gen_email_id == gen_email.id)
2020-03-17 11:10:50 +01:00
.order_by(EmailLog.id.desc())
2020-02-04 17:28:05 +01:00
.limit(PAGE_LIMIT)
.offset(page_id * PAGE_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,
2020-02-22 07:27:22 +01:00
bounced=fel.bounced,
2020-02-22 15:10:31 +01:00
mailbox=mailbox,
2019-11-16 18:45:23 +01:00
)
logs.append(al)
logs = sorted(logs, key=lambda l: l.when, reverse=True)
return logs