Skip to content

Commit

Permalink
placeholder with tests, fixed #201
Browse files Browse the repository at this point in the history
  • Loading branch information
sehmaschine committed Feb 21, 2014
1 parent a007efd commit 6f78ed3
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
2 changes: 0 additions & 2 deletions filebrowser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,6 @@ def version_path(self, version_suffix):
def version_generate(self, version_suffix):
"Generate a version" # FIXME: version_generate for version?
path = self.path
if FORCE_PLACEHOLDER or (SHOW_PLACEHOLDER and not self.site.storage.isfile(path)):
path = PLACEHOLDER
version_path = self.version_path(version_suffix)
if not self.site.storage.isfile(version_path):
version_path = self._generate_version(version_suffix)
Expand Down
6 changes: 5 additions & 1 deletion filebrowser/templatetags/fb_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


# FILEBROWSER IMPORTS
from filebrowser.settings import VERSIONS
from filebrowser.settings import VERSIONS, PLACEHOLDER, SHOW_PLACEHOLDER, FORCE_PLACEHOLDER
from filebrowser.base import FileObject
from filebrowser.sites import get_default_site
register = Library()
Expand All @@ -38,6 +38,8 @@ def render(self, context):
else: # string
source = source
site = context.get('filebrowser_site', get_default_site())
if FORCE_PLACEHOLDER or (SHOW_PLACEHOLDER and not site.storage.isfile(source)):
source = PLACEHOLDER
fileobject = FileObject(source, site=site)
try:
version = fileobject.version_generate(version_suffix)
Expand Down Expand Up @@ -85,6 +87,8 @@ def render(self, context):
else: # string
source = source
site = context.get('filebrowser_site', get_default_site())
if FORCE_PLACEHOLDER or (SHOW_PLACEHOLDER and not site.storage.isfile(source)):
source = PLACEHOLDER
fileobject = FileObject(source, site=site)
try:
version = fileobject.version_generate(version_suffix)
Expand Down
80 changes: 78 additions & 2 deletions filebrowser/tests/test_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def setUp(self):
self.original_versions_basedir = filebrowser.base.VERSIONS_BASEDIR
self.original_versions = filebrowser.base.VERSIONS
self.original_admin_versions = filebrowser.base.ADMIN_VERSIONS
self.original_placeholder = filebrowser.templatetags.fb_versions.PLACEHOLDER
self.original_show_placeholder = filebrowser.templatetags.fb_versions.SHOW_PLACEHOLDER
self.original_force_placeholder = filebrowser.templatetags.fb_versions.FORCE_PLACEHOLDER

# DIRECTORY
# custom directory because this could be set with sites
Expand Down Expand Up @@ -71,13 +74,26 @@ def setUp(self):
self.fail("Testimage not found.")
shutil.copy(self.image_path, self.tmpdir_path)

# create temporary test folder (placeholder) and move testimage
# FIXME: find better path names
self.tmpdir_name_ph = os.path.join("fb_tmp_dir", "fb_tmp_placeholder")
self.tmpdir_path_ph = os.path.join(site.storage.location, self.directory, self.tmpdir_name_ph)
if os.path.exists(self.tmpdir_path_ph):
self.fail("Temporary testfolder (placeholder) already exists.")
else:
os.makedirs(self.tmpdir_path_ph)

# copy test image to temporary test folder (placeholder)
shutil.copy(self.image_path, self.tmpdir_path_ph)

# set posixpath
filebrowser.base.os.path = posixpath

# fileobjects
self.f_image = FileObject(os.path.join(self.directory, self.tmpdir_name, "testimage.jpg"), site=site)
self.f_image_not_exists = FileObject(os.path.join(self.directory, self.tmpdir_name, "testimage_does_not_exist.jpg"), site=site)
self.f_folder = FileObject(os.path.join(self.directory, self.tmpdir_name), site=site)
self.f_placeholder = FileObject(os.path.join(self.directory, self.tmpdir_name_ph, "testimage.jpg"), site=site)

