Skip to content

Commit

Permalink
add profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
syrk4web committed Dec 14, 2023
1 parent 549bbe1 commit cab17e0
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 17 deletions.
42 changes: 42 additions & 0 deletions src/ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,48 @@ def home():
)


@app.route("/profile", methods=["GET", "POST"])
@login_required
def profile():
if request.method == "POST":
# Check form data validity
if not request.form:
flash("Missing form data.", "error")
return redirect(url_for("profile"))

if not any(key in request.form for key in ("admin_username", "admin_password", "admin_password_check")):
flash("Missing either admin_username, admin_password or admin_password_check.", "error")
return redirect(url_for("profile"))

error = False

if len(request.form["admin_username"]) > 256:
flash("The admin username is too long. It must be less than 256 characters.", "error")
error = True

if request.form["admin_password"] != request.form["admin_password_check"]:
flash("The passwords do not match.", "error")
error = True

if not USER_PASSWORD_RX.match(request.form["admin_password"]):
flash("The admin password is not strong enough. It must contain at least 8 characters, including at least 1 uppercase letter, 1 lowercase letter, 1 number and 1 special character (#@?!$%^&*-).", "error")
error = True

if error:
return redirect(url_for("profile"))

# TODO: Update username and password (if changed)
return Response(status=200)

return render_template(
"profile.html",
title="Profile",
username=getenv("ADMIN_USERNAME", ""),
password=getenv("ADMIN_PASSWORD", ""),
dark_mode=app.config["DARK_MODE"],
)


