Пример #1
0
    def update(self, instance, validated_data):
        # 获取规格信息
        specs = self.context['request'].data.get('specs')
        # 因为sku表中没有specs字段,所以在保存的时候需要删除validated_data中specs数据

        with transaction.atomic():
            # 开启事务
            sid = transaction.savepoint()
            try:
                # 1、更新sku表
                SKU.objects.filter(id=instance.id).update(**validated_data)

                # 2、更新SKU具体规格表
                for spec in specs:
                    # SKUSpecification.objects.filter(sku=instance,spec_id=spec['spec_id']).update(**spec)
                    SKUSpecification.objects.create(
                        sku=instance,
                        spec_id=spec['spec_id'],
                        option_id=spec['option_id'])
            except:
                # 捕获异常,说明数据库操作失败,进行回滚
                transaction.savepoint_rollback(sid)
                return serializers.ValidationError('数据库错误')
            else:
                # 没有捕获异常,数据库操作成功,进行提交
                transaction.savepoint_commit(sid)
                # 执行异步任务生成新的静态页面

                # 生成详情页的静态页面
                get_detail_html.delay(instance.id)

                return instance
Пример #2
0
 def create(self, validated_data):
   # self指的是当前序列化器对象,在self下面有个context属性保存了请求对象
   specs=self.context['request'].data.get('specs')
   
   # specs = validated_data['specs']
   # 因为sku表中没有specs字段,所以在保存的时候需要删除validated_data中specs数据
   #del validated_data['specs']
   with transaction.atomic():
     # 开启事务
     sid = transaction.savepoint()
     try:
       # 1、保存sku表
       sku = SKU.objects.create(**validated_data)
       # 2、保存SKU具体规格
       for spec in specs:
         SKUSpecification.objects.create(sku=sku, spec_id=spec['spec_id'], option_id=spec['option_id'])
     except:
       # 捕获异常,说明数据库操作失败,进行回滚
       transaction.savepoint_rollback(sid)
       return serializers.ValidationError('数据库错误')
     else:
       # 没有捕获异常,数据库操作成功,进行提交
       transaction.savepoint_commit(sid)
       # 执行异步任务生成新的静态页面
       get_detail_html.delay(sku.id)
       return sku
Пример #3
0
    def create(self, validated_data):
        specs = self.context['request'].data.get('specs')
        # 开启事物
        with transaction.atomic():
            # 设置保存点
            save_point = transaction.savepoint()
            try:
                # 保存sku表
                sku = SKU.objects.create(**validated_data)

                # 保存sku具体规格表
                for spec in specs:
                    SKUSpecification.objects.create(
                        spec_id=spec['spec_id'],
                        option_id=spec['option_id'],
                        sku=sku)
            except:
                # 回滚
                transaction.savepoint_rollback(save_point)
                raise serializers.ValidationError('保存失败')
            else:
                # 提交
                transaction.savepoint_commit(save_point)

                # 生成详情页的静态页面
                get_detail_html.delay(sku.id)

                return sku
Пример #4
0
    def update(self, instance, validated_data):
        specs = self.context['request'].data.get('specs')
        # 开启事务
        with transaction.atomic():
            # 设置保存点
            save_point = transaction.savepoint()
            try:
                # 修改sku表
                SKU.objects.filter(id=instance.id).update(**validated_data)
                # 修改sku具体规格表
                for spec in specs:
                    SKUSpecification.objects.filter(sku=instance).update(
                        **spec)

            except:
                # 回滚
                transaction.savepoint_rollback(save_point)
                raise serializers.ValidationError('保存失败')

            else:
                # 提交
                transaction.savepoint_commit(save_point)

                # 生成详情页的静态页面
                get_detail_html.delay(instance.id)

                return instance
Пример #5
0
    def update(self, instance, validated_data):
        # 3、建立fastdfs的客户端
        client = Fdfs_client(settings.FASTDFS_PATH)

        # self.context['request'] 获取request对象

        file = self.context['request'].FILES.get('image')
        # 4、上传图片
        res = client.upload_by_buffer(file.read())
        # 5、判断是否上传成功
        if res['Status'] != 'Upload successed.':
            raise serializers.ValidationError({'error': '图片上传失败'})

        # 6、更新图片表
        instance.image = res['Remote file_id']
        instance.save()

        # 异步生成详情页静态页面
        get_detail_html.delay(instance.sku_id)
        return instance
    def create(self, validated_data):
        # 3、建立fastdfs的客户端
        client = Fdfs_client(dev.FASTDFS_PATH)

        # self.context['request'] 获取request对象

        file = self.context['request'].FILES.get('image')
        # 4、上传图片
        res = client.upload_by_buffer(file.read())
        # 5、判断是否上传成功
        if res['Status'] != 'Upload successed.':
            raise serializers.ValidationError({'error': '图片上传失败'})

        # 6、保存图片表
        img = SKUImage.objects.create(sku=validated_data['sku'], image=res['Remote file_id'])

        # 异步生成详情页静态页面
        get_detail_html.delay(img.sku.id)

        return img
Пример #7
0
    def update(self, instance, validated_data):
        # 3.建立fastdfs的客户端
        client = Fdfs_client(settings.FASTDFS_PATH)

        # 4.上传upload图片
        # self.context['request']  # 获取request对象

        image = self.context['request'].FILES.get('image')
        res = client.upload_appender_by_buffer(image.read())  # 文件byte数据

        # 5.判断图片是否上传成功
        if res['Status'] != 'Upload successed.':
            raise serializers.ValidationError({'error':'图片上传失败'})

        # 6.更新图片表
        instance.image = res['Remote file_id']
        instance.save()

        # 异步生成详情页静态页面
        get_detail_html.delay(instance.sku.id)  # 此处不能传对象
        return instance
Пример #8
0
    def create(self, validated_data):
        # 3.建立fastdfs的客户端
        client = Fdfs_client(settings.FASTDFS_PATH)

        # 4.上传upload图片
        # self.context['request']  # 获取request对象

        image = self.context['request'].FILES.get('image')
        res = client.upload_appender_by_buffer(image.read())  # 文件byte数据

        # 5.判断图片是否上传成功
        if res['Status'] != 'Upload successed.':
            raise serializers.ValidationError({'error':'图片上传失败'})

        # 6.保存图片表
        sku = validated_data['sku']  # 验证后的数据是一个对象
        img = SKUImage.objects.create(sku_id=sku.id, image=res['Remote file_id'])

        # 异步生成详情页静态页面
        get_detail_html.delay(img.sku.id)  # 此处不能传对象

        return img