我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.contrib.admin.helpers.ACTION_CHECKBOX_NAME。
def test_delete_files_or_folders_action(self): self.assertNotEqual(File.objects.count(), 0) self.assertNotEqual(Image.objects.count(), 0) self.assertNotEqual(Folder.objects.count(), 0) url = reverse('admin:filer-directory_listing-root') folders = [] for folder in FolderRoot().children.all(): folders.append('folder-%d' % (folder.id,)) # this returns the confirmation for the admin action response = self.client.post(url, { 'action': 'delete_files_or_folders', 'post': 'no', helpers.ACTION_CHECKBOX_NAME: folders, }) # this does the actual deleting response = self.client.post(url, { 'action': 'delete_files_or_folders', 'post': 'yes', helpers.ACTION_CHECKBOX_NAME: folders, }) self.assertEqual(File.objects.count(), 0) self.assertEqual(Folder.objects.count(), 0)
def test_delete_files_or_folders_action_with_mixed_types(self): # add more files/images so we can test the polymorphic queryset with multiple types self.create_file(folder=self.src_folder) self.create_image(folder=self.src_folder) self.create_file(folder=self.src_folder) self.create_image(folder=self.src_folder) self.assertNotEqual(File.objects.count(), 0) self.assertNotEqual(Image.objects.count(), 0) url = reverse('admin:filer-directory_listing', args=(self.folder.id,)) folders = [] for f in File.objects.filter(folder=self.folder): folders.append('file-%d' % (f.id,)) folders.append('folder-%d' % self.sub_folder1.id) response = self.client.post(url, { 'action': 'delete_files_or_folders', 'post': 'yes', helpers.ACTION_CHECKBOX_NAME: folders, }) self.assertEqual(File.objects.filter(folder__in=[self.folder.id, self.sub_folder1.id]).count(), 0)
def rate_limit_tasks(self, request, queryset): tasks = set([task.name for task in queryset]) opts = self.model._meta app_label = opts.app_label if request.POST.get('post'): rate = request.POST['rate_limit'] with current_app.default_connection() as connection: for task_name in tasks: rate_limit(task_name, rate, connection=connection) return None context = { 'title': _('Rate limit selection'), 'queryset': queryset, 'object_name': force_text(opts.verbose_name), 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, 'opts': opts, 'app_label': app_label, } return render_to_response( self.rate_limit_confirmation_template, context, context_instance=RequestContext(request), )
def test_activate_users(self): """ Test the admin custom command 'activate users' """ new_user = UserModel().objects.create_user(**self.user_info) profile = RegistrationProfile.objects.create_profile(new_user) self.assertFalse(profile.activated) registrationprofile_list = urlresolvers.reverse('admin:registration_registrationprofile_changelist') post_data = { 'action': 'activate_users', helpers.ACTION_CHECKBOX_NAME: [profile.pk], } self.client.post(registrationprofile_list, post_data, follow=True) profile = RegistrationProfile.objects.get(user=new_user) self.assertTrue(profile.activated)
def test_resend_activation_email(self): """ Test the admin custom command 'resend activation email' """ new_user = UserModel().objects.create_user(**self.user_info) profile = RegistrationProfile.objects.create_profile(new_user) registrationprofile_list = urlresolvers.reverse('admin:registration_registrationprofile_changelist') post_data = { 'action': 'resend_activation_email', helpers.ACTION_CHECKBOX_NAME: [profile.pk], } self.client.post(registrationprofile_list, post_data, follow=True) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.user_info['email']])
def cms_import(self, request, queryset): if request.POST.get('post', 'no') == 'yes': form = CMSImportForm(request.POST) if form.is_valid(): return render_to_response('cms_articles/import_wordpress/cms_import.html', { 'title': _('Running import'), 'items': queryset, 'options': form.cleaned_data['options'], 'media': self.media, 'opts': self.model._meta, }, context_instance=RequestContext(request)) else: form = CMSImportForm() return render_to_response('cms_articles/import_wordpress/form.html', { 'title': _('Select predefined import options'), 'queryset': queryset, 'opts': self.model._meta, 'form': form, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, }, context_instance=RequestContext(request))
def test_move_files_and_folders_action(self): # TODO: Test recursive (files and folders tree) move self.assertEqual(self.src_folder.files.count(), 1) self.assertEqual(self.dst_folder.files.count(), 0) url = reverse('admin:filer-directory_listing', kwargs={ 'folder_id': self.src_folder.id, }) response = self.client.post(url, { 'action': 'move_files_and_folders', 'post': 'yes', 'destination': self.dst_folder.id, helpers.ACTION_CHECKBOX_NAME: 'file-%d' % (self.image_obj.id,), }) self.assertEqual(self.src_folder.files.count(), 0) self.assertEqual(self.dst_folder.files.count(), 1) url = reverse('admin:filer-directory_listing', kwargs={ 'folder_id': self.dst_folder.id, }) response = self.client.post(url, { 'action': 'move_files_and_folders', 'post': 'yes', 'destination': self.src_folder.id, helpers.ACTION_CHECKBOX_NAME: 'file-%d' % (self.image_obj.id,), }) self.assertEqual(self.src_folder.files.count(), 1) self.assertEqual(self.dst_folder.files.count(), 0)
def test_files_set_public_action(self): self.image_obj.is_public = False self.image_obj.save() self.assertEqual(self.image_obj.is_public, False) url = reverse('admin:filer-directory_listing', kwargs={ 'folder_id': self.src_folder.id, }) response = self.client.post(url, { 'action': 'files_set_public', helpers.ACTION_CHECKBOX_NAME: 'file-%d' % (self.image_obj.id,), }) self.image_obj = Image.objects.get(id=self.image_obj.id) self.assertEqual(self.image_obj.is_public, True)
def test_files_set_private_action(self): self.image_obj.is_public = True self.image_obj.save() self.assertEqual(self.image_obj.is_public, True) url = reverse('admin:filer-directory_listing', kwargs={ 'folder_id': self.src_folder.id, }) response = self.client.post(url, { 'action': 'files_set_private', helpers.ACTION_CHECKBOX_NAME: 'file-%d' % (self.image_obj.id,), }) self.image_obj = Image.objects.get(id=self.image_obj.id) self.assertEqual(self.image_obj.is_public, False) self.image_obj.is_public = True self.image_obj.save()
def test_copy_files_and_folders_action(self): # TODO: Test recursive (files and folders tree) copy self.assertEqual(self.src_folder.files.count(), 1) self.assertEqual(self.dst_folder.files.count(), 0) self.assertEqual(self.image_obj.original_filename, 'test_file.jpg') url = reverse('admin:filer-directory_listing', kwargs={ 'folder_id': self.src_folder.id, }) response = self.client.post(url, { 'action': 'copy_files_and_folders', 'post': 'yes', 'suffix': 'test', 'destination': self.dst_folder.id, helpers.ACTION_CHECKBOX_NAME: 'file-%d' % (self.image_obj.id,), }) self.assertEqual(response.status_code, 302) # check if copying to the same folder gives 403 response = self.client.post(url, { 'action': 'copy_files_and_folders', 'post': 'yes', 'suffix': 'test', 'destination': self.src_folder.id, helpers.ACTION_CHECKBOX_NAME: 'file-%d' % (self.image_obj.id,), }) self.assertEqual(response.status_code, 403) self.assertEqual(self.src_folder.files.count(), 1) self.assertEqual(self.dst_folder.files.count(), 1) self.assertEqual(self.src_folder.files[0].id, self.image_obj.id) dst_image_obj = self.dst_folder.files[0] self.assertEqual(dst_image_obj.original_filename, 'test_filetest.jpg')
def _test_resize_image(self, crop, target_width, target_height, expected_width, expected_height, expected_subj_x, expected_subj_y): image_obj = self.create_image(self.src_folder) self.assertEqual(image_obj.width, 800) self.assertEqual(image_obj.height, 600) image_obj.subject_location = '100,200' image_obj.save() url = reverse('admin:filer-directory_listing', kwargs={ 'folder_id': self.src_folder.id, }) response = self.client.post(url, { 'action': 'resize_images', 'post': 'yes', 'width': target_width, 'height': target_height, 'crop': crop, 'upscale': False, helpers.ACTION_CHECKBOX_NAME: 'file-%d' % (image_obj.id,), }) self.assertEqual(response.status_code, 302) image_obj = Image.objects.get(id=image_obj.id) self.assertEqual(image_obj.width, expected_width) self.assertEqual(image_obj.height, expected_height) self.assertEqual( normalize_subject_location(image_obj.subject_location), (expected_subj_x, expected_subj_y))
def rename_files(self, request, files_queryset, folders_queryset): opts = self.model._meta app_label = opts.app_label current_folder = self._get_current_action_folder(request, files_queryset, folders_queryset) perms_needed = self._check_move_perms(request, files_queryset, folders_queryset) to_rename = self._list_all_to_copy_or_move(request, files_queryset, folders_queryset) if request.method == 'POST' and request.POST.get('post'): if perms_needed: raise PermissionDenied form = RenameFilesForm(request.POST) if form.is_valid(): if files_queryset.count() + folders_queryset.count(): n = self._rename_files_impl(files_queryset, folders_queryset, form.cleaned_data, 0) self.message_user(request, _("Successfully renamed %(count)d files.") % { "count": n, }) return None else: form = RenameFilesForm() context = admin_each_context(self.admin_site, request) context.update({ "title": _("Rename files"), "instance": current_folder, "breadcrumbs_action": _("Rename files"), "to_rename": to_rename, "rename_form": form, "files_queryset": files_queryset, "folders_queryset": folders_queryset, "perms_lacking": perms_needed, "opts": opts, "root_path": reverse('admin:index'), "app_label": app_label, "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME, }) # Display the rename format selection page return render(request, "admin/filer/folder/choose_rename_format.html", context)
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))
def _do_test_rename(self, url, new_name, file_obj=None, folder_obj=None): """ Helper to submit rename form and check renaming result. 'new_name' should be a plain string, no formatting supported. """ if file_obj is not None: checkbox_name = 'file-{}'.format(file_obj.id) files = [file_obj] elif folder_obj is not None: checkbox_name = 'folder-{}'.format(folder_obj.id) # files inside this folder, non-recursive files = File.objects.filter(folder=folder_obj) else: raise(ValueError('file_obj or folder_obj is required')) response = self.client.post(url, { 'action': 'rename_files', 'post': 'yes', 'rename_format': new_name, helpers.ACTION_CHECKBOX_NAME: checkbox_name, }) self.assertEquals(response.status_code, 302) for f in files: f = f._meta.model.objects.get(pk=f.pk) self.assertEqual(f.name, new_name)
def test_validate_no_duplicate_folders_on_move(self): """Create the following folder hierarchy: root | |--foo | |-bar | |--bar and try to move the owter bar in foo. This has to fail since it would result in two folders with the same name and parent. """ root = Folder.objects.create(name='root', owner=self.superuser) foo = Folder.objects.create(name='foo', parent=root, owner=self.superuser) bar = Folder.objects.create(name='bar', parent=root, owner=self.superuser) foos_bar = Folder.objects.create(name='bar', parent=foo, owner=self.superuser) url = reverse('admin:filer-directory_listing', kwargs={ 'folder_id': root.pk, }) response = self.client.post(url, { 'action': 'move_files_and_folders', 'post': 'yes', 'destination': foo.pk, helpers.ACTION_CHECKBOX_NAME: 'folder-%d' % (bar.pk,), }) # refresh from db and validate that it hasn't been moved bar = Folder.objects.get(pk=bar.pk) self.assertEqual(bar.parent.pk, root.pk) # TODO: Delete/refactor, deprecated since clipboard is deprecated # def test_move_to_clipboard_action(self): # # TODO: Test recursive (files and folders tree) move # # self.assertEqual(self.src_folder.files.count(), 1) # self.assertEqual(self.dst_folder.files.count(), 0) # url = reverse('admin:filer-directory_listing', kwargs={ # 'folder_id': self.src_folder.id, # }) # response = self.client.post(url, { # 'action': 'move_to_clipboard', # helpers.ACTION_CHECKBOX_NAME: 'file-%d' % (self.image_obj.id,), # }) # self.assertEqual(self.src_folder.files.count(), 0) # self.assertEqual(self.dst_folder.files.count(), 0) # clipboard = Clipboard.objects.get(user=self.superuser) # self.assertEqual(clipboard.files.count(), 1) # tools.move_files_from_clipboard_to_folder(clipboard, self.src_folder) # tools.discard_clipboard(clipboard) # self.assertEqual(clipboard.files.count(), 0) # self.assertEqual(self.src_folder.files.count(), 1)
def move_files_and_folders(self, request, files_queryset, folders_queryset): opts = self.model._meta app_label = opts.app_label current_folder = self._get_current_action_folder(request, files_queryset, folders_queryset) perms_needed = self._check_move_perms(request, files_queryset, folders_queryset) to_move = self._list_all_to_copy_or_move(request, files_queryset, folders_queryset) folders = self._list_all_destination_folders(request, folders_queryset, current_folder, False) if request.method == 'POST' and request.POST.get('post'): if perms_needed: raise PermissionDenied try: destination = Folder.objects.get(pk=request.POST.get('destination')) except Folder.DoesNotExist: raise PermissionDenied folders_dict = dict(folders) if destination not in folders_dict or not folders_dict[destination][1]: raise PermissionDenied # We count only topmost files and folders here n = files_queryset.count() + folders_queryset.count() conflicting_names = [folder.name for folder in Folder.objects.filter(parent=destination, name__in=folders_queryset.values('name'))] if conflicting_names: messages.error(request, _("Folders with names %s already exist at the selected " "destination") % ", ".join(conflicting_names)) elif n: self._move_files_and_folders_impl(files_queryset, folders_queryset, destination) self.message_user(request, _("Successfully moved %(count)d files and/or folders to folder '%(destination)s'.") % { "count": n, "destination": destination, }) return None context = admin_each_context(self.admin_site, request) context.update({ "title": _("Move files and/or folders"), "instance": current_folder, "breadcrumbs_action": _("Move files and/or folders"), "to_move": to_move, "destination_folders": folders, "files_queryset": files_queryset, "folders_queryset": folders_queryset, "perms_lacking": perms_needed, "opts": opts, "root_path": reverse('admin:index'), "app_label": app_label, "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME, }) # Display the destination folder selection page return render(request, "admin/filer/folder/choose_move_destination.html", context)
def resize_images(self, request, files_queryset, folders_queryset): opts = self.model._meta app_label = opts.app_label current_folder = self._get_current_action_folder(request, files_queryset, folders_queryset) perms_needed = self._check_resize_perms(request, files_queryset, folders_queryset) to_resize = self._list_all_to_resize(request, files_queryset, folders_queryset) if request.method == 'POST' and request.POST.get('post'): if perms_needed: raise PermissionDenied form = ResizeImagesForm(request.POST) if form.is_valid(): if form.cleaned_data.get('thumbnail_option'): form.cleaned_data['width'] = form.cleaned_data['thumbnail_option'].width form.cleaned_data['height'] = form.cleaned_data['thumbnail_option'].height form.cleaned_data['crop'] = form.cleaned_data['thumbnail_option'].crop form.cleaned_data['upscale'] = form.cleaned_data['thumbnail_option'].upscale if files_queryset.count() + folders_queryset.count(): # We count all files here (recursivelly) n = self._resize_images_impl(files_queryset, folders_queryset, form.cleaned_data) self.message_user(request, _("Successfully resized %(count)d images.") % {"count": n, }) return None else: form = ResizeImagesForm() context = admin_each_context(self.admin_site, request) context.update({ "title": _("Resize images"), "instance": current_folder, "breadcrumbs_action": _("Resize images"), "to_resize": to_resize, "resize_form": form, "cmsplugin_enabled": 'cmsplugin_filer_image' in django_settings.INSTALLED_APPS, "files_queryset": files_queryset, "folders_queryset": folders_queryset, "perms_lacking": perms_needed, "opts": opts, "root_path": reverse('admin:index'), "app_label": app_label, "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME, }) # Display the resize options page return render(request, "admin/filer/folder/choose_images_resize_options.html", context)