def zone(request, zone_id=False): if zone_id: if 'delete_range' in request.POST: pr = IPRange.objects.get(id=request.POST.get('delete_range')) pr.delete() if 'ip' in request.POST: z = Zone.objects.get(id=zone_id) new_range = IPRange( ip_address=request.POST.get('ip'), mask=request.POST.get('mask'), description=request.POST.get('description'), zone=z) new_range.save() return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: z = Zone.objects.get(id=zone_id) return render(request, 'zone.html', {'zone': z}) else: if 'delete_zone' in request.POST: dz = Zone.objects.get(id=request.POST.get('delete_zone')) dz.delete() if 'zone_name' in request.POST: new_zone = Zone( name=request.POST.get('zone_name'), description=request.POST.get('description'), criticality=request.POST.get('criticality')) new_zone.save() return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: zones = Zone.objects.all() return render(request, 'all_zones.html', {'zones': zones})
def SaveZone(lst): try: logok = '' logero = '' first_sheet = lst.sheet_by_index(0) for r in range(1, first_sheet.nrows): m = first_sheet.cell(r, 0).value m2 = first_sheet.cell(r, 2).value chk = Zone.objects.filter(ZoneName=m, Network=m2).exists() if chk == False: sv = Zone( ZoneName=m, Description=first_sheet.cell(r, 1).value, Network=m2, ) sv.save() logok += m.encode("utf-8") + '/' elif chk == True: logero += m.encode("utf-8") + '/' return {'y0': logok, 'y1': logero} except: return False
def save(self, *args, **kwargs): if self.id is None: zone_1 = Zone() zone_1.save() self.grid_zone_1 = zone_1 zone_2 = Zone() zone_2.save() self.grid_zone_2 = zone_2 zone_3 = Zone() zone_3.save() self.grid_zone_3 = zone_3 zone_4 = Zone() zone_4.save() self.grid_zone_4 = zone_4 self.type = 'Grid_' + '_'.join(self.GRID_CHOICES[self.grid_type-1][1].split('/')) super(GridWidget, self).save(*args, **kwargs)
def addnew(request): ## The add zone function can be used in two ways. ## one way, is to click add new zone from the zone list page..... ## which will display all the regions and create a zone by selecting a region from the list ## second way, is to click add new zone from the region detailed page... ## which means you already selected the region and adding a zone in that region ## that's why the following variable says whether you already selected region or not selRegionName = request.REQUEST.get("regionname", "") ## Check if there are any regions defined. If not, then zones cannot be added regionsCount = getRegionCount() if (regionsCount == 0): message = "No Regions Defined. First create Region and then try to add Zone" html = "<html><body> %s.</body></html>" % message return HttpResponse(html) ## Check if there are any resource types defined. If not, then zones cannot be added resourceTypesCount = getResourceTypesCount() if (resourceTypesCount == 0): message = "No Resource Types Defined. First create Resource Types and then try to add Zone" html = "<html><body> %s. </body></html>" % message return HttpResponse(html) groups = request.META.get('ADFS_GROUP','') groupsList = groups.split(';') userIsSuperUser = isSuperUser(groupsList) ## If the request is through form submission, then try to add the region or else display the add form if request.method == 'POST': redirectURL = '/cloudman/message/?msg=' regionName = request.POST['region'] zoneName = request.POST['name'] description = request.POST['description'] hepSpecs = request.POST['hepspecs'] memory = request.POST['memory'] storage = request.POST['storage'] bandwidth = request.POST['bandwidth'] hepSpec_overcommit = request.POST['hepspec_overcommit'] memory_overcommit = request.POST['memory_overcommit'] comment = request.POST['comment'] try: validate_name(regionName) validate_name(zoneName) validate_descr(description) validate_float(hepSpecs) validate_int(memory) validate_int(storage) validate_float(bandwidth) validate_comment(comment) validate_float(hepSpec_overcommit) validate_float(memory_overcommit) except ValidationError as e: message ='Add Zone Form '+', '.join(e.messages) html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/zone/list/\"></HEAD><body> %s.</body></html>" % message return HttpResponse(html) ## check for uniqueness of this zone name in the region nameExists = checkNameIgnoreCase(regionName, zoneName) if nameExists: msgAlreadyExists = 'Zone Name ' + zoneName + ' in Region ' + regionName + ' already exists. Hence Add New Zone Operation Stopped' transaction.rollback() return HttpResponseRedirect(redirectURL + msgAlreadyExists) ## check whether user has any of the following rights ## cloudman resource manager privileges ## If not, then has membership of region admin_group if not userIsSuperUser: userIsAdminOfRegion = isAdminForRegion(regionName, groupsList) if not userIsAdminOfRegion: message = "You neither have membership of administrative group of region " + regionName + " nor possess Cloudman Resource Manager Privileges. Hence you are not authorized to add new Zone"; return HttpResponseRedirect(redirectURL + message) ## Get the Region Object try: region = Region.objects.get(name=regionName) except Region.DoesNotExist: errorMessage = 'No Record Found for Region ' + regionName + '. Hence Add New Zone Operation Stopped' return HttpResponseRedirect(redirectURL + errorMessage) ## validate hepspec, memory, storage and bandwidth values errorMsg = checkAttributeValues(hepSpecs, memory, storage, bandwidth) if (errorMsg != ''): return HttpResponseRedirect(redirectURL + errorMsg) ## validate hepspec over commit and memory over commit values if hepSpec_overcommit < 1: msgFailure = "Hepspec Over Commit value should be greater than or equal to 1. Hence Add Zone Operation Stopped" return HttpResponseRedirect(redirectURL + msgFailure) if memory_overcommit < 1: msgFailure = "Memory Over Commit value should be greater than or equal to 1. Hence Add Zone Operation Stopped" return HttpResponseRedirect(redirectURL + msgFailure) ## get all the resource types check boxes values (resource type name), ## the allowed resource types for this zone will be the ones whose checkbox is selected totalResourceTypes = request.POST['totalresourcetypes'] index = 1 atleastOneRTsel = False resourceTypesList = [] while index <= int(totalResourceTypes): if ('resourcetype'+str(index)) in request.POST.keys() : ## if checkbox selected resourceTypesList.append(request.POST['resourcetype'+str(index)]) atleastOneRTsel = True index = index + 1 if not atleastOneRTsel: message = "No Resource Types selected that are allowed for this Zone. Hence Zone Add Operation Stopped" return HttpResponseRedirect(redirectURL + message) ## get the resource type objects for all those that are selected resourceTypesFullInfoList = [] msgSuccess = '' for oneRT in resourceTypesList: try: resourceType = ResourceType.objects.get(name=oneRT) resourceTypesFullInfoList.append(resourceType) msgSuccess = msgSuccess + ' ' + oneRT; except ResourceType.DoesNotExist: errorMessage = 'No Record Found for Resource Type ' + oneRT + '. Hence Add New Zone Operation Stopped' return HttpResponseRedirect(redirectURL + errorMessage) msgSuccess = 'New Zone ' + zoneName + ' added successfully ' + ' to Region ' + regionName + '. The allowed Resource Types are ' + msgSuccess if hepSpecs == '': hepSpecs = None else: hepSpecs = round((float(hepSpecs)), 3) if memory == '': memory = None else: memory = round((float(memory)), 3) if storage == '': storage = None else: storage = round((float(storage)), 3) if bandwidth == '': bandwidth = None else: bandwidth = round((float(bandwidth)), 3) ## create the zone object and save it newzone = Zone(name=zoneName, description=description, region=region, hepspecs=hepSpecs, memory=memory, storage=storage, bandwidth=bandwidth, hepspec_overcommit=hepSpec_overcommit, memory_overcommit=memory_overcommit) newzone.save() ## get the newly added zone object ## add the allowed resource types for this zone addedZone = Zone.objects.get(name=zoneName, region=region) for oneRT in resourceTypesFullInfoList: zrt = ZoneAllowedResourceType(zone=addedZone, resource_type=oneRT) zrt.save() ##Add the Log for Zone if addLog(request,zoneName,comment,addedZone,None,'zone','add',True): transaction.commit() else: transaction.rollback() msgSuccess = 'Error in creating new zone' + zoneName ## return the success message to the user html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/zone/list/\"></HEAD><body> %s.</body></html>" % msgSuccess return HttpResponse(html) else: ## Zone can be added provided you have any of the following privileges ## cloudman resource manager privileges ## only to regions for which user has membership of its admin_group if not userIsSuperUser: userIsAdmin = True ## if region is not already selected, then check user has membership of atleast one region admin_group if selRegionName == '': userIsAdmin = isAdminOfAnyRegion(groupsList) if not userIsAdmin: message = "You neither have membership of administrative groups of any region nor possess Cloudman Resource Manager Privileges. Hence you are not authorized to add new Zone"; html = "<html><body> %s.</body></html>" % message return HttpResponse(html) else: userIsAdmin = isAdminForRegion(selRegionName, groupsList) if not userIsAdmin: message = "You neither have membership of administrative group of region " + selRegionName + " nor possess Cloudman Resource Manager Privileges. Hence you are not authorized to add new Zone"; html = "<html><body> %s.</body></html>" % message return HttpResponse(html) form = ZoneForm(userGroups=groupsList, superUserRights=userIsSuperUser) resourceForm=ResourceForm resourceType = ResourceType.objects.all() return render_to_response('zone/addnew.html',locals(),context_instance=RequestContext(request))