Poprawienie wyświetlania motywów.
[redakcja.git] / apps / sorl / thumbnail / tests / fields.py
1 import os.path
2
3 from django.db import models
4 from django.conf import settings
5 from django.core.files.base import ContentFile
6
7 from sorl.thumbnail.fields import ImageWithThumbnailsField, ThumbnailField
8 from sorl.thumbnail.tests.base import BaseTest, RELATIVE_PIC_NAME, PIC_NAME
9
10 thumbnail = {
11     'size': (50, 50),
12 }
13 extra_thumbnails = {
14     'admin': {
15         'size': (30, 30),
16         'options': ('crop',),
17     }
18 }
19 extension_thumbnail = thumbnail.copy()
20 extension_thumbnail['extension'] = 'png'
21
22
23 # Temporary models for field_tests
24 class TestThumbnailFieldModel(models.Model):
25     avatar = ThumbnailField(upload_to='test', size=(300, 300))
26     photo = ImageWithThumbnailsField(upload_to='test', thumbnail=thumbnail,
27                                      extra_thumbnails=extra_thumbnails)
28
29
30 class TestThumbnailFieldExtensionModel(models.Model):
31     photo = ImageWithThumbnailsField(upload_to='test',
32                                      thumbnail=extension_thumbnail,
33                                      extra_thumbnails=extra_thumbnails)
34
35
36 class TestThumbnailFieldGenerateModel(models.Model):
37     photo = ImageWithThumbnailsField(upload_to='test', thumbnail=thumbnail,
38                                      extra_thumbnails=extra_thumbnails,
39                                      generate_on_save=True)
40
41
42 class FieldTest(BaseTest):
43     """
44     Test the base field functionality. These use an ImageWithThumbnailsField
45     but all the functionality tested is from BaseThumbnailField.
46     """
47     def test_extra_thumbnails(self):
48         model = TestThumbnailFieldModel(photo=RELATIVE_PIC_NAME)
49         self.assertTrue('admin' in model.photo.extra_thumbnails)
50         thumb = model.photo.extra_thumbnails['admin']
51         tag = model.photo.extra_thumbnails_tag['admin']
52         expected_filename = os.path.join(settings.MEDIA_ROOT,
53             'sorl-thumbnail-test_source_jpg_30x30_crop_q85.jpg')
54         self.verify_thumbnail((30, 30), thumb, expected_filename)
55         expected_tag = '<img src="%s" width="30" height="30" alt="" />' % \
56             '/'.join((settings.MEDIA_URL.rstrip('/'),
57                       'sorl-thumbnail-test_source_jpg_30x30_crop_q85.jpg'))
58         self.assertEqual(tag, expected_tag)
59
60     def test_extension(self):
61         model = TestThumbnailFieldExtensionModel(photo=RELATIVE_PIC_NAME)
62         thumb = model.photo.thumbnail
63         tag = model.photo.thumbnail_tag
64         expected_filename = os.path.join(settings.MEDIA_ROOT,
65             'sorl-thumbnail-test_source_jpg_50x50_q85.png')
66         self.verify_thumbnail((50, 37), thumb, expected_filename)
67         expected_tag = '<img src="%s" width="50" height="37" alt="" />' % \
68             '/'.join((settings.MEDIA_URL.rstrip('/'),
69                       'sorl-thumbnail-test_source_jpg_50x50_q85.png'))
70         self.assertEqual(tag, expected_tag)
71
72     def test_delete_thumbnails(self):
73         model = TestThumbnailFieldModel(photo=RELATIVE_PIC_NAME)
74         thumb_file = model.photo.thumbnail.dest
75         open(thumb_file, 'wb').close()
76         self.assert_(os.path.exists(thumb_file))
77         model.photo.delete(save=False)
78         self.assertFalse(os.path.exists(thumb_file))
79
80     def test_generate_on_save(self):
81         main_thumb = os.path.join(settings.MEDIA_ROOT, 'test',
82                         'sorl-thumbnail-test_source_jpg_50x50_q85.jpg')
83         admin_thumb = os.path.join(settings.MEDIA_ROOT, 'test',
84                         'sorl-thumbnail-test_source_jpg_30x30_crop_q85.jpg')
85         self.images_to_delete.add(main_thumb)
86         self.images_to_delete.add(admin_thumb)
87         # Default setting is to only generate when the thumbnail is used.
88         model = TestThumbnailFieldModel()
89         source = ContentFile(open(PIC_NAME).read())
90         model.photo.save(RELATIVE_PIC_NAME, source, save=False)
91         self.images_to_delete.add(model.photo.path)
92         self.assertFalse(os.path.exists(main_thumb))
93         self.assertFalse(os.path.exists(admin_thumb))
94         os.remove(model.photo.path)
95         # But it's easy to set it up the other way...
96         model = TestThumbnailFieldGenerateModel()
97         source = ContentFile(open(PIC_NAME).read())
98         model.photo.save(RELATIVE_PIC_NAME, source, save=False)
99         self.assert_(os.path.exists(main_thumb))
100         self.assert_(os.path.exists(admin_thumb))
101
102
103 class ImageWithThumbnailsFieldTest(BaseTest):
104     def test_thumbnail(self):
105         model = TestThumbnailFieldModel(photo=RELATIVE_PIC_NAME)
106         thumb = model.photo.thumbnail
107         tag = model.photo.thumbnail_tag
108         base_name = RELATIVE_PIC_NAME.replace('.', '_')
109         expected_filename = os.path.join(settings.MEDIA_ROOT,
110                                          '%s_50x50_q85.jpg' % base_name)
111         self.verify_thumbnail((50, 37), thumb, expected_filename)
112         expected_tag = ('<img src="%s" width="50" height="37" alt="" />' %
113                         '/'.join([settings.MEDIA_URL.rstrip('/'),
114                                   '%s_50x50_q85.jpg' % base_name]))
115         self.assertEqual(tag, expected_tag)
116
117
118 class ThumbnailFieldTest(BaseTest):
119     def test_thumbnail(self):
120         model = TestThumbnailFieldModel()
121         source = ContentFile(open(PIC_NAME).read())
122         dest_name = 'sorl-thumbnail-test_dest.jpg'
123         model.avatar.save(dest_name, source, save=False)
124         expected_filename = os.path.join(model.avatar.path)
125         self.verify_thumbnail((300, 225), expected_filename=expected_filename)
126
127         tag = model.avatar.thumbnail_tag
128         expected_tag = ('<img src="%s" width="300" height="225" alt="" />' %
129                         '/'.join([settings.MEDIA_URL.rstrip('/'), 'test',
130                                   dest_name]))
131         self.assertEqual(tag, expected_tag)