Esempio n. 1
0
    def save_model(self, request, obj, form, change):
        # 数据保存
        obj.save()

        # 附加逻辑:发出重新生成静态详情页面的任务消息
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.id)
Esempio n. 2
0
    def update(self, instance, validated_data):
        # 根据传入的specs规格和选项信息插入中间表数据
        specs = validated_data.pop('specs')

        with transaction.atomic():
            save_id = transaction.savepoint()

            try:
                # 更新主表数据
                SKU.objects.filter(pk=instance.id).update(**validated_data)
                # 更新中间数据
                # 1、删除原有的中间表数据
                SKUSpecification.objects.filter(sku_id=instance.id).delete()
                # 2、插入新的中间表数据
                for temp in specs:
                    # temp: {spec_id: "4", option_id: 8}
                    temp['sku_id'] = instance.id
                    SKUSpecification.objects.create(**temp)
            except Exception as e:
                transaction.savepoint_rollback(save_id)
                raise e

            transaction.savepoint_commit(save_id)

        generate_static_sku_detail_html.delay(instance.id)

        return instance
Esempio n. 3
0
    def delete_model(self, request, obj):
        """admin后台删除了数据时调用"""

        sku_id = obj.sku.id
        obj.delete()
        # 生成静态页面 -- sku详情页面
        generate_static_sku_detail_html.delay(sku_id)
Esempio n. 4
0
 def save_model(self, request, obj, form, change):
     # 当新增、修改对象时,这个方法会执行
     super().save_model(request, obj, form, change)
     print(1)
     #生成静态文件
     generate_static_sku_detail_html.delay(obj.id)
     print(2)
Esempio n. 5
0
    def create(self, validated_data):
        # 1,获取用户对象


        code = validated_data['img'].replace('data:image/jpeg;base64,', '').replace('data:image/png;base64,','')
        imgData = base64.b64decode(code)
        url = storage(imgData)

        del validated_data['img']



        blog = super().create(validated_data)


        blog.user_id = 12

        blog.img = 'http://pps8t245f.bkt.clouddn.com/'+url
        # 获取当前时间
        time_now = int(time.time())
        # 转换成localtime
        time_local = time.localtime(time_now)
        # 转换成新的时间格式(2016-05-09 18:59:20)
        dt = time.strftime("%Y-%m-%d", time_local)
        blog.time = dt

        blog.save()

        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(blog.id)

        return blog
Esempio n. 6
0
    def save_model(self, request, obj, form, change):
        obj.save()
        sku= obj.sku  # 获取图片所对应的sku

        if sku.default_image_url == None: # 如果当前sku没有默认的图片
            sku.default_image_url = obj.image.url # 把当前的图片路径设置到sku中
        generate_static_sku_detail_html.delay(sku.id)
Esempio n. 7
0
    def save_models(self):
        """admin后台新增或修改了数据时调用"""

        obj = self.new_obj
        obj.save()
        # 生成静态页面 -- sku详情页面
        generate_static_sku_detail_html.delay(obj.id)
Esempio n. 8
0
    def delete_model(self, request, obj):
        sku_id = obj.sku.id
        obj.delete()

        # 补充操作: 发送重新生成对应商品详情页面的任务消息
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)
Esempio n. 9
0
    def save_models(self):
        # 保存数据对象
        obj = self.new_obj
        obj.save()

        # 补充自定义行为
        generate_static_sku_detail_html.delay(obj.sku.id)
Esempio n. 10
0
    def delete_model(self):
        obj = self.obj
        sku_id = obj.sku.id
        obj.delete()

        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)
Esempio n. 11
0
    def delete_models(self):
        """admin后台删除了数据时调用"""

        obj = self.obj
        sku_id = obj.sku.id
        obj.delete()
        generate_static_sku_detail_html.delay(sku_id)
Esempio n. 12
0
    def save_models(self):
        # 保存数据对象
        obj = self.new_obj
        obj.save()

        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.sku.id)
Esempio n. 13
0
    def update(self, instance, validated_data):

        specs = validated_data.get('specs')

        if 'specs' in validated_data:
            del validated_data['specs']

        with transaction.atomic():
            save_point = transaction.savepoint()

            try:
                # instance = super().update(instance, validated_data)
                SKU.objects.filter(id=instance.id).update(**validated_data)

                for item in specs:
                    SKUSpecification.objects.filter(
                        sku_id=instance.id,
                        spec_id=item.get('spec_id')).update(
                            option_id=item.get('option_id'))
            except Exception:
                transaction.savepoint_rollback(save_point)
                raise serializers.ValidationError('更新失败')
            else:
                transaction.savepoint_commit(save_point)

                # 生成详情页面
                from celery_tasks.html.tasks import generate_static_sku_detail_html
                generate_static_sku_detail_html.delay(instance.id)

        return instance
