Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Written a couple of unit tests for testing the post save hook

Fixed bugs
  • Loading branch information...
commit fa2be09532a3b0b1ff4e4a770a9af72efde2fd1b 1 parent 015c7ef
@kux kux authored
View
23 filertags/signals.py
@@ -13,10 +13,10 @@
_LOGICAL_URL_TEMPLATE = "/* logicalurl('%s') */"
_RESOURCE_URL_TEMPLATE = "url('%s') " + _LOGICAL_URL_TEMPLATE
-
-_RESOURCE_URL_REGEX = re.compile(r"url\(['\"]?([^'\"\)]+?)['\"]?\)")
+_RESOURCE_URL_REGEX = re.compile(r"\burl\(([^\)]*)\)")
_COMMENT_REGEX = re.compile(r"/\*.*?\*/")
+_ALREADY_PARSED_MARKER = '/* Filer urls already resolved */'
def _is_in_clipboard(filer_file):
@@ -44,6 +44,12 @@ def _rewrite_file_content(filer_file, new_content):
def _resolve_resource_urls(css_file):
logical_folder_path = _construct_logical_folder_path(css_file)
content = css_file.file.read()
+ if content.startswith(_ALREADY_PARSED_MARKER):
+ # this css' resource urls have already been resolved
+ # this happens when moving the css in and out of the clipboard
+ # multiple times
+ return
+
commented_regions = _get_commented_regions(content)
local_cache = {}
@@ -52,7 +58,8 @@ def change_urls(match):
# we don't make any changes to urls that are part of commented regions
if start < match.start() < end or start < match.end() < end:
return match.group()
- url = match.group(1)
+ # strip spaces and quotes
+ url = match.group(1).strip('\'\" ')
parsed_url = urlparse.urlparse(url)
if parsed_url.netloc:
# if the url is absolute, leave it unchaged
@@ -65,14 +72,20 @@ def change_urls(match):
filerfile(logical_file_path), logical_file_path)
return local_cache[logical_file_path]
- new_content = re.sub(_RESOURCE_URL_REGEX, change_urls, content)
+ new_content = '%s\n%s' % (
+ _ALREADY_PARSED_MARKER,
+ re.sub(_RESOURCE_URL_REGEX, change_urls, content))
_rewrite_file_content(css_file, new_content)
def _update_referencing_css_files(resource_file):
+ if resource_file.name:
+ resource_name = resource_file.name
+ else:
+ resource_name = resource_file.original_filename
logical_file_path = os.path.join(
_construct_logical_folder_path(resource_file),
- resource_file.original_filename)
+ resource_name)
css_files = File.objects.filter(original_filename__endswith=".css")
for css in css_files:
View
8 filertags/templatetags/filertags.py
@@ -1,3 +1,5 @@
+import logging
+
from django import template
from django.db.models import Q
from django.core.cache import cache
@@ -5,6 +7,8 @@
from filer.models import File, Folder
+logger = logging.getLogger(__name__)
+
def filerthumbnail(path):
parts = path.strip('/').split('/')
@@ -25,8 +29,8 @@ def filerthumbnail(path):
q |= Q(original_filename=file_name, folder=folder, name__isnull=True)
q |= Q(name=file_name, folder=folder)
return File.objects.get(q).file
- except (File.DoesNotExist, Folder.DoesNotExist):
- pass
+ except (File.DoesNotExist, File.MultipleObjectsReturned, Folder.DoesNotExist), e:
+ logger.info('%s on %s' % (e.message, path))
def get_filerfile_cache_key(path):
View
72 filertags/tests.py
@@ -1,16 +1,64 @@
-"""
-This file demonstrates writing tests using the unittest module. These will pass
-when you run "manage.py test".
-
-Replace this with more appropriate tests for your application.
-"""
+import os
+from django.core.files import File as DjangoFile
from django.test import TestCase
+from filer.models.foldermodels import Folder
+from filer.models.imagemodels import Image
+from filer.models.filemodels import File
+
+from filer.tests.helpers import (create_superuser, create_folder_structure,
+ create_image, create_clipboard_item)
+
+from filertags.signals import _ALREADY_PARSED_MARKER
-class SimpleTest(TestCase):
- def test_basic_addition(self):
- """
- Tests that 1 + 1 always equals 2.
- """
- self.assertEqual(1 + 1, 2)
+
+class CssResourceUrlResolvingTest(TestCase):
+
+ def setUp(self):
+ self.superuser = create_superuser()
+ self.client.login(username='admin', password='secret')
+ self.image = create_image()
+ self.image_name = 'test_file.jpg'
+ self.image_filename = os.path.join(
+ os.path.dirname(__file__), 'test_resources', self.image_name)
+ self.image.save(self.image_filename, 'JPEG')
+ self.resource_folder = Folder.objects.create(
+ owner=self.superuser,
+ name='css_resources_test')
+ self.css_folder = Folder.objects.create(
+ owner=self.superuser,
+ name='css',
+ parent=self.resource_folder)
+ self.images_folder = Folder.objects.create(
+ owner=self.superuser,
+ name='images',
+ parent=self.resource_folder)
+ image_file_obj = DjangoFile(open(self.image_filename), name=self.image_name)
+ filer_image = Image(
+ owner=self.superuser,
+ original_filename=self.image_name,
+ folder=self.images_folder,
+ file=image_file_obj)
+ filer_image.save()
+
+ def test_resolve_urls_quoted(self):
+ css_content = """
+.bgimage-single-quotes {
+ background:#CCCCCC url( '../images/test_file.jpg' ) no-repeat center center;
+ background-color: black;
+}
+"""
+ css_path = os.path.join(os.path.dirname(__file__),
+ 'test_resources', 'resources.css')
+ with open(css_path, 'w') as f:
+ f.write(css_content)
+ css_file_obj = DjangoFile(open(css_path), name='resources.css')
+ filer_css = File(owner=self.superuser,
+ original_filename='resources.css',
+ folder=self.css_folder,
+ file=css_file_obj)
+ filer_css.save()
+ with open(filer_css.path) as f:
+ new_content = f.read()
+ self.assertTrue(new_content.startswith(_ALREADY_PARSED_MARKER))
Please sign in to comment.
Something went wrong with that request. Please try again.