def update(self, request, pk=None): asset = self.getObject(pk) front_ip1 = asset.ip front_ip2 = asset.other_ip serializer = AssetSerializer(asset, data=request.data, context={'request': request}) if serializer.is_valid(): serializer = self.getValidatedData(serializer) ip1 = serializer.validated_data.get('ip', '') ip2 = serializer.validated_data.get('other_ip', '') if ip1: if front_ip1 != ip1: if Asset.objects.filter(ip=ip1).exists(): raise ValidationError( {'ip': '{0} is exist.'.format(ip1)}) if ip2: if front_ip2 != ip2: if Asset.objects.filter(other_ip=ip2).exists(): raise ValidationError( {'other_ip': '{0} is exist.'.format(ip2)}) serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, id): try: asset = Asset.objects.get(id=id) serializer = AssetSerializer(asset) return self.generateGetSuccessResponse(self, serializer) except Exception as e: return JsonResponse(e, safe=False)
def post(self, request): if request.is_ajax(): assets = AssetSerializer(instance=Asset.objects.all(), many=True) return HttpResponse(json.dumps(assets.data), content_type='application/json') else: assets = Asset.objects.all() return render(request, 'assets.html', {'assets': assets})
def create(self, request): try: type = FileType.objects.get(type=request.POST.get('fileType')) data = {'fileType': type.id} serializer = AssetSerializer(data=data) if serializer.is_valid(): validatedData = self.validateFile(self, data['fileType'], request.FILES.get('file')) if validatedData is not False: return self.generateBadDataErrorResponse( self, validatedData) serializer.save() self.uploadFile(self, serializer.data['id'], request.FILES.get('file')) return self.generatePostSuccessResponse(self, serializer) return self.generateFormErrorResponse(self, serializer) except Exception as e: return self.handleException(self, e)
def create(self, request): serializer = AssetSerializer(data=request.data, context={'request': request}) if serializer.is_valid(): serializer = self.getValidatedData(serializer) ip1 = serializer.validated_data.get('ip') ip2 = serializer.validated_data.get('other_ip') if ip1: if Asset.objects.filter(ip=ip1).exists(): # pay attention to distinctions between rest_framework.exceptions.ValidationError and serializers.ValidationError raise ValidationError({'ip': '{0} is exist.'.format(ip1)}) if ip2: if Asset.objects.filter(other_ip=ip2).exists(): raise ValidationError( {'other_ip': '{0} is exist.'.format(ip2)}) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def AssetList(request): if request.method == 'GET': asset_list = models.Asset.objects.all() serializer = AssetSerializer(asset_list, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = AssetSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): if request.is_ajax(): assets = AssetSerializer(instance=Asset.objects.all(), many=True) return HttpResponse(json.dumps(assets.data), content_type='application/json') else: asset_creation_form = AssetCreationForm( auto_id="form-asset-create-%s", label_suffix='') asset_form = AssetForm(auto_id="form-asset-%s", label_suffix='') asset_sku_form = AssetSkuForm(auto_id="form-asset-sku-%s", label_suffix='') asset_set_form = AssetSetForm(auto_id="form-asset-set-%s", label_suffix='') ret = { 'asset_creation_form': asset_creation_form, 'asset_form': asset_form, 'asset_sku_form': asset_sku_form, 'asset_set_form': asset_set_form, } return render(request, 'create-asset.html', ret)