def test_css_url(self): """ Test auto-generated CSS urls """ self.assertEquals(dirs.get_css_url(), "%s%s" % (settings.STATIC_URL, "css")) with self.settings(MEDIABRUTE_USE_STATIC=False): self.assertEquals(dirs.get_css_url(), "%s%s" % (settings.MEDIA_URL, "css")) with self.settings(MEDIABRUTE_CSS_URL_PATH="heyo/yoyo"): self.assertEquals(dirs.get_css_url(), "%s%s" % (settings.STATIC_URL, "heyo/yoyo")) with self.settings(MEDIABRUTE_USE_STATIC=False, MEDIABRUTE_CSS_URL_PATH="heyo/yoyo"): self.assertEquals(dirs.get_css_url(), "%s%s" % (settings.MEDIA_URL, "heyo/yoyo"))
def compile_and_cache_css(css_dirs, cache_dir, app_name=None): """ Return the cache_name of the compiled file It has been compiled and written to a cache file """ css_files = [] for css_dir in css_dirs: css_files += list_media_in_dirs("css", css_dir) if not app_name: app_name = "css" timestamp = latest_timestamp(css_files) cache_name = generate_cache_name("css", timestamp, app_name) cache_fullpath = os.path.join(cache_dir, cache_name) top, mid, bottom = organize_css_files(css_files) css_files = top + mid + bottom if not os.path.isfile(cache_fullpath): css_contents = compile_files(css_files) abs_path = dirs.get_css_url() if not abs_path.endswith("/"): abs_path += "/" # remove spaces css_contents = css_contents.replace("url (", "url(") # regex an absolute path into URLs that qualify css_contents = re.sub(r'url\(("|\')?([^"\'(https?\:)(//)])([^")]+)("|\')?\)', r'url("%s\2\3")' % abs_path, css_contents) # remove any double quotes at the end of url lines css_contents = css_contents.replace("'\")","\")") if dirs.remove_old_files(): unlink_cache(cache_dir, "css", app_name) cache_file = open(cache_fullpath, "w") cache_file.write(minify.cssmin(css_contents)) cache_file.close() return cache_name
def minify_css(app_name=None): """ {{ MINI_CSS }} Context processor """ css_dir = dirs.get_main_css_dir() cache_dir = dirs.generate_cache_dir(css_dir) # check for a possible 'lock' file possible_cache = os.path.join(cache_dir, "mediabrute_usefile") if os.path.isfile(possible_cache): txt = open(possible_cache) css_urls = txt.readlines() return [url for url in css_urls if url != ""] # continue to do an on-the-fly cache if no lock file was found css_dirs = [css_dir, dirs.APP_CSS_DIRS] cache_files = [compile_and_cache_css(css_dirs, cache_dir), ] if app_name and app_name in dirs.get_separated_apps("css"): cache_files.append(compile_and_cache_css([dirs.get_separated_css(app_name), ], cache_dir, app_name=app_name)) return ["%s/cache/%s" % (dirs.get_css_url(), cache_name) for cache_name in cache_files]