You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.5 KiB
Python

import base64
8 years ago
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.utils.safestring import mark_safe
class FormPureRender:
def as_pure_aligned(self):
html = ""
8 years ago
for f in self:
html += '<div class="pure-control-group">\n'
html += str(f.label_tag()) + "\n"
html += str(f) + "\n"
8 years ago
if f.errors:
html += str(f.errors) + "\n"
html += "</div>\n"
8 years ago
return mark_safe(html)
class UserField(forms.RegexField):
def clean(self, value):
super(UserField, self).clean(value)
try:
User.objects.get(username=value)
raise forms.ValidationError(_("Username taken."))
except User.DoesNotExist:
return value
class SignupForm(forms.Form, FormPureRender):
username = UserField(
label=_("Username"),
min_length=2,
max_length=16,
regex="^[a-zA-Z0-9_-]+$",
widget=forms.TextInput(
attrs={
"required": "true",
"pattern": "[a-zA-Z0-9_-]{2,32}",
"placeholder": _("Username"),
"autofocus": "true",
}
),
8 years ago
)
password = forms.CharField(
label=_("Password"),
widget=forms.PasswordInput(attrs={"placeholder": _("Anything")}),
8 years ago
)
password2 = forms.CharField(
label=_("Repeat"),
widget=forms.PasswordInput(attrs={"placeholder": _("Same Anything")}),
8 years ago
)
email = forms.EmailField(
label=_("E-Mail"),
widget=forms.EmailInput(attrs={"placeholder": _("E-Mail")}),
8 years ago
required=False,
)
def clean_password(self):
if self.data["password"] != self.data["password2"]:
8 years ago
raise forms.ValidationError(_("Passwords are not the same"))
return self.data["password"]
8 years ago
class ReqEmailForm(forms.Form, FormPureRender):
email = forms.EmailField(
label=_("E-Mail"),
widget=forms.EmailInput(attrs={"placeholder": _("E-Mail")}),
)
def wg_pk_validator(s):
try:
data = base64.b64decode(s, validate=True)
except:
raise forms.ValidationError("Invalid public key format")
if len(data) != 32:
raise forms.ValidationError("Invalid public key length")
class WgPeerForm(forms.Form):
public_key = forms.CharField(
min_length=3,
max_length=100,
strip=True,
required=False,
validators=[wg_pk_validator],
)
name = forms.CharField(max_length=21, required=False)