1 from django.conf import settings
2 from sorl.thumbnail.utils import *
7 from sets import Set as set # For Python 2.3
9 MEDIA_ROOT_LENGTH = len(os.path.normpath(settings.MEDIA_ROOT))
12 >>> from sorl.thumbnail.tests.utils import *
13 >>> from sorl.thumbnail.tests.base import ChangeSettings
14 >>> from django.conf import settings
16 >>> change_settings = ChangeSettings()
17 >>> change_settings.change()
19 >>> media_root = settings.MEDIA_ROOT.rstrip('/')
21 #==============================================================================
23 #==============================================================================
25 >>> make_image('test-thumbnail-utils/subdir/test_jpg_110x110_q85.jpg')
26 >>> make_image('test-thumbnail-utils/test_jpg_80x80_q85.jpg')
27 >>> make_image('test-thumbnail-utils/test_jpg_80x80_q95.jpg')
28 >>> make_image('test-thumbnail-utils/another_test_jpg_80x80_q85.jpg')
29 >>> make_image('test-thumbnail-utils/test_with_opts_jpg_80x80_crop_bw_q85.jpg')
30 >>> make_image('test-thumbnail-basedir/test-thumbnail-utils/test_jpg_100x100_'
32 >>> make_image('test-thumbnail-utils/prefix-test_jpg_120x120_q85.jpg')
34 #==============================================================================
36 #==============================================================================
39 >>> thumb_dir = os.path.join(settings.MEDIA_ROOT, 'test-thumbnail-utils')
40 >>> thumbs = all_thumbnails(thumb_dir)
43 >>> [consistent_slash(path) for path in k]
44 ['another_test.jpg', 'prefix-test.jpg', 'subdir/test.jpg', 'test.jpg',
47 # Find all thumbs, no recurse
48 >>> thumbs = all_thumbnails(thumb_dir, recursive=False)
52 ['another_test.jpg', 'prefix-test.jpg', 'test.jpg', 'test_with_opts.jpg']
54 #==============================================================================
55 # thumbnails_for_file()
56 #==============================================================================
59 >>> for thumb in thumbs['test.jpg']:
60 ... thumb['rel_fn'] = strip_media_root(thumb['filename'])
61 ... output.append('%(x)sx%(y)s %(quality)s %(rel_fn)s' % thumb)
64 ['80x80 85 test-thumbnail-utils/test_jpg_80x80_q85.jpg',
65 '80x80 95 test-thumbnail-utils/test_jpg_80x80_q95.jpg']
69 >>> for thumb in thumbnails_for_file('test-thumbnail-utils/test.jpg'):
70 ... output.append(strip_media_root(thumb['filename']))
73 ['test-thumbnail-utils/test_jpg_80x80_q85.jpg',
74 'test-thumbnail-utils/test_jpg_80x80_q95.jpg']
76 # Thumbnails for file - shouldn't choke on non-existant file
77 >>> thumbnails_for_file('test-thumbnail-utils/non-existant.jpg')
80 # Thumbnails for file, with basedir setting
81 >>> change_settings.change({'BASEDIR': 'test-thumbnail-basedir'})
82 >>> for thumb in thumbnails_for_file('test-thumbnail-utils/test.jpg'):
83 ... print strip_media_root(thumb['filename'])
84 test-thumbnail-basedir/test-thumbnail-utils/test_jpg_100x100_q85.jpg
86 # Thumbnails for file, with subdir setting
87 >>> change_settings.change({'SUBDIR': 'subdir', 'BASEDIR': ''})
88 >>> for thumb in thumbnails_for_file('test-thumbnail-utils/test.jpg'):
89 ... print strip_media_root(thumb['filename'])
90 test-thumbnail-utils/subdir/test_jpg_110x110_q85.jpg
92 # Thumbnails for file, with prefix setting
93 >>> change_settings.change({'PREFIX': 'prefix-', 'SUBDIR': ''})
94 >>> for thumb in thumbnails_for_file('test-thumbnail-utils/test.jpg'):
95 ... print strip_media_root(thumb['filename'])
96 test-thumbnail-utils/prefix-test_jpg_120x120_q85.jpg
98 #==============================================================================
99 # Clean up images / directories
100 #==============================================================================
105 images_to_delete = set()
109 def make_image(relative_image):
110 absolute_image = os.path.join(settings.MEDIA_ROOT, relative_image)
111 make_dirs(os.path.dirname(relative_image))
112 open(absolute_image, 'w').close()
113 images_to_delete.add(absolute_image)
116 def make_dirs(relative_path):
117 if not relative_path:
119 absolute_path = os.path.join(settings.MEDIA_ROOT, relative_path)
120 if os.path.isdir(absolute_path):
122 if absolute_path not in dirs_to_delete:
123 dirs_to_delete.append(absolute_path)
124 make_dirs(os.path.dirname(relative_path))
125 os.mkdir(absolute_path)
129 for image in images_to_delete:
131 for path in dirs_to_delete:
135 def strip_media_root(path):
136 path = os.path.normpath(path)
137 # chop off the MEDIA_ROOT and strip any leading os.sep
138 path = path[MEDIA_ROOT_LENGTH:].lstrip(os.sep)
139 return consistent_slash(path)
142 def consistent_slash(path):
144 Ensure we're always testing against the '/' os separator (otherwise tests
145 fail against Windows).
148 path = path.replace(os.sep, '/')