-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Mockup utils to create dummy PIL/django-filer images with Text
- Loading branch information
Showing
6 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,3 +66,6 @@ target/ | |
# PyDev | ||
.project | ||
.pydevproject | ||
|
||
# django-filer (created by unittest run) | ||
filer_public/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# coding: utf-8 | ||
|
||
""" | ||
Mockups | ||
~~~~~~~ | ||
""" | ||
|
||
from __future__ import unicode_literals, print_function | ||
|
||
import tempfile | ||
|
||
from PIL import Image, ImageDraw | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand, CommandError | ||
from django.core.files import File as DjangoFile | ||
|
||
from filer.models import Image as FilerImage | ||
|
||
|
||
DUMMY_TEXT = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula | ||
eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient | ||
montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque | ||
eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, | ||
fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, | ||
imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. | ||
Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate | ||
eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, | ||
enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus | ||
viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam | ||
ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. | ||
Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper | ||
libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, | ||
luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt | ||
tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam | ||
sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit | ||
amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum | ||
sodales, augue velit cursus nunc""" | ||
|
||
|
||
def create_pil_image(width, height): | ||
img = Image.new('RGB', (width, height), 'black') # create a new black image | ||
pixels = img.load() # create the pixel map | ||
|
||
# Fill image | ||
for i in range(img.size[0]): | ||
for j in range(img.size[1]): | ||
pixels[i,j] = (i, j, 1) | ||
|
||
return img | ||
|
||
|
||
def create_info_image(width, height, text, fill='#ffffff', align='center'): | ||
im = create_pil_image(width, height) | ||
draw = ImageDraw.Draw(im) | ||
draw.multiline_text((10, 10), text, fill=fill, align=align) | ||
return im | ||
|
||
|
||
def create_filer_image(pil_image, user): | ||
file_obj = DjangoFile(pil_image, name=pil_image.name) | ||
image = FilerImage.objects.create( | ||
owner=user, | ||
original_filename=pil_image.name, | ||
file=file_obj, | ||
folder=None | ||
) | ||
return image | ||
|
||
|
||
def create_temp_filer_info_image(width, height, text, user): | ||
f = tempfile.NamedTemporaryFile(prefix='dummy_', suffix='.png') | ||
im = create_info_image(width, height, text) | ||
im.save(f, format='png') | ||
filer_image = create_filer_image(f, user) | ||
return filer_image | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ tox | |
coverage | ||
coveralls | ||
python-creole | ||
docutils | ||
docutils | ||
Pillow | ||
cmsplugin-filer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# coding: utf-8 | ||
|
||
""" | ||
test Mockups | ||
~~~~~~~~~~~~ | ||
""" | ||
|
||
from __future__ import unicode_literals, print_function | ||
|
||
from django_tools.unittest_utils.mockup import create_temp_filer_info_image, \ | ||
create_pil_image, create_info_image | ||
from django_tools.unittest_utils.unittest_base import BaseTestCase | ||
|
||
|
||
class TestMockup(BaseTestCase): | ||
def test_create_pil_image(self): | ||
pil_image = create_pil_image(width=300, height=150) | ||
|
||
self.assertEqual(pil_image.width, 300) | ||
self.assertEqual(pil_image.height, 150) | ||
|
||
self.assertEqual(pil_image.verify(), None) | ||
|
||
def test_create_info_image(self): | ||
pil_image = create_info_image(width=400, height=200, text="foo", fill='#ffffff', align='center') | ||
|
||
self.assertEqual(pil_image.width, 400) | ||
self.assertEqual(pil_image.height, 200) | ||
|
||
self.assertEqual(pil_image.verify(), None) | ||
|
||
def test_create_temp_filer_info_image(self): | ||
self.create_testusers(verbosity=1) | ||
|
||
user = self.login(usertype="normal") | ||
|
||
filer_info_image = create_temp_filer_info_image(width=500, height=300, text="A test image", user=user) | ||
self.assertEqual(filer_info_image.width, 500) | ||
self.assertEqual(filer_info_image.height, 300) | ||
|
||
self.assertEqual(filer_info_image.size, 1791) | ||
self.assertIn("django-tools/filer_public/", filer_info_image.path) | ||
self.assertIn(".png", filer_info_image.path) |