本文整理匯總了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())
示例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)
示例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")
)
示例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)
示例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
示例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)
示例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")
)
示例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")
)
示例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()
示例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)
示例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)
示例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)
示例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()
示例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()
示例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)