Python django.contrib.admin.helpers.ACTION_CHECKBOX_NAME Examples

The following are 30 code examples of django.contrib.admin.helpers.ACTION_CHECKBOX_NAME(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.contrib.admin.helpers , or try the search function .
Example #1
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_user_message_on_none_selected(self):
        """
        User sees a warning when 'Go' is pressed and no items are selected.
        """
        action_data = {
            ACTION_CHECKBOX_NAME: [],
            'action': 'delete_selected',
            'index': 0,
        }
        url = reverse('admin:admin_views_subscriber_changelist')
        response = self.client.post(url, action_data)
        self.assertRedirects(response, url, fetch_redirect_response=False)
        response = self.client.get(response.url)
        msg = 'Items must be selected in order to perform actions on them. No items have been changed.'
        self.assertContains(response, msg)
        self.assertEqual(Subscriber.objects.count(), 2) 
Example #2
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_user_message_on_none_selected(self):
        """
        User sees a warning when 'Go' is pressed and no items are selected.
        """
        action_data = {
            ACTION_CHECKBOX_NAME: [],
            'action': 'delete_selected',
            'index': 0,
        }
        url = reverse('admin:admin_views_subscriber_changelist')
        response = self.client.post(url, action_data)
        self.assertRedirects(response, url, fetch_redirect_response=False)
        response = self.client.get(response.url)
        msg = 'Items must be selected in order to perform actions on them. No items have been changed.'
        self.assertContains(response, msg)
        self.assertEqual(Subscriber.objects.count(), 2) 
Example #3
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_model_admin_default_delete_action(self):
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
            'action': 'delete_selected',
            'index': 0,
        }
        delete_confirmation_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
            'action': 'delete_selected',
            'post': 'yes',
        }
        confirmation = self.client.post(reverse('admin:admin_views_subscriber_changelist'), action_data)
        self.assertIsInstance(confirmation, TemplateResponse)
        self.assertContains(confirmation, 'Are you sure you want to delete the selected subscribers?')
        self.assertContains(confirmation, '<h2>Summary</h2>')
        self.assertContains(confirmation, '<li>Subscribers: 2</li>')
        self.assertContains(confirmation, '<li>External subscribers: 1</li>')
        self.assertContains(confirmation, ACTION_CHECKBOX_NAME, count=2)
        self.client.post(reverse('admin:admin_views_subscriber_changelist'), delete_confirmation_data)
        self.assertEqual(Subscriber.objects.count(), 0) 
Example #4
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_model_admin_default_delete_action(self):
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
            'action': 'delete_selected',
            'index': 0,
        }
        delete_confirmation_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
            'action': 'delete_selected',
            'post': 'yes',
        }
        confirmation = self.client.post(reverse('admin:admin_views_subscriber_changelist'), action_data)
        self.assertIsInstance(confirmation, TemplateResponse)
        self.assertContains(confirmation, 'Are you sure you want to delete the selected subscribers?')
        self.assertContains(confirmation, '<h2>Summary</h2>')
        self.assertContains(confirmation, '<li>Subscribers: 2</li>')
        self.assertContains(confirmation, '<li>External subscribers: 1</li>')
        self.assertContains(confirmation, ACTION_CHECKBOX_NAME, count=2)
        self.client.post(reverse('admin:admin_views_subscriber_changelist'), delete_confirmation_data)
        self.assertEqual(Subscriber.objects.count(), 0) 
