Пример #1
0
def client_update(genericAPIView, request, partial, set_data):
    """全量更新数据"""
    with transaction.atomic():
        client_user_pip.add_login_user_data(genericAPIView, set_data)
        forward_relation_hand(genericAPIView.model, set_data)

        # partial = kwargs.pop('partial', False)
        instance = genericAPIView.get_object()
        old_instance = copy(instance)

        serializer = genericAPIView.get_validate_form(genericAPIView.action)(
            instance, data=set_data, partial=partial)
        serializer.is_valid(raise_exception=True)
        instance = genericAPIView.perform_update(serializer)

        reverse_relation_hand(genericAPIView.model, set_data, instance)
        instance = genericAPIView.get_queryset().get(pk=instance.pk)

        # with transaction.atomic():
        log.debug(
            'sending Post Update signal with: model: %s, instance: %s',
            genericAPIView.model,
            instance,
        )
        post_bsm_create.send(
            sender=genericAPIView.model,
            instance=instance,
            create=False,
            request=genericAPIView.request,
            old_instance=old_instance,
        )

        serializer = genericAPIView.get_serializer(
            genericAPIView.get_queryset().get(pk=instance.pk))
        return success_response(serializer.data)
Пример #2
0
def manage_update(genericAPIView, request, partial, set_data):
    """全量更新数据"""
    print('进入全量更新了吗?')
    with transaction.atomic():
        forward_relation_hand(genericAPIView.model, set_data)

        # partial = kwargs.pop('partial', False)
        instance = genericAPIView.get_object()
        old_instance = copy(instance)
        serializer = genericAPIView.get_validate_form(genericAPIView.action)(
            instance,
            data=set_data,
            partial=partial,
            context=genericAPIView.get_serializer_context(),
        )
        serializer.is_valid(raise_exception=True)

        instance = genericAPIView.perform_update(serializer)
        serializer = genericAPIView.get_serializer(instance)

        if getattr(instance, '_prefetched_objects_cache', None):
            instance._prefetched_objects_cache = {}

        reverse_relation_hand(genericAPIView.model, set_data, instance)

    with transaction.atomic():
        log.debug(
            'sending Post Update signal with: model: %s, instance: %s',
            genericAPIView.model,
            instance,
        )
        post_bsm_create.send(
            sender=genericAPIView.model,
            instance=instance,
            create=False,
            old_instance=old_instance,
            request=genericAPIView.request,
            scope='admin'
        )
    return success_response(serializer.data)
Пример #3
0
def manage_create(genericAPIView, request, set_data):
    """
        这里校验表单和序列化类分开创建

        原因:序列化类有可能嵌套
        """
    many = isinstance(set_data, list)
    with transaction.atomic():
        forward_relation_hand(genericAPIView.model, set_data)
        serializer = genericAPIView.get_validate_form(genericAPIView.action)(
            data=set_data,
            context=genericAPIView.get_serializer_context(),
            many=many)
        serializer.is_valid(raise_exception=True)
        instance = genericAPIView.perform_create(serializer)

        if many:
            # 如果是批量插入,则直接返回
            return success_response()

        # 如果有联合查询,单个对象创建后并没有联合查询
        instance = genericAPIView.get_queryset().filter(pk=instance.pk).first()
        serializer = genericAPIView.get_serializer(instance)
        reverse_relation_hand(genericAPIView.model,
                              set_data,
                              instance,
                              detail=False)

        log.debug(
            'sending Post Save signal with: model: %s, instance: %s',
            genericAPIView.model,
            instance,
        )
        post_bsm_create.send(sender=genericAPIView.model,
                             instance=instance,
                             create=True,
                             request=genericAPIView.request,
                             old_instance=None,
                             scope='admin')
    return success_response(serializer.data)
Пример #4
0
def client_create(genericAPIView, request, set_data):
    """
        这里校验表单和序列化类分开创建

        原因:序列化类有可能嵌套
        """

    with transaction.atomic():
        client_user_pip.add_login_user_data(genericAPIView, set_data)
        forward_relation_hand(genericAPIView.model, set_data)
        serializer = genericAPIView.get_validate_form(genericAPIView.action)(
            data=set_data
        )
        serializer.is_valid(raise_exception=True)
        instance = genericAPIView.perform_create(serializer)
        reverse_relation_hand(genericAPIView.model, set_data, instance, detail=False)
        instance = genericAPIView.get_queryset().get(pk=instance.pk)

        # with transaction.atomic():
        log.debug(
            'sending Post Save signal with: model: %s, instance: %s',
            genericAPIView.model,
            instance,
        )
        post_bsm_create.send(
            sender=genericAPIView.model,
            instance=instance,
            create=True,
            request=genericAPIView.request,
            old_instance=None,
            scope='client',
        )
        # 如果有联合查询,单个对象创建后并没有联合查询, 所以要多查一次?
        serializer = genericAPIView.get_serializer(
            genericAPIView.get_queryset().get(pk=instance.pk)
        )
        return success_response(serializer.data)