当前位置: 首页>>代码示例>>Python>>正文


Python models.ProtectedError方法代码示例

本文整理汇总了Python中django.db.models.ProtectedError方法的典型用法代码示例。如果您正苦于以下问题:Python models.ProtectedError方法的具体用法?Python models.ProtectedError怎么用?Python models.ProtectedError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.db.models的用法示例。


在下文中一共展示了models.ProtectedError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: delete_product

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def delete_product(request, pk, group):
    from django.db.models import ProtectedError
    product = get_object_or_404(Product, pk=pk)

    if request.method == 'POST':
        try:
            product.delete()
            Inventory.objects.filter(product=product).delete()
            messages.success(request, _("Product deleted"))
        except ProtectedError:
            messages.error(request, _('Cannot delete product'))

        return redirect(list_products, group)

    action = request.path
    return render(request, 'products/remove.html', locals()) 
开发者ID:fpsw,项目名称:Servo,代码行数:18,代码来源:product.py

示例2: delete_device

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def delete_device(request, product_line, model, pk):
    dev = get_object_or_404(Device, pk=pk)

    if request.method == 'POST':
        from django.db.models import ProtectedError
        try:
            dev.delete()
            messages.success(request, _("Device deleted"))
        except ProtectedError:
            messages.error(request, _("Cannot delete device with GSX repairs"))
            return redirect(dev)

        return redirect(index)

    data = {'action': request.path}
    data['device'] = dev

    return render(request, "devices/remove.html", data) 
开发者ID:fpsw,项目名称:Servo,代码行数:20,代码来源:device.py

示例3: test_shouldNotAllowWorkflowToBeDeletedWhenThereIsATransitionApproval

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def test_shouldNotAllowWorkflowToBeDeletedWhenThereIsATransitionApproval(self):
        content_type = ContentType.objects.get_for_model(BasicTestModel)

        state1 = StateObjectFactory(label="state1")
        state2 = StateObjectFactory(label="state2")

        workflow = WorkflowFactory(initial_state=state1, content_type=content_type, field_name="my_field")

        transition_meta = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state1,
            destination_state=state2,
        )

        TransitionApprovalMetaFactory.create(workflow=workflow, transition_meta=transition_meta, priority=0)

        BasicTestModelObjectFactory()
        TransitionApproval.objects.filter(workflow=workflow).update(status=APPROVED)
        approvals = TransitionApproval.objects.filter(workflow=workflow)
        assert_that(approvals, has_length(1))

        assert_that(
            calling(workflow.delete),
            raises(ProtectedError, "Cannot delete some instances of model 'Workflow' because they are referenced through a protected foreign key")
        ) 
开发者ID:javrasya,项目名称:django-river,代码行数:27,代码来源:test__transition_approval.py

示例4: delete

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def delete(self, *args, **kwargs):
        """Deletes a user instance.

        Trying to delete a meta user raises the `ProtectedError` exception.
        """
        if self.is_meta:
            raise ProtectedError("Cannot remove meta user instances", None)

        purge = kwargs.pop("purge", False)

        if purge:
            UserPurger(self).purge()
        else:
            UserMerger(self, User.objects.get_nobody_user()).merge()

        super().delete(*args, **kwargs) 
开发者ID:evernote,项目名称:zing,代码行数:18,代码来源:models.py

示例5: delete

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def delete(self):
        from api.models.data_nodes import DataNode
        if self.parents.count() != 0:
            raise models.ProtectedError(
                'Cannot delete template "@%s" because it is contained by '
                '1 or more other templates' % self.uuid, 
                [template for template in self.parents.all()])
        nodes_to_delete = set()
	queryset = DataNode.objects.filter(
            templateinput__template__uuid=self.uuid)
        for item in queryset.all():
            nodes_to_delete.add(item)
        super(Template, self).delete()
        for item in nodes_to_delete:
            try:
                item.delete()
            except models.ProtectedError:
                pass 
开发者ID:StanfordBioinformatics,项目名称:loom,代码行数:20,代码来源:templates.py

示例6: custom_wagtail_page_delete

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def custom_wagtail_page_delete(request, page_id):
    """
    Currently, ProtectedError exception is not caught in Wagtail admin.
    This workaround shows warning to the user if the page model like Fund, Round
    can not be deleted instead of raising 500.
    More details at https://github.com/wagtail/wagtail/issues/1602
    Once the issue is fixed in Wagtail core, we can remove this workaround.
    """
    try:
        return delete(request, page_id)
    except ProtectedError as e:
        protected_details = ", ".join([str(obj) for obj in e.protected_objects])
        page = get_object_or_404(Page, id=page_id).specific
        parent_id = page.get_parent().id
        messages.warning(request, _("Page '{0}' can't be deleted because is in use in '{1}'.").format(
            page.get_admin_display_title(), protected_details
        ))
        return redirect('wagtailadmin_explore', parent_id) 
开发者ID:OpenTechFund,项目名称:hypha,代码行数:20,代码来源:views.py

示例7: test_shouldNotAllowTheStateToBeDeletedWhenThereIsATransitionApprovalThatIsUsedAsSource

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def test_shouldNotAllowTheStateToBeDeletedWhenThereIsATransitionApprovalThatIsUsedAsSource(self):
        content_type = ContentType.objects.get_for_model(BasicTestModel)

        state1 = StateObjectFactory(label="state1")
        state2 = StateObjectFactory(label="state2")
        state3 = StateObjectFactory(label="state3")

        workflow = WorkflowFactory(initial_state=state1, content_type=content_type, field_name="my_field")

        transition_meta_1 = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state1,
            destination_state=state2,
        )

        transition_meta_2 = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state2,
            destination_state=state3,
        )

        TransitionApprovalMetaFactory.create(workflow=workflow, transition_meta=transition_meta_1, priority=0)
        TransitionApprovalMetaFactory.create(workflow=workflow, transition_meta=transition_meta_2, priority=0)

        BasicTestModelObjectFactory()
        TransitionApproval.objects.filter(workflow=workflow).update(status=APPROVED)
        approvals = TransitionApproval.objects.filter(workflow=workflow)
        assert_that(approvals, has_length(2))

        assert_that(
            calling(state2.delete),
            raises(ProtectedError, "Cannot delete some instances of model 'State' because they are referenced through a protected foreign key")
        ) 
