AdminPanel: Show up to 10 mailboxes found (#2204)

* AdminPanel: Show up to 10 mailboxes found

* Add links
This commit is contained in:
Adrià Casajús 2024-09-03 17:05:05 +02:00 committed by GitHub
parent 1fb2e8f01c
commit cc44247482
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 22 deletions

View File

@ -736,7 +736,8 @@ class InvalidMailboxDomainAdmin(SLModelView):
class EmailSearchResult: class EmailSearchResult:
no_match: bool = True no_match: bool = True
alias: Optional[Alias] = None alias: Optional[Alias] = None
mailbox: Optional[Mailbox] = None mailbox: list[Mailbox] = []
mailbox_count: int = 0
deleted_alias: Optional[DeletedAlias] = None deleted_alias: Optional[DeletedAlias] = None
deleted_custom_alias: Optional[DomainDeletedAlias] = None deleted_custom_alias: Optional[DomainDeletedAlias] = None
user: Optional[User] = None user: Optional[User] = None
@ -752,9 +753,12 @@ class EmailSearchResult:
if user: if user:
output.user = user output.user = user
output.no_match = False output.no_match = False
mailbox = Mailbox.get_by(email=email) mailboxes = (
if mailbox: Mailbox.filter_by(email=email).order_by(Mailbox.id.desc()).limit(10).all()
output.mailbox = mailbox )
if mailboxes:
output.mailbox = mailboxes
output.mailbox_count = Mailbox.filter_by(email=email).count()
output.no_match = False output.no_match = False
deleted_alias = DeletedAlias.get_by(email=email) deleted_alias = DeletedAlias.get_by(email=email)
if deleted_alias: if deleted_alias:
@ -779,11 +783,13 @@ class EmailSearchHelpers:
@staticmethod @staticmethod
def mailbox_count(user: User) -> int: def mailbox_count(user: User) -> int:
return Mailbox.filter_by(user_id=user.id).order_by(Mailbox.id.asc()).count() return Mailbox.filter_by(user_id=user.id).order_by(Mailbox.id.desc()).count()
@staticmethod @staticmethod
def alias_list(user: User) -> list[Alias]: def alias_list(user: User) -> list[Alias]:
return Alias.filter_by(user_id=user.id).order_by(Alias.id.asc()).limit(10).all() return (
Alias.filter_by(user_id=user.id).order_by(Alias.id.desc()).limit(10).all()
)
@staticmethod @staticmethod
def alias_count(user: User) -> int: def alias_count(user: User) -> int:
@ -806,9 +812,8 @@ class EmailSearchAdmin(BaseView):
@expose("/", methods=["GET", "POST"]) @expose("/", methods=["GET", "POST"])
def index(self): def index(self):
search = EmailSearchResult() search = EmailSearchResult()
email = "" email = request.args.get("email")
if request.form and request.form["email"]: if email is not None and len(email) > 0:
email = request.form["email"]
email = email.strip() email = email.strip()
search = EmailSearchResult.from_email(email) search = EmailSearchResult.from_email(email)

View File

@ -19,7 +19,7 @@
<tbody> <tbody>
<tr> <tr>
<td>{{ user.id }}</td> <td>{{ user.id }}</td>
<td>{{ user.email }}</td> <td><a href="?email={{ user.email }}">{{ user.email }}</a></td>
{% if user.disabled %} {% if user.disabled %}
<td class="text-danger">Disabled</td> <td class="text-danger">Disabled</td>
@ -32,7 +32,7 @@
<td>{{ user.updated_at }}</td> <td>{{ user.updated_at }}</td>
{% if pu %} {% if pu %}
<td>{{ pu.partner_email }}</td> <td><a href="?email={{ pu.partner_email }}">{{ pu.partner_email }}</a></td>
{% else %} {% else %}
<td>No</td> <td>No</td>
{% endif %} {% endif %}
@ -43,7 +43,7 @@
{% macro list_mailboxes(mbox_count, mboxes) %} {% macro list_mailboxes(mbox_count, mboxes) %}
<h4> <h4>
{{ mbox_count }} Mailboxes found. {{ mbox_count }} Mailboxes found.
{% if mbox_count>10 %}Showing only the first 10.{% endif %} {% if mbox_count>10 %}Showing only the last 10.{% endif %}
</h4> </h4>
<table class="table"> <table class="table">
<thead> <thead>
@ -59,7 +59,7 @@
<tr> <tr>
<td>{{ mailbox.id }}</td> <td>{{ mailbox.id }}</td>
<td>{{ mailbox.email }}</td> <td><a href="?email={{mailbox.email}}">{{mailbox.email}}</a></td>
<td>{{ "Yes" if mailbox.verified else "No" }}</td> <td>{{ "Yes" if mailbox.verified else "No" }}</td>
<td> <td>
{{ mailbox.created_at }} {{ mailbox.created_at }}
@ -72,7 +72,7 @@
{% macro list_alias(alias_count, aliases) %} {% macro list_alias(alias_count, aliases) %}
<h4> <h4>
{{ alias_count }} Aliases found. {{ alias_count }} Aliases found.
{% if alias_count>10 %}Showing only the first 10.{% endif %} {% if alias_count>10 %}Showing only the last 10.{% endif %}
</h4> </h4>
<table class="table"> <table class="table">
<thead> <thead>
@ -95,7 +95,7 @@
{% for alias in aliases %} {% for alias in aliases %}
<tr> <tr>
<td>{{ alias.id }}</td> <td>{{ alias.id }}</td>
<td>{{ alias.email }}</td> <td><a href="?email={{alias.email}}">{{alias.email}}</a></td>
<td>{{ "Yes" if alias.verified else "No" }}</td> <td>{{ "Yes" if alias.verified else "No" }}</td>
<td>{{ alias.created_at }}</td> <td>{{ alias.created_at }}</td>
</tr> </tr>
@ -156,7 +156,7 @@
{% block body %} {% block body %}
<div class="border border-dark border-2 mt-1 mb-2 p-3"> <div class="border border-dark border-2 mt-1 mb-2 p-3">
<form method="post"> <form method="get">
<div class="form-group"> <div class="form-group">
<label for="email">Email to search:</label> <label for="email">Email to search:</label>
<input type="text" <input type="text"
@ -167,7 +167,7 @@
<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" class="btn btn-primary">Submit</button>
</form> </form>
</div> </div>
{% if no_match %} {% if data.no_match and email %}
<div class="border border-dark border-2 mt-1 mb-2 p-3 alert alert-warning" <div class="border border-dark border-2 mt-1 mb-2 p-3 alert alert-warning"
role="alert">No user, alias or mailbox found for {{ email }}</div> role="alert">No user, alias or mailbox found for {{ email }}</div>
@ -190,14 +190,19 @@
{{ list_alias(helper.alias_count(data.user) ,helper.alias_list(data.user)) }} {{ list_alias(helper.alias_count(data.user) ,helper.alias_list(data.user)) }}
</div> </div>
{% endif %} {% endif %}
{% if data.mailbox %} {% if data.mailbox_count > 10 %}
<h3>Found more than 10 mailboxes for {{ email }}. Showing the last 10</h3>
{% elif data.mailbox_count > 0 %}
<h3>Found {{ data.mailbox_count }} mailbox(es) for {{ email }}</h3>
{% endif %}
{% for mailbox in data.mailbox %}
<div class="border border-dark mt-1 mb-2 p-3"> <div class="border border-dark mt-1 mb-2 p-3">
<h3 class="mb-3">Found Mailbox {{ data.mailbox.email }}</h3> <h3 class="mb-3">Found Mailbox {{ mailbox.email }}</h3>
{{ list_mailboxes(1, [data.mailbox]) }} {{ list_mailboxes(1, [mailbox]) }}
{{ show_user(data.mailbox.user) }} {{ show_user(mailbox.user) }}
</div> </div>
{% endif %} {% endfor %}
{% if data.deleted_alias %} {% if data.deleted_alias %}
<div class="border border-dark mt-1 mb-2 p-3"> <div class="border border-dark mt-1 mb-2 p-3">