def domainedit(request, domain_name): ''' This method edits an existing domain of the system. it fills the form with the domain details and then awaits posted data from the user, validates said data, and if and error occurs uses the responder to display them to the user. A succesfull post will update the domain using the viewdb function and redirect to avoid post problems ''' # Create model instance model = AdminModel() # Set the mode mode ="edit" # Retrieve the requested user. request_domain = model.getdomain( domain_name) # If there was no user in the db with this name or # the function has thrown an error, we have been # given bogus input data if (request_domain and not request_domain.domainname) or (not request_domain and model.error): # Raise user has not be found raise Http404 # Check we have posted data if request.method =="POST": # copy posted data new_data = request.POST.copy() # remove whitespaces removewhitespace(new_data) # bind form instance form = DomainEdit(request_domain.domainname, kargs=new_data) # Ensure for is valid if form.is_valid(): # Clean the data cleaned_data = form.cleaned_data # create function parameter list params = {'mode': 2, # edit 'domainname': request_domain.domainname, 'newdomainname': cleaned_data['domain']} # call managedomain in edit mode domainedited = model.managedomain(params) # Ensure no errors have occured if not domainedited and model.error: # if we have errors display them errors = {'form':form, 'new_data':new_data, 'requestdomain':request_domain, 'mode':mode, 'dberror':model.error} # Use the responder return _usermanageresponder(request, adict=errors) # Redirect to avoid post problems return HttpResponseRedirect (urlrel + 'admin/domain/manage/') # If not valid call responder with error else: errors = {'form':form, 'new_data':new_data, 'mode':mode, 'requestdomain':request_domain} return _domainmanageresponder(request, adict=errors) return _domainmanageresponder(request, adict={'requestdomain':request_domain, 'mode':mode})
def useradd(request): ''' This method adds a user into the system. It awaits posted data from the user, validates said data, and if and error occurs uses the responder to display them to the user. A succesfull post will create the user using the viewdb function and redirect to avoid post problems ''' # Create model instance model = AdminModel() # Set the mode mode ="add" # Check we have posted data if request.method == "POST": # copy posted data new_data = request.POST.copy() # Strip strings of leading/preceding whitespace removewhitespace(new_data) # bind form instance form = UserAdd(new_data) # Ensure for is valid if form.is_valid(): # Standardized data cleaned_data = form.cleaned_data # Create the parameters params = {'mode': 1, 'userid': cleaned_data['userid'], 'password': cleaned_data['password'], 'home': mailstore_home % cleaned_data['userid'], 'domain': cleaned_data['domain'], 'uid': user_uid, 'gid': user_gid, 'status': 1, 'typer': cleaned_data['typer'].capitalize(), 'role': cleaned_data['role'], 'fullname': cleaned_data['fullname'], 'notes': cleaned_data['notes'] } # add the user useradded = model.manageuser(params) # Ensure there was no error if not useradded or model.error: # If there was an error, display it to the user errors = {'form':form, 'new_data':new_data, 'mode':mode, 'dberror':model.error} # Use the responder to show. return _usermanageresponder(request, adict=errors) # If not valid call responder with error else: # This gets rendered if the form entries were incorrect errors = {'form':form, 'new_data':new_data, 'mode':mode} return _usermanageresponder(request, adict=errors) return HttpResponseRedirect (urlrel + 'admin/user/manage/')
def domainadd(request): ''' This method adds a domain into the system. It awaits posted data from the user, validates said data, and if and error occurs uses the responder to display them to the user. A succesfull post will create the domain using the viewdb function and redirect to avoid post problems ''' # Create model instance model = AdminModel() # Set the mode mode ="add" # Check we have posted data if request.method =="POST": # copy posted data new_data = request.POST.copy() # Remove white space removewhitespace(new_data) # bind form instance form = DomainAdd(new_data) # Ensure for is valid if form.is_valid(): # Get standardized data cleaned_data = form.cleaned_data # Set up for add mode. params = {'mode': 1, 'domainname': cleaned_data['domain'], 'newdomainname': None, } # Add domain domainadded = model.managedomain(params) # Make sure no errors occured. if not domainadded and model.error: errors = {'form':form, 'new_data':new_data, 'mode':mode, 'dberror':model.error } return _domainmanageresponder(request, adict=errors) # If not valid call responder with error else: errors = {'form':form,'new_data':new_data, 'mode':mode} return _domainmanageresponder(request, adict=errors) return HttpResponseRedirect (urlrel + 'admin/domain/manage/')
def groupadd(request): ''' This method adds a group into the system. It awaits posted data from the user, validates said data, and if and error occurs uses the responder to display them to the user. A succesfull post will create the group using the viewdb function and redirect to avoid post problems ''' # Create model instance model = AdminModel() # Set the mode mode ="add" # Check we have posted data if request.method =="POST": # copy posted data new_data = request.POST.copy() # The selected users users = [str(user) for user in request.POST.getlist('users')] # Update the post dict new_data.update({'users':users}) # remove white spaces removewhitespace(new_data) # bind form instance form = GroupAdd(kargs=new_data) # Ensure for is valid if form.is_valid(): # get standardized data cleaned_data = form.cleaned_data # Set up for add mode params = {'mode': 1, 'alias': cleaned_data['alias'], 'domain': cleaned_data['domain'], 'new_alias': None, 'notes': cleaned_data['notes'] } # Add alias aliasadded = model.managealias(params) # Ensure not errors have occured if not aliasadded and model.error: # Create a new template context errors = {'form':form, 'new_data':new_data, 'mode':mode, 'dberror':model.error } # User responder to show errors return _groupmanageresponder(request, adict=errors) # Once the alias has been created, we add the selected users for user in users: # Split the string of usernames These need to be usernames as # that is the only unique constraint in the db userid, domain = user.split('@') # set up for add alias user params = {'mode':1, # add alias users 'alias':cleaned_data['alias'], 'userid':userid, 'domain':domain } # Add alias user aliasuseradded = model.managealiasuser(params) # Ensure no errors have occured if not aliasuseradded and model.error: # Make a new template context errors = {'form':form, 'new_data':new_data, 'mode':mode, 'dberror':model.error } # User group responder to show errors return _groupmanageresponder(request, adict=errors) # If not valid form call responder with error else: errors = {'form':form,'new_data':new_data, 'mode':mode} return _groupmanageresponder(request, adict=errors) return HttpResponseRedirect (urlrel + 'admin/group/manage/')
def useredit(request, userid, domain): ''' This method edits an existing user of the system. it fills the form with the users details and then awaits posted data from the user, validates said data, and if and error occurs uses the responder to display them to the user. A succesfull post will update the user using the viewdb function and redirect to avoid post problems ''' # Create model instance model = AdminModel() # Set the mode mode ="edit" # Retrieve the requested user. request_user = model.getuser(userid, domain) # If there was no user in the db with this name or the function has thrown # an error, we have been given bogus input data if (request_user and not request_user.userid) or (not request_user and model.error): # Raise user has not be found raise Http404 # Check we have posted data if request.method =="POST": # copy posted data new_data = request.POST.copy() # Remove white space. removewhitespace(new_data) # bind form instance form = UserEdit(new_data) # Ensure for is valid if form.is_valid(): # Clean the data cleaned_data = form.cleaned_data # create function parameter list params = {'mode': 2, # edit 'userid': cleaned_data['userid'], 'password': cleaned_data['password'], 'home': mailstore_home % cleaned_data['userid'], 'domain': cleaned_data['domain'], 'uid': user_uid, 'gid': user_gid, 'status': 1, 'typer': cleaned_data['typer'].capitalize(), 'role': cleaned_data['role'], 'fullname': cleaned_data['fullname'], 'notes': cleaned_data['notes']} # call manageuser in edit mode useredited = model.manageuser(params) # Ensure no errors have occured if not useredited and model.error: # if we have errors display them errors = {'form':form, 'new_data':new_data, 'requestuser':request_user, 'mode':mode, 'dberror':model.error} # Use the responder return _usermanageresponder(request, adict=errors) # Redirect to avoid post problems return HttpResponseRedirect (urlrel + 'admin/user/manage/') # If not valid call responder with error else: errors = {'form':form, 'new_data':new_data, 'mode':mode, 'requestuser':request_user} return _usermanageresponder(request, adict=errors) return _usermanageresponder(request, adict={'requestuser':request_user, 'mode':mode})