def update(request): rtName = request.REQUEST.get("name", "") redirectURL = '/cloudman/message/?msg=' groups = request.META.get('ADFS_GROUP','') groupsList = groups.split(';') ; userIsSuperUser = isSuperUser(groupsList) ## Check - Logged in user has administrative privileges if not userIsSuperUser: message = "You don't have cloudman resource manager privileges. Hence you are not authorized to update Resource Type " + rtName; html = "<html><body> %s.</body></html>" % message return HttpResponse(html) ## Get the Resource Type Object resourceTypeObject = None try: resourceTypeObject = ResourceType.objects.get(name=rtName) except ResourceType.DoesNotExist: failureMessage = "Resource Type with Name " + rtName + " could not be found" return HttpResponseRedirect(redirectURL+failureMessage) oldRTInfo = getResourceTypeInfo(resourceTypeObject) ## if this is a form submission, then do the update or else display the form for update if request.method == 'POST': ## Existing values currName = resourceTypeObject.name currResourceClass = resourceTypeObject.resource_class currHepSpecs = resourceTypeObject.hepspecs currMemory = resourceTypeObject.memory currStorage = resourceTypeObject.storage currBandwidth = resourceTypeObject.bandwidth ## Get the new values newName = request.POST['name'] newResourceClass = request.POST['resource_class'] newHepSpecs = request.POST['hepspecs'] newMemory = request.POST['memory'] newStorage = request.POST['storage'] newBandwidth = request.POST['bandwidth'] comment = request.POST['comment'] try: validate_name(newName) validate_name(newResourceClass) validate_float(newHepSpecs) validate_float(newMemory) validate_float(newStorage) validate_float(newBandwidth) validate_comment(comment) except ValidationError as e: message ='Edit Resource Type Form '+', '.join(e.messages) html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/resourcetype/list/\"></HEAD><body> %s.</body></html>" % message return HttpResponse(html) ## If resource parameters are changed, then validate them errorMsg = checkAttributeValues(newHepSpecs, newMemory, newStorage, newBandwidth) if (errorMsg != ''): return HttpResponseRedirect(redirectURL + errorMsg) ## convert empty values for the following fields into None i.e NULL and if not empty, then round off to 3 decimals if (newHepSpecs == ''): newHepSpecs = None else: newHepSpecs = round((float(newHepSpecs)), 3) if (newMemory == ''): newMemory = None else: newMemory = round((float(newMemory)), 3) if (newStorage == ''): newStorage = None else: newStorage = round((float(newStorage)), 3) if (newBandwidth == ''): newBandwidth = None else: newBandwidth = round((float(newBandwidth)), 3) ## Check atleast one parameter is changed from its existing value if ( (currName == newName) and (currResourceClass == newResourceClass) and (currHepSpecs== newHepSpecs) and (currMemory == newMemory) and (currStorage == newStorage) and (currBandwidth == newBandwidth) ): message = 'No New Value provided for any field to perform Edit Operation. Hence Edit Resource Type ' + rtName + ' aborted' return HttpResponseRedirect(redirectURL + message) ## If name is changed, then validate it and if success, then assign the new name to the object if (currName != newName): if (newName == ''): errorMsg = 'Name field cannot be left blank. So Edit Resource Type operation stopped' return HttpResponseRedirect(redirectURL + errorMsg) nameExists = checkNameIgnoreCase(newName) if nameExists: msgAlreadyExists = 'Resource Type ' + newName + ' already exists. Hence Edit Resource Type Operation Stopped' return HttpResponseRedirect(redirectURL + msgAlreadyExists); resourceTypeObject.name = newName ## check the remaining parameters and if changed, then assign to the object if (currResourceClass != newResourceClass): resourceTypeObject.resource_class = newResourceClass if (currHepSpecs != newHepSpecs): resourceTypeObject.hepspecs = newHepSpecs if (currMemory != newMemory): resourceTypeObject.memory = newMemory if (currStorage != newStorage): resourceTypeObject.storage = newStorage if (currBandwidth != newBandwidth): resourceTypeObject.bandwidth = newBandwidth ## update the object and return the success message to the user resourceTypeObject.save() newRTInfo = getResourceTypeInfo(resourceTypeObject) objectId = resourceTypeObject.id if addUpdateLog(request,newName,objectId,comment,oldRTInfo,newRTInfo,'resourcetype',True): message = 'Resource Type ' + rtName + ' Successfully Updated' transaction.commit() else: transaction.rollback() message = 'Error in Updating Resource Type ' + rtName html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/resourcetype/list/\"></HEAD><body> %s.</body></html>" % message return HttpResponse(html) ## if it is not form submission, then call the template to display the update form else: form = ResourceTypeForm() #resourceTypeList=list(resourceTypeObject) #json_object=simplejson.dumps(resourceTypeObject) return render_to_response('resourcetype/update.html',locals(),context_instance=RequestContext(request))
def update(request): regionName = request.REQUEST.get("name", "") redirectURL = '/cloudman/message/?msg=' groups = request.META.get('ADFS_GROUP','') groupsList = groups.split(';') ; ## Update is allowed if user has either cloudman resource manager privileges or ## belongs to the egroup selected as administrative e-group for this region if not isUserAllowedToUpdateOrDeleteRegion(regionName,groupsList): message = "You neither have membership of administrative group of region " + regionName + " nor possess Cloudman Resource Manager Privileges. Hence you are not authorized to Edit Region"; html = "<html><body> %s.</body></html>" % message return HttpResponse(html) ## Get the region Object regionObject = None try: regionObject = Region.objects.get(name=regionName) except Region.DoesNotExist: failureMessage = "Region with Name " + regionName + " could not be found" return HttpResponseRedirect(redirectURL+failureMessage) oldRegionInfo = getRegionInfo(regionObject) ## If the current request is due to form submission then do update ## or else return to template to display the update form if request.method == 'POST': ## Existing values currName = regionObject.name currDescription = regionObject.description currAdmin_group = regionObject.admin_group ## New Values newName = request.POST['name'] newDescription = request.POST['description'] newAdmin_group = request.POST['admin_group'] comment = request.REQUEST.get("comment", "") try: validate_name(newName) validate_descr(newDescription) validate_name(newAdmin_group) validate_comment(comment) except ValidationError as e: message ='Edit Region Form '+', '.join(e.messages) html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/region/list/\"></HEAD><body> %s.</body></html>" % message return HttpResponse(html) ## Check for atleast one field value change if ( (currName == newName) and (currDescription == newDescription) and (currAdmin_group == newAdmin_group) ): message = 'No New Value provided for any field to perform Edit Operation. Hence Edit Region ' + regionName + ' aborted' return HttpResponseRedirect(redirectURL + message) ## Assign the new name to the region if it is changed if (currName != newName): if (newName == ''): errorMsg = 'Region name field cannot be left blank. So Edit Region operation stopped' return HttpResponseRedirect(redirectURL + errorMsg) regionExists = checkNameIgnoreCase(newName) if regionExists: msgAlreadyExists = 'Region ' + newName + ' already exists. Hence Edit Region Operation Stopped' return HttpResponseRedirect(redirectURL + msgAlreadyExists); regionObject.name = newName ## Assign the new description if it is changed if (currDescription != newDescription): regionObject.description = newDescription ## If admin egroup is changed, then first check its existence in the local egroups table ## If not present, then check its existence in the external egroup database through ldap ## If checked using external database and found, then add the egroup to the local egroups table ## If not found both local and external, then return an error to the user egroup = None if (currAdmin_group != newAdmin_group): if (newAdmin_group == ''): errorMsg = 'Admin E-Group field cannot be left blank. So Edit Region operation stopped' return HttpResponseRedirect(redirectURL + errorMsg) try: egroup = Egroups.objects.get(name=newAdmin_group) except Egroups.DoesNotExist: if not (checkEGroup(newAdmin_group)): errorMessage = 'Selected Admin E-Group ' + newAdmin_group + ' does not exists' return HttpResponseRedirect(redirectURL + errorMessage) egroup = Egroups(name=newAdmin_group) egroup.save() regionObject.admin_group = egroup ## Save the new values and return success message to the user regionObject.save() newRegionInfo = getRegionInfo(regionObject) objectId = regionObject.id if addUpdateLog(request,newName,objectId,comment,oldRegionInfo,newRegionInfo,'region',True): transaction.commit() message = 'Region ' + regionName + ' Successfully Updated' else: message = 'Error in Updating Region ' + regionName transaction.rollback() html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/region/list/\"></HEAD><body> %s.</body></html>" % message return HttpResponse(html) else: form=RegionForm(); return render_to_response('region/update.html',locals(),context_instance=RequestContext(request))