<s> from django . contrib . auth import get_user_model <EOL> from core . modal import BaseModal , ModalPermissionException <EOL> from account . models import DepartmentGroup <EOL> from core . models import Department <EOL> from . forms import account_create_form , UserForm , DepartmentGroupForm , UserSystemForm <EOL> _user = get_user_model ( ) <EOL> class BaseAccountModal ( BaseModal ) : <EOL> def get_form_creator ( self ) : <EOL> return account_create_form <EOL> def _on_form_create_user ( self ) : <EOL> if not self . form . instance . id : <EOL> self . form . fields [ '<STR_LIT:password>' ] . required = True <EOL> if self . request . user . is_superuser : <EOL> self . form . fields [ '<STR_LIT>' ] . department_prefix = True <EOL> groups = DepartmentGroup . objects . all ( ) <EOL> else : <EOL> groups = DepartmentGroup . objects . filter ( department_id = self . request . current_department_id ) <EOL> self . form . set_groups ( groups ) <EOL> class UserModal ( BaseAccountModal ) : <EOL> form = UserForm <EOL> form_name = '<STR_LIT:user>' <EOL> def permission_check ( self , action ) : <EOL> if not self . request . user . has_perm ( '<STR_LIT>' , Department ( id = self . request . current_department_id ) ) : <EOL> raise ModalPermissionException <EOL> def on_form_create ( self ) : <EOL> self . _on_form_create_user ( ) <EOL> def on_view ( self ) : <EOL> self . data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def on_before_save ( self ) : <EOL> instance = self . form . instance <EOL> instance . username = instance . email <EOL> if len ( instance . password ) : <EOL> instance . set_password ( instance . password ) <EOL> else : <EOL> instance . password = _user . objects . get ( pk = instance . id ) . password <EOL> class UsersystemModal ( BaseAccountModal ) : <EOL> form = UserSystemForm <EOL> form_name = '<STR_LIT>' <EOL> def permission_check ( self , action ) : <EOL> if not self . request . user . is_superuser : <EOL> raise ModalPermissionException <EOL> def on_form_create ( self ) : <EOL> self . _on_form_create_user ( ) <EOL> class GroupModal ( BaseAccountModal ) : <EOL> form = DepartmentGroupForm <EOL> form_name = '<STR_LIT>' <EOL> def permission_check ( self , action ) : <EOL> if not self . request . user . has_perm ( '<STR_LIT>' , Department ( id = self . request . current_department_id ) ) : <EOL> raise ModalPermissionException <EOL> def on_before_save ( self ) : <EOL> self . form . instance . department_id = self . request . current_department_id <EOL> def on_create ( self ) : <EOL> DepartmentGroup . assign_department_perms ( self . instance , Department ( id = self . request . current_department_id ) ) </s>