开发者ID:javrasya,项目名称:django-river,代码行数:35,代码来源:test__transition_approval.py

示例8: test_shouldNotAllowTheStateToBeDeletedWhenThereIsATransitionApprovalThatIsUsedAsDestination

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def test_shouldNotAllowTheStateToBeDeletedWhenThereIsATransitionApprovalThatIsUsedAsDestination(self):
        content_type = ContentType.objects.get_for_model(BasicTestModel)

        state1 = StateObjectFactory(label="state1")
        state2 = StateObjectFactory(label="state2")
        state3 = StateObjectFactory(label="state3")

        workflow = WorkflowFactory(initial_state=state1, content_type=content_type, field_name="my_field")

        transition_meta_1 = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state1,
            destination_state=state2,
        )

        transition_meta_2 = TransitionMetaFactory.create(
            workflow=workflow,
            source_state=state2,
            destination_state=state3,
        )

        TransitionApprovalMetaFactory.create(workflow=workflow, transition_meta=transition_meta_1, priority=0)
        TransitionApprovalMetaFactory.create(workflow=workflow, transition_meta=transition_meta_2, priority=0)

        BasicTestModelObjectFactory()
        TransitionApproval.objects.filter(workflow=workflow).update(status=APPROVED)
        approvals = TransitionApproval.objects.filter(workflow=workflow)
        assert_that(approvals, has_length(2))

        assert_that(
            calling(state3.delete),
            raises(ProtectedError, "Cannot delete some instances of model 'State' because they are referenced through a protected foreign key")
        ) 
开发者ID:javrasya,项目名称:django-river,代码行数:35,代码来源:test__transition_approval.py

示例9: assertDeleteProtected

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def assertDeleteProtected(self, deleted, protected):
        protected.clean()
        with self.assertRaises(ProtectedError):
            deleted.delete()
        protected.delete() 
开发者ID:GamesDoneQuick,项目名称:donation-tracker,代码行数:7,代码来源:test_delete_protection.py

示例10: collect

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def collect(self, objs, source_attr=None, **kwargs):
        for obj in objs:
            if source_attr and hasattr(obj, source_attr):
                self.add_edge(getattr(obj, source_attr), obj)
            else:
                self.add_edge(None, obj)
        try:
            return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs)
        except models.ProtectedError as e:
            self.protected.update(e.protected_objects) 
开发者ID:stormsha,项目名称:StormOnline,代码行数:12,代码来源:util.py

示例11: collect

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def collect(self, objs, source=None, source_attr=None, **kwargs):
        for obj in objs:
            if source_attr and not source_attr.endswith('+'):
                related_name = source_attr % {
                    'class': source._meta.model_name,
                    'app_label': source._meta.app_label,
                }
                self.add_edge(getattr(obj, related_name), obj)
            else:
                self.add_edge(None, obj)
            self.model_count[obj._meta.verbose_name_plural] += 1
        try:
            return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs)
        except models.ProtectedError as e:
            self.protected.update(e.protected_objects) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:17,代码来源:utils.py

示例12: collect

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def collect(self, objs, source=None, source_attr=None, **kwargs):
        for obj in objs:
            if source_attr and not source_attr.endswith('+'):
                related_name = source_attr % {
                    'class': source._meta.model_name,
                    'app_label': source._meta.app_label,
                }
                self.add_edge(getattr(obj, related_name), obj)
            else:
                self.add_edge(None, obj)
            self.model_objs[obj._meta.model].add(obj)
        try:
            return super().collect(objs, source_attr=source_attr, **kwargs)
        except models.ProtectedError as e:
            self.protected.update(e.protected_objects) 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:17,代码来源:utils.py

示例13: test_canonical_page_deletion_is_protected

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def test_canonical_page_deletion_is_protected(segmented_page):
    # When deleting canonical page without deleting variants, it should return
    # an error. All variants should be deleted beforehand.
    with pytest.raises(ProtectedError):
        segmented_page.personalisation_metadata.canonical_page.delete() 
开发者ID:wagtail,项目名称:wagtail-personalisation,代码行数:7,代码来源:test_models.py

示例14: test_page_protection_when_deleting_segment

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def test_page_protection_when_deleting_segment(segmented_page):
    segment = segmented_page.personalisation_metadata.segment
    assert len(segment.get_used_pages())
    with pytest.raises(ProtectedError):
        segment.delete() 
开发者ID:wagtail,项目名称:wagtail-personalisation,代码行数:7,代码来源:test_models.py

示例15: delete

# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ProtectedError [as 别名]
def delete(self, *args, **kwargs):  # pylint: disable=arguments-differ
        """
        Delete this email notification channel.
        """
        if not self.shared:
            newrelic.delete_email_notification_channel(self.id)
            super().delete(*args, **kwargs)
        else:
            raise ProtectedError('Cannot delete a shared email notification channel', self) 
开发者ID:open-craft,项目名称:opencraft,代码行数:11,代码来源:openedx_monitoring.py


注:本文中的django.db.models.ProtectedError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。