Skip to content

Commit

Permalink
AdminPanel: Show up to 10 mailboxes found (#2204)
Browse files Browse the repository at this point in the history
* AdminPanel: Show up to 10 mailboxes found

* Add links
  • Loading branch information
acasajus authored Sep 3, 2024
1 parent 1fb2e8f commit cc44247
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
23 changes: 14 additions & 9 deletions app/admin_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,8 @@ class InvalidMailboxDomainAdmin(SLModelView):
class EmailSearchResult:
no_match: bool = True
alias: Optional[Alias] = None
mailbox: Optional[Mailbox] = None
mailbox: list[Mailbox] = []
mailbox_count: int = 0
deleted_alias: Optional[DeletedAlias] = None
deleted_custom_alias: Optional[DomainDeletedAlias] = None
user: Optional[User] = None
Expand All @@ -752,9 +753,12 @@ def from_email(email: str) -> EmailSearchResult:
if user:
output.user = user
output.no_match = False
mailbox = Mailbox.get_by(email=email)
if mailbox:
output.mailbox = mailbox
mailboxes = (
Mailbox.filter_by(email=email).order_by(Mailbox.id.desc()).limit(10).all()
)
if mailboxes:
output.mailbox = mailboxes
output.mailbox_count = Mailbox.filter_by(email=email).count()
output.no_match = False
deleted_alias = DeletedAlias.get_by(email=email)
if deleted_alias:
Expand All @@ -779,11 +783,13 @@ def mailbox_list(user: User) -> list[Mailbox]:

@staticmethod
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
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
def alias_count(user: User) -> int:
Expand All @@ -806,9 +812,8 @@ def inaccessible_callback(self, name, **kwargs):
@expose("/", methods=["GET", "POST"])
def index(self):
search = EmailSearchResult()
email = ""
if request.form and request.form["email"]:
email = request.form["email"]
email = request.args.get("email")
if email is not None and len(email) > 0:
email = email.strip()
search = EmailSearchResult.from_email(email)

Expand Down
31 changes: 18 additions & 13 deletions templates/admin/email_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h4>User {{ user.email }} with ID {{ user.id }}.</h4>
<tbody>
<tr>
<td>{{ user.id }}</td>
<td>{{ user.email }}</td>
<td><a href="?email={{ user.email }}">{{ user.email }}</a></td>
{% if user.disabled %}

<td class="text-danger">Disabled</td>
Expand All @@ -32,7 +32,7 @@ <h4>User {{ user.email }} with ID {{ user.id }}.</h4>
<td>{{ user.updated_at }}</td>
{% if pu %}

<td>{{ pu.partner_email }}</td>
<td><a href="?email={{ pu.partner_email }}">{{ pu.partner_email }}</a></td>
{% else %}
<td>No</td>
{% endif %}
Expand All @@ -43,7 +43,7 @@ <h4>User {{ user.email }} with ID {{ user.id }}.</h4>
{% macro list_mailboxes(mbox_count, mboxes) %}
<h4>
{{ 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>
<table class="table">
<thead>
Expand All @@ -59,7 +59,7 @@ <h4>

<tr>
<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>
{{ mailbox.created_at }}
Expand All @@ -72,7 +72,7 @@ <h4>
{% macro list_alias(alias_count, aliases) %}
<h4>
{{ 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>
<table class="table">
<thead>
Expand All @@ -95,7 +95,7 @@ <h4>
{% for alias in aliases %}
<tr>
<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>{{ alias.created_at }}</td>
</tr>
Expand Down Expand Up @@ -156,7 +156,7 @@ <h4>
{% block body %}

<div class="border border-dark border-2 mt-1 mb-2 p-3">
<form method="post">
<form method="get">
<div class="form-group">
<label for="email">Email to search:</label>
<input type="text"
Expand All @@ -167,7 +167,7 @@ <h4>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</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"
role="alert">No user, alias or mailbox found for {{ email }}</div>
Expand All @@ -190,14 +190,19 @@ <h3 class="mb-3">Found User {{ data.user.email }}</h3>
{{ list_alias(helper.alias_count(data.user) ,helper.alias_list(data.user)) }}
</div>
{% 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">
<h3 class="mb-3">Found Mailbox {{ data.mailbox.email }}</h3>
{{ list_mailboxes(1, [data.mailbox]) }}
{{ show_user(data.mailbox.user) }}
<h3 class="mb-3">Found Mailbox {{ mailbox.email }}</h3>
{{ list_mailboxes(1, [mailbox]) }}
{{ show_user(mailbox.user) }}
</div>
{% endif %}
{% endfor %}
{% if data.deleted_alias %}

<div class="border border-dark mt-1 mb-2 p-3">
Expand Down

0 comments on commit cc44247

Please sign in to comment.