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_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) self.assertTrue(response.context['doc'].file_hash) # check that it is in the 'evil plans' collection doc = models.get_document_model().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( set(response.context['form'].fields), set(models.get_document_model().admin_form_fields) - {'file'} | {'collection'}, ) 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_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): # 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_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_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 document = models.Document.objects.get(title="Test document") root_collection = Collection.get_first_root_node() self.assertEqual( document.collection, root_collection ) # Check that the file_size field was set self.assertTrue(document.file_size)
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): # 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): 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 test_ordering(self): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Milk") root_collection.add_child(name="Bread") root_collection.add_child(name="Avacado") response = self.get() self.assertEqual( [collection.name for collection in response.context['object_list']], ['Avacado', 'Bread', 'Milk'])
def test_index_with_collection(self): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Evil plans") self.make_docs() response = self.client.get(reverse('wagtaildocs:index')) self.assertContains(response, '<th>Collection</th>') self.assertContains(response, '<td>Root</td>')
def receiver(sender, **kwargs): instance = kwargs['instance'] # Get voting group_id, it will automatically create the new group _ = instance.voting_group_id() # Create a collection if Collection.objects.filter(name=instance.title).count() == 0: root_coll = Collection.get_first_root_node() root_coll.add_child(name=instance.title) root_coll.save()
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") # self.holiday_photos_collection's path has been updated out from under it by the addition of a sibling with # an alphabetically earlier name (due to Collection.node_order_by = ['name']), so we need to refresh it from # the DB to get the new path. self.holiday_photos_collection.refresh_from_db()
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_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_ordering(self): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Milk") root_collection.add_child(name="Bread") root_collection.add_child(name="Avocado") response = self.get() # Note that the Collections have been automatically sorted by name. self.assertEqual([ collection.name for collection in response.context['object_list'] ], ['Avocado', 'Bread', 'Milk'])
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_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 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 setUp(self): self.user = self.login() self.root_collection = Collection.get_first_root_node() self.dest_collection = self.root_collection.add_child(name="Destination") self.documents = [ Document.objects.create(title=f"Test document - {i}") for i in range(1, 6) ] self.url = reverse('wagtail_bulk_action', args=('wagtaildocs', 'document', 'add_to_collection',)) + '?' for document in self.documents: self.url += f'id={document.id}&' self.post_data = {'collection': str(self.dest_collection.id)}
def __init__(self, collection_name=''): if collection_name: try: col = Collection.objects.get(name=collection_name) except Collection.DoesNotExist: root_col = Collection.get_first_root_node() col = root_col.add_child(name=collection_name) self.collection = col else: self.collection = None self.images = {}
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 create_collections(apps, schema_editor): names = [ 'team videos', 'varied videos', 'team images', 'varied images', 'team documents', 'varied documents', 'logos', 'landing page', 'profile photos', 'feed images' ] # Get collection's root root_collection = Collection.get_first_root_node() for name in names: root_collection.add_child(name=name)
def create_og_image(request, page, browser_output=False, extra_data={}): # Set up canvas and drawable canvas = Image.new('RGB', (OG_WIDTH, OG_HEIGHT)) drawable = ImageDraw.Draw(canvas) # Delegate creation to a custom hook function if there is one registered custom_hook = hooks.get_hooks( 'wagtail_opengraph_image_generator_generation') if custom_hook: canvas = custom_hook[-1](request, page, browser_output, extra_data, canvas, drawable) else: canvas = create_default_og_image(request, page, browser_output, extra_data, canvas, drawable) # Create image # Append random string to avoid S3 CDN cache issues og_file_name = 'og_{}_{}.png'.format( page.slug if page else 'preview', urlsafe_b64encode(os.urandom(6)).decode('utf-8'), ) buf = BytesIO() canvas.save(buf, format='PNG') buf.seek(0) if browser_output: return buf else: # Get correct Collection or create if it doesn't exist try: collection = Collection.objects.get( name=setting('COLLECTION_NAME')) except Collection.DoesNotExist: collection = Collection(name=setting('COLLECTION_NAME')) root_collection = Collection.get_first_root_node() root_collection.add_child(instance=collection) # Clear other/older versions of the page's generated OG images WagtailImage.objects.filter(title=og_file_name, collection=collection).delete() # Convert Django image to Wagtail image django_image = InMemoryUploadedFile(buf, 'open_graph_image', og_file_name, 'image/png', buf.tell(), None) wagtail_image = WagtailImage(title=og_file_name, file=django_image, collection=collection) wagtail_image.save() # Return new OG image return wagtail_image
def test_existing_collection(self): new_collection_name = 'first collection' root_collection = Collection.get_first_root_node() new_collection = root_collection.add_child(name=new_collection_name) total_images_in_new_collection = Image.objects.filter( collection=new_collection).count() self.assertEqual(total_images_in_new_collection, 0) new_image = create_wagtail_image(self.image_path, image_name='fake teddy.jpg', collection_name=new_collection_name) self.assertEqual(new_image.collection.name, new_collection_name)
def setUp(self): # Create S3 bucket bucket_name = settings.AWS_STORAGE_BUCKET_NAME conn = boto3.resource('s3', region_name='eu-west-1') conn.create_bucket(Bucket=bucket_name) self.client = Client() self.root_collection = Collection.get_first_root_node() self.private_collection = self.root_collection.add_child( name='Restricted collection', ) self.private_collection_restriction = CollectionViewRestrictionFactory( collection=self.private_collection) # noqa self.view_restriction_session_key = self.private_collection_restriction.passed_view_restrictions_session_key # noqa
def test_index_with_collection(self): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Evil plans") root_collection.add_child(name="Good plans") self.make_docs() response = self.client.get(reverse('wagtaildocs:index')) self.assertContains(response, '<th>Collection</th>') self.assertContains(response, '<td>Root</td>') self.assertEqual( [collection.name for collection in response.context['collections']], ['Root', 'Evil plans', 'Good plans'])
def test_index_with_collection(self): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Evil plans") root_collection.add_child(name="Good plans") self.make_docs() response = self.client.get(reverse('wagtaildocs:index')) self.assertContains(response, '<th>Collection</th>') self.assertContains(response, '<td>Root</td>') self.assertEqual([ collection.name for collection in response.context['collections'] ], ['Root', 'Evil plans', 'Good plans'])
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_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): 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 __init__(self, *args, **kwargs): kwargs.pop("user") super(forms.ModelForm, self).__init__(*args, **kwargs) # Get or initiate the Public uploads collection. try: public_collection = Collection.objects.get( name=NON_ADMIN_DRAFTAIL_PUBLIC_COLLECTION_NAME ) except Collection.DoesNotExist: root_coll = Collection.get_first_root_node() root_coll.add_child(name=NON_ADMIN_DRAFTAIL_PUBLIC_COLLECTION_NAME) public_collection = Collection.objects.get( name=NON_ADMIN_DRAFTAIL_PUBLIC_COLLECTION_NAME ) self.collections = [public_collection]
def ensure(self, name: str) -> Collection: """Ensures that a collection called ``name`` exists, and returns that collection. Args: name (str): The name of the collection you want """ try: coll = Collection.objects.get(name=name) except Collection.DoesNotExist: try: root_collection = Collection.get_first_root_node() coll = root_collection.add_child(name=name) except Exception: raise return coll
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" novalidate>' .format(reverse('wagtailmedia:add', args=('video', ))), count=1)
def test_add_post(self): """ This tests that a POST request to the add view saves the document and returns an edit form """ response = self.client.post(reverse('wagtaildocs:add_multiple'), { 'files[]': SimpleUploadedFile('test.png', b"Simple text document"), }, 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) self.assertTrue(response.context['doc'].file_hash) # check that it is in the root collection doc = get_document_model().objects.get(title='test.png') root_collection = Collection.get_first_root_node() self.assertEqual(doc.collection, root_collection) # Check form self.assertIn('form', response.context) self.assertEqual( set(response.context['form'].fields), set(get_document_model().admin_form_fields) - {'file', 'collection'}, ) 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 not contain a collection chooser self.assertNotIn('Collection', response_json['form'])
def handle(self, *args, **options): all_products = ProductPage.objects.all() total_products = all_products.count() try: pni_collection = Collection.objects.get(name="PNI Images") except Collection.DoesNotExist: root_collection = Collection.get_first_root_node() pni_collection = root_collection.add_child(name="PNI Images") for index, product in enumerate(all_products): print(f"Processing product {index+1} of {total_products}") if product.cloudinary_image: mime = MimeTypes() mime_type = mime.guess_type( product.cloudinary_image.url) # -> ('image/jpeg', None) if mime_type: mime_type = mime_type[0].split('/')[1].upper() else: # Default to a JPEG mimetype. mime_type = 'JPEG' # Temporarily download the image response = requests.get(product.cloudinary_image.url, stream=True) if response.status_code == 200: # Create an image out of the Cloudinary URL and write it to a PIL Image. pil_image = PILImage.open(response.raw) f = BytesIO() pil_image.save(f, mime_type) # Get the file name in a nice way. new_image_name = ntpath.basename( product.cloudinary_image.url) # Store the image as a WagtailImage object wagtail_image = WagtailImage.objects.create( title=new_image_name, file=ImageFile(f, name=new_image_name), collection=pni_collection) # Associate product.image with wagtail_image product.image = wagtail_image # Always generate a new revision. revision = product.save_revision() if product.live: # Re-publish existing "live" pages from the latest revision revision.publish()
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_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" novalidate>'.format( reverse('wagtailmedia:add', args=('video',)) ), count=1 )
def setUp(self): self.user = self.login() self.root_collection = Collection.get_first_root_node() self.dest_collection = self.root_collection.add_child( name="Destination") self.images = [ Image.objects.create(title=f"Test image - {i}", file=test_file) for i in range(1, 6) ] self.url = reverse('wagtail_bulk_action', args=( 'wagtailimages', 'image', 'add_to_collection', )) + '?' for image in self.images: self.url += f'id={image.id}&' self.post_data = {'collection': str(self.dest_collection.id)}
def test_nested_ordering(self): root_collection = Collection.get_first_root_node() vegetables = root_collection.add_child(name="Vegetable") vegetables.add_child(name="Spinach") vegetables.add_child(name="Cucumber") animals = root_collection.add_child(name="Animal") animals.add_child(name="Dog") animals.add_child(name="Cat") response = self.get() # Note that while we added the collections at level 1 in reverse-alpha order, they come back out in alpha order. # And we added the Collections at level 2 in reverse-alpha order as well, but they were also alphabetized # within their respective trees. This is the result of setting Collection.node_order_by = ['name']. self.assertEqual([ collection.name for collection in response.context['object_list'] ], ['Animal', 'Cat', 'Dog', 'Vegetable', 'Cucumber', 'Spinach'])
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 video", 'file': SimpleUploadedFile('small.mp4', create_test_video_file().read(), "video/mp4"), 'collection': evil_plans_collection.id, }) # Should redirect back to index self.assertRedirects(response, reverse('wagtailvideos:index')) # Check that the video was created videos = Video.objects.filter(title="Test video") self.assertEqual(videos.count(), 1) # Test that it was placed in the Evil Plans collection video = videos.first() self.assertEqual(video.collection, evil_plans_collection)
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_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_post_audio(self): # Build a fake file fake_file = ContentFile(b("A boring example song"), 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 setUp(self): self.user = self.login() self.root_collection = Collection.get_first_root_node() self.dest_collection = self.root_collection.add_child(name="Destination") self.documents = [ Document.objects.create(title=f"Test document - {i}") for i in range(1, 6) ] self.url = ( reverse( "wagtail_bulk_action", args=( "wagtaildocs", "document", "add_to_collection", ), ) + "?" ) for document in self.documents: self.url += f"id={document.id}&" self.post_data = {"collection": str(self.dest_collection.id)}
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 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_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_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_post(self): """ This tests that a POST request to the add view saves the document and returns an edit form """ response = self.client.post(reverse('wagtaildocs:add_multiple'), { 'files[]': SimpleUploadedFile('test.png', b"Simple text document"), }, 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 root collection doc = models.Document.objects.get(title='test.png') root_collection = Collection.get_first_root_node() self.assertEqual(doc.collection, root_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 not contain a collection chooser self.assertNotIn('Collection', response_json['form'])
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 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_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): # 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 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().order_by('name')
def setUp(self): self.login() self.root_collection = Collection.get_first_root_node() self.collection = self.root_collection.add_child(name="Holiday snaps")
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