<s> from django import forms <EOL> from django . conf import settings <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . forms import ReadOnlyPasswordHashField <EOL> from django . contrib . auth import authenticate <EOL> from models import ( Comment , Site , Thread ) <EOL> from django . utils . translation import ugettext as _ <EOL> import re <EOL> UserModel = get_user_model ( ) <EOL> class UserLoginForm ( forms . Form ) : <EOL> email = forms . EmailField ( required = True ) <EOL> password = forms . CharField ( required = True , widget = forms . PasswordInput ( ) ) <EOL> def clean ( self ) : <EOL> email = self . cleaned_data . get ( '<STR_LIT:email>' ) <EOL> password = self . cleaned_data . get ( '<STR_LIT:password>' ) <EOL> self . user = authenticate ( email = email , password = password ) <EOL> if self . user is None : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> if not self . user . is_active : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data <EOL> class RegularUserLoginForm ( UserLoginForm ) : <EOL> site_id = forms . CharField ( required = True , widget = forms . HiddenInput ( ) ) <EOL> def clean ( self ) : <EOL> super ( RegularUserLoginForm , self ) . clean ( ) <EOL> if not self . user : <EOL> return self . cleaned_data <EOL> if self . user . hidden . filter ( id__in = [ self . cleaned_data [ '<STR_LIT>' ] ] ) : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data <EOL> def get_user ( self ) : <EOL> return self . user <EOL> class StaffUserLoginForm ( UserLoginForm ) : <EOL> def clean ( self ) : <EOL> super ( StaffUserLoginForm , self ) . clean ( ) <EOL> if not self . user : <EOL> return self . cleaned_data <EOL> if not self . user . is_staff : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> if not self . user . get_sites ( ) . count ( ) : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data <EOL> def get_user ( self ) : <EOL> return self . user <EOL> class CustomUserCreationForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> model = UserModel <EOL> fields = ( "<STR_LIT:email>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> email = forms . EmailField ( required = True ) <EOL> password = forms . CharField ( label = "<STR_LIT>" , widget = forms . PasswordInput ) <EOL> password2 = forms . CharField ( <EOL> label = "<STR_LIT>" , <EOL> widget = forms . PasswordInput <EOL> ) <EOL> def clean_email ( self ) : <EOL> email = self . cleaned_data . get ( '<STR_LIT:email>' ) <EOL> if UserModel . objects . filter ( email = email ) . exists ( ) : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> return email <EOL> def clean_full_name ( self ) : <EOL> full_name = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if not full_name : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> return full_name <EOL> def clean_password2 ( self ) : <EOL> password1 = self . cleaned_data . get ( "<STR_LIT:password>" ) <EOL> password2 = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> if password1 and password2 and password1 != password2 : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise forms . ValidationError ( msg ) <EOL> return password2 <EOL> def save ( self , commit = True ) : <EOL> user = super ( CustomUserCreationForm , self ) . save ( commit = False ) <EOL> user . set_password ( self . cleaned_data [ "<STR_LIT:password>" ] ) <EOL> user . full_name = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if commit : <EOL> user . save ( ) <EOL> return user <EOL> class CustomUserChangeForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> model = UserModel <EOL> password = ReadOnlyPasswordHashField ( <EOL> help_text = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> def clean_password ( self ) : <EOL> return self . initial [ "<STR_LIT:password>" ] <EOL> class PostCommentForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Comment <EOL> fields = ( '<STR_LIT:text>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> domain = forms . CharField ( required = True ) <EOL> poster_name = forms . CharField ( required = False ) <EOL> def __init__ ( self , data , request , * args , ** kwargs ) : <EOL> self . user = request . user <EOL> self . avatar_num = request . session . get ( '<STR_LIT>' , <NUM_LIT:6> ) <EOL> self . ip_address = data . get ( '<STR_LIT>' ) <EOL> super ( PostCommentForm , self ) . __init__ ( data ) <EOL> def clean_poster_name ( self ) : <EOL> name = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if name : <EOL> return name <EOL> elif not self . user . is_anonymous ( ) : <EOL> return self . user . get_full_name ( ) <EOL> else : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> def clean_domain ( self ) : <EOL> domain = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if domain : <EOL> if Site . objects . filter ( domain = domain ) . exists ( ) : <EOL> return domain <EOL> else : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) % domain <EOL> ) <EOL> else : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> def clean ( self ) : <EOL> domain = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> thread = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if not thread or not domain : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> if not thread . allow_comments : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> try : <EOL> site = Site . objects . get ( domain = domain ) <EOL> if not self . user . is_anonymous ( ) : <EOL> if self . user . hidden . filter ( id = site . id ) : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) % site . id <EOL> ) <EOL> site . threads . get ( id = thread . id ) <EOL> except Thread . DoesNotExist : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) % thread . id <EOL> ) <EOL> return self . cleaned_data <EOL> def save ( self , * args , ** kwargs ) : <EOL> comment = super ( PostCommentForm , self ) . save ( <EOL> * args , <EOL> commit = False , <EOL> ** kwargs <EOL> ) <EOL> comment . user = None if self . user . is_anonymous ( ) else self . user <EOL> comment . avatar_num = self . avatar_num <EOL> if self . ip_address : <EOL> comment . ip_address = self . ip_address <EOL> comment . save ( ) <EOL> return comment <EOL> class GetRequestValidationForm ( forms . Form ) : <EOL> domain = forms . CharField ( ) <EOL> thread = forms . IntegerField ( ) <EOL> start = forms . DateTimeField ( required = False ) <EOL> count = forms . IntegerField ( required = False , initial = <NUM_LIT:100> ) <EOL> class SiteForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Site <EOL> def clean_domain ( self ) : <EOL> p = re . compile ( settings . DOMAIN_URL_PATTERN ) <EOL> m = p . match ( self . cleaned_data [ '<STR_LIT>' ] ) <EOL> if m : <EOL> self . cleaned_data [ '<STR_LIT>' ] = m . groups ( ) [ <NUM_LIT:1> ] <EOL> else : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data [ '<STR_LIT>' ] </s>
