def test_ajax_upload_clipboardadmin_same_name_as_existing_file_in_moderation(self):
        image = self.create_image_obj(
            original_filename='test1.jpg',
            folder=self.folder,
            publish=False,
        )
        file = self.create_image('test1.jpg')
        self.assertEquals(FileGrouper.objects.count(), 3)
        with nonversioned_manager(File):
            self.assertEquals(File.objects.count(), 3)

        from djangocms_moderation.models import Workflow, ModerationCollection
        wf = Workflow.objects.create(name='Workflow 1', is_default=True)
        collection = ModerationCollection.objects.create(
            author=self.superuser, name='Collection 1', workflow=wf,
        )
        collection.add_version(Version.objects.get_for_content(image))

        with self.login_user_context(self.superuser):
            response = self.client.post(
                reverse('admin:filer-ajax_upload', kwargs={'folder_id': self.folder.id}),
                data={'file': file},
            )

        self.assertEquals(FileGrouper.objects.count(), 3)
        with nonversioned_manager(File):
            self.assertEquals(File.objects.count(), 3)
        error_msg = 'Cannot archive existing test1.jpg file version'
        self.assertEquals(response.json()['error'], error_msg)
    def test_changing_file_path_when_publishing_file(self):
        folder = Folder.objects.create(name='folder_no_3',
                                       parent=self.folder_inside)
        grouper = FileGrouper.objects.create()
        file_obj = self.create_file_obj(
            original_filename='test-test.doc',
            folder=folder,
            grouper=grouper,
            publish=False,
        )
        storage = file_obj.file.storage
        self.assertIn('/media/filer_public/', file_obj.url)
        self.assertIn('test-test.doc', file_obj.url)
        draft_file_path = file_obj.file.path
        self.assertTrue(storage.exists(draft_file_path))

        version = file_obj.versions.first()
        version.publish(self.superuser)
        with nonversioned_manager(File):
            file_obj.refresh_from_db()

        self.assertEquals(
            file_obj.url,
            '/media/{}/{}/{}/test-test.doc'.format(self.folder.name,
                                                   self.folder_inside.name,
                                                   folder.name))
        self.assertFalse(storage.exists(draft_file_path))
        self.assertTrue(storage.exists(file_obj.file.path))
 def to_python(self, value):
     obj = super().to_python(value)
     if not obj:
         return obj
     with nonversioned_manager(File):
         obj._prefetched_objects_cache = {'files': [obj.file]}
     return obj
    def test_ajax_upload_clipboardadmin_same_name_as_existing_draft_file(self):
        file_grouper = FileGrouper.objects.create()
        file_obj = self.create_file_obj(
            original_filename='test1.pdf',
            folder=self.folder,
            grouper=file_grouper,
            publish=False,
        )
        file = self.create_file('test1.pdf')
        self.assertEquals(FileGrouper.objects.count(), 3)
        with self.login_user_context(self.superuser):
            self.client.post(
                reverse('admin:filer-ajax_upload', kwargs={'folder_id': self.folder.id}),
                data={'file': file},
            )

        self.assertEquals(FileGrouper.objects.count(), 3)

        with nonversioned_manager(File):
            files = self.folder.files.all()
        self.assertEquals(files.count(), 4)
        self.assertEquals(file_obj.label, 'test1.pdf')
        self.assertEquals(file_obj.grouper, file_grouper)

        versions = Version.objects.filter_by_grouper(file_grouper).order_by('pk')
        self.assertEquals(versions.count(), 2)
        self.assertEquals(versions[0].state, ARCHIVED)
        self.assertEquals(versions[0].content, file_obj)
        self.assertEquals(versions[1].state, DRAFT)
        self.assertEquals(versions[1].content, files.latest('pk'))
 def to_python(self, value):
     # Filter out any repeated values for the grouper
     self.queryset = self.queryset.distinct()
     obj = super().to_python(value)
     if not obj:
         return obj
     with nonversioned_manager(File):
         obj._prefetched_objects_cache = {'files': [obj.file]}
     return obj
    def test_ajax_upload_clipboardadmin_for_image_file(self):
        file = self.create_image('circles.jpg')
        self.assertEquals(FileGrouper.objects.count(), 2)
        with self.login_user_context(self.superuser):
            self.client.post(
                reverse('admin:filer-ajax_upload', kwargs={'folder_id': self.folder.id}),
                data={'file': file},
            )

        self.assertEquals(FileGrouper.objects.count(), 3)

        with nonversioned_manager(File):
            files = self.folder.files.all()
        new_file = files.latest('pk')
        self.assertEquals(new_file.label, 'circles.jpg')
        self.assertEquals(new_file.grouper, FileGrouper.objects.latest('pk'))
    def test_changing_file_path_when_unpublishing_file(self):
        storage = self.file.file.storage
        published_file_path = self.file.file.path
        self.assertTrue(storage.exists(published_file_path))
        self.assertEquals(self.file.url,
                          '/media/{}/test.pdf'.format(self.folder.name))
        self.assertTrue(storage.exists(published_file_path))

        version = self.file.versions.first()
        version.unpublish(self.superuser)
        with nonversioned_manager(File):
            self.file.refresh_from_db()

        self.assertFalse(storage.exists(published_file_path))
        self.assertTrue(storage.exists(self.file.file.path))
        self.assertIn('/media/filer_public', self.file.url)
        self.assertIn('test.pdf', self.file.url)
