Skip to content

Commit

Permalink
add jinja support, template rendering tests
Browse files Browse the repository at this point in the history
  • Loading branch information
defrex committed Jun 19, 2014
1 parent d6a0484 commit 79100be
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions resize/templates/resize/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

{% load resize %}

{{ 'test.jpg'|resize_static:'20x20' }}
2 changes: 2 additions & 0 deletions resize/templates/resize/test.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

{{ resize_static('test.jpg', '20x20') }}
16 changes: 16 additions & 0 deletions resize/templatetags/resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@

from resize.utils import resize_image, calc_height, calc_width

try:
from django_jinja import library
lib = library.Library()

except ImportError:
class LibraryStub(object):
@staticmethod
def global_function(func):
return func
lib = LibraryStub()

logger = logging.getLogger(__name__)

register = Library()


@register.filter
@lib.global_function
def resize(img_file, size=100):
try:
return resize_image(img_file, size)
Expand All @@ -25,6 +37,7 @@ def resize(img_file, size=100):


@register.filter
@lib.global_function
def resize_static(img_path, size=100):
try:
abs_path = find_file(img_path)
Expand All @@ -40,6 +53,7 @@ def resize_static(img_path, size=100):


@register.filter
@lib.global_function
def resize_absolute(img_file, size=100):
try:
path = resize_image(img_file, size=size)
Expand All @@ -52,6 +66,7 @@ def resize_absolute(img_file, size=100):


@register.filter
@lib.global_function
def height(img_file, size=100):
try:
return calc_height(img_file, size)
Expand All @@ -61,6 +76,7 @@ def height(img_file, size=100):


@register.filter
@lib.global_function
def width(img_file, size=100):
try:
return calc_width(img_file, size)
Expand Down
7 changes: 7 additions & 0 deletions resize/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from django.test import TestCase
from django.core.files.images import ImageFile
from django.template.loader import render_to_string

from resize.utils import calc_height, calc_width, resize_image

Expand Down Expand Up @@ -123,3 +124,9 @@ def test_resize_crop(self):
img = self.get_resize(size)
self.assertEqual(img.size[0], 100)
self.assertEqual(img.size[1], 100)

def test_django_template(self):
render_to_string('resize/test.html')

def test_jinja_template(self):
render_to_string('resize/test.jinja')
19 changes: 19 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))


INSTALLED_APPS = (
'django.contrib.staticfiles',
'django_jinja',
'resize',
)

Expand All @@ -11,3 +18,15 @@
}

SECRET_KEY = 'notsecure'


STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'test_images'),
)


TEMPLATE_LOADERS = (
'django_jinja.loaders.AppLoader',
'django_jinja.loaders.FileSystemLoader',
)

0 comments on commit 79100be

Please sign in to comment.