def test_version(self):
"""
Expand Down Expand Up @@ -133,7 +149,34 @@ def test_version(self):
# r = t.render(c)
# self.assertEqual(r, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg"))

# FIXME: test placeholder
# test placeholder with existing image
filebrowser.templatetags.fb_versions.PLACEHOLDER = "fb_test_directory/fb_tmp_dir/fb_tmp_placeholder/testimage.jpg"
filebrowser.templatetags.fb_versions.SHOW_PLACEHOLDER = True
filebrowser.templatetags.fb_versions.FORCE_PLACEHOLDER = True
t = Template('{% load fb_versions %}{% version obj.path suffix %}')
c = Context({"obj": self.f_image, "suffix": "large"})
r = t.render(c)
self.assertEqual(r, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_placeholder/testimage_large.jpg"))

filebrowser.templatetags.fb_versions.FORCE_PLACEHOLDER = False
t = Template('{% load fb_versions %}{% version obj.path suffix %}')
c = Context({"obj": self.f_image, "suffix": "large"})
r = t.render(c)
self.assertEqual(r, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg"))

# test placeholder with non-existing image
filebrowser.templatetags.fb_versions.FORCE_PLACEHOLDER = True
t = Template('{% load fb_versions %}{% version obj.path suffix %}')
c = Context({"obj": self.f_image_not_exists, "suffix": "large"})
r = t.render(c)
self.assertEqual(r, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_placeholder/testimage_large.jpg"))

filebrowser.templatetags.fb_versions.FORCE_PLACEHOLDER = False
t = Template('{% load fb_versions %}{% version obj.path suffix %}')
c = Context({"obj": self.f_image_not_exists, "suffix": "large"})
r = t.render(c)
self.assertEqual(r, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_placeholder/testimage_large.jpg"))


def test_version_object(self):
"""
Expand Down Expand Up @@ -189,7 +232,37 @@ def test_version_object(self):
# self.assertEqual(c["version_large"].url, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg"))
# self.assertEqual(r, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg"))

# FIXME: test placeholder
# test placeholder with existing image
filebrowser.templatetags.fb_versions.PLACEHOLDER = "fb_test_directory/fb_tmp_dir/fb_tmp_placeholder/testimage.jpg"
filebrowser.templatetags.fb_versions.SHOW_PLACEHOLDER = True
filebrowser.templatetags.fb_versions.FORCE_PLACEHOLDER = True
t = Template('{% load fb_versions %}{% version_object obj suffix as version_large %}{{ version_large.url }}')
c = Context({"obj": self.f_image, "suffix": "large"})
r = t.render(c)
self.assertEqual(c["version_large"].url, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_placeholder/testimage_large.jpg"))
self.assertEqual(r, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_placeholder/testimage_large.jpg"))

filebrowser.templatetags.fb_versions.FORCE_PLACEHOLDER = False
t = Template('{% load fb_versions %}{% version_object obj suffix as version_large %}{{ version_large.url }}')
c = Context({"obj": self.f_image, "suffix": "large"})
r = t.render(c)
self.assertEqual(c["version_large"].url, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg"))
self.assertEqual(r, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg"))

# test placeholder with non-existing image
filebrowser.templatetags.fb_versions.FORCE_PLACEHOLDER = True
t = Template('{% load fb_versions %}{% version_object obj suffix as version_large %}{{ version_large.url }}')
c = Context({"obj": self.f_image_not_exists, "suffix": "large"})
r = t.render(c)
self.assertEqual(c["version_large"].url, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_placeholder/testimage_large.jpg"))
self.assertEqual(r, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_placeholder/testimage_large.jpg"))

filebrowser.templatetags.fb_versions.FORCE_PLACEHOLDER = False
t = Template('{% load fb_versions %}{% version_object obj suffix as version_large %}{{ version_large.url }}')
c = Context({"obj": self.f_image_not_exists, "suffix": "large"})
r = t.render(c)
self.assertEqual(c["version_large"].url, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_placeholder/testimage_large.jpg"))
self.assertEqual(r, os.path.join(settings.MEDIA_URL, "fb_test_directory/_versions/fb_tmp_dir/fb_tmp_placeholder/testimage_large.jpg"))

def test_version_setting(self):
pass
Expand All @@ -205,6 +278,9 @@ def tearDown(self):
filebrowser.settings.VERSIONS = self.original_versions
filebrowser.templatetags.fb_versions.VERSIONS = self.original_versions
filebrowser.base.ADMIN_VERSIONS = self.original_admin_versions
filebrowser.templatetags.fb_versions.PLACEHOLDER = self.original_placeholder
filebrowser.templatetags.fb_versions.SHOW_PLACEHOLDER = self.original_show_placeholder
filebrowser.templatetags.fb_versions.FORCE_PLACEHOLDER = self.original_force_placeholder

# remove temporary directory and test folder
shutil.rmtree(self.directory_path)
Expand Down

0 comments on commit 6f78ed3

Please sign in to comment.