Example #1
0
    def get(self):
        if self.many:
            if self.full:
                if self.ip:
                    res = NetworkIPSerializer(self.net, self.ip,
                                              many=True).data
                else:
                    res = []
            else:
                res = list(self.ip.values_list('ip', flat=True))
        else:
            res = NetworkIPSerializer(self.net, self.ip).data

        return SuccessTaskResponse(self.request, res, dc_bound=False)
Example #2
0
    def post(self):
        dd = {}

        if 'note' in self.data:
            dd['note'] = note = self.data['note']
        else:
            note = ''

        if 'usage' in self.data:
            dd['usage'] = usage = self.data['usage']
        else:
            usage = IPAddress.VM

        if self.many:
            ips = self.ips

            if not ips:
                raise InvalidInput('Invalid ips')

            if self.ip.exists():  # SELECT count(*) from IPAddress
                raise ObjectAlreadyExists(model=IPAddress)

            msg = LOG_IPS_CREATE
            data = [{'ip': ip, 'usage': usage, 'note': note} for ip in ips]
            dd['ips'] = '%s-%s' % (ips[0], ips[-1])

        else:
            msg = LOG_IP_CREATE
            data = dd
            data['ip'] = self.ip.ip

        ser = NetworkIPSerializer(self.net, data=data, many=self.many)

        if not ser.is_valid():
            return FailureTaskResponse(self.request,
                                       ser.errors,
                                       dc_bound=False)

        if self.many:
            IPAddress.objects.bulk_create(ser.object)  # INSERT into IPAddress
        else:
            ser.object.save()  # INSERT into IPAddress

        return SuccessTaskResponse(self.request,
                                   ser.data,
                                   status=HTTP_201_CREATED,
                                   obj=self.net,
                                   msg=msg,
                                   detail_dict=dd,
                                   dc_bound=False)
Example #3
0
    def put(self):
        data = {'ip': self.ip.ip}

        if 'note' in self.data:
            data['note'] = self.data['note']
        if 'usage' in self.data:
            data['usage'] = self.data['usage']

        ser = NetworkIPSerializer(self.net, self.ip, data=data, partial=True)

        if not ser.is_valid():
            return FailureTaskResponse(self.request,
                                       ser.errors,
                                       dc_bound=False)

        ser.object.save()

        return SuccessTaskResponse(self.request,
                                   ser.data,
                                   obj=self.net,
                                   msg=LOG_IP_UPDATE,
                                   dc_bound=False,
                                   detail_dict=data)