def addnew(request): groups = request.META.get('ADFS_GROUP','') groupsList = groups.split(';') ; ## Check the user has cloudman resource manager privileges userIsSuperUser = isSuperUser(groupsList) '''if not userIsSuperUser: message = "You don't have cloudman resource manager privileges. Hence you are not authorized to add new Group"; html = "<html><body> %s.</body></html>" % message return HttpResponse(html) ''' ## if the request is through form submission, then add the group or else return a new form if request.method == 'POST': ## Validate the form by checking whether all the required values are provided or not form = GroupsForm(request.POST) if form.is_valid(): redirectURL = '/cloudman/message/?msg=' name = form.cleaned_data['name'] groupExists = checkNameIgnoreCase(name) if groupExists: msgAlreadyExists = 'Group ' + name + ' already exists. Hence Add Group Operation Failed' return HttpResponseRedirect(redirectURL + msgAlreadyExists); description = form.cleaned_data['description'] admin_group = form.cleaned_data['admin_group'] comment = form.cleaned_data['comment'] ## check first whether the admin_group exists in the local egroup table ## if not, then in the remote egroup database through ldap. If exists here, then add to the local table egroup = None try: egroup = Egroups.objects.get(name=admin_group) except Egroups.DoesNotExist: if not (checkEGroup(admin_group)): errorMessage = 'Admin E-Group Entered ' + admin_group + ' does not exists' return HttpResponseRedirect(redirectURL + errorMessage) else: egroup = Egroups(name=admin_group) egroup.save() ## Create the group and return a success message to the user groupObj = Groups(name=name, description=description, admin_group=egroup) groupObj.save() groupObj = Groups.objects.get(name=name) if addLog(request,name,comment,groupObj,None,'group','add',True): msgSuccess = 'New group ' + name + ' added successfully' else: transaction.rollback() msgSuccess = 'Error in creating group ' + name html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/group/list/\"></HEAD><body> %s.</body></html>" % msgSuccess return HttpResponse(html) else: form = GroupsForm() return render_to_response('group/addnew.html',locals(),context_instance=RequestContext(request))
def add_new_group(request): if request.method == "POST": form = GroupsForm(request.POST, auto_id=False) response_dict = {} if request.is_ajax(): if form.is_valid(): form.save() response_dict['message'] = 'Changes have been saved' response_dict['result'] = 'success' else: response_dict['result'] = 'error' response = {} for error in form.errors: response[error] = form.errors[error][0] response_dict['response'] = response json = simplejson.dumps(response_dict, ensure_ascii=False) return HttpResponse(json, mimetype='application/json') else: if form.is_valid(): form.save() form = GroupsForm() return direct_to_template(request, "add_group.html", {'form':form})