Example #5
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_model_admin_default_delete_action_protected(self):
        """
        The default delete action where some related objects are protected
        from deletion.
        """
        q1 = Question.objects.create(question='Why?')
        a1 = Answer.objects.create(question=q1, answer='Because.')
        a2 = Answer.objects.create(question=q1, answer='Yes.')
        q2 = Question.objects.create(question='Wherefore?')
        action_data = {
            ACTION_CHECKBOX_NAME: [q1.pk, q2.pk],
            'action': 'delete_selected',
            'index': 0,
        }
        delete_confirmation_data = action_data.copy()
        delete_confirmation_data['post'] = 'yes'
        response = self.client.post(reverse('admin:admin_views_question_changelist'), action_data)
        self.assertContains(response, 'would require deleting the following protected related objects')
        self.assertContains(
            response,
            '<li>Answer: <a href="%s">Because.</a></li>' % reverse('admin:admin_views_answer_change', args=(a1.pk,)),
            html=True
        )
        self.assertContains(
            response,
            '<li>Answer: <a href="%s">Yes.</a></li>' % reverse('admin:admin_views_answer_change', args=(a2.pk,)),
            html=True
        )
        # A POST request to delete protected objects displays the page which
        # says the deletion is prohibited.
        response = self.client.post(reverse('admin:admin_views_question_changelist'), delete_confirmation_data)
        self.assertContains(response, 'would require deleting the following protected related objects')
        self.assertEqual(Question.objects.count(), 2) 
Example #6
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_custom_function_action_no_perm_response(self):
        """A custom action may returns an HttpResponse with a 403 code."""
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            'action': 'no_perm',
            'index': 0,
        }
        response = self.client.post(reverse('admin:admin_views_externalsubscriber_changelist'), action_data)
        self.assertEqual(response.status_code, 403)
        self.assertEqual(response.content, b'No permission to perform this action') 
Example #7
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_multiple_actions_form(self):
        """
        Actions come from the form whose submit button was pressed (#10618).
        """
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            # Two different actions selected on the two forms...
            'action': ['external_mail', 'delete_selected'],
            # ...but "go" was clicked on the top form.
            'index': 0
        }
        self.client.post(reverse('admin:admin_views_externalsubscriber_changelist'), action_data)
        # The action sends mail rather than deletes.
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Greetings from a function action') 
Example #8
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_user_message_on_no_action(self):
        """
        User sees a warning when 'Go' is pressed and no action is selected.
        """
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
            'action': '',
            'index': 0,
        }
        url = reverse('admin:admin_views_subscriber_changelist')
        response = self.client.post(url, action_data)
        self.assertRedirects(response, url, fetch_redirect_response=False)
        response = self.client.get(response.url)
        self.assertContains(response, 'No action selected.')
        self.assertEqual(Subscriber.objects.count(), 2) 
Example #9
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_model_admin_no_delete_permission(self):
        """
        Permission is denied if the user doesn't have delete permission for the
        model (Subscriber).
        """
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            'action': 'delete_selected',
        }
        url = reverse('admin:admin_views_subscriber_changelist')
        response = self.client.post(url, action_data)
        self.assertRedirects(response, url, fetch_redirect_response=False)
        response = self.client.get(response.url)
        self.assertContains(response, 'No action selected.') 
Example #10
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_model_admin_custom_action(self):
        """A custom action defined in a ModelAdmin method."""
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            'action': 'mail_admin',
            'index': 0,
        }
        self.client.post(reverse('admin:admin_views_subscriber_changelist'), action_data)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Greetings from a ModelAdmin action') 
Example #11
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_default_delete_action_nonexistent_pk(self):
        self.assertFalse(Subscriber.objects.filter(id=9998).exists())
        action_data = {
            ACTION_CHECKBOX_NAME: ['9998'],
            'action': 'delete_selected',
            'index': 0,
        }
        response = self.client.post(reverse('admin:admin_views_subscriber_changelist'), action_data)
        self.assertContains(response, 'Are you sure you want to delete the selected subscribers?')
        self.assertContains(response, '<ul></ul>', html=True) 
Example #12
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_non_localized_pk(self):
        """
        If USE_THOUSAND_SEPARATOR is set, the ids for the objects selected for
        deletion are rendered without separators.
        """
        s = ExternalSubscriber.objects.create(id=9999)
        action_data = {
            ACTION_CHECKBOX_NAME: [s.pk, self.s2.pk],
            'action': 'delete_selected',
            'index': 0,
        }
        response = self.client.post(reverse('admin:admin_views_subscriber_changelist'), action_data)
        self.assertTemplateUsed(response, 'admin/delete_selected_confirmation.html')
        self.assertContains(response, 'value="9999"')  # Instead of 9,999
        self.assertContains(response, 'value="%s"' % self.s2.pk) 
