You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
2.1 KiB
55 lines
2.1 KiB
from django.contrib.auth.forms import UserCreationForm
|
|
from django.contrib.auth.models import User
|
|
from django import forms
|
|
from django.core.exceptions import ValidationError
|
|
from .choices import *
|
|
|
|
class PostForm(forms.Form):
|
|
title = forms.CharField(label='Title', max_length=200)
|
|
body = forms.CharField(label='Optional text', max_length=10000)
|
|
url = forms.CharField(label='URL', max_length=200)
|
|
# date = forms.DateTimeField('date published')
|
|
# state = forms.IntegerField(label='State', default=1)
|
|
style = forms.ChoiceField(label='Style',choices=STYLE_CHOICES, required=True)
|
|
image = forms.FileField()
|
|
|
|
class SigninForm(forms.Form):
|
|
username = forms.CharField(label='Enter Username', min_length=4, max_length=150)
|
|
password = forms.CharField(label='Enter password', widget=forms.PasswordInput)
|
|
|
|
class CustomUserCreationForm(forms.Form):
|
|
username = forms.CharField(label='Enter Username', min_length=4, max_length=150)
|
|
email = forms.EmailField(label='Enter email')
|
|
password1 = forms.CharField(label='Enter password', widget=forms.PasswordInput)
|
|
password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)
|
|
|
|
def clean_username(self):
|
|
username = self.cleaned_data['username'].lower()
|
|
r = User.objects.filter(username=username)
|
|
if r.count():
|
|
raise ValidationError("Username already exists")
|
|
return username
|
|
|
|
def clean_email(self):
|
|
email = self.cleaned_data['email'].lower()
|
|
r = User.objects.filter(email=email)
|
|
if r.count():
|
|
raise ValidationError("Email already exists")
|
|
return email
|
|
|
|
def clean_password2(self):
|
|
password1 = self.cleaned_data.get('password1')
|
|
password2 = self.cleaned_data.get('password2')
|
|
|
|
if password1 and password2 and password1 != password2:
|
|
raise ValidationError("Password don't match")
|
|
|
|
return password2
|
|
|
|
def save(self, commit=True):
|
|
user = User.objects.create_user(
|
|
self.cleaned_data['username'],
|
|
self.cleaned_data['email'],
|
|
self.cleaned_data['password1']
|
|
)
|
|
return user
|
|
|