Beispiel #8
0
 def render(self, name, value, attrs=None):
     obj = self.obj_for_value(value)
     if obj:
         with nonversioned_manager(File):
             file_obj = obj.file
     else:
         file_obj = None
     css_id = attrs.get('id', 'id_image_x')
     related_url = None
     if value:
         try:
             related_url = file_obj.logical_folder.get_admin_directory_listing_url_path(
             )
         except Exception as e:
             # catch exception and manage it. We can re-raise it for debugging
             # purposes and/or just logging it, provided user configured
             # proper logging configuration
             if filer_settings.FILER_ENABLE_LOGGING:
                 logger.error('Error while rendering file widget: %s', e)
             if filer_settings.FILER_DEBUG:
                 raise
     if not related_url:
         related_url = reverse('admin:filer-directory_listing-last')
     params = self.url_parameters()
     params['_pick'] = 'grouper'
     if params:
         lookup_url = '?' + urlencode(sorted(params.items()))
     else:
         lookup_url = ''
     if 'class' not in attrs:
         # The JavaScript looks for this hook.
         attrs['class'] = 'vForeignKeyRawIdAdminField filer-grouper-filer'
     # rendering the super for ForeignKeyRawIdWidget on purpose here because
     # we only need the input and none of the other stuff that
     # ForeignKeyRawIdWidget adds
     hidden_input = super().render(name, value, attrs)
     context = {
         'hidden_input': hidden_input,
         'lookup_url': '%s%s' % (related_url, lookup_url),
         'object': file_obj,
         'lookup_name': name,
         'id': css_id,
         'admin_icon_delete': 'admin/img/icon-deletelink.svg',
     }
     html = render_to_string('admin/filer/widgets/admin_file.html', context)
     return mark_safe(html)
    def test_ajax_upload_clipboardadmin(self):
        file = self.create_file('test2.pdf')
        same_file_in_other_folder_grouper = FileGrouper.objects.create()
        same_file_in_other_folder = self.create_file_obj(
            original_filename='test2.pdf',
            folder=Folder.objects.create(),
            grouper=same_file_in_other_folder_grouper,
            publish=True,
        )
        self.assertEquals(FileGrouper.objects.count(), 3)

        with self.login_user_context(self.superuser):
            self.client.post(
                reverse('admin:filer-ajax_upload', kwargs={'folder_id': self.folder.id}),
                data={'file': file},
            )

        self.assertEquals(FileGrouper.objects.count(), 4)
        with nonversioned_manager(File):
            files = self.folder.files.all()
        new_file = files.latest('pk')
        new_file_grouper = FileGrouper.objects.latest('pk')
        self.assertEquals(new_file.label, 'test2.pdf')
        self.assertEquals(new_file.grouper, new_file_grouper)
        versions = Version.objects.filter_by_grouper(new_file_grouper).order_by('pk')
        self.assertEquals(versions.count(), 1)
        self.assertEquals(versions[0].state, DRAFT)

        # Checking existing in self.folder file
        self.assertEquals(self.file.label, 'test.pdf')
        self.assertEquals(self.file.grouper, self.file_grouper)
        versions = Version.objects.filter_by_grouper(self.file_grouper).order_by('pk')
        self.assertEquals(versions.count(), 1)
        self.assertEquals(versions[0].state, PUBLISHED)

        # Checking file in diffrent folder with the same name as newly created file
        self.assertEquals(same_file_in_other_folder.label, 'test2.pdf')
        self.assertEquals(same_file_in_other_folder.grouper, same_file_in_other_folder_grouper)
        versions = Version.objects.filter_by_grouper(same_file_in_other_folder_grouper).order_by('pk')
        self.assertEquals(versions.count(), 1)
        self.assertEquals(versions[0].state, PUBLISHED)
