Skip to content
This repository has been archived by the owner on May 5, 2020. It is now read-only.

Commit

Permalink
feat: Use username field metadata for form/serializer fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rubengrill authored and relekang committed Sep 14, 2018
1 parent 4c74112 commit 8d46122
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 9 additions & 3 deletions nopassword/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from django import forms
from django.contrib.auth import authenticate, get_user_model
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _


Expand All @@ -9,7 +10,7 @@ class AuthenticationForm(forms.Form):
Base class for authenticating users. Extend this to get a form that accepts
username logins.
"""
username = forms.CharField(label=_("Username"), max_length=30)
username = forms.CharField()

error_messages = {
'invalid_login': _("Please enter a correct username. "
Expand All @@ -26,10 +27,15 @@ def __init__(self, request=None, *args, **kwargs):
cookie with the key TEST_COOKIE_NAME and value TEST_COOKIE_VALUE before
running this validation.
"""
super(AuthenticationForm, self).__init__(*args, **kwargs)

self.request = request
self.login_code = None
super(AuthenticationForm, self).__init__(*args, **kwargs)
self.fields['username'].label = _(get_user_model().USERNAME_FIELD.capitalize())
self.username_field = get_user_model()._meta.get_field(get_user_model().USERNAME_FIELD)
self.fields['username'].max_length = self.username_field.max_length or 254

if self.fields['username'].label is None:
self.fields['username'].label = capfirst(self.username_field.verbose_name)

def clean_username(self):
username = self.cleaned_data['username']
Expand Down
7 changes: 6 additions & 1 deletion nopassword/rest/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@


class LoginCodeRequestSerializer(serializers.Serializer):
username = serializers.CharField(label=_('Username'), max_length=30)
username = serializers.CharField()

login_code_request_form_class = AuthenticationForm

def __init__(self, *args, **kwargs):
super(LoginCodeRequestSerializer, self).__init__(*args, **kwargs)
self.username_field = get_user_model()._meta.get_field(get_user_model().USERNAME_FIELD)
self.fields['username'].max_length = self.username_field.max_length or 254

def validate(self, data):
username = data.get('username')

Expand Down

0 comments on commit 8d46122

Please sign in to comment.