Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add password toggle feature in forms

AKSHIT78b5d7076d21
diff --git a/wger/core/forms.py b/wger/core/forms.py
index 77a19496a..eb8641ee1 100644
--- a/wger/core/forms.py
+++ b/wger/core/forms.py
@@ -53,6 +53,19 @@ from django_recaptcha.widgets import ReCaptchaV3
from wger.core.models import UserProfile
+class PasswordInputWithToggle(PasswordInput):
+ """
+ Custom PasswordInput widget with eye icon toggle functionality
+ """
+ template_name = 'forms/password_with_toggle.html'
+
+ def __init__(self, attrs=None, render_value=False):
+ default_attrs = {'class': 'form-control'}
+ if attrs:
+ default_attrs.update(attrs)
+ super().__init__(default_attrs, render_value)
+
+
class UserLoginForm(AuthenticationForm):
"""
Form for logins
@@ -63,6 +76,9 @@ class UserLoginForm(AuthenticationForm):
def __init__(self, authenticate_on_clean=True, *args, **kwargs):
super(UserLoginForm, self).__init__(*args, **kwargs)
+ # Apply custom password widget
+ self.fields['password'].widget = PasswordInputWithToggle()
+
self.authenticate_on_clean = authenticate_on_clean
self.helper = FormHelper()
@@ -237,7 +253,7 @@ class PasswordConfirmationForm(Form):
password = CharField(
label=_('Password'),
- widget=PasswordInput,
+ widget=PasswordInputWithToggle,
help_text=_('Please enter your current password.'),
)
@@ -273,6 +289,11 @@ class RegistrationForm(UserCreationForm, UserEmailForm):
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
+
+ # Apply custom password widgets
+ self.fields['password1'].widget = PasswordInputWithToggle()
+ self.fields['password2'].widget = PasswordInputWithToggle()
+
self.helper = FormHelper()
self.helper.form_class = 'wger-form'
self.helper.layout = Layout(
@@ -295,6 +316,11 @@ class RegistrationFormNoCaptcha(UserCreationForm, UserEmailForm):
def __init__(self, *args, **kwargs):
super(RegistrationFormNoCaptcha, self).__init__(*args, **kwargs)
+
+ # Apply custom password widgets
+ self.fields['password1'].widget = PasswordInputWithToggle()
+ self.fields['password2'].widget = PasswordInputWithToggle()
+
self.helper = FormHelper()
self.helper.form_class = 'wger-form'
self.helper.layout = Layout(
@@ -344,4 +370,4 @@ class FeedbackAnonymousForm(FeedbackRegisteredForm):
widget=ReCaptchaV3,
label='reCaptcha',
help_text=_('The form is secured with reCAPTCHA'),
- )
+ )
\ No newline at end of file
diff --git a/wger/core/templates/forms/password_with_toggle.html b/wger/core/templates/forms/password_with_toggle.html
new file mode 100644
index 000000000..d0db2fa94
--- /dev/null
+++ b/wger/core/templates/forms/password_with_toggle.html
@@ -0,0 +1,92 @@
+<!-- templates/forms/password_with_toggle.html -->
+<div class="password-wrapper" style="position: relative;">
+ <input type="{{ widget.type }}" name="{{ widget.name }}"{% if widget.value %} value="{{ widget.value }}"{% endif %}{% if widget.attrs.id %} id="{{ widget.attrs.id }}"{% endif %}{% if widget.attrs.class %} class="{{ widget.attrs.class }}"{% endif %}{% if widget.attrs.placeholder %} placeholder="{{ widget.attrs.placeholder }}"{% endif %}{% if widget.attrs.required %} required{% endif %}{% if widget.attrs.maxlength %} maxlength="{{ widget.attrs.maxlength }}"{% endif %}{% if widget.attrs.autocomplete %} autocomplete="{{ widget.attrs.autocomplete }}"{% endif %}>
+ <button type="button" class="password-toggle-btn" aria-label="Toggle password visibility" style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); background: none; border: none; cursor: pointer; padding: 5px; z-index: 10;">
+ <svg class="eye-icon eye-open" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
+ <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
+ <circle cx="12" cy="12" r="3"></circle>
+ </svg>
+ <svg class="eye-icon eye-closed" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;">
+ <path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"></path>
+ <line x1="1" y1="1" x2="23" y2="23"></line>
+ </svg>
+ </button>
+</div>
+
+<script>
+(function() {
+ function initPasswordToggle() {
+ const passwordWrappers = document.querySelectorAll('.password-wrapper');
+
+ passwordWrappers.forEach(function(wrapper) {
+ // Check if already initialized
+ if (wrapper.hasAttribute('data-toggle-initialized')) {
+ return;
+ }
+
+ const passwordInput = wrapper.querySelector('input');
+ const toggleBtn = wrapper.querySelector('.password-toggle-btn');
+ const eyeOpen = wrapper.querySelector('.eye-open');
+ const eyeClosed = wrapper.querySelector('.eye-closed');
+
+ if (toggleBtn && passwordInput && eyeOpen && eyeClosed) {
+ toggleBtn.addEventListener('click', function(e) {
+ e.preventDefault();
+ e.stopPropagation();
+
+ if (passwordInput.type === 'password') {
+ passwordInput.type = 'text';
+ eyeOpen.style.display = 'none';
+ eyeClosed.style.display = 'block';
+ } else {
+ passwordInput.type = 'password';
+ eyeOpen.style.display = 'block';
+ eyeClosed.style.display = 'none';
+ }
+ });
+
+ // Mark as initialized
+ wrapper.setAttribute('data-toggle-initialized', 'true');
+ }
+ });
+ }
+
+ // Initialize on DOM ready
+ if (document.readyState === 'loading') {
+ document.addEventListener('DOMContentLoaded', initPasswordToggle);
+ } else {
+ initPasswordToggle();
+ }
+
+ // Also initialize after a short delay to catch late-rendered elements
+ setTimeout(initPasswordToggle, 100);
+ setTimeout(initPasswordToggle, 500);
+})();
+</script>
+
+<style>
+.password-wrapper {
+ display: inline-block;
+ position: relative;
+ width: auto;
+ max-width: 100%;
+}
+
+.password-wrapper input {
+ padding-right: 40px !important;
+ box-sizing: border-box;
+}
+
+.password-toggle-btn {
+ color: #6c757d;
+}
+
+.password-toggle-btn:hover {
+ color: #495057;
+}
+
+.password-toggle-btn:focus {
+ outline: 2px solid #007bff;
+ outline-offset: 2px;
+}
+</style>
\ No newline at end of file