def post(self, request, *args, **kwargs): if not can_delete_pages(self.get_affected_page_objects(), self.request.user): context = self.get_context_data( cannot_delete_page_variants_error=True, ) return self.render_to_response(context) return super().post(request, *args, **kwargs)
def delete_instance(self): page_variants = self.get_affected_page_objects() if not can_delete_pages(page_variants, self.request.user): raise PermissionDenied( 'User has no permission to delete variant page objects.') # Deleting page objects triggers deletion of the personalisation # metadata too because of models.CASCADE. with transaction.atomic(): for variant in page_variants.iterator(): # Delete each one separately so signals are called. variant.delete() super().delete_instance()
def test_cannot_delete_pages_with_standard_user(user, segmented_page): assert not can_delete_pages([segmented_page], user)
def test_can_delete_pages_with_superuser(rf, user, segmented_page): user.is_superuser = True assert can_delete_pages([segmented_page], user)