rtc_demo/rtc/forms.py

28 lines
969 B
Python
Raw Normal View History

2024-09-21 03:17:07 +00:00
from django import forms
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
class LoginForm(forms.Form):
screen_name = forms.CharField(label="First name (or nickname)", max_length=12)
name = forms.CharField(label="Real name", max_length=50)
def clean(self):
super().clean()
screen_name = self.cleaned_data.get('screen_name', '')
name = self.cleaned_data.get('name', '')
self.user = None
found_name = User.objects.filter(username=name)
if found_name:
if found_name.exclude(first_name=screen_name):
self.add_error("name",
"The name exists but does not match the screen name used"
)
else:
self.user = found_name.first()
if screen_name == name:
self.add_error("screen_name",
"Do not use your real name as your screen name"
)