Example #13
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_custom_function_action_with_redirect(self):
        """Another custom action defined in a function."""
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            'action': 'redirect_to',
            'index': 0,
        }
        response = self.client.post(reverse('admin:admin_views_externalsubscriber_changelist'), action_data)
        self.assertEqual(response.status_code, 302) 
Example #14
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_delete_queryset_hook(self):
        delete_confirmation_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
            'action': 'delete_selected',
            'post': 'yes',
            'index': 0,
        }
        SubscriberAdmin.overridden = False
        self.client.post(reverse('admin:admin_views_subscriber_changelist'), delete_confirmation_data)
        # SubscriberAdmin.delete_queryset() sets overridden to True.
        self.assertIs(SubscriberAdmin.overridden, True)
        self.assertEqual(Subscriber.objects.all().count(), 0) 
Example #15
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_delete_selected_uses_get_deleted_objects(self):
        """The delete_selected action uses ModelAdmin.get_deleted_objects()."""
        book = Book.objects.create(name='Test Book')
        data = {
            ACTION_CHECKBOX_NAME: [book.pk],
            'action': 'delete_selected',
            'index': 0,
        }
        response = self.client.post(reverse('admin2:admin_views_book_changelist'), data)
        # BookAdmin.get_deleted_objects() returns custom text.
        self.assertContains(response, 'a deletable object') 
Example #16
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_custom_function_mail_action(self):
        """A custom action may be defined in a function."""
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            'action': 'external_mail',
            'index': 0,
        }
        self.client.post(reverse('admin:admin_views_externalsubscriber_changelist'), action_data)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Greetings from a function action') 
Example #17
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_custom_function_action_with_redirect(self):
        """Another custom action defined in a function."""
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            'action': 'redirect_to',
            'index': 0,
        }
        response = self.client.post(reverse('admin:admin_views_externalsubscriber_changelist'), action_data)
        self.assertEqual(response.status_code, 302) 
Example #18
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_default_redirect(self):
        """
        Actions which don't return an HttpResponse are redirected to the same
        page, retaining the querystring (which may contain changelist info).
        """
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            'action': 'external_mail',
            'index': 0,
        }
        url = reverse('admin:admin_views_externalsubscriber_changelist') + '?o=1'
        response = self.client.post(url, action_data)
        self.assertRedirects(response, url) 
Example #19
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_custom_function_action_no_perm_response(self):
        """A custom action may returns an HttpResponse with a 403 code."""
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            'action': 'no_perm',
            'index': 0,
        }
        response = self.client.post(reverse('admin:admin_views_externalsubscriber_changelist'), action_data)
        self.assertEqual(response.status_code, 403)
        self.assertEqual(response.content, b'No permission to perform this action') 
Example #20
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_multiple_actions_form(self):
        """
        Actions come from the form whose submit button was pressed (#10618).
        """
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            # Two different actions selected on the two forms...
            'action': ['external_mail', 'delete_selected'],
            # ...but "go" was clicked on the top form.
            'index': 0
        }
        self.client.post(reverse('admin:admin_views_externalsubscriber_changelist'), action_data)
        # The action sends mail rather than deletes.
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Greetings from a function action') 
Example #21
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_user_message_on_no_action(self):
        """
        User sees a warning when 'Go' is pressed and no action is selected.
        """
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
            'action': '',
            'index': 0,
        }
        url = reverse('admin:admin_views_subscriber_changelist')
        response = self.client.post(url, action_data)
        self.assertRedirects(response, url, fetch_redirect_response=False)
        response = self.client.get(response.url)
        self.assertContains(response, 'No action selected.')
        self.assertEqual(Subscriber.objects.count(), 2) 
Example #22
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_model_admin_no_delete_permission(self):
        """
        Permission is denied if the user doesn't have delete permission for the
        model (Subscriber).
        """
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            'action': 'delete_selected',
        }
        url = reverse('admin:admin_views_subscriber_changelist')
        response = self.client.post(url, action_data)
        self.assertRedirects(response, url, fetch_redirect_response=False)
        response = self.client.get(response.url)
        self.assertContains(response, 'No action selected.') 