Esempio n. 14
0
    def save_model(self, request, obj, form, change):
        # 进行数据保存
        obj.save()

        # 附加操作: 发送重新生成指定商品详情页面任务
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.id)
Esempio n. 15
0
    def create(self, validated_data):

        # 1.写入sku
        # specs = [OrderedDict([('spec_id', 4), ('option_id', 8)]), OrderedDict([('spec_id', 5), ('option_id', 12)])]
        if validated_data['specs']:
            specs = validated_data.get('specs')
            del validated_data['specs']

        from django.db import transaction
        with transaction.atomic():
            save_id = transaction.savepoint()
            try:
                sku = SKU.objects.create(**validated_data)

                # 2.写入sku对应的规格选项信息
                for spec in specs:
                    SKUSpecification.objects.create(
                        sku=sku,
                        spec_id=spec.get('spec_id'),
                        option_id=spec.get('option_id'))

                # 调用异步方法之前,自己添加一个默认图片
                sku.default_image = 'group1/M00/00/02/CtM3BVrRdssdhfjDFGhfweu00672544'
                sku.save()
            except Exception as e:
                transaction.savepoint_rollback(save_id)
            else:
                transaction.savepoint_commit(save_id)
                # 触发一个异步任务
                from celery_tasks.html.tasks import generate_static_sku_detail_html
                generate_static_sku_detail_html.delay(sku.id)

        return sku
Esempio n. 16
0
    def save_models(self):
        """保存数据对象"""
        obj = self.new_obj
        obj.save()

        # 补充自定义的异步任务
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.sku.id)
Esempio n. 17
0
    def delete_model(self, request, obj):
        # 数据保存
        sku_id = obj.sku.id
        obj.delete()

        # 附加逻辑:发出重新生成静态详情页面的任务消息
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)
Esempio n. 18
0
    def delete_model(self):
        # 删除数据对象
        obj = self.obj
        sku_id = obj.sku.id
        obj.delete()

        # 补充自定义行为
        generate_static_sku_detail_html.delay(sku_id)
Esempio n. 19
0
    def delete_model(self, request, obj):
        # 数据删除
        sku_id = obj.sku.id
        obj.delete()

        # 发出任务消息
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)
Esempio n. 20
0
    def save_models(self):
        # 获取保存数据对象
        obj = self.new_obj
        obj.save()

        # 附加操作: 发出重新生成指定商品的详情页面的任务消息
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.sku.id)
Esempio n. 21
0
    def save_model(self, request, obj, form, change):
        obj.save()
        generate_static_sku_detail_html.delay(obj.sku.id)

        sku=obj.sku
        if not sku.default_image_url:
            sku.default_image_url=obj.image.url
            sku.save()
Esempio n. 22
0
    def save_models(self):
        # 数据保存
        obj = self.new_obj
        obj.save()

        # 附加逻辑:发出重新生成静态详情页面的任务消息
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.sku.id)
Esempio n. 23
0
    def delete_model(self, request, obj):

        obj.delete()

        sku = obj.sku   # 获取到图片模型对象关联的sku模型

        # 重新生成新的列表静态界面
        generate_static_sku_detail_html.delay(sku.id)
Esempio n. 24
0
    def save_model(self, request, obj, form, change):
        """数据保存时调用(更新&新增)"""
        # 数据保存
        obj.save()

        # 补充操作: 发送重新生成对应商品详情页面的任务消息
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.id)
Esempio n. 25
0
    def save_models(self):
        obj = self.new_obj
        obj.save()

        sku = obj.sku  # 获取图片所对应的sku
        if sku.default_image_url == None:  # 如果当前sku没有默认的图片
            sku.default_image_url = obj.image.url  # 把当前的图片路径设置到sku中

        generate_static_sku_detail_html.delay(sku.id)
Esempio n. 26
0
    def save_model(self, request, obj, form, change):
        obj.save()

        # 获取sku
        sku = obj.sku  # 获取sku所对应的图片
        if not sku.default_image_url:  # 如果sku没有默认图片
            sku.default_image_url = obj.image.url  # 把当前的图片路径设置到sku中

        generate_static_sku_detail_html.delay(sku.id)
Esempio n. 27
0
File: adminx.py Progetto: PyCN/book
 def save_models(self):
     obj = self.new_obj
     obj.save()
     from celery_tasks.html.tasks import generate_static_sku_detail_html
     generate_static_sku_detail_html.delay(obj.id)
     sku = obj.sku
     # 设置默认图片
     if not sku.default_image_url:
         sku.default_image_url = obj.image.url
Esempio n. 28
0
    def delete_model(self):
        # 获取删除数据对象
        obj = self.obj
        sku_id = obj.sku.id
        obj.delete()

        # 附加操作: 发出重新生成指定商品的详情页面的任务消息
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)
Esempio n. 29
0
    def save_model(self, request, obj, form, change):
        obj.save()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.sku.id)

        # 设置SKU默认图片
        sku = obj.sku
        if not sku.default_image_url:
            sku.default_image_url = obj.image.url
            sku.save()
Esempio n. 30
0
    def delete_model(self, request, obj):
        """
        监听事件
        :param request:
        :param obj:
        :return:
        """
        obj.delete()

        generate_static_sku_detail_html.delay(obj.id)