<s> import os <EOL> from django . conf import settings <EOL> from django import forms <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . contrib . auth . models import User <EOL> from django . forms import ModelForm <EOL> from django . contrib . sites . models import Site <EOL> from django . template . loader import render_to_string <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import Q <EOL> from mptt . forms import TreeNodeChoiceField <EOL> from zorna . account . models import UserProfile , UserAvatar <EOL> from zorna . account . models import UserGroup <EOL> from zorna . site . models import SiteRegistration , REGISTRATION_NO_VALIDATION , REGISTRATION_EMAIL_VALIDATION , REGISTRATION_ADMIN_VALIDATION <EOL> from zorna . site . email import ZornaEmail <EOL> class AccountEditPasswordForm ( forms . Form ) : <EOL> password1 = forms . CharField ( widget = forms . PasswordInput ( render_value = False ) , <EOL> label = _ ( u'<STR_LIT>' ) ) <EOL> password2 = forms . CharField ( widget = forms . PasswordInput ( render_value = False ) , <EOL> label = _ ( u'<STR_LIT>' ) ) <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . cleaned_data and '<STR_LIT>' in self . cleaned_data : <EOL> if self . cleaned_data [ '<STR_LIT>' ] != self . cleaned_data [ '<STR_LIT>' ] or self . cleaned_data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> raise forms . ValidationError ( _ ( <EOL> u'<STR_LIT>' ) ) <EOL> else : <EOL> raise forms . ValidationError ( _ ( u'<STR_LIT>' ) ) <EOL> return self . cleaned_data <EOL> class AccountEditProfileForm ( forms . Form ) : <EOL> username = forms . RegexField ( regex = r'<STR_LIT>' , <EOL> max_length = <NUM_LIT:30> , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT:size>' : <NUM_LIT> } ) , label = _ ( u'<STR_LIT>' ) ) <EOL> email = forms . EmailField ( widget = forms . TextInput ( <EOL> attrs = { '<STR_LIT:size>' : <NUM_LIT> } ) , label = _ ( u'<STR_LIT>' ) ) <EOL> firstname = forms . CharField ( widget = forms . TextInput ( <EOL> attrs = { '<STR_LIT:size>' : <NUM_LIT> } ) , label = _ ( u'<STR_LIT>' ) ) <EOL> lastname = forms . CharField ( widget = forms . TextInput ( <EOL> attrs = { '<STR_LIT:size>' : <NUM_LIT> } ) , label = _ ( u'<STR_LIT>' ) ) <EOL> is_active = forms . BooleanField ( label = _ ( '<STR_LIT>' ) , required = False , help_text = _ ( <EOL> "<STR_LIT>" ) ) <EOL> def __init__ ( self , user , * args , ** kwargs ) : <EOL> super ( AccountEditProfileForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . user = user <EOL> self . initial [ '<STR_LIT:email>' ] = user . email <EOL> self . initial [ '<STR_LIT>' ] = user . first_name <EOL> self . initial [ '<STR_LIT>' ] = user . last_name <EOL> self . initial [ '<STR_LIT:username>' ] = user . username <EOL> self . initial [ '<STR_LIT>' ] = user . is_active <EOL> def clean_username ( self ) : <EOL> """<STR_LIT>""" <EOL> username = self . cleaned_data . get ( "<STR_LIT:username>" ) <EOL> if not set ( username ) . issubset ( "<STR_LIT>" ) : <EOL> raise forms . ValidationError ( _ ( <EOL> "<STR_LIT>" ) ) <EOL> try : <EOL> user = User . objects . get ( <EOL> username__iexact = self . cleaned_data [ '<STR_LIT:username>' ] ) <EOL> except User . DoesNotExist : <EOL> return self . cleaned_data [ '<STR_LIT:username>' ] <EOL> if self . user and self . user . pk == user . pk : <EOL> return self . cleaned_data [ '<STR_LIT:username>' ] <EOL> raise forms . ValidationError ( _ ( <EOL> u'<STR_LIT>' ) ) <EOL> class AccountRegistrationForm ( forms . Form ) : <EOL> username = forms . RegexField ( regex = r'<STR_LIT>' , <EOL> max_length = <NUM_LIT:30> , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT:size>' : <NUM_LIT> } ) , label = _ ( u'<STR_LIT>' ) ) <EOL> password1 = forms . CharField ( widget = forms . PasswordInput ( render_value = False ) , <EOL> label = _ ( u'<STR_LIT>' ) ) <EOL> password2 = forms . CharField ( widget = forms . PasswordInput ( render_value = False ) , <EOL> label = _ ( u'<STR_LIT>' ) ) <EOL> firstname = forms . CharField ( widget = forms . TextInput ( <EOL> attrs = { '<STR_LIT:size>' : <NUM_LIT> } ) , label = _ ( u'<STR_LIT>' ) ) <EOL> lastname = forms . CharField ( widget = forms . TextInput ( <EOL> attrs = { '<STR_LIT:size>' : <NUM_LIT> } ) , label = _ ( u'<STR_LIT>' ) ) <EOL> email = forms . EmailField ( widget = forms . TextInput ( <EOL> attrs = { '<STR_LIT:size>' : <NUM_LIT> } ) , label = _ ( u'<STR_LIT>' ) ) <EOL> def clean_username ( self ) : <EOL> """<STR_LIT>""" <EOL> username = self . cleaned_data . get ( "<STR_LIT:username>" ) <EOL> if not set ( username ) . issubset ( "<STR_LIT>" ) : <EOL> raise forms . ValidationError ( _ ( <EOL> "<STR_LIT>" ) ) <EOL> try : <EOL> User . objects . get ( username__iexact = self . cleaned_data [ '<STR_LIT:username>' ] ) <EOL> except User . DoesNotExist : <EOL> return self . cleaned_data [ '<STR_LIT:username>' ] <EOL> raise forms . ValidationError ( _ ( <EOL> u'<STR_LIT>' ) ) <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . cleaned_data and '<STR_LIT>' in self . cleaned_data : <EOL> if self . cleaned_data [ '<STR_LIT>' ] != self . cleaned_data [ '<STR_LIT>' ] : <EOL> raise forms . ValidationError ( _ ( <EOL> u'<STR_LIT>' ) ) <EOL> return self . cleaned_data <EOL> def save ( self , request , admin_origin , is_active , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> current_site = Site . objects . get_current ( ) <EOL> try : <EOL> reg = SiteRegistration . objects . get ( site = current_site ) <EOL> except SiteRegistration . DoesNotExist : <EOL> reg = None <EOL> if reg and reg . validation_type == REGISTRATION_NO_VALIDATION : <EOL> is_active = True <EOL> new_user = User . objects . create_user ( self . cleaned_data [ <EOL> '<STR_LIT:username>' ] , self . cleaned_data [ '<STR_LIT:email>' ] , self . cleaned_data [ '<STR_LIT>' ] ) <EOL> new_user . is_active = is_active <EOL> new_user . first_name = self . cleaned_data [ '<STR_LIT>' ] <EOL> new_user . last_name = self . cleaned_data [ '<STR_LIT>' ] <EOL> new_user . save ( ) <EOL> up = new_user . get_profile ( ) <EOL> if admin_origin or ( reg and reg . validation_type == REGISTRATION_NO_VALIDATION ) : <EOL> up . activation_key = UserProfile . ACTIVATED <EOL> up . save ( ) <EOL> if reg : <EOL> groups = reg . groups . all ( ) <EOL> if groups : <EOL> args = [ obj for obj in groups ] <EOL> up . groups . clear ( ) <EOL> up . groups . add ( * args ) <EOL> if admin_origin is False and reg : <EOL> ec = { '<STR_LIT>' : up . activation_key , <EOL> '<STR_LIT>' : settings . ACCOUNT_ACTIVATION_DAYS , <EOL> '<STR_LIT>' : current_site , <EOL> '<STR_LIT:user>' : new_user , '<STR_LIT>' : '<STR_LIT:text>' , <EOL> '<STR_LIT:url>' : request . build_absolute_uri ( reverse ( '<STR_LIT>' , args = [ up . activation_key ] ) ) <EOL> } <EOL> if reg . validation_type == REGISTRATION_EMAIL_VALIDATION : <EOL> subject = _ ( "<STR_LIT>" ) + '<STR_LIT:U+0020>' + current_site . name <EOL> body_text = render_to_string ( <EOL> '<STR_LIT>' , ec ) <EOL> ec [ '<STR_LIT>' ] = '<STR_LIT:html>' <EOL> body_html = render_to_string ( <EOL> '<STR_LIT>' , ec ) <EOL> email = ZornaEmail ( ) <EOL> email . append ( subject , body_text , body_html , settings . DEFAULT_FROM_EMAIL , [ <EOL> new_user . email ] ) <EOL> email . send ( ) <EOL> elif reg . validation_type == REGISTRATION_ADMIN_VALIDATION : <EOL> from zorna . site . models import SiteOptions <EOL> email_lists = SiteOptions . objects . get_authorized_user ( <EOL> '<STR_LIT>' ) <EOL> if email_lists : <EOL> subject = _ ( "<STR_LIT>" ) <EOL> body_text = render_to_string ( <EOL> '<STR_LIT>' , ec ) <EOL> ec [ '<STR_LIT>' ] = '<STR_LIT:html>' <EOL> body_html = render_to_string ( <EOL> '<STR_LIT>' , ec ) <EOL> email = ZornaEmail ( ) <EOL> email . append ( subject , body_text , body_html , settings . DEFAULT_FROM_EMAIL , [ <EOL> u . email for u in email_lists ] ) <EOL> email . send ( ) <EOL> return new_user <EOL> class UserGroupForm ( ModelForm ) : <EOL> parent = TreeNodeChoiceField ( queryset = UserGroup . objects . filter ( <EOL> id__gte = <NUM_LIT:3> ) . order_by ( '<STR_LIT>' ) , required = True ) <EOL> name = forms . CharField ( max_length = <NUM_LIT> , <EOL> widget = forms . TextInput ( { '<STR_LIT:size>' : <NUM_LIT:100> } ) <EOL> ) <EOL> class Meta : <EOL> model = UserGroup <EOL> class UserAvatarForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> exclude = ( '<STR_LIT:user>' , ) <EOL> model = UserAvatar <EOL> def clean_avatar ( self ) : <EOL> data = self . cleaned_data [ '<STR_LIT>' ] <EOL> if data : <EOL> ( root , ext ) = os . path . splitext ( data . name . lower ( ) ) <EOL> allowed_extensions = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if ext not in allowed_extensions : <EOL> raise forms . ValidationError ( <EOL> _ ( u"<STR_LIT>" ) % <EOL> { '<STR_LIT>' : ext , '<STR_LIT>' : "<STR_LIT:U+002CU+0020>" . join ( allowed_extensions ) } ) <EOL> else : <EOL> raise forms . ValidationError ( _ ( <EOL> u"<STR_LIT>" ) ) <EOL> return data <EOL> SEPARATOR_CHOICES = ( <EOL> ( "<STR_LIT:;>" , _ ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT:U+002C>" , _ ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ) <EOL> ENCODING_CHOICES = ( <EOL> ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) , ) <EOL> class UsersFormImportCsv ( forms . Form ) : <EOL> csv_file = forms . CharField ( label = _ ( <EOL> u'<STR_LIT>' ) , widget = forms . FileInput ( attrs = { '<STR_LIT:size>' : '<STR_LIT>' } ) ) <EOL> separator = forms . ChoiceField ( label = _ ( <EOL> "<STR_LIT>" ) , choices = SEPARATOR_CHOICES , required = True ) <EOL> other = forms . CharField ( widget = forms . TextInput ( attrs = { <EOL> '<STR_LIT:size>' : <NUM_LIT:1> } ) , label = _ ( u'<STR_LIT>' ) , max_length = <NUM_LIT:1> , required = False ) <EOL> encoding = forms . ChoiceField ( label = _ ( <EOL> "<STR_LIT>" ) , choices = ENCODING_CHOICES , required = True ) <EOL> class RequestNewPasswordForm ( forms . Form ) : <EOL> username = forms . CharField ( widget = forms . TextInput ( attrs = { <EOL> '<STR_LIT:size>' : '<STR_LIT>' } ) , label = _ ( u'<STR_LIT>' ) , help_text = _ ( u'<STR_LIT>' ) ) <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> User . objects . get ( Q ( username = self . cleaned_data [ <EOL> '<STR_LIT:username>' ] ) | Q ( email = self . cleaned_data [ '<STR_LIT:username>' ] ) ) <EOL> except User . DoesNotExist : <EOL> raise forms . ValidationError ( _ ( u'<STR_LIT>' ) ) <EOL> except : <EOL> raise forms . ValidationError ( _ ( u'<STR_LIT>' ) ) <EOL> return self . cleaned_data <EOL> class ResetPasswordForm ( forms . Form ) : <EOL> password1 = forms . CharField ( widget = forms . PasswordInput ( render_value = False ) , <EOL> label = _ ( u'<STR_LIT>' ) ) <EOL> password2 = forms . CharField ( widget = forms . PasswordInput ( render_value = False ) , <EOL> label = _ ( u'<STR_LIT>' ) ) <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . cleaned_data and '<STR_LIT>' in self . cleaned_data : <EOL> if self . cleaned_data [ '<STR_LIT>' ] != self . cleaned_data [ '<STR_LIT>' ] : <EOL> raise forms . ValidationError ( _ ( <EOL> u'<STR_LIT>' ) ) <EOL> return self . cleaned_data </s>
