Dodanie ignorowania console dla nie posiadajÄ…cych tego obiektu przeglÄ…darek.
[redakcja.git] / apps / sorl / thumbnail / tests / utils.py
1 from django.conf import settings
2 from sorl.thumbnail.utils import *
3
4 try:
5     set
6 except NameError:
7     from sets import Set as set     # For Python 2.3
8
9 MEDIA_ROOT_LENGTH = len(os.path.normpath(settings.MEDIA_ROOT))
10
11 utils_tests = r"""
12 >>> from sorl.thumbnail.tests.utils import *
13 >>> from sorl.thumbnail.tests.base import ChangeSettings
14 >>> from django.conf import settings
15
16 >>> change_settings = ChangeSettings()
17 >>> change_settings.change()
18
19 >>> media_root = settings.MEDIA_ROOT.rstrip('/')
20
21 #==============================================================================
22 # Set up test images
23 #==============================================================================
24
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_'
31 ...            'q85.jpg')
32 >>> make_image('test-thumbnail-utils/prefix-test_jpg_120x120_q85.jpg')
33
34 #==============================================================================
35 # all_thumbnails()
36 #==============================================================================
37
38 # Find all thumbs
39 >>> thumb_dir = os.path.join(settings.MEDIA_ROOT, 'test-thumbnail-utils')
40 >>> thumbs = all_thumbnails(thumb_dir)
41 >>> k = thumbs.keys()
42 >>> k.sort()
43 >>> [consistent_slash(path) for path in k]
44 ['another_test.jpg', 'prefix-test.jpg', 'subdir/test.jpg', 'test.jpg',
45  'test_with_opts.jpg']
46
47 # Find all thumbs, no recurse
48 >>> thumbs = all_thumbnails(thumb_dir, recursive=False)
49 >>> k = thumbs.keys()
50 >>> k.sort()
51 >>> k
52 ['another_test.jpg', 'prefix-test.jpg', 'test.jpg', 'test_with_opts.jpg']
53
54 #==============================================================================
55 # thumbnails_for_file()
56 #==============================================================================
57
58 >>> output = []
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)
62 >>> output.sort()
63 >>> output
64 ['80x80 85 test-thumbnail-utils/test_jpg_80x80_q85.jpg',
65  '80x80 95 test-thumbnail-utils/test_jpg_80x80_q95.jpg']
66
67 # Thumbnails for file
68 >>> output = []
69 >>> for thumb in thumbnails_for_file('test-thumbnail-utils/test.jpg'):
70 ...    output.append(strip_media_root(thumb['filename']))
71 >>> output.sort()
72 >>> output
73 ['test-thumbnail-utils/test_jpg_80x80_q85.jpg',
74  'test-thumbnail-utils/test_jpg_80x80_q95.jpg']
75
76 # Thumbnails for file - shouldn't choke on non-existant file
77 >>> thumbnails_for_file('test-thumbnail-utils/non-existant.jpg')
78 []
79
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
85
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
91
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
97
98 #==============================================================================
99 # Clean up images / directories
100 #==============================================================================
101
102 >>> clean_up()
103 """
104
105 images_to_delete = set()
106 dirs_to_delete = []
107
108
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)
114
115
116 def make_dirs(relative_path):
117     if not relative_path:
118         return
119     absolute_path = os.path.join(settings.MEDIA_ROOT, relative_path)
120     if os.path.isdir(absolute_path):
121         return
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)
126
127
128 def clean_up():
129     for image in images_to_delete:
130         os.remove(image)
131     for path in dirs_to_delete:
132         os.rmdir(path)
133
134
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)
140
141
142 def consistent_slash(path):
143     """
144     Ensure we're always testing against the '/' os separator (otherwise tests
145     fail against Windows).
146     """
147     if os.sep != '/':
148         path = path.replace(os.sep, '/')
149     return path