improve placeholder on login template

This commit is contained in:
Jens Langhammer 2019-02-25 19:43:33 +01:00
parent 9d344d887c
commit f2569b6424
No known key found for this signature in database
GPG key ID: BEBC05297D92821B
4 changed files with 17 additions and 3 deletions

View file

@ -8,6 +8,7 @@ from django.utils.translation import gettext_lazy as _
from passbook.core.models import User from passbook.core.models import User
from passbook.lib.config import CONFIG from passbook.lib.config import CONFIG
from passbook.lib.utils.ui import human_list
LOGGER = getLogger(__name__) LOGGER = getLogger(__name__)
@ -15,13 +16,16 @@ class LoginForm(forms.Form):
"""Allow users to login""" """Allow users to login"""
title = _('Log in to your account') title = _('Log in to your account')
uid_field = forms.CharField(widget=forms.TextInput(attrs={'placeholder': _('UID')})) uid_field = forms.CharField()
remember_me = forms.BooleanField(required=False) remember_me = forms.BooleanField(required=False)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if CONFIG.y('passbook.uid_fields') == ['email']: if CONFIG.y('passbook.uid_fields') == ['e-mail']:
self.fields['uid_field'] = forms.EmailField() self.fields['uid_field'] = forms.EmailField()
self.fields['uid_field'].widget.attrs = {
'placeholder': _(human_list([x.title() for x in CONFIG.y('passbook.uid_fields')]))
}
def clean_uid_field(self): def clean_uid_field(self):
"""Validate uid_field after EmailValidator if 'email' is the only selected uid_fields""" """Validate uid_field after EmailValidator if 'email' is the only selected uid_fields"""

View file

@ -52,6 +52,9 @@ class LoginView(UserPassesTestMixin, FormView):
def get_user(self, uid_value) -> User: def get_user(self, uid_value) -> User:
"""Find user instance. Returns None if no user was found.""" """Find user instance. Returns None if no user was found."""
for search_field in CONFIG.y('passbook.uid_fields'): for search_field in CONFIG.y('passbook.uid_fields'):
# Workaround for E-Mail -> email
if search_field == 'e-mail':
search_field = 'email'
users = User.objects.filter(**{search_field: uid_value}) users = User.objects.filter(**{search_field: uid_value})
if users.exists(): if users.exists():
LOGGER.debug("Found user %s with uid_field %s", users.first(), search_field) LOGGER.debug("Found user %s with uid_field %s", users.first(), search_field)

View file

@ -61,7 +61,7 @@ passbook:
# Specify which fields can be used to authenticate. Can be any combination of `username` and `email` # Specify which fields can be used to authenticate. Can be any combination of `username` and `email`
uid_fields: uid_fields:
- username - username
- email - e-mail
# Factors to load # Factors to load
factors: factors:
- passbook.core.auth.factors.backend - passbook.core.auth.factors.backend

7
passbook/lib/utils/ui.py Normal file
View file

@ -0,0 +1,7 @@
"""passbook UI utils"""
def human_list(_list) -> str:
"""Convert a list of items into 'a, b or c'"""
last_item = _list.pop()
result = ', '.join(_list)
return '%s or %s' % (result, last_item)