Beispiel #10
0
    def create_file_obj(self,
                        original_filename,
                        file=None,
                        folder=None,
                        publish=True,
                        content="data",
                        is_public=True,
                        grouper=None,
                        mime_type="application/octet-stream",
                        **kwargs):
        if not kwargs.get('created_by'):
            kwargs['owner'] = self.superuser

        if file is None:
            file = self.create_file(original_filename, content)

        if grouper is None:
            grouper = FileGrouper.objects.create()

        for filer_class in filer.settings.FILER_FILE_MODELS:
            FileSubClass = load_model(filer_class)
            if FileSubClass.matches_file_type(original_filename, file,
                                              mime_type):
                break

        file_obj = FileSubClass.objects.create(
            is_public=is_public,
            original_filename=original_filename,
            file=file,
            folder=folder,
            grouper=grouper,
            mime_type=mime_type,
            **kwargs)
        version = create_file_version(file_obj, kwargs['owner'])
        if publish:
            version.publish(kwargs['owner'])
            with nonversioned_manager(File):
                file_obj.refresh_from_db()

        return file_obj
    def test_ajax_upload_clipboardadmin_same_name_as_existing_file(self):
        file = self.create_file('test.pdf')
        self.assertEquals(FileGrouper.objects.count(), 2)
        with self.login_user_context(self.superuser):
            self.client.post(
                reverse('admin:filer-ajax_upload', kwargs={'folder_id': self.folder.id}),
                data={'file': file},
            )

        self.assertEquals(FileGrouper.objects.count(), 2)

        with nonversioned_manager(File):
            files = self.folder.files.all()
        self.assertEquals(files.count(), 3)
        self.assertEquals(self.file.label, 'test.pdf')
        self.assertEquals(self.file.grouper, self.file_grouper)

        versions = Version.objects.filter_by_grouper(self.file_grouper).order_by('pk')
        self.assertEquals(versions.count(), 2)
        self.assertEquals(versions[0].state, PUBLISHED)
        self.assertEquals(versions[0].content, self.file)
        self.assertEquals(versions[1].state, DRAFT)
        self.assertEquals(versions[1].content, files.latest('pk'))
    def test_folder_name_change_rebuild_urls_for_published_files(self):
        folder0 = Folder.objects.create(name='f0')
        folder1 = Folder.objects.create(name='f1')
        folder2 = Folder.objects.create(name='f2', parent=folder1)
        folder3 = Folder.objects.create(name='f3', parent=folder1)
        folder4 = Folder.objects.create(name='f4', parent=folder3)

        file0 = self.create_file_obj(original_filename='test.xls', folder=folder0)

        file1 = self.create_file_obj(original_filename='test.xls', folder=folder1)
        file2 = self.create_file_obj(original_filename='test.xls', folder=folder2)
        file3 = self.create_file_obj(original_filename='test.xls', folder=folder3)
        file4 = self.create_file_obj(original_filename='test.xls', folder=folder4)
        draft_file = self.create_file_obj(original_filename='test2.xls', folder=folder4, publish=False)
        unpublished_file = self.create_file_obj(original_filename='test3.xls', folder=folder4, publish=True)
        unpublished_file.versions.latest('pk').unpublish(self.superuser)
        archived_file = self.create_file_obj(original_filename='test4.xls', folder=folder4, publish=False)
        archived_file.versions.latest('pk').archive(self.superuser)

        files = [file0, file1, file2, file3, file4, draft_file, unpublished_file, archived_file]

        with self.login_user_context(self.superuser):
            self.client.post(
                reverse('admin:filer_folder_change', args=[folder0.id]),
                data={'name': 'f00'},
            )
        for f in files:
            with nonversioned_manager(File):
                f.refresh_from_db()

        self.assertEquals(file0.url, '/media/f00/test.xls')
        self.assertFalse(file0.file.storage.exists('f0/test.xls'))
        self.assertTrue(file0.file.storage.exists('f00/test.xls'))

        self.assertEquals(file1.url, '/media/f1/test.xls')
        self.assertEquals(file2.url, '/media/f1/f2/test.xls')
        self.assertEquals(file3.url, '/media/f1/f3/test.xls')
        self.assertEquals(file4.url, '/media/f1/f3/f4/test.xls')
        self.assertIn('filer_public', draft_file.url)
        self.assertIn('test2.xls', draft_file.url)
        self.assertIn('filer_public', unpublished_file.url)
        self.assertIn('test3.xls', unpublished_file.url)
        self.assertIn('filer_public', archived_file.url)
        self.assertIn('test4.xls', archived_file.url)

        with self.login_user_context(self.superuser):
            self.client.post(
                reverse('admin:filer_folder_change', args=[folder1.id]),
                data={'name': 'f10'},
            )
        for f in files:
            with nonversioned_manager(File):
                f.refresh_from_db()

        self.assertEquals(file0.url, '/media/f00/test.xls')
        self.assertEquals(file1.url, '/media/f10/test.xls')
        self.assertEquals(file2.url, '/media/f10/f2/test.xls')
        self.assertEquals(file3.url, '/media/f10/f3/test.xls')
        self.assertEquals(file4.url, '/media/f10/f3/f4/test.xls')
        self.assertIn('filer_public', draft_file.url)
        self.assertIn('test2.xls', draft_file.url)
        self.assertNotIn('f10', draft_file.url)
        self.assertIn('filer_public', unpublished_file.url)
        self.assertIn('test3.xls', unpublished_file.url)
        self.assertNotIn('f10', unpublished_file.url)
        self.assertIn('filer_public', archived_file.url)
        self.assertIn('test4.xls', archived_file.url)
        self.assertNotIn('f10', archived_file.url)

        with self.login_user_context(self.superuser):
            self.client.post(
                reverse('admin:filer_folder_change', args=[folder3.id]),
                data={'name': 'f30 test'},
            )
        for f in files:
            with nonversioned_manager(File):
                f.refresh_from_db()

        self.assertEquals(file0.url, '/media/f00/test.xls')
        self.assertEquals(file1.url, '/media/f10/test.xls')
        self.assertEquals(file2.url, '/media/f10/f2/test.xls')
        self.assertEquals(file3.url, '/media/f10/f30%20test/test.xls')
        self.assertEquals(file4.url, '/media/f10/f30%20test/f4/test.xls')
        self.assertIn('filer_public', draft_file.url)
        self.assertIn('test2.xls', draft_file.url)
        self.assertNotIn('f10', draft_file.url)
        self.assertIn('filer_public', unpublished_file.url)
        self.assertIn('test3.xls', unpublished_file.url)
        self.assertNotIn('f10', unpublished_file.url)
        self.assertIn('filer_public', archived_file.url)
        self.assertIn('test4.xls', archived_file.url)
        self.assertNotIn('f10', archived_file.url)