示例#1
0
文件: views.py 项目: rorycl/clwmail
def getaliasusers(request, domain_name):
	''' 
		This method gets all the users associated to particular domain. It is
		used as an ajax call on the group management page. There reason for
		this is that only users of the domain associated to the alias can be
		selected. This is is not known ahead of time and is hence done
		dynamically.
	'''

	# Make model instance
	model = AdminModel()
	# Get the domain to see if it is valid
	domain = model.getdomain(domain_name)
	# Is it a valid domain
	is_domain = False
	# Check valid domain
	if domain and domain.domainname and not model.error:
		is_domain = True

	domain_users = DomainUsers(domain_name, model.getnondisabledusers(domain_name))
	# Default new_data values
	new_data = None

	if request.method == "POST":
		new_data = request.POST.copy()
		try:
			users_string = new_data['users']
		except KeyError:
			new_data = None	
		else:
			new_data.update({'users':users_string.split(',')})

	return render_to_response('admin/aliasusers.html',
							  {'domain_users':domain_users,
							   'new_data':new_data,
							   'is_domain':is_domain},
							  context_instance=RequestContext(request))
示例#2
0
文件: views.py 项目: rorycl/clwmail
def groupedit(request, alias, domain):
	''' This method edits an existing alias of the system. it fills
		the form with the alias 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 alias using
		the viewdb function and redirect to avoid post problems '''

	# Create model instance
	model = AdminModel()
	# Set the mode
	mode ="edit"
	# Retrieve the requested alias.
 	request_alias =  model.getalias(alias, 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_alias and not request_alias.aliasname) or
	   (not request_alias and model.error)):
		# Raise alias has not be found
		raise Http404

	else:
		# Retrieve current alias users
		aliasusers = model.getaliasusers(alias, domain)
		# a list of users
		# associated to the domain of the alias 
		domain_users = DomainUsers(domain, 
								   model.getnondisabledusers(domain))
		request_users =[] 
		if aliasusers:
			request_users = [user.username for user in model.getaliasusers(alias, domain)]
		# Check we have posted data
		if request.method =="POST":
			# copy posted data
			new_data = request.POST.copy()
			# The selected users
			users = request.POST.getlist('users')
			# Update post dict
			new_data.update({'users':users})
			#  bind form instance
			form = GroupEdit(groupname=request_alias.aliasname, kargs=new_data)
			# Ensure for is valid
			if form.is_valid():
				# Clean the data
				cleaned_data = form.clean()
				# create function parameter list
				params = {'mode': 2,
						  'alias': request_alias.aliasname,
						  'domain': cleaned_data['domain'],
						  'new_alias': cleaned_data['alias'],
						  'notes': cleaned_data['notes']
						  }
				# call managealias in edit mode
				groupedited = model.managealias(params)
				# Ensure no errors have occured
				if not groupedited and model.error:
					errors = {'form':form, 'new_data':new_data,
							  'requestalias':request_alias, 'mode':mode,
							  'requestusers':request_users,
							  'domain_users':domain_users,
							  'dberror':model.error }
					return _groupmanageresponder(request, adict=errors)
				
				# Once the alias has been edit,
				# we update the selected users
				# Start by deleteing the previous alias users		
				if request_users:
					for user in request_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('@')
						# All previously selected users
						params = {'mode':3, # add alias users
								  'alias':cleaned_data['alias'],
								  'userid':userid,
								  'domain':domain
								 }
						aliasuserdeleted = model.managealiasuser(params)
						if not aliasuserdeleted and model.error:
							errors = {'form':form, 'new_data':new_data, 
									  'mode':mode, 'dberror':model.error,
									  'domain_users':domain_users,
									  'requestusers':request_users}
							return _groupmanageresponder(request, adict=errors)

				# Add new users
				if 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('@')
						# All previously selected users
						params = {'mode':1, # add alias users
								  'alias':cleaned_data['alias'],
								  'userid':userid,
								  'domain':domain
								 }
						aliasuseradded = model.managealiasuser(params)
						if not aliasuseradded and model.error:
							errors = {'form':form, 'new_data':new_data, 
									  'mode':mode, 'dberror':model.error,
									  'domain_users':domain_users,
									  'requestusers':request_users}
							return _groupmanageresponder(request, adict=errors)

				# Redirect to avoid post problems
				return HttpResponseRedirect (urlrel + 'admin/group/manage/')

			# If not valid call responder with error
			else:
				errors = {'form':form,'new_data':new_data, 
						  'mode':mode, 'requestalias':request_alias,
						  'domain_users':domain_users,
						  'requestusers':request_users}
				return _groupmanageresponder(request, adict=errors)
			
		return _groupmanageresponder(request, adict= {'requestalias':request_alias,
													  'requestusers':request_users,
													  'domain_users':domain_users,
													  'mode': mode})

	return HttpResponseRedirect(urlrel + 'admin/group/manage/')