Ejemplo n.º 1
0
    def put(self,request,id,format=None):
        col_object = self.get_object(id)
        serializer = CollectionGetSerializer(col_object)
        collect_details = serializer.data
        collect_details['range'] = json.loads(collect_details['range'])
        serializer = CollectionPutSerializer(data=request.data)
        
        if serializer.is_valid():
            type = serializer.data['type']
            
            if type == 'AutoGenerate':
                return Response(status=status.HTTP_400_BAD_REQUEST)
            
            if type == 'Integer' or type =='Vlan':
                for i in range(0,len(serializer.data['range'])):
                    start = int(serializer.data['range'][i]['start'])
                    end = int(serializer.data['range'][i]['end'])
                    diff = end - start
                
                    for flag in range(start,end + 1):
                        coll = CollectionDetail()
                        coll.index = col_object
                        coll.value = flag
                        coll.save()
                    
                    collect_details['range'].append(serializer.data['range'][i])
                    col_object.available = int(col_object.available) + diff + 1
                    
                col_object.range = json.dumps(collect_details['range'])
                col_object.save()
                
                serializer = CollectionGetSerializer(col_object)
                collect_details = serializer.data
                collect_details['range'] = json.loads(collect_details['range'])
                return Response(collect_details, status=status.HTTP_206_PARTIAL_CONTENT)
            
            if type =='IP' or type =='MgmtIP' or type =='IPv6':
                for i in range(0,len(serializer.data['range'])):
                    start_ip = IPNetwork(str(serializer.data['range'][0]['start']))
                    end_ip = IPNetwork(str(serializer.data['range'][0]['end']))

                    for ip_val in range (start_ip.ip,end_ip.ip+1):
                        coll = CollectionDetail()
                        coll.index = col_object
                        coll.value = IPAddress(ip_val).__str__()+"/"+start_ip.prefixlen.__str__()
                        coll.save()
                        
                    diff = int(end_ip.ip-start_ip.ip) + 1
                    collect_details['range'].append(serializer.data['range'][i])
                    col_object.available = int(col_object.available) + diff
                    
                col_object.range = json.dumps(collect_details['range'])
                col_object.save()
                serializer = CollectionGetSerializer(col_object)
                collect_details = serializer.data
                collect_details['range'] = json.loads(collect_details['range'])
                return Response(collect_details, status=status.HTTP_206_PARTIAL_CONTENT)
               
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
    def post(self, request, format=None):
        serializer = CollectionSerializer(data=request.data)
        if serializer.is_valid():

            col_object = Collection()
            col_object.type = serializer.data['type']
            col_object.name = serializer.data['name']
            col_object.scope = serializer.data['scope']
            col_object.save()
            
            type = serializer.data['type']
            coll_range=[{'start':0,'end':1}]
            if type == 'AutoGenerate':
                start = 10
                end = 20
                for flag in range(start,end + 1):
                    coll = CollectionDetail()
                    coll.index = col_object
                    coll.value = flag
                    coll.save()
                coll_range[0]['start'] = start
                coll_range[0]['end'] = end
                col_object.range = json.dumps(coll_range)
            else:
                col_object.range = json.dumps(serializer.data['range'])
            col_object.save()

            
            if type == 'Integer' or type =='Vlan':
                for i in range(0,len(serializer.data['range'])):
                    start = int(serializer.data['range'][i]['start'])
                    end = int(serializer.data['range'][i]['end'])
                    for flag in range(start,end + 1):
                        coll = CollectionDetail()
                        coll.index = col_object
                        coll.value = flag
                        coll.save()
                    
            if type == 'IP' or type == 'MgmtIP' or type == 'IPv6':
                for i in range(0,len(serializer.data['range'])):
                    start_ip = IPNetwork(str(serializer.data['range'][i]['start']))
                    end_ip = IPNetwork(str(serializer.data['range'][i]['end']))
                    for ip_val in range (start_ip.ip,end_ip.ip+1):
                        coll = CollectionDetail()
                        coll.index = col_object
                        coll.value = IPAddress(ip_val).__str__()+"/"+start_ip.prefixlen.__str__()
                        coll.save()
                    
        
        #    if type == 'IPv6':
        #        start_ip = IPNetwork(str(serializer.data['range'][0]['start']))
        #        end_ip = IPNetwork(str(serializer.data['range'][0]['end']))
        #        for ip_val in range (start_ip.ip,end_ip.ip+1):
        #            coll = CollectionDetail()
        #            coll.index = col_object
        #            coll.value = IPAddress(ip_val).__str__()+"/"+start_ip.prefixlen.__str__()
        #            coll.save()
                    
            get_coll_detail = CollectionDetail.objects.filter(index = col_object.id,assigned ='')
            available = len(get_coll_detail)
            col_object.available = available
            col_object.used = len(CollectionDetail.objects.filter(index = col_object.id)) - available
            col_object.save()
            serializer = CollectionGetSerializer(col_object)
            collect_details = serializer.data
            collect_details['range'] = json.loads(collect_details['range'])
            return Response(collect_details, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)