@app.route("/instances", methods=["GET", "POST"])
@login_required
def instances():
Expand Down
2 changes: 1 addition & 1 deletion src/ui/static/css/dashboard.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/ui/static/js/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Download {
window.open(
`${location.href.replace(
"cache",
"jobs",
)}/download?job_name=${jobName}&file_name=${fileName}`,
"jobs"
)}/download?job_name=${jobName}&file_name=${fileName}`
);
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/ui/static/js/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ class News {
return res.json();
})
.then((res) => {
console.log(res);
return this.render(res.data);
});
} catch (err) {
console.log(err);
}
} catch (err) {}
});
}

Expand Down
98 changes: 98 additions & 0 deletions src/ui/static/js/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
class SubmitProfile {
constructor() {
this.pwEl = document.querySelector("#admin_password");
this.pwCheckEl = document.querySelector("#admin_password_check");
this.pwAlertEl = document.querySelector("[data-pw-alert]");
this.formEl = document.querySelector("#profile-form");
this.init();
}

init() {
// Check password to send
this.checkSamePW();
this.pwCheckEl.addEventListener("input", (e) => {
this.checkSamePW();
});

this.pwEl.addEventListener("input", (e) => {
this.checkSamePW();
});

// Submit
this.formEl.addEventListener("submit", (e) => {
// Case not same PW
if (this.pwEl.value !== this.pwCheckEl.value) return e.preventDefault();

this.formEl.submit();
});
}

checkSamePW() {
if (!this.pwEl.value || !this.pwCheckEl.value) return this.hidePWAlert();

if (this.pwEl.value !== this.pwCheckEl.value) return this.showPWAlert();

return this.hidePWAlert();
}

hidePWAlert() {
this.pwCheckEl.classList.remove(
"focus:!border-red-500",
"focus:valid:!border-red-500",
"active:!border-red-500",
"active:valid:!border-red-500",
"valid:!border-red-500"
);
this.pwAlertEl.classList.add("opacity-0");
this.pwAlertEl.setAttribute("aria-hidden", "true");
}

showPWAlert() {
this.pwCheckEl.classList.add(
"focus:!border-red-500",
"focus:valid:!border-red-500",
"active:!border-red-500",
"active:valid:!border-red-500",
"valid:!border-red-500"
);
this.pwAlertEl.classList.remove("opacity-0");
this.pwAlertEl.setAttribute("aria-hidden", "false");
}
}

class PwBtn {
constructor() {
this.init();
}

init() {
window.addEventListener("click", (e) => {
if (!e.target.getAttribute("data-setting-password")) return;
const passwordContainer = e.target.closest("[data-input-group]");
const inpEl = passwordContainer.querySelector("input");
const invBtn = passwordContainer.querySelector(
'[data-setting-password="invisible"]'
);
const visBtn = passwordContainer.querySelector(
'[data-setting-password="visible"]'
);
inpEl.setAttribute(
"type",
inpEl.getAttribute("type") === "password" ? "text" : "password"
);

if (inpEl.getAttribute("type") === "password") {
invBtn.classList.add("hidden");
visBtn.classList.add("flex");
visBtn.classList.remove("hidden");
} else {
visBtn.classList.add("hidden");
invBtn.classList.add("flex");
invBtn.classList.remove("hidden");
}
});
}
}

const setPWBtn = new PwBtn();
const setSubmit = new SubmitProfile();
2 changes: 2 additions & 0 deletions src/ui/templates/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@
/>
{% elif current_endpoint == "jobs" %}
<script type="module" src="./js/jobs.js"></script>
{% elif current_endpoint == "profile" %}
<script defer src="./js/profile.js"></script>
{% endif %}
</head>
42 changes: 32 additions & 10 deletions src/ui/templates/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<!-- left sidebar -->
<aside
data-sidebar-menu
class="transition-all mt-[4.5rem] fixed flex inset-y-0 flex-wrap justify-between w-full p-0 my-4 overflow-y-auto antialiased transition-transform duration-200 -translate-x-full bg-white border-0 shadow-xl dark:shadow-none dark:bg-slate-850 dark:brightness-110 max-w-64 z-[1000] xl:ml-6 rounded-2xl xl:left-0 xl:translate-x-0"
class="transition-all mt-[4.5rem] fixed flex inset-y-0 flex-wrap justify-between w-full p-0 my-4 overflow-y-auto antialiased duration-200 -translate-x-full bg-white border-0 shadow-xl dark:shadow-none dark:bg-slate-850 dark:brightness-110 max-w-64 z-[1000] xl:ml-6 rounded-2xl xl:left-0 xl:translate-x-0"
aria-expanded="false"
>
<!-- close btn-->
Expand Down Expand Up @@ -86,7 +86,6 @@
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
Expand Down Expand Up @@ -118,7 +117,6 @@
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
Expand Down Expand Up @@ -149,7 +147,6 @@
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
Expand Down Expand Up @@ -180,7 +177,6 @@
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
Expand Down Expand Up @@ -211,7 +207,6 @@
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
Expand Down Expand Up @@ -247,7 +242,6 @@
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
Expand Down Expand Up @@ -278,7 +272,6 @@
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
Expand Down Expand Up @@ -309,7 +302,6 @@
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
Expand Down Expand Up @@ -340,7 +332,6 @@
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
Expand All @@ -355,6 +346,37 @@
</a>
</li>
<!-- end item -->
<li class="mt-0.5 w-full">
<a
class="{% if current_endpoint == 'profile' %}font-semibold text-slate-700 dark:bg-primary/50 rounded-lg dark:hover:bg-primary/60 bg-primary/20 hover:bg-primary/30{% else %}dark:hover:bg-primary/20 hover:bg-primary/5 {% endif %} hover:rounded-lg dark:text-white dark:opacity-80 py-1 text-sm ease-nav-brand my-0 mx-2 flex items-center whitespace-nowrap px-4 transition"
href="{% if current_endpoint == 'profile' %}javascript:void(0){% else %}loading?next={{ url_for('profile') }}{% endif %}"
>
<div
class="mr-2 flex items-center justify-center rounded-lg bg-center stroke-0 text-center p-1 xl:p-1.5"
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="stroke-teal-600 h-6 w-6 relative"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M17.982 18.725A7.488 7.488 0 0012 15.75a7.488 7.488 0 00-5.982 2.975m11.963 0a9 9 0 10-11.963 0m11.963 0A8.966 8.966 0 0112 21a8.966 8.966 0 01-5.982-2.275M15 9.75a3 3 0 11-6 0 3 3 0 016 0z"
/>
</svg>
</div>
<span
class="ml-1 duration-300 opacity-100 pointer-events-none ease"
>
Profile
</span>
</a>
</li>
<!-- end item -->
</ul>
<!-- end default anchor -->

Expand Down
Loading

0 comments on commit cab17e0

Please sign in to comment.