2018-12-10 13:21:42 +00:00
|
|
|
"""passbook core invitation form"""
|
2018-12-10 13:13:18 +00:00
|
|
|
|
|
|
|
from django import forms
|
2018-12-10 14:27:55 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.utils.translation import gettext as _
|
2018-12-10 13:13:18 +00:00
|
|
|
|
2018-12-10 14:27:55 +00:00
|
|
|
from passbook.core.models import Invitation, User
|
2018-12-10 13:13:18 +00:00
|
|
|
|
|
|
|
|
2018-12-10 13:49:15 +00:00
|
|
|
class InvitationForm(forms.ModelForm):
|
|
|
|
"""InvitationForm"""
|
2018-12-10 13:13:18 +00:00
|
|
|
|
2018-12-10 14:27:55 +00:00
|
|
|
def clean_fixed_username(self):
|
|
|
|
"""Check if username is already used"""
|
2019-12-31 11:51:16 +00:00
|
|
|
username = self.cleaned_data.get("fixed_username")
|
2018-12-10 14:27:55 +00:00
|
|
|
if User.objects.filter(username=username).exists():
|
2019-12-31 11:51:16 +00:00
|
|
|
raise ValidationError(_("Username is already in use."))
|
2018-12-10 14:27:55 +00:00
|
|
|
return username
|
|
|
|
|
|
|
|
def clean_fixed_email(self):
|
|
|
|
"""Check if email is already used"""
|
2019-12-31 11:51:16 +00:00
|
|
|
email = self.cleaned_data.get("fixed_email")
|
2018-12-10 14:27:55 +00:00
|
|
|
if User.objects.filter(email=email).exists():
|
2019-12-31 11:51:16 +00:00
|
|
|
raise ValidationError(_("E-Mail is already in use."))
|
2018-12-10 14:27:55 +00:00
|
|
|
return email
|
|
|
|
|
2018-12-10 13:13:18 +00:00
|
|
|
class Meta:
|
|
|
|
|
2018-12-10 13:49:15 +00:00
|
|
|
model = Invitation
|
2019-12-31 11:51:16 +00:00
|
|
|
fields = ["expires", "fixed_username", "fixed_email", "needs_confirmation"]
|
2018-12-10 13:38:44 +00:00
|
|
|
labels = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"fixed_username": "Force user's username (optional)",
|
|
|
|
"fixed_email": "Force user's email (optional)",
|
2018-12-10 13:38:44 +00:00
|
|
|
}
|
|
|
|
widgets = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"fixed_username": forms.TextInput(),
|
|
|
|
"fixed_email": forms.TextInput(),
|
2018-12-10 13:38:44 +00:00
|
|
|
}
|