本文整理汇总了Python中django.contrib.admin.utils.NestedObjects类的典型用法代码示例。如果您正苦于以下问题:Python NestedObjects类的具体用法?Python NestedObjects怎么用?Python NestedObjects使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NestedObjects类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: NestedObjectsTests
class NestedObjectsTests(TestCase):
"""
Tests for ``NestedObject`` utility collection.
"""
def setUp(self):
self.n = NestedObjects(using=DEFAULT_DB_ALIAS)
self.objs = [Count.objects.create(num=i) for i in range(5)]
def _check(self, target):
self.assertEqual(self.n.nested(lambda obj: obj.num), target)
def _connect(self, i, j):
self.objs[i].parent = self.objs[j]
self.objs[i].save()
def _collect(self, *indices):
self.n.collect([self.objs[i] for i in indices])
def test_unrelated_roots(self):
self._connect(2, 1)
self._collect(0)
self._collect(1)
self._check([0, 1, [2]])
def test_siblings(self):
self._connect(1, 0)
self._connect(2, 0)
self._collect(0)
self._check([0, [1, 2]])
def test_non_added_parent(self):
self._connect(0, 1)
self._collect(0)
self._check([0])
def test_cyclic(self):
self._connect(0, 2)
self._connect(1, 0)
self._connect(2, 1)
self._collect(0)
self._check([0, [1, [2]]])
def test_queries(self):
self._connect(1, 0)
self._connect(2, 0)
# 1 query to fetch all children of 0 (1 and 2)
# 1 query to fetch all children of 1 and 2 (none)
# Should not require additional queries to populate the nested graph.
self.assertNumQueries(2, self._collect, 0)
def test_on_delete_do_nothing(self):
"""
Check that the nested collector doesn't query for DO_NOTHING objects.
"""
n = NestedObjects(using=DEFAULT_DB_ALIAS)
objs = [Event.objects.create()]
EventGuide.objects.create(event=objs[0])
with self.assertNumQueries(2):
# One for Location, one for Guest, and no query for EventGuide
n.collect(objs)
示例2: delete_rule
def delete_rule(request, tid):
rule = get_object_or_404(Rule, pk=tid)
form = DeleteRuleForm(instance=rule)
if request.method == 'POST':
print >> sys.stderr, 'id' in request.POST
print >> sys.stderr, str(rule.id) == request.POST['id']
print >> sys.stderr, str(rule.id) == request.POST['id']
# if 'id' in request.POST and str(rule.id) == request.POST['id']:
form = DeleteRuleForm(request.POST, instance=rule)
print >> sys.stderr, form.is_valid()
print >> sys.stderr, form.errors
print >> sys.stderr, form.non_field_errors()
print >> sys.stderr, 'id' in request.POST
if form.is_valid():
rule.delete()
messages.add_message(request,
messages.SUCCESS,
'Rule deleted.',
extra_tags='alert-success')
return HttpResponseRedirect(reverse('rules'))
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([rule])
rels = collector.nested()
add_breadcrumb(parent=rule, title="Delete", top_level=False, request=request)
system_settings = System_Settings.objects.get()
return render(request, 'dojo/delete_rule.html',
{'rule': rule,
'form': form,
'active_tab': 'findings',
'system_settings': system_settings,
'rels': rels,
})
示例3: delete
def delete(self, cascade=True, **kwargs):
if self.PREVENT_DELETE:
raise DeleteNotPermitted()
if cascade:
collector = NestedObjects(using='default')
collector.collect([self])
field_updates = collector.field_updates
for cls, to_update in field_updates.iteritems():
for (field, value), instances in to_update.iteritems():
cls.objects.filter(
pk__in={o.pk for o in instances}
).update(
**{field.attname: value}
)
for klass, objs in collector.data.iteritems():
try:
klass._meta.get_field('is_void')
except models.FieldDoesNotExist:
pass
else:
klass.objects.filter(pk__in={o.pk for o in objs}).update(
is_void=True
)
else:
self.is_void = True
self.save()
signals.post_delete.send(
sender=self.__class__, instance=self
)
示例4: delete_product
def delete_product(request, pid):
product = get_object_or_404(Product, pk=pid)
form = DeleteProductForm(instance=product)
if request.method == 'POST':
if 'id' in request.POST and str(product.id) == request.POST['id']:
form = DeleteProductForm(request.POST, instance=product)
if form.is_valid():
if product.tags:
del product.tags
product.delete()
messages.add_message(request,
messages.SUCCESS,
'Product and relationships removed.',
extra_tags='alert-success')
return HttpResponseRedirect(reverse('product'))
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([product])
rels = collector.nested()
product_tab = Product_Tab(pid, title="Product", tab="settings")
return render(request, 'dojo/delete_product.html',
{'product': product,
'form': form,
'product_tab': product_tab,
'rels': rels,
})
示例5: delete_test
def delete_test(request, tid):
test = get_object_or_404(Test, pk=tid)
eng = test.engagement
form = DeleteTestForm(instance=test)
from django.contrib.admin.utils import NestedObjects
from django.db import DEFAULT_DB_ALIAS
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([test])
rels = collector.nested()
if request.method == 'POST':
if 'id' in request.POST and str(test.id) == request.POST['id']:
form = DeleteTestForm(request.POST, instance=test)
if form.is_valid():
del test.tags
test.delete()
messages.add_message(request,
messages.SUCCESS,
'Test and relationships removed.',
extra_tags='alert-success')
return HttpResponseRedirect(reverse('view_engagement', args=(eng.id,)))
add_breadcrumb(parent=test, title="Delete", top_level=False, request=request)
return render(request, 'dojo/delete_test.html',
{'test': test,
'form': form,
'rels': rels,
'deletable_objects': rels,
})
示例6: delete_user
def delete_user(request, uid):
user = get_object_or_404(Dojo_User, id=uid)
form = DeleteUserForm(instance=user)
from django.contrib.admin.utils import NestedObjects
from django.db import DEFAULT_DB_ALIAS
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([user])
rels = collector.nested()
if user.id == request.user.id:
messages.add_message(request,
messages.ERROR,
'You may not delete yourself.',
extra_tags='alert-danger')
return HttpResponseRedirect(reverse('edit_user', args=(user.id,)))
if request.method == 'POST':
if 'id' in request.POST and str(user.id) == request.POST['id']:
form = DeleteUserForm(request.POST, instance=user)
if form.is_valid():
user.delete()
messages.add_message(request,
messages.SUCCESS,
'User and relationships removed.',
extra_tags='alert-success')
return HttpResponseRedirect(reverse('users'))
add_breadcrumb(title="Delete User", top_level=False, request=request)
return render(request, 'dojo/delete_user.html',
{'to_delete': user,
'form': form,
'rels': rels,
})
示例7: delete_engagement_presets
def delete_engagement_presets(request, pid, eid):
prod = get_object_or_404(Product, id=pid)
preset = get_object_or_404(Engagement_Presets, id=eid)
form = DeleteEngagementPresetsForm(instance=preset)
if request.method == 'POST':
if 'id' in request.POST:
form = DeleteEngagementPresetsForm(request.POST, instance=preset)
if form.is_valid():
preset.delete()
messages.add_message(request,
messages.SUCCESS,
'Engagement presets and engagement relationships removed.',
extra_tags='alert-success')
return HttpResponseRedirect(reverse('engagement_presets', args=(pid,)))
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([preset])
rels = collector.nested()
product_tab = Product_Tab(pid, title="Delete Engagement Preset", tab="settings")
return render(request, 'dojo/delete_presets.html',
{'product': product,
'form': form,
'product_tab': product_tab,
'rels': rels,
})
示例8: delete_test
def delete_test(request, tid):
test = get_object_or_404(Test, pk=tid)
eng = test.engagement
form = DeleteTestForm(instance=test)
if request.method == 'POST':
if 'id' in request.POST and str(test.id) == request.POST['id']:
form = DeleteTestForm(request.POST, instance=test)
if form.is_valid():
del test.tags
test.delete()
messages.add_message(request,
messages.SUCCESS,
'Test and relationships removed.',
extra_tags='alert-success')
return HttpResponseRedirect(reverse('view_engagement', args=(eng.id,)))
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([test])
rels = collector.nested()
product_tab = Product_Tab(test.engagement.product.id, title="Delete Test", tab="engagements")
product_tab.setEngagement(test.engagement)
return render(request, 'dojo/delete_test.html',
{'test': test,
'product_tab': product_tab,
'form': form,
'rels': rels,
'deletable_objects': rels,
})
示例9: delete_product
def delete_product(request, pid):
product = get_object_or_404(Product, pk=pid)
form = DeleteProductForm(instance=product)
from django.contrib.admin.utils import NestedObjects
from django.db import DEFAULT_DB_ALIAS
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([product])
rels = collector.nested()
if request.method == 'POST':
if 'id' in request.POST and str(product.id) == request.POST['id']:
form = DeleteProductForm(request.POST, instance=product)
if form.is_valid():
if product.tags:
del product.tags
product.delete()
messages.add_message(request,
messages.SUCCESS,
'Product and relationships removed.',
extra_tags='alert-success')
return HttpResponseRedirect(reverse('product'))
add_breadcrumb(parent=product, title="Delete", top_level=False, request=request)
return render(request, 'dojo/delete_product.html',
{'product': product,
'form': form,
'rels': rels,
})
示例10: delete_engagement
def delete_engagement(request, eid):
engagement = get_object_or_404(Engagement, pk=eid)
product = engagement.product
form = DeleteEngagementForm(instance=engagement)
if request.method == 'POST':
if 'id' in request.POST and str(engagement.id) == request.POST['id']:
form = DeleteEngagementForm(request.POST, instance=engagement)
if form.is_valid():
del engagement.tags
engagement.delete()
messages.add_message(
request,
messages.SUCCESS,
'Engagement and relationships removed.',
extra_tags='alert-success')
return HttpResponseRedirect(reverse("view_engagements", args=(product.id, )))
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([engagement])
rels = collector.nested()
product_tab = Product_Tab(product.id, title="Delete Engagement", tab="engagements")
product_tab.setEngagement(engagement)
return render(request, 'dojo/delete_engagement.html', {
'product_tab': product_tab,
'engagement': engagement,
'form': form,
'rels': rels,
})
示例11: delete_sql_data
def delete_sql_data(self):
for model_class, queryset in get_querysets_to_dump(self.domain_name, []):
collector = NestedObjects(using=queryset.db)
collector.collect(queryset)
collector.delete()
self.assertEqual([], list(get_objects_to_dump(self.domain_name, [])))
示例12: hand_clean_DELETE
def hand_clean_DELETE(self):
"""
We don't validate the 'DELETE' field itself because on
templates it's not rendered using the field information, but
just using a generic "deletion_field" of the InlineModelAdmin.
"""
if self.cleaned_data.get(DELETION_FIELD_NAME, False):
using = router.db_for_write(self._meta.model)
collector = NestedObjects(using=using)
if self.instance.pk is None:
return
collector.collect([self.instance])
if collector.protected:
objs = []
for p in collector.protected:
objs.append(
# Translators: Model verbose name and instance representation,
# suitable to be an item in a list.
_('%(class_name)s %(instance)s') % {
'class_name': p._meta.verbose_name,
'instance': p}
)
params = {'class_name': self._meta.model._meta.verbose_name,
'instance': self.instance,
'related_objects': get_text_list(objs, _('and'))}
msg = _("Deleting %(class_name)s %(instance)s would require "
"deleting the following protected related objects: "
"%(related_objects)s")
raise ValidationError(msg, code='deleting_protected', params=params)
示例13: delete_jira
def delete_jira(request, tid):
jira_instance = get_object_or_404(JIRA_Conf, pk=tid)
# eng = test.engagement
# TODO Make Form
form = DeleteJIRAConfForm(instance=jira_instance)
if request.method == 'POST':
if 'id' in request.POST and str(jira_instance.id) == request.POST['id']:
form = DeleteJIRAConfForm(request.POST, instance=jira_instance)
if form.is_valid():
jira_instance.delete()
messages.add_message(request,
messages.SUCCESS,
'JIRA Conf and relationships removed.',
extra_tags='alert-success')
return HttpResponseRedirect(reverse('jira'))
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([jira_instance])
rels = collector.nested()
add_breadcrumb(title="Delete", top_level=False, request=request)
return render(request, 'dojo/delete_jira.html',
{'inst': jira_instance,
'form': form,
'rels': rels,
'deletable_objects': rels,
})
示例14: will_be_deleted_with
def will_be_deleted_with(instance):
"""Get items that would be deleted along with model ``instance``.
Pass in any Django model instance that you intend to delete and get
an iterator of related objects that would also be deleted.
Since this is implemented as a generator, if you want a list of
items, you'll need to do ``list(will_be_deleted_with(instance))``.
Args:
instance: A Django ORM instance
Returns:
pairs: (model class, items of that class that will be deleted)
"""
# XXX: Not sure why this import can't be moved to module scope.
from django.contrib.admin.utils import NestedObjects
# The collector returns a list of all objects in the database that
# would be deleted if `obj` were deleted.
collector = NestedObjects(using='default')
collector.collect([instance])
for cls, items_to_delete in collector.data.items():
# XXX: Not sure the collector will ever include the original
# XXX: instance, but this check was in the original version and
# XXX: I don't have time to verify at the moment.
if instance in items_to_delete:
items_to_delete.remove(instance)
if items_to_delete:
yield cls, items_to_delete
示例15: get_deleted_objects
def get_deleted_objects(object):
"""
List the related objects before delete an object
"""
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([object])
return collector.nested()