def get_or_create_collection(title, page_id, parent_page_id): if page_id: page = Page.objects.get(id=page_id) parent_page = page.get_parent() else: parent_page = Page.objects.get(id=parent_page_id) ancestors = parent_page.get_ancestors(inclusive=True) ancestor_titles = [] count = 0 for ancestor in ancestors: # Do nothing for count == 0, it's the root node, we don't need that if count == 1: ancestor_titles.append(ancestor.get_site().site_name) if count > 1: ancestor_titles.append(ancestor.title) count += 1 ancestor_titles.append(title) collection_name = ":".join(ancestor_titles) try: collection = Collection.objects.filter( name__exact=collection_name).get() except Collection.DoesNotExist: collection = Collection() collection.name = collection_name root = Collection.get_first_root_node() root.add_child(instance=collection) return collection
def test_post_with_collections(self): root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document", 'file': fake_file, 'collection': evil_plans_collection.id, } response = self.client.post(reverse('wagtaildocs:add'), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document should be created, and be placed in the Evil Plans collection self.assertTrue( models.Document.objects.filter(title="Test document").exists()) root_collection = Collection.get_first_root_node() self.assertEqual( models.Document.objects.get(title="Test document").collection, evil_plans_collection)
def test_post_with_collections(self): root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document", 'file': fake_file, 'collection': evil_plans_collection.id, } response = self.client.post(reverse('wagtaildocs:add'), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document should be created, and be placed in the Evil Plans collection self.assertTrue(models.Document.objects.filter(title="Test document").exists()) root_collection = Collection.get_first_root_node() self.assertEqual( models.Document.objects.get(title="Test document").collection, evil_plans_collection )
def test_add(self): response = self.post({ 'title': "Test image", 'file': SimpleUploadedFile('test.png', get_test_image_file().file.getvalue()), }) # Should redirect back to index self.assertRedirects(response, reverse('wagtailimages:index')) # Check that the image was created images = Image.objects.filter(title="Test image") self.assertEqual(images.count(), 1) # Test that size was populated correctly image = images.first() self.assertEqual(image.width, 640) self.assertEqual(image.height, 480) # Test that the file_size field was set self.assertTrue(image.file_size) # Test that it was placed in the root collection root_collection = Collection.get_first_root_node() self.assertEqual(image.collection, root_collection)
def setUp(self): # Create some user accounts for testing permissions User = get_user_model() self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.owner = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.editor = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.editor.groups.add(Group.objects.get(name='Editors')) self.administrator = User.objects.create_superuser( username='******', email='*****@*****.**', password='******' ) # Owner user must have the add_image permission image_adders_group = Group.objects.create(name="Image adders") GroupCollectionPermission.objects.create( group=image_adders_group, collection=Collection.get_first_root_node(), permission=Permission.objects.get(codename='add_image'), ) self.owner.groups.add(image_adders_group) # Create an image for running tests on self.image = Image.objects.create( title="Test image", uploaded_by_user=self.owner, file=get_test_image_file(), )
def test_pagination_preserves_other_params(self): root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") for i in range(1, 50): self.image = Image.objects.create(title="Test image %i" % i, file=get_test_image_file(), collection=evil_plans_collection) response = self.get({ 'collection_id': evil_plans_collection.id, 'p': 2 }) self.assertEqual(response.status_code, 200) response_body = response.content.decode('utf8') # prev link should exist and include collection_id self.assertTrue( ("?p=1&collection_id=%i" % evil_plans_collection.id) in response_body or ("?collection_id=%i&p=1" % evil_plans_collection.id) in response_body) # next link should exist and include collection_id self.assertTrue( ("?p=3&collection_id=%i" % evil_plans_collection.id) in response_body or ("?collection_id=%i&p=3" % evil_plans_collection.id) in response_body)
def setUp(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' self.root_collection = Collection.get_first_root_node() self.evil_plans_collection = self.root_collection.add_child(name="Evil plans") self.nice_plans_collection = self.root_collection.add_child(name="Nice plans") # Create a document to edit self.document = models.Document.objects.create( title="Test document", file=fake_file, collection=self.nice_plans_collection ) # Create a user with change_document permission but not add_document user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******' ) change_permission = Permission.objects.get( content_type__app_label='wagtaildocs', codename='change_document' ) admin_permission = Permission.objects.get( content_type__app_label='wagtailadmin', codename='access_admin' ) self.changers_group = Group.objects.create(name='Document changers') GroupCollectionPermission.objects.create( group=self.changers_group, collection=self.root_collection, permission=change_permission ) user.groups.add(self.changers_group) user.user_permissions.add(admin_permission) self.assertTrue(self.client.login(username='******', password='******'))
def setUp(self): add_doc_permission = Permission.objects.get( content_type__app_label='wagtaildocs', codename='add_document' ) admin_permission = Permission.objects.get( content_type__app_label='wagtailadmin', codename='access_admin' ) root_collection = Collection.get_first_root_node() self.evil_plans_collection = root_collection.add_child(name="Evil plans") conspirators_group = Group.objects.create(name="Evil conspirators") conspirators_group.permissions.add(admin_permission) GroupCollectionPermission.objects.create( group=conspirators_group, collection=self.evil_plans_collection, permission=add_doc_permission ) user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******' ) user.groups.add(conspirators_group) self.client.login(username='******', password='******')
def test_pagination_preserves_other_params(self): root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") for i in range(1, 50): self.image = Image.objects.create( title="Test image %i" % i, file=get_test_image_file(), collection=evil_plans_collection ) response = self.get({'collection_id': evil_plans_collection.id, 'p': 2}) self.assertEqual(response.status_code, 200) response_body = response.content.decode('utf8') # prev link should exist and include collection_id self.assertTrue( ("?p=1&collection_id=%i" % evil_plans_collection.id) in response_body or ("?collection_id=%i&p=1" % evil_plans_collection.id) in response_body ) # next link should exist and include collection_id self.assertTrue( ("?p=3&collection_id=%i" % evil_plans_collection.id) in response_body or ("?collection_id=%i&p=3" % evil_plans_collection.id) in response_body )
def get_collection_for_site(self, site): """ Args: site: site object Returns: Collection associated with given site """ site_collection_name = site.site_name collection = Collection.objects.filter( name=site_collection_name).first() if not collection: collection = Collection(name=site_collection_name) root_collection = Collection.get_first_root_node() root_collection.add_child(instance=collection) return collection
def setUp(self): # Create a group to edit self.test_group = Group.objects.create(name='test group') self.root_page = Page.objects.get(pk=1) self.root_add_permission = GroupPagePermission.objects.create( page=self.root_page, permission_type='add', group=self.test_group) self.home_page = Page.objects.get(pk=2) # Get the hook-registered permissions, and add one to this group self.registered_permissions = Permission.objects.none() for fn in hooks.get_hooks('register_permissions'): self.registered_permissions = self.registered_permissions | fn() self.existing_permission = self.registered_permissions.order_by( 'pk')[0] self.another_permission = self.registered_permissions.order_by('pk')[1] self.test_group.permissions.add(self.existing_permission) # set up collections to test document permissions self.root_collection = Collection.get_first_root_node() self.evil_plans_collection = self.root_collection.add_child( name="Evil plans") self.add_doc_permission = Permission.objects.get( content_type__app_label='wagtaildocs', codename='add_document') self.change_doc_permission = Permission.objects.get( content_type__app_label='wagtaildocs', codename='change_document') GroupCollectionPermission.objects.create( group=self.test_group, collection=self.evil_plans_collection, permission=self.add_doc_permission, ) # Login self.login()
def setUp(self): # Create an image to edit self.image = Image.objects.create( title="Test image", file=get_test_image_file(), ) # Create a user with change_image permission but not add_image user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******') change_permission = Permission.objects.get( content_type__app_label='wagtailimages', codename='change_image') admin_permission = Permission.objects.get( content_type__app_label='wagtailadmin', codename='access_admin') image_changers_group = Group.objects.create(name='Image changers') image_changers_group.permissions.add(admin_permission) GroupCollectionPermission.objects.create( group=image_changers_group, collection=Collection.get_first_root_node(), permission=change_permission) user.groups.add(image_changers_group) self.assertTrue( self.client.login(username='******', password='******'))
def test_post_video_with_collections(self): root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") # Build a fake file fake_file = ContentFile(b("A boring example movie")) fake_file.name = 'movie.mp3' # Submit post_data = { 'title': "Test media", 'file': fake_file, 'duration': 100, 'collection': evil_plans_collection.id, } response = self.client.post(reverse('wagtailmedia:add', args=('video', )), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtailmedia:index')) # Media should be created, and be placed in the Evil Plans collection self.assertTrue(models.Media.objects.filter(title="Test media").exists()) media = models.Media.objects.get(title="Test media") self.assertEqual(media.collection, evil_plans_collection) self.assertEqual(media.type, 'video')
def setUp(self): # Create a group to edit self.test_group = Group.objects.create(name='test group') self.root_page = Page.objects.get(id=1) self.root_add_permission = GroupPagePermission.objects.create(page=self.root_page, permission_type='add', group=self.test_group) self.home_page = Page.objects.get(id=2) # Get the hook-registered permissions, and add one to this group self.registered_permissions = Permission.objects.none() for fn in hooks.get_hooks('register_permissions'): self.registered_permissions = self.registered_permissions | fn() self.existing_permission = self.registered_permissions.order_by('pk')[0] self.another_permission = self.registered_permissions.order_by('pk')[1] self.test_group.permissions.add(self.existing_permission) # set up collections to test document permissions self.root_collection = Collection.get_first_root_node() self.evil_plans_collection = self.root_collection.add_child(name="Evil plans") self.add_doc_permission = Permission.objects.get( content_type__app_label='wagtaildocs', codename='add_document' ) self.change_doc_permission = Permission.objects.get( content_type__app_label='wagtaildocs', codename='change_document' ) GroupCollectionPermission.objects.create( group=self.test_group, collection=self.evil_plans_collection, permission=self.add_doc_permission, ) # Login self.login()
def setUp(self): # Create some user accounts for testing permissions User = get_user_model() self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.owner = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.editor = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.editor.groups.add(Group.objects.get(name='Editors')) self.administrator = User.objects.create_superuser( username='******', email='*****@*****.**', password='******') # Owner user must have the add_media permission self.adders_group = Group.objects.create(name='Media adders') GroupCollectionPermission.objects.create( group=self.adders_group, collection=Collection.get_first_root_node(), permission=Permission.objects.get(codename='add_media')) self.owner.groups.add(self.adders_group) # Create a media for running tests on self.media = models.Media.objects.create(title="Test media", duration=100, uploaded_by_user=self.owner)
def test_add(self): video_file = create_test_video_file() title = "Test Video" response = self.post({ 'title': title, 'file': SimpleUploadedFile('small.mp4', video_file.read(), "video/mp4"), }) # Should redirect back to index self.assertRedirects(response, reverse('wagtailvideos:index')) # Check that the video was created videos = Video.objects.filter(title=title) self.assertEqual(videos.count(), 1) # Test that extra fields were populated from post_save signal video = videos.first() self.assertTrue(video.thumbnail) self.assertTrue(video.duration) self.assertTrue(video.file_size) # Test that it was placed in the root collection root_collection = Collection.get_first_root_node() self.assertEqual(video.collection, root_collection)
def setUp(self): # Create some user accounts for testing permissions User = get_user_model() self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.owner = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.editor = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.editor.groups.add(Group.objects.get(name='Editors')) self.administrator = User.objects.create_superuser( username='******', email='*****@*****.**', password='******') # Owner user must have the add_image permission image_adders_group = Group.objects.create(name="Image adders") GroupCollectionPermission.objects.create( group=image_adders_group, collection=Collection.get_first_root_node(), permission=Permission.objects.get(codename='add_image'), ) self.owner.groups.add(image_adders_group) # Create an image for running tests on self.image = Image.objects.create( title="Test image", uploaded_by_user=self.owner, file=get_test_image_file(), )
def setUp(self): add_image_permission = Permission.objects.get( content_type__app_label='wagtailimages', codename='add_image' ) admin_permission = Permission.objects.get( content_type__app_label='wagtailadmin', codename='access_admin' ) root_collection = Collection.get_first_root_node() self.evil_plans_collection = root_collection.add_child(name="Evil plans") conspirators_group = Group.objects.create(name="Evil conspirators") conspirators_group.permissions.add(admin_permission) GroupCollectionPermission.objects.create( group=conspirators_group, collection=self.evil_plans_collection, permission=add_image_permission ) user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******' ) user.groups.add(conspirators_group) self.client.login(username='******', password='******')
def test_add_post_with_collections(self): """ This tests that a POST request to the add view saves the document and returns an edit form, when collections are active """ root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") response = self.client.post( reverse("wagtaildocs:add_multiple"), { "files[]": SimpleUploadedFile("test.png", b"Simple text document"), "collection": evil_plans_collection.id, }, HTTP_X_REQUESTED_WITH="XMLHttpRequest", ) # Check response self.assertEqual(response.status_code, 200) self.assertEqual(response["Content-Type"], "application/json") self.assertTemplateUsed(response, "wagtaildocs/multiple/edit_form.html") # Check document self.assertIn("doc", response.context) self.assertEqual(response.context["doc"].title, "test.png") self.assertTrue(response.context["doc"].file_size) # check that it is in the 'evil plans' collection doc = Document.objects.get(title="test.png") root_collection = Collection.get_first_root_node() self.assertEqual(doc.collection, evil_plans_collection) # Check form self.assertIn("form", response.context) self.assertEqual(response.context["form"].initial["title"], "test.png") # Check JSON response_json = json.loads(response.content.decode()) self.assertIn("doc_id", response_json) self.assertIn("form", response_json) self.assertIn("success", response_json) self.assertEqual(response_json["doc_id"], response.context["doc"].id) self.assertTrue(response_json["success"]) # form should contain a collection chooser self.assertIn("Collection", response_json["form"])
def test_add_post_with_collections(self): """ This tests that a POST request to the add view saves the document and returns an edit form, when collections are active """ root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") response = self.client.post( reverse('wagtaildocs:add_multiple'), { 'files[]': SimpleUploadedFile('test.png', b"Simple text document"), 'collection': evil_plans_collection.id }, HTTP_X_REQUESTED_WITH='XMLHttpRequest') # Check response self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/json') self.assertTemplateUsed(response, 'wagtaildocs/multiple/edit_form.html') # Check document self.assertIn('doc', response.context) self.assertEqual(response.context['doc'].title, 'test.png') self.assertTrue(response.context['doc'].file_size) # check that it is in the 'evil plans' collection doc = models.Document.objects.get(title='test.png') root_collection = Collection.get_first_root_node() self.assertEqual(doc.collection, evil_plans_collection) # Check form self.assertIn('form', response.context) self.assertEqual(response.context['form'].initial['title'], 'test.png') # Check JSON response_json = json.loads(response.content.decode()) self.assertIn('doc_id', response_json) self.assertIn('form', response_json) self.assertIn('success', response_json) self.assertEqual(response_json['doc_id'], response.context['doc'].id) self.assertTrue(response_json['success']) # form should contain a collection chooser self.assertIn('Collection', response_json['form'])
def setUp(self): self.root_collection = Collection.get_first_root_node() self.holiday_photos_collection = self.root_collection.add_child( name="Holiday photos" ) self.evil_plans_collection = self.root_collection.add_child( name="Evil plans" )
def dummy_wagtail_doc(request): if not Collection.objects.exists(): # pragma: no cover Collection.add_root() doc = Document(title='hello') doc.file.save('foo.txt', ContentFile('foo', 'foo.txt')) doc.save() doc = Document.objects.get(pk=doc.pk) # Reload to ensure the upload took def nuke(): try: # Try cleaning up so `/var/media` isn't full of foo doc.file.delete() doc.delete() except: # pragma: no cover pass request.addfinalizer(nuke) return doc
def test_get_with_collections(self): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Evil plans") response = self.get() self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "wagtailimages/images/add.html") self.assertContains(response, '<label for="id_collection">') self.assertContains(response, "Evil plans")
def test_add_post_with_collections(self): """ This tests that a POST request to the add view saves the document and returns an edit form, when collections are active """ root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") response = self.client.post(reverse('wagtaildocs:add_multiple'), { 'files[]': SimpleUploadedFile('test.png', b"Simple text document"), 'collection': evil_plans_collection.id }, HTTP_X_REQUESTED_WITH='XMLHttpRequest') # Check response self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/json') self.assertTemplateUsed(response, 'wagtaildocs/multiple/edit_form.html') # Check document self.assertIn('doc', response.context) self.assertEqual(response.context['doc'].title, 'test.png') self.assertTrue(response.context['doc'].file_size) # check that it is in the 'evil plans' collection doc = Document.objects.get(title='test.png') root_collection = Collection.get_first_root_node() self.assertEqual(doc.collection, evil_plans_collection) # Check form self.assertIn('form', response.context) self.assertEqual(response.context['form'].initial['title'], 'test.png') # Check JSON response_json = json.loads(response.content.decode()) self.assertIn('doc_id', response_json) self.assertIn('form', response_json) self.assertIn('success', response_json) self.assertEqual(response_json['doc_id'], response.context['doc'].id) self.assertTrue(response_json['success']) # form should contain a collection chooser self.assertIn('Collection', response_json['form'])
def test_get_with_collections(self): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Evil plans") response = self.client.get(reverse('wagtaildocs:add')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtaildocs/documents/add.html') self.assertContains(response, '<label for="id_collection">') self.assertContains(response, "Evil plans")
def test_get_with_collections(self): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Evil plans") response = self.get() self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailimages/images/add.html') self.assertContains(response, '<label for="id_collection">') self.assertContains(response, "Evil plans")
class GalleryPage(Page): # test root_coll = Collection.get_first_root_node() root_coll.add_child(name='testcoll') collection_name = models.CharField(max_length=250) image_dir = models.ForeignKey( 'wagtaildocs.Document', null=True, blank=True, on_delete=models.SET_NULL, related_name='+' ) # how to point to a directory? # how to include a button??? content_panels = Page.content_panels + [ FieldPanel('collection_name'), DocumentChooserPanel('image_dir'), Button(_on_button_press) # ??? ] def _on_button_press(self): self.imgs = self.get_image_files() self.create_collection() self.save_images_to_cms() def create_collection(self): root_coll = Collection.get_first_root_node() root_coll.add_child(name=self.collection_name) def get_image_files(self, exts=['.jpg', '.jpeg', '.JPG', '.JPEG', '.png', '.PNG']): '''Find images within the img_dir location.''' imgs = [] if not os.isdir(self.img_dir): # raise exception ?? return imgs for root, dirs, files in os.walk(self.img_dir): for fil in files: if os.path.splitext(fil)[1] in exts: img_path = os.path.abspath(os.path.join(root, fil)) dir_name = os.path.split(os.path.dirname(img_path))[1] # tag img_name = os.path.splitext(os.path.basename(img_path))[0] # title imgs += [(img_path, dir_name, img_name)] return imgs def save_images_to_cms(self): '''Save images to the database with: - title: the file name - tags: the directory containing the image''' for img_path, dirname, img_name in self.imgs: image = Image(title=img_name, file=ImageFile(open(img_path, "rb"), name=os.path.basename(img_path)), tags=img_name) # is this correct?? image.save()
def test_get_with_collections(self): root_collection = Collection.get_first_root_node() collection_name = "Takeflight manifesto" root_collection.add_child(name=collection_name) response = self.get() self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailvideos/videos/add.html') self.assertContains(response, '<label for="id_collection">') self.assertContains(response, collection_name)
def test_add_with_collections(self): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Evil plans") # Send request response = self.client.get(reverse('wagtaildocs:add_multiple')) # Check response self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtaildocs/multiple/add.html') # collection chooser should exisst self.assertContains(response, '<label for="id_adddocument_collection">') self.assertContains(response, 'Evil plans')
def test_post(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = "test.txt" # Submit post_data = {"title": "Test document", "file": fake_file} response = self.client.post(reverse("wagtaildocs:add"), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse("wagtaildocs:index")) # Document should be created, and be placed in the root collection self.assertTrue(models.Document.objects.filter(title="Test document").exists()) root_collection = Collection.get_first_root_node() self.assertEqual(models.Document.objects.get(title="Test document").collection, root_collection)
def test_post(self): response = self.post({ 'name': "Holiday snaps", }) # Should redirect back to index self.assertRedirects(response, reverse('wagtailadmin_collections:index')) # Check that the collection was created and is a child of root self.assertEqual(Collection.objects.filter(name="Holiday snaps").count(), 1) root_collection = Collection.get_first_root_node() self.assertEqual( Collection.objects.get(name="Holiday snaps").get_parent(), root_collection )
def test_post(self): response = self.post({ 'name': "Holiday snaps", }) # Should redirect back to index self.assertRedirects(response, reverse('wagtailadmin_collections:index')) # Check that the collection was created and is a child of root self.assertEqual( Collection.objects.filter(name="Holiday snaps").count(), 1) root_collection = Collection.get_first_root_node() self.assertEqual( Collection.objects.get(name="Holiday snaps").get_parent(), root_collection)
def test_as_ordinary_editor(self): user = get_user_model().objects.create_user(username="******", email="*****@*****.**", password="******") add_permission = Permission.objects.get(content_type__app_label="wagtailimages", codename="add_image") admin_permission = Permission.objects.get(content_type__app_label="wagtailadmin", codename="access_admin") image_adders_group = Group.objects.create(name="Image adders") image_adders_group.permissions.add(admin_permission) GroupCollectionPermission.objects.create( group=image_adders_group, collection=Collection.get_first_root_node(), permission=add_permission ) user.groups.add(image_adders_group) self.client.login(username="******", password="******") response = self.client.get(reverse("wagtailimages:add_multiple")) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "wagtailimages/multiple/add.html")
def post(self, request, *args, **kwargs): file = request.FILES.get('file') # get the related page page_pk = kwargs.get('page_pk') module_id = kwargs.get('module_id') # Get page try: page = Page.objects.get(pk=page_pk).specific except Page.DoesNotExist: return HttpResponse(status=404) # Check permissions if page.permissions_for_user(request.user).can_edit() is False: return HttpResponse(status=401) # Get or create collection try: collection = Collection.objects.get(name=self.dsf_collection_name) except Collection.DoesNotExist: root_collection = Collection.get_first_root_node() collection = root_collection.add_child( name=self.dsf_collection_name) # Add the document to the upload block. As there are multiple upload # blocks on the same page, the correct one needs to be found. # The stream_data may be nested in multiple layouts. data = page.content.stream_data page_element = self.iterate_block(*data) if not page_element: return HttpResponse(status=404) self.append_document_to_module(item=page_element, module_id=module_id, page=page, file=file, collection=collection, user=request.user) # Save the new data data_json = json.dumps(data) page.content = data_json page.save() return HttpResponse('file uploaded')
def test_simple(self): response = self.get() self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailadmin/collections/index.html') # Initially there should be no collections listed # (Root should not be shown) self.assertContains(response, "No collections have been created.") root_collection = Collection.get_first_root_node() self.collection = root_collection.add_child(name="Holiday snaps") # Now the listing should contain our collection response = self.get() self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailadmin/collections/index.html') self.assertNotContains(response, "No collections have been created.") self.assertContains(response, "Holiday snaps")
def test_get_video_with_collections(self): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Evil plans") response = self.client.get(reverse('wagtailmedia:add', args=('video', ))) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailmedia/media/add.html') self.assertContains(response, '<label for="id_collection">') self.assertContains(response, "Evil plans") self.assertContains(response, 'Add video') self.assertContains( response, '<form action="{0}" method="POST" enctype="multipart/form-data">'.format( reverse('wagtailmedia:add', args=('video',)) ), count=1 )
def setUp(self): add_image_permission = Permission.objects.get(content_type__app_label="wagtailimages", codename="add_image") admin_permission = Permission.objects.get(content_type__app_label="wagtailadmin", codename="access_admin") root_collection = Collection.get_first_root_node() self.evil_plans_collection = root_collection.add_child(name="Evil plans") conspirators_group = Group.objects.create(name="Evil conspirators") conspirators_group.permissions.add(admin_permission) GroupCollectionPermission.objects.create( group=conspirators_group, collection=self.evil_plans_collection, permission=add_image_permission ) user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password="******" ) user.groups.add(conspirators_group) self.client.login(username="******", password="******")
def setUp(self): # Create an image to edit self.image = Image.objects.create(title="Test image", file=get_test_image_file()) # Create a user with change_image permission but not add_image user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password="******" ) change_permission = Permission.objects.get(content_type__app_label="wagtailimages", codename="change_image") admin_permission = Permission.objects.get(content_type__app_label="wagtailadmin", codename="access_admin") image_changers_group = Group.objects.create(name="Image changers") image_changers_group.permissions.add(admin_permission) GroupCollectionPermission.objects.create( group=image_changers_group, collection=Collection.get_first_root_node(), permission=change_permission ) user.groups.add(image_changers_group) self.assertTrue(self.client.login(username="******", password="******"))
def test_group_create_adding_permissions(self): response = self.post( { "name": "test group", "page_permissions-0-page": ["1"], "page_permissions-0-permission_types": ["edit", "publish"], "page_permissions-TOTAL_FORMS": ["1"], "document_permissions-0-collection": [Collection.get_first_root_node().id], "document_permissions-0-permissions": [self.add_doc_permission.id], "document_permissions-TOTAL_FORMS": ["1"], } ) self.assertRedirects(response, reverse("wagtailusers_groups:index")) # The test group now exists, with two page permissions # and one 'add document' collection permission new_group = Group.objects.get(name="test group") self.assertEqual(new_group.page_permissions.all().count(), 2) self.assertEqual(new_group.collection_permissions.filter(permission=self.add_doc_permission).count(), 1)
def test_add_with_collections(self): root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") response = self.post({ 'title': "Test image", 'file': SimpleUploadedFile('test.png', get_test_image_file().file.getvalue()), 'collection': evil_plans_collection.id, }) # Should redirect back to index self.assertRedirects(response, reverse('wagtailimages:index')) # Check that the image was created images = Image.objects.filter(title="Test image") self.assertEqual(images.count(), 1) # Test that it was placed in the Evil Plans collection image = images.first() self.assertEqual(image.collection, evil_plans_collection)
def test_group_create_adding_permissions(self): response = self.post({ 'name': "test group", 'page_permissions-0-page': ['1'], 'page_permissions-0-permission_types': ['edit', 'publish'], 'page_permissions-TOTAL_FORMS': ['1'], 'document_permissions-0-collection': [Collection.get_first_root_node().id], 'document_permissions-0-permissions': [self.add_doc_permission.id], 'document_permissions-TOTAL_FORMS': ['1'], }) self.assertRedirects(response, reverse('wagtailusers_groups:index')) # The test group now exists, with two page permissions # and one 'add document' collection permission new_group = Group.objects.get(name='test group') self.assertEqual(new_group.page_permissions.all().count(), 2) self.assertEqual( new_group.collection_permissions.filter(permission=self.add_doc_permission).count(), 1 )
def test_group_create_adding_permissions(self): response = self.post({ 'name': "test group", 'page_permissions-0-page': ['1'], 'page_permissions-0-permission_types': ['edit', 'publish'], 'page_permissions-TOTAL_FORMS': ['1'], 'document_permissions-0-collection': [Collection.get_first_root_node().pk], 'document_permissions-0-permissions': [self.add_doc_permission.pk], 'document_permissions-TOTAL_FORMS': ['1'], }) self.assertRedirects(response, reverse('wagtailusers_groups:index')) # The test group now exists, with two page permissions # and one 'add document' collection permission new_group = Group.objects.get(name='test group') self.assertEqual(new_group.page_permissions.all().count(), 2) self.assertEqual( new_group.collection_permissions.filter(permission=self.add_doc_permission).count(), 1 )
def test_duplicate_document_permissions_error(self): # Try to submit multiple document permission entries for the same collection root_collection = Collection.get_first_root_node() response = self.post({ 'name': "test group", 'document_permissions-0-collection': [root_collection.id], 'document_permissions-0-permissions': [self.add_doc_permission.id], 'document_permissions-1-collection': [root_collection.id], 'document_permissions-1-permissions': [self.change_doc_permission.id], 'document_permissions-TOTAL_FORMS': ['2'], }) self.assertEqual(response.status_code, 200) # formset should have a non-form error about the duplication # (we don't know what index in permission_panels the formset will be, # so just assert that it happens on at least one permission_panel) self.assertTrue( any( hasattr(panel, 'non_form_errors') and panel.non_form_errors for panel in response.context['permission_panels'] ) )
def test_as_ordinary_editor(self): user = get_user_model().objects.create_user(username='******', email='*****@*****.**', password='******') add_permission = Permission.objects.get( content_type__app_label='wagtailimages', codename='add_image') admin_permission = Permission.objects.get( content_type__app_label='wagtailadmin', codename='access_admin') image_adders_group = Group.objects.create(name='Image adders') image_adders_group.permissions.add(admin_permission) GroupCollectionPermission.objects.create( group=image_adders_group, collection=Collection.get_first_root_node(), permission=add_permission) user.groups.add(image_adders_group) self.client.login(username='******', password='******') response = self.client.get(reverse('wagtailimages:add_multiple')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailimages/multiple/add.html')
def setUp(self): # Create some user accounts for testing permissions User = get_user_model() self.user = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.owner = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.editor = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.editor.groups.add(Group.objects.get(name="Editors")) self.administrator = User.objects.create_superuser( username="******", email="*****@*****.**", password="******" ) # Owner user must have the add_document permission self.adders_group = Group.objects.create(name="Document adders") GroupCollectionPermission.objects.create( group=self.adders_group, collection=Collection.get_first_root_node(), permission=Permission.objects.get(codename="add_document"), ) self.owner.groups.add(self.adders_group) # Create a document for running tests on self.document = models.Document.objects.create(title="Test document", uploaded_by_user=self.owner)
def test_duplicate_document_permissions_error(self): # Try to submit multiple document permission entries for the same collection root_collection = Collection.get_first_root_node() response = self.post({ 'name': "test group", 'document_permissions-0-collection': [root_collection.pk], 'document_permissions-0-permissions': [self.add_doc_permission.pk], 'document_permissions-1-collection': [root_collection.pk], 'document_permissions-1-permissions': [self.change_doc_permission.pk], 'document_permissions-TOTAL_FORMS': ['2'], }) self.assertEqual(response.status_code, 200) # formset should have a non-form error about the duplication # (we don't know what index in permission_panels the formset will be, # so just assert that it happens on at least one permission_panel) self.assertTrue( any( hasattr(panel, 'non_form_errors') and panel.non_form_errors for panel in response.context['permission_panels']))
def test_post_audio(self): # Build a fake file fake_file = ContentFile(b("A boring example song")) fake_file.name = 'song.mp3' # Submit post_data = { 'title': "Test media", 'file': fake_file, 'duration': 100, } response = self.client.post(reverse('wagtailmedia:add', args=('audio', )), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtailmedia:index')) # Media should be created, and be placed in the root collection self.assertTrue(models.Media.objects.filter(title="Test media").exists()) root_collection = Collection.get_first_root_node() media = models.Media.objects.get(title="Test media") self.assertEqual(media.collection, root_collection) self.assertEqual(media.type, 'audio')
def test_add(self): response = self.post( {"title": "Test image", "file": SimpleUploadedFile("test.png", get_test_image_file().file.getvalue())} ) # Should redirect back to index self.assertRedirects(response, reverse("wagtailimages:index")) # Check that the image was created images = Image.objects.filter(title="Test image") self.assertEqual(images.count(), 1) # Test that size was populated correctly image = images.first() self.assertEqual(image.width, 640) self.assertEqual(image.height, 480) # Test that the file_size field was set self.assertTrue(image.file_size) # Test that it was placed in the root collection root_collection = Collection.get_first_root_node() self.assertEqual(image.collection, root_collection)
def setUp(self): # Build a fake file fake_file = ContentFile(b("A boring example song")) fake_file.name = 'song.mp3' self.root_collection = Collection.get_first_root_node() self.evil_plans_collection = self.root_collection.add_child( name="Evil plans") self.nice_plans_collection = self.root_collection.add_child( name="Nice plans") # Create a media to edit self.media = models.Media.objects.create( title="Test media", file=fake_file, collection=self.nice_plans_collection, duration=100) # Create a user with change_media permission but not add_media user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******') change_permission = Permission.objects.get( content_type__app_label='wagtailmedia', codename='change_media') admin_permission = Permission.objects.get( content_type__app_label='wagtailadmin', codename='access_admin') self.changers_group = Group.objects.create(name='Media changers') GroupCollectionPermission.objects.create( group=self.changers_group, collection=self.root_collection, permission=change_permission) user.groups.add(self.changers_group) user.user_permissions.add(admin_permission) self.assertTrue( self.client.login(username='******', password='******'))
def setUp(self): # Permissions document_content_type = ContentType.objects.get_for_model(Document) add_doc_permission = Permission.objects.get( content_type=document_content_type, codename='add_document' ) change_doc_permission = Permission.objects.get( content_type=document_content_type, codename='change_document' ) # Collections self.root_collection = Collection.get_first_root_node() self.reports_collection = self.root_collection.add_child(name="Reports") # Groups doc_changers_group = Group.objects.create(name="Document changers") GroupCollectionPermission.objects.create( group=doc_changers_group, collection=self.root_collection, permission=change_doc_permission ) report_changers_group = Group.objects.create(name="Report changers") GroupCollectionPermission.objects.create( group=report_changers_group, collection=self.reports_collection, permission=change_doc_permission ) report_adders_group = Group.objects.create(name="Report adders") GroupCollectionPermission.objects.create( group=report_adders_group, collection=self.reports_collection, permission=add_doc_permission ) # Users User = get_user_model() self.superuser = User.objects.create_superuser( 'superuser', '*****@*****.**', 'password' ) self.inactive_superuser = User.objects.create_superuser( 'inactivesuperuser', '*****@*****.**', 'password', is_active=False ) # a user with change_document permission through the 'Document changers' group self.doc_changer = User.objects.create_user( 'docchanger', '*****@*****.**', 'password' ) self.doc_changer.groups.add(doc_changers_group) # a user that has change_document permission, but is inactive self.inactive_doc_changer = User.objects.create_user( 'inactivedocchanger', '*****@*****.**', 'password', is_active=False ) self.inactive_doc_changer.groups.add(doc_changers_group) # a user with change_document permission on reports via the report_changers group self.report_changer = User.objects.create_user( 'reportchanger', '*****@*****.**', 'password' ) self.report_changer.groups.add(report_changers_group) # a user with add_document permission on reports via the report_adders group self.report_adder = User.objects.create_user( 'reportadder', '*****@*****.**', 'password' ) self.report_adder.groups.add(report_adders_group) # a user with no permissions self.useless_user = User.objects.create_user( 'uselessuser', '*****@*****.**', 'password' ) self.anonymous_user = AnonymousUser() # Documents # a document in the root owned by 'reportchanger' self.changer_doc = Document.objects.create( title="reportchanger's document", collection=self.root_collection, uploaded_by_user=self.report_changer ) # a document in reports owned by 'reportchanger' self.changer_report = Document.objects.create( title="reportchanger's report", collection=self.reports_collection, uploaded_by_user=self.report_changer ) # a document in reports owned by 'reportadder' self.adder_report = Document.objects.create( title="reportadder's report", collection=self.reports_collection, uploaded_by_user=self.report_adder ) # a document in reports owned by 'uselessuser' self.useless_report = Document.objects.create( title="uselessuser's report", collection=self.reports_collection, uploaded_by_user=self.useless_user ) # a document with no owner self.anonymous_report = Document.objects.create( title="anonymous report", collection=self.reports_collection )
def save_instance(self): # Always create new collections as children of root instance = self.form.save(commit=False) root_collection = Collection.get_first_root_node() root_collection.add_child(instance=instance) return instance
def get_queryset(self): # Only return children of the root node, so that the root is not editable return Collection.get_first_root_node().get_children()
def setUp(self): self.login() self.root_collection = Collection.get_first_root_node() self.collection = self.root_collection.add_child(name="Holiday snaps")