<s> """<STR_LIT>""" <EOL> from collections import OrderedDict <EOL> from django import forms <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import QueryDict <EOL> from django . utils . translation import ugettext as _ , ugettext_lazy <EOL> from passwords . fields import PasswordField <EOL> from modoboa . core . models import User <EOL> from modoboa . core import signals as core_signals <EOL> from modoboa . lib import events , parameters <EOL> from modoboa . lib . email_utils import split_mailbox <EOL> from modoboa . lib import exceptions as lib_exceptions <EOL> from modoboa . lib import fields as lib_fields <EOL> from modoboa . lib . form_utils import ( <EOL> DynamicForm , TabForms , WizardForm , WizardStep <EOL> ) <EOL> from modoboa . lib . permissions import get_account_roles <EOL> from modoboa . lib . validators import validate_utf8_email <EOL> from modoboa . lib . web_utils import render_to_json_response <EOL> from . . models import Domain , Mailbox , Alias <EOL> class AccountFormGeneral ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> username = forms . CharField ( <EOL> label = ugettext_lazy ( "<STR_LIT>" ) , <EOL> help_text = ugettext_lazy ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> ) <EOL> role = forms . ChoiceField ( <EOL> label = ugettext_lazy ( "<STR_LIT>" ) , <EOL> choices = [ ( '<STR_LIT>' , ugettext_lazy ( "<STR_LIT>" ) ) ] , <EOL> help_text = ugettext_lazy ( "<STR_LIT>" ) <EOL> ) <EOL> password1 = PasswordField ( <EOL> label = ugettext_lazy ( "<STR_LIT>" ) , widget = forms . widgets . PasswordInput <EOL> ) <EOL> password2 = PasswordField ( <EOL> label = ugettext_lazy ( "<STR_LIT>" ) , <EOL> widget = forms . widgets . PasswordInput , <EOL> help_text = ugettext_lazy ( <EOL> "<STR_LIT>" <EOL> ) <EOL> ) <EOL> class Meta : <EOL> model = User <EOL> fields = ( <EOL> "<STR_LIT:username>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) <EOL> def __init__ ( self , user , * args , ** kwargs ) : <EOL> super ( AccountFormGeneral , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields = OrderedDict ( <EOL> ( key , self . fields [ key ] ) for key in <EOL> [ '<STR_LIT>' , '<STR_LIT:username>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> self . fields [ "<STR_LIT>" ] . label = _ ( "<STR_LIT>" ) <EOL> self . user = user <EOL> if user . group == "<STR_LIT>" : <EOL> self . fields [ "<STR_LIT>" ] = forms . CharField ( <EOL> label = "<STR_LIT>" , <EOL> widget = forms . HiddenInput ( attrs = { "<STR_LIT:class>" : "<STR_LIT>" } ) , <EOL> required = False <EOL> ) <EOL> else : <EOL> self . fields [ "<STR_LIT>" ] . choices = [ ( '<STR_LIT>' , ugettext_lazy ( "<STR_LIT>" ) ) ] <EOL> self . fields [ "<STR_LIT>" ] . choices += get_account_roles ( user , kwargs [ '<STR_LIT>' ] ) if '<STR_LIT>' in kwargs else get_account_roles ( user ) <EOL> if not user . is_superuser : <EOL> del self . fields [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in kwargs : <EOL> account = kwargs [ "<STR_LIT>" ] <EOL> domain_disabled = ( <EOL> hasattr ( account , "<STR_LIT>" ) and <EOL> not account . mailbox . domain . enabled <EOL> ) <EOL> if domain_disabled : <EOL> self . fields [ "<STR_LIT>" ] . widget . attrs [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> if args : <EOL> if args [ <NUM_LIT:0> ] . get ( "<STR_LIT>" , "<STR_LIT>" ) == "<STR_LIT>" and args [ <NUM_LIT:0> ] . get ( "<STR_LIT>" , "<STR_LIT>" ) == "<STR_LIT>" : <EOL> self . fields [ "<STR_LIT>" ] . required = False <EOL> self . fields [ "<STR_LIT>" ] . required = False <EOL> if domain_disabled : <EOL> del self . fields [ "<STR_LIT>" ] <EOL> self . fields [ "<STR_LIT>" ] . initial = account . group <EOL> if not account . is_local and parameters . get_admin ( <EOL> "<STR_LIT>" , app = "<STR_LIT>" ) == "<STR_LIT>" : <EOL> del self . fields [ "<STR_LIT>" ] <EOL> del self . fields [ "<STR_LIT>" ] <EOL> def domain_is_disabled ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self . instance , "<STR_LIT>" ) : <EOL> return False <EOL> return not self . instance . mailbox . domain . enabled <EOL> def clean_role ( self ) : <EOL> if self . user . group == "<STR_LIT>" : <EOL> if self . instance == self . user : <EOL> return "<STR_LIT>" <EOL> return "<STR_LIT>" <EOL> return self . cleaned_data [ "<STR_LIT>" ] <EOL> def clean_username ( self ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" not in self . cleaned_data : <EOL> return self . cleaned_data [ "<STR_LIT:username>" ] <EOL> if self . cleaned_data [ "<STR_LIT>" ] != "<STR_LIT>" : <EOL> return self . cleaned_data [ "<STR_LIT:username>" ] <EOL> uname = self . cleaned_data [ "<STR_LIT:username>" ] . lower ( ) <EOL> validate_utf8_email ( uname ) <EOL> return uname <EOL> def clean_password2 ( self ) : <EOL> password1 = self . cleaned_data . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> password2 = self . cleaned_data [ "<STR_LIT>" ] <EOL> if password1 != password2 : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return password2 <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( AccountFormGeneral , self ) . clean ( ) <EOL> if self . errors : <EOL> return self . cleaned_data <EOL> condition = ( <EOL> self . cleaned_data . get ( "<STR_LIT>" ) and <EOL> self . cleaned_data [ "<STR_LIT>" ] != "<STR_LIT>" <EOL> ) <EOL> if condition : <EOL> self . add_error ( <EOL> "<STR_LIT>" , <EOL> _ ( "<STR_LIT>" ) <EOL> ) <EOL> return self . cleaned_data <EOL> def save ( self , commit = True ) : <EOL> account = super ( AccountFormGeneral , self ) . save ( commit = False ) <EOL> if self . user == account and not self . cleaned_data [ "<STR_LIT>" ] : <EOL> raise lib_exceptions . PermDeniedException ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> if commit : <EOL> if "<STR_LIT>" in self . cleaned_data and self . cleaned_data [ "<STR_LIT>" ] != "<STR_LIT>" : <EOL> account . set_password ( self . cleaned_data [ "<STR_LIT>" ] ) <EOL> account . save ( ) <EOL> account . role = self . cleaned_data [ "<STR_LIT>" ] <EOL> return account <EOL> class AccountFormMail ( forms . Form , DynamicForm ) : <EOL> """<STR_LIT>""" <EOL> email = lib_fields . UTF8EmailField ( <EOL> label = ugettext_lazy ( "<STR_LIT>" ) , required = False ) <EOL> quota = forms . IntegerField ( <EOL> label = ugettext_lazy ( "<STR_LIT>" ) , <EOL> required = False , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> widget = forms . widgets . TextInput ( attrs = { "<STR_LIT:class>" : "<STR_LIT>" } ) <EOL> ) <EOL> quota_act = forms . BooleanField ( required = False ) <EOL> aliases = lib_fields . UTF8AndEmptyUserEmailField ( <EOL> label = ugettext_lazy ( "<STR_LIT>" ) , <EOL> required = False , <EOL> help_text = ugettext_lazy ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . mb = kwargs . pop ( "<STR_LIT>" , None ) <EOL> super ( AccountFormMail , self ) . __init__ ( * args , ** kwargs ) <EOL> self . field_widths = { <EOL> "<STR_LIT>" : <NUM_LIT:3> <EOL> } <EOL> self . extra_fields = [ ] <EOL> result = events . raiseQueryEvent ( '<STR_LIT>' , '<STR_LIT>' , self . mb ) <EOL> for fname , field in result : <EOL> self . fields [ fname ] = field <EOL> self . extra_fields . append ( fname ) <EOL> if self . mb is not None : <EOL> self . fields [ "<STR_LIT:email>" ] . required = True <EOL> cpt = <NUM_LIT:1> <EOL> qset = self . mb . aliasrecipient_set . select_related ( "<STR_LIT>" ) <EOL> for ralias in qset : <EOL> name = "<STR_LIT>" % cpt <EOL> self . _create_field ( <EOL> lib_fields . UTF8AndEmptyUserEmailField , name , <EOL> ralias . alias . address ) <EOL> cpt += <NUM_LIT:1> <EOL> self . fields [ "<STR_LIT:email>" ] . initial = self . mb . full_address <EOL> self . fields [ "<STR_LIT>" ] . initial = self . mb . use_domain_quota <EOL> if not self . mb . use_domain_quota and self . mb . quota : <EOL> self . fields [ "<STR_LIT>" ] . initial = self . mb . quota <EOL> else : <EOL> self . fields [ "<STR_LIT>" ] . initial = True <EOL> if len ( args ) and isinstance ( args [ <NUM_LIT:0> ] , QueryDict ) : <EOL> self . _load_from_qdict ( <EOL> args [ <NUM_LIT:0> ] , "<STR_LIT>" , lib_fields . UTF8AndEmptyUserEmailField ) <EOL> def clean_email ( self ) : <EOL> """<STR_LIT>""" <EOL> email = self . cleaned_data [ "<STR_LIT:email>" ] . lower ( ) <EOL> self . locpart , domname = split_mailbox ( email ) <EOL> if not domname : <EOL> return email <EOL> try : <EOL> self . domain = Domain . objects . get ( name = domname ) <EOL> except Domain . DoesNotExist : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> if not self . mb : <EOL> try : <EOL> core_signals . can_create_object . send ( <EOL> sender = self . __class__ , context = self . domain , <EOL> object_type = "<STR_LIT>" ) <EOL> except lib_exceptions . ModoboaException as inst : <EOL> raise forms . ValidationError ( inst ) <EOL> return email <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( AccountFormMail , self ) . clean ( ) <EOL> if not self . cleaned_data [ "<STR_LIT>" ] and self . cleaned_data [ '<STR_LIT>' ] is not None : <EOL> if self . cleaned_data [ "<STR_LIT>" ] < <NUM_LIT:0> : <EOL> self . add_error ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) <EOL> self . aliases = [ ] <EOL> for name , value in self . cleaned_data . iteritems ( ) : <EOL> if not name . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> if value == "<STR_LIT>" : <EOL> continue <EOL> local_part , domname = split_mailbox ( value ) <EOL> if not Domain . objects . filter ( name = domname ) . exists ( ) : <EOL> self . add_error ( name , _ ( "<STR_LIT>" ) ) <EOL> break <EOL> self . aliases . append ( value . lower ( ) ) <EOL> return self . cleaned_data <EOL> def create_mailbox ( self , user , account ) : <EOL> """<STR_LIT>""" <EOL> if not user . can_access ( self . domain ) : <EOL> raise lib_exceptions . PermDeniedException <EOL> core_signals . can_create_object . send ( <EOL> self . __class__ , context = user , object_type = "<STR_LIT>" ) <EOL> self . mb = Mailbox ( <EOL> address = self . locpart , domain = self . domain , user = account , <EOL> use_domain_quota = self . cleaned_data [ "<STR_LIT>" ] ) <EOL> self . mb . set_quota ( self . cleaned_data [ "<STR_LIT>" ] , <EOL> user . has_perm ( "<STR_LIT>" ) ) <EOL> self . mb . save ( creator = user ) <EOL> def _update_aliases ( self , user , account ) : <EOL> """<STR_LIT>""" <EOL> qset = self . mb . aliasrecipient_set . select_related ( "<STR_LIT>" ) . filter ( <EOL> alias__internal = False ) <EOL> for ralias in qset : <EOL> if ralias . alias . address not in self . aliases : <EOL> alias = ralias . alias <EOL> ralias . delete ( ) <EOL> if alias . recipients_count > <NUM_LIT:0> : <EOL> continue <EOL> alias . delete ( ) <EOL> else : <EOL> self . aliases . remove ( ralias . alias . address ) <EOL> if not self . aliases : <EOL> return <EOL> core_signals . can_create_object . send ( <EOL> self . __class__ , context = user , object_type = "<STR_LIT>" , <EOL> count = len ( self . aliases ) ) <EOL> core_signals . can_create_object . send ( <EOL> self . __class__ , context = self . mb . domain , <EOL> object_type = "<STR_LIT>" , count = len ( self . aliases ) ) <EOL> for alias in self . aliases : <EOL> if self . mb . aliasrecipient_set . select_related ( "<STR_LIT>" ) . filter ( <EOL> alias__address = alias ) . exists ( ) : <EOL> continue <EOL> local_part , domname = split_mailbox ( alias ) <EOL> al = Alias ( address = alias , enabled = account . is_active ) <EOL> al . domain = Domain . objects . get ( name = domname ) <EOL> al . save ( ) <EOL> al . set_recipients ( [ self . mb . full_address ] ) <EOL> al . post_create ( user ) <EOL> def save ( self , user , account ) : <EOL> """<STR_LIT>""" <EOL> if self . cleaned_data [ "<STR_LIT:email>" ] == "<STR_LIT>" : <EOL> return None <EOL> if self . cleaned_data [ "<STR_LIT>" ] : <EOL> self . cleaned_data [ "<STR_LIT>" ] = None <EOL> if not hasattr ( self , "<STR_LIT>" ) or self . mb is None : <EOL> self . create_mailbox ( user , account ) <EOL> else : <EOL> self . cleaned_data [ "<STR_LIT>" ] = ( <EOL> self . cleaned_data [ "<STR_LIT>" ] ) <EOL> self . mb . update_from_dict ( user , self . cleaned_data ) <EOL> events . raiseEvent ( <EOL> '<STR_LIT>' , '<STR_LIT>' , self . mb , self . cleaned_data <EOL> ) <EOL> account . email = self . cleaned_data [ "<STR_LIT:email>" ] <EOL> account . save ( ) <EOL> self . _update_aliases ( user , account ) <EOL> return self . mb <EOL> class AccountPermissionsForm ( forms . Form , DynamicForm ) : <EOL> """<STR_LIT>""" <EOL> domains = lib_fields . DomainNameField ( <EOL> label = ugettext_lazy ( "<STR_LIT>" ) , <EOL> required = False , <EOL> help_text = ugettext_lazy ( "<STR_LIT>" ) <EOL> ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if "<STR_LIT>" in kwargs : <EOL> self . account = kwargs [ "<STR_LIT>" ] <EOL> del kwargs [ "<STR_LIT>" ] <EOL> super ( AccountPermissionsForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if not hasattr ( self , "<STR_LIT>" ) or self . account is None : <EOL> return <EOL> for pos , dom in enumerate ( Domain . objects . get_for_admin ( self . account ) ) : <EOL> name = "<STR_LIT>" % ( pos + <NUM_LIT:1> ) <EOL> self . _create_field ( lib_fields . DomainNameField , name , dom . name ) <EOL> if len ( args ) and isinstance ( args [ <NUM_LIT:0> ] , QueryDict ) : <EOL> self . _load_from_qdict ( <EOL> args [ <NUM_LIT:0> ] , "<STR_LIT>" , lib_fields . DomainNameField ) <EOL> def save ( self ) : <EOL> current_domains = [ <EOL> dom . name for dom in Domain . objects . get_for_admin ( self . account ) <EOL> ] <EOL> for name , value in self . cleaned_data . items ( ) : <EOL> if not name . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> if value in [ "<STR_LIT>" , None ] : <EOL> continue <EOL> if value not in current_domains : <EOL> domain = Domain . objects . get ( name = value ) <EOL> domain . add_admin ( self . account ) <EOL> for domain in Domain . objects . get_for_admin ( self . account ) : <EOL> if not filter ( lambda name : self . cleaned_data [ name ] == domain . name , <EOL> self . cleaned_data . keys ( ) ) : <EOL> domain . remove_admin ( self . account ) <EOL> class AccountForm ( TabForms ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> self . user = request . user <EOL> self . forms = [ <EOL> dict ( id = "<STR_LIT>" , title = _ ( "<STR_LIT>" ) , <EOL> formtpl = "<STR_LIT>" , <EOL> cls = AccountFormGeneral , <EOL> new_args = [ self . user ] , mandatory = True ) , <EOL> dict ( id = "<STR_LIT>" , <EOL> title = _ ( "<STR_LIT>" ) , formtpl = "<STR_LIT>" , <EOL> cls = AccountFormMail ) , <EOL> dict ( <EOL> id = "<STR_LIT>" , title = _ ( "<STR_LIT>" ) , <EOL> formtpl = "<STR_LIT>" , <EOL> cls = AccountPermissionsForm <EOL> ) <EOL> ] <EOL> cbargs = [ self . user ] <EOL> if "<STR_LIT>" in kwargs : <EOL> cbargs += [ kwargs [ "<STR_LIT>" ] [ "<STR_LIT>" ] ] <EOL> self . forms += events . raiseQueryEvent ( "<STR_LIT>" , * cbargs ) <EOL> super ( AccountForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> def extra_context ( self , context ) : <EOL> account = self . instances [ "<STR_LIT>" ] <EOL> context . update ( { <EOL> '<STR_LIT:title>' : account . username , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:action>' : reverse ( "<STR_LIT>" , <EOL> args = [ account . id ] ) , <EOL> } ) <EOL> def check_perms ( self , account ) : <EOL> if account . is_superuser : <EOL> return False <EOL> return self . user . has_perm ( "<STR_LIT>" ) and account . has_perm ( "<STR_LIT>" ) <EOL> def _before_is_valid ( self , form ) : <EOL> if form [ "<STR_LIT:id>" ] == "<STR_LIT>" : <EOL> return True <EOL> if hasattr ( self , "<STR_LIT>" % form [ "<STR_LIT:id>" ] ) : <EOL> if not getattr ( self , "<STR_LIT>" % form [ "<STR_LIT:id>" ] ) ( self . account ) : <EOL> return False <EOL> return True <EOL> extra_forms = events . raiseQueryEvent ( <EOL> "<STR_LIT>" , self . account , form ) <EOL> if False in extra_forms : <EOL> return False <EOL> return True <EOL> def is_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> self . instances [ "<STR_LIT>" ] . oldgroup = self . instances [ "<STR_LIT>" ] . group <EOL> if super ( AccountForm , self ) . is_valid ( mandatory_only = True ) : <EOL> self . account = self . forms [ <NUM_LIT:0> ] [ "<STR_LIT>" ] . save ( ) <EOL> return super ( AccountForm , self ) . is_valid ( optional_only = True ) <EOL> return False <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> events . raiseEvent ( <EOL> "<STR_LIT>" , self . instances [ "<STR_LIT>" ] , self . account <EOL> ) <EOL> self . forms [ <NUM_LIT:1> ] [ "<STR_LIT>" ] . save ( self . user , self . account ) <EOL> if len ( self . forms ) <= <NUM_LIT:2> : <EOL> return <EOL> for f in self . forms [ <NUM_LIT:2> : ] : <EOL> f [ "<STR_LIT>" ] . save ( ) <EOL> def done ( self ) : <EOL> return render_to_json_response ( _ ( "<STR_LIT>" ) ) <EOL> class AccountWizard ( WizardForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request ) : <EOL> super ( AccountWizard , self ) . __init__ ( request ) <EOL> self . add_step ( <EOL> WizardStep ( <EOL> "<STR_LIT>" , AccountFormGeneral , _ ( "<STR_LIT>" ) , <EOL> new_args = [ request . user ] <EOL> ) <EOL> ) <EOL> self . add_step ( <EOL> WizardStep ( <EOL> "<STR_LIT>" , AccountFormMail , _ ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> ) <EOL> def extra_context ( self , context ) : <EOL> context . update ( { <EOL> '<STR_LIT:title>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT:action>' : reverse ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> def done ( self ) : <EOL> from modoboa . lib . web_utils import render_to_json_response <EOL> account = self . first_step . form . save ( ) <EOL> account . post_create ( self . request . user ) <EOL> mailform = self . steps [ <NUM_LIT:1> ] . form <EOL> mailform . save ( self . request . user , account ) <EOL> return render_to_json_response ( _ ( "<STR_LIT>" ) ) </s>
