Merge pull request #136 from simple-login/refactor

Refactor: remove unused POST
This commit is contained in:
Son Nguyen Kim 2020-04-25 15:33:18 +02:00 committed by GitHub
commit c8ed1437f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 156 deletions

View file

@ -110,9 +110,6 @@
</span> </span>
</div> </div>
<div class="col text-right"> <div class="col text-right">
<form method="post">
<input type="hidden" name="form-name" value="switch-email-forwarding">
<input type="hidden" name="alias-id" value="{{ alias.id }}">
<label class="custom-switch cursor" <label class="custom-switch cursor"
data-toggle="tooltip" data-toggle="tooltip"
{% if alias.enabled %} {% if alias.enabled %}
@ -131,13 +128,13 @@
{% endif %} {% endif %}
style="padding-left: 0px" style="padding-left: 0px"
> >
<input type="hidden" name="alias" class="alias" value="{{ alias.email }}"> <input type="checkbox" class="custom-switch-input"
<input type="checkbox" class="custom-switch-input" data-alias="{{ alias.id }}" data-alias="{{ alias.id }}"
data-alias-email="{{ alias.email }}"
{{ "checked" if alias.enabled else "" }}> {{ "checked" if alias.enabled else "" }}>
<span class="custom-switch-indicator"></span> <span class="custom-switch-indicator"></span>
</label> </label>
</form>
</div> </div>
</div> </div>
@ -218,14 +215,14 @@
<div class="collapse" id="alias-{{ alias.id }}"> <div class="collapse" id="alias-{{ alias.id }}">
{% if mailboxes|length > 1 %} {% if mailboxes|length > 1 %}
<form method="post">
<div class="small-text mt-2">Current mailbox</div> <div class="small-text mt-2">Current mailbox</div>
<div class="d-flex"> <div class="d-flex">
<div class="flex-grow-1 mr-2"> <div class="flex-grow-1 mr-2">
<select id="mailbox-{{ alias.id }}" <select id="mailbox-{{ alias.id }}"
class="form-control form-control-sm custom-select" name="mailbox"> class="form-control form-control-sm custom-select" name="mailbox">
{% for mailbox in mailboxes %} {% for mailbox in mailboxes %}
<option value="{{ mailbox.id }}" {% if mailbox.id == alias_info.mailbox.id %} selected {% endif %}> <option value="{{ mailbox.id }}" {% if mailbox.id == alias_info.mailbox.id %}
selected {% endif %}>
{{ mailbox.email }} {{ mailbox.email }}
</option> </option>
{% endfor %} {% endfor %}
@ -233,9 +230,6 @@
</div> </div>
<div class=""> <div class="">
<input type="hidden" name="form-name" value="set-mailbox">
<input type="hidden" name="alias-id" value="{{ alias.id }}">
<a data-alias="{{ alias.id }}" <a data-alias="{{ alias.id }}"
class="save-mailbox btn btn-sm btn-outline-info w-100"> class="save-mailbox btn btn-sm btn-outline-info w-100">
Update Update
@ -243,16 +237,13 @@
</div> </div>
</div> </div>
</form>
{% elif alias_info.mailbox != None and alias_info.mailbox.email != current_user.email %} {% elif alias_info.mailbox != None and alias_info.mailbox.email != current_user.email %}
<div class="small-text"> <div class="small-text">
Owned by <b>{{ alias_info.mailbox.email }}</b> mailbox Owned by <b>{{ alias_info.mailbox.email }}</b> mailbox
</div> </div>
{% endif %} {% endif %}
<form method="post">
<div class="d-flex mt-2"> <div class="d-flex mt-2">
<div class="flex-grow-1 mr-2"> <div class="flex-grow-1 mr-2">
<textarea <textarea
id="note-{{ alias.id }}" id="note-{{ alias.id }}"
@ -263,16 +254,12 @@
</div> </div>
<div class=""> <div class="">
<input type="hidden" name="form-name" value="set-note">
<input type="hidden" name="alias-id" value="{{ alias.id }}">
<a data-alias="{{ alias.id }}" <a data-alias="{{ alias.id }}"
class="save-note btn btn-sm btn-outline-success w-100"> class="save-note btn btn-sm btn-outline-success w-100">
Save Save
</a> </a>
</div> </div>
</div> </div>
</form>
<div class="row mt-3"> <div class="row mt-3">
<div class="col"> <div class="col">
@ -409,22 +396,9 @@
}); });
}); });
$(".trigger-email").on("click", function (e) {
notie.confirm({
text: "SimpleLogin server will send an email to this alias " +
"and it will arrive to your inbox, please confirm.",
cancelCallback: () => {
// nothing to do
},
submitCallback: () => {
$(this).closest("form").submit();
}
});
});
$(".custom-switch-input").change(async function (e) { $(".custom-switch-input").change(async function (e) {
let aliasId = $(this).data("alias"); let aliasId = $(this).data("alias");
let alias = $(this).parent().find(".alias").val(); let alias = $(this).data("alias-email");
try { try {
let res = await fetch(`/api/aliases/${aliasId}/toggle`, { let res = await fetch(`/api/aliases/${aliasId}/toggle`, {
@ -474,7 +448,7 @@
}); });
if (res.ok) { if (res.ok) {
toastr.success(`Saved`); toastr.success(`Note Saved`);
} else { } else {
toastr.error("Sorry for the inconvenience! Could you refresh the page & retry please?", "Unknown Error"); toastr.error("Sorry for the inconvenience! Could you refresh the page & retry please?", "Unknown Error");
// reset to the original value // reset to the original value

View file

@ -33,19 +33,7 @@ def index():
# User generates a new email # User generates a new email
if request.method == "POST": if request.method == "POST":
if request.form.get("form-name") == "trigger-email": if request.form.get("form-name") == "create-custom-email":
alias_id = request.form.get("alias-id")
alias = Alias.get(alias_id)
LOG.d("trigger an email to %s", alias)
email_utils.send_test_email_alias(alias.email, alias.user.name)
flash(
f"An email sent to {alias.email} is on its way, please check your inbox/spam folder",
"success",
)
elif request.form.get("form-name") == "create-custom-email":
if current_user.can_create_new_alias(): if current_user.can_create_new_alias():
return redirect(url_for("dashboard.custom_alias")) return redirect(url_for("dashboard.custom_alias"))
else: else:
@ -69,36 +57,27 @@ def index():
return redirect( return redirect(
url_for( url_for(
"dashboard.index", highlight_alias_id=alias.id, query=query, "dashboard.index",
highlight_alias_id=alias.id,
query=query,
sort=sort,
) )
) )
else: else:
flash(f"You need to upgrade your plan to create new alias.", "warning") flash(f"You need to upgrade your plan to create new alias.", "warning")
elif request.form.get("form-name") == "switch-email-forwarding":
alias_id = request.form.get("alias-id")
alias: Alias = Alias.get(alias_id)
LOG.d("switch email forwarding for %s", alias)
alias.enabled = not alias.enabled
if alias.enabled:
flash(f"Alias {alias.email} is enabled", "success")
else:
flash(f"Alias {alias.email} is disabled", "warning")
db.session.commit()
return redirect(
url_for("dashboard.index", highlight_alias_id=alias.id, query=query)
)
elif request.form.get("form-name") == "delete-email": elif request.form.get("form-name") == "delete-email":
alias_id = request.form.get("alias-id") alias_id = request.form.get("alias-id")
alias: Alias = Alias.get(alias_id) alias: Alias = Alias.get(alias_id)
if not alias: if not alias:
flash("Unknown error, sorry for the inconvenience", "error") flash("Unknown error, sorry for the inconvenience", "error")
return redirect( return redirect(
url_for("dashboard.index", highlight_alias_id=alias.id, query=query) url_for(
"dashboard.index",
highlight_alias_id=alias.id,
query=query,
sort=sort,
)
) )
LOG.d("delete gen email %s", alias) LOG.d("delete gen email %s", alias)
@ -116,42 +95,7 @@ def index():
LOG.error("alias %s has been added before to DeletedAlias", email) LOG.error("alias %s has been added before to DeletedAlias", email)
db.session.rollback() db.session.rollback()
elif request.form.get("form-name") == "set-note": return redirect(url_for("dashboard.index", query=query, sort=sort))
alias_id = request.form.get("alias-id")
alias: Alias = Alias.get(alias_id)
note = request.form.get("note")
alias.note = note
db.session.commit()
flash(f"Update note for alias {alias.email}", "success")
return redirect(
url_for("dashboard.index", highlight_alias_id=alias.id, query=query)
)
elif request.form.get("form-name") == "set-mailbox":
alias_id = request.form.get("alias-id")
alias: Alias = Alias.get(alias_id)
mailbox_email = request.form.get("mailbox")
mailbox = Mailbox.get_by(email=mailbox_email)
if not mailbox or mailbox.user_id != current_user.id:
flash("Something went wrong, please retry", "warning")
else:
alias.mailbox_id = mailbox.id
db.session.commit()
LOG.d("Set alias %s mailbox to %s", alias, mailbox)
flash(
f"Update mailbox for {alias.email} to {mailbox_email}", "success",
)
return redirect(
url_for(
"dashboard.index", highlight_alias_id=alias.id, query=query,
)
)
return redirect(url_for("dashboard.index", query=query))
client_users = ( client_users = (
ClientUser.filter_by(user_id=current_user.id) ClientUser.filter_by(user_id=current_user.id)