Example #23
Source File: options.py    From python2017 with MIT License 5 votes vote down vote up
def action_checkbox(self, obj):
        """
        A list_display column containing a checkbox widget.
        """
        return helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, force_text(obj.pk)) 
Example #24
Source File: options.py    From bioforum with MIT License 5 votes vote down vote up
def action_checkbox(self, obj):
        """
        A list_display column containing a checkbox widget.
        """
        return helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, str(obj.pk)) 
Example #25
Source File: base.py    From django2-project-template with MIT License 5 votes vote down vote up
def hard_delete_selected(modeladmin, request, queryset):
    opts = modeladmin.model._meta  # # pylint: disable=W0212

    deletable_objects, model_count, perms_needed, protected = modeladmin.get_deleted_objects(queryset, request)

    if request.POST.get('post') and not protected:
        if perms_needed:
            raise PermissionDenied
        if queryset.count():
            number_of_rows_deleted, __ = queryset.hard_delete()  # __ = deleted_items
            if number_of_rows_deleted == 1:
                message_bit = _('1 record was')
            else:
                message_bit = _('%(number_of_rows)s records were') % dict(number_of_rows=number_of_rows_deleted)
            message = _('%(message_bit)s deleted') % dict(message_bit=message_bit)
            modeladmin.message_user(request, message)
        return None

    objects_name = model_ngettext(queryset)
    if perms_needed or protected:
        title = _('Cannot delete %(name)s') % {'name': objects_name}
    else:
        title = _('Are you sure?')

    context = {
        **modeladmin.admin_site.each_context(request),
        'title': title,
        'objects_name': str(objects_name),
        'deletable_objects': [deletable_objects],
        'model_count': dict(model_count).items(),
        'queryset': queryset,
        'perms_lacking': perms_needed,
        'protected': protected,
        'opts': opts,
        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
        'media': modeladmin.media,
    }

    request.current_app = modeladmin.admin_site.name

    return TemplateResponse(request, 'admin/hard_delete_selected_confirmation.html', context) 
Example #26
Source File: admin.py    From mangaki with GNU Affero General Public License v3.0 5 votes vote down vote up
def refresh_work(self, request, queryset):
        if request.POST.get('confirm'):  # Confirmed
            downloaded_titles = []
            for obj in queryset:
                chosen_poster = request.POST.get('chosen_poster_{:d}'.format(obj.id))
                if not chosen_poster:
                    continue
                if obj.retrieve_poster(chosen_poster):
                    downloaded_titles.append(obj.title)
            if downloaded_titles:
                self.message_user(
                    request,
                    "Des posters ont été trouvés pour les anime suivants : %s." % ', '.join(downloaded_titles))
            else:
                self.message_user(request, "Aucun poster n'a été trouvé, essayez de changer le titre.")
            return None
        bundle = []
        for work in queryset:
            bundle.append((work.id, work.title, get_potential_posters(work)))
        context = {
            'queryset': queryset,
            'bundle': bundle,
            'opts': self.model._meta,
            'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME
        }
        return TemplateResponse(request, 'admin/refresh_poster_confirmation.html', context) 
Example #27
Source File: options.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def action_checkbox(self, obj):
        """
        A list_display column containing a checkbox widget.
        """
        return helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, str(obj.pk)) 
Example #28
Source File: options.py    From python with Apache License 2.0 5 votes vote down vote up
def action_checkbox(self, obj):
        """
        A list_display column containing a checkbox widget.
        """
        return helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, force_text(obj.pk)) 
Example #29
Source File: options.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def action_checkbox(self, obj):
        """
        A list_display column containing a checkbox widget.
        """
        return helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, force_text(obj.pk)) 
Example #30
Source File: test_actions.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_default_redirect(self):
        """
        Actions which don't return an HttpResponse are redirected to the same
        page, retaining the querystring (which may contain changelist info).
        """
        action_data = {
            ACTION_CHECKBOX_NAME: [self.s1.pk],
            'action': 'external_mail',
            'index': 0,
        }
        url = reverse('admin:admin_views_externalsubscriber_changelist') + '?o=1'
        response = self.client.post(url, action_data)
        self.assertRedirects(response, url)