def handle(self, *args, **kwargs): for screenshot in models.Screenshot.load(): print screenshot.name img = Image.open(staticfiles_storage.path(screenshot.path)) # Ensure we're in RGB if img.mode not in ('L', 'RGB'): img = img.convert('RGB') # Magic! # The thumbnail is only limited by width, so use # a larger-than-needed height. img.thumbnail((200 * screenshot.screens, 1000), Image.ANTIALIAS) # Save the thumbnail to a tmpfile fd, tmp = tempfile.mkstemp() file = os.fdopen(fd, 'w+b') type = mimetypes.guess_type(screenshot.name)[0].split('/')[1] img.save(file, type) file.close() # Nuke previous version if it exists if staticfiles_storage.exists(screenshot.thumbnail): staticfiles_storage.delete(screenshot.thumbnail) # save thumbnail to stattic dir file = File(open(tmp, 'rb')) staticfiles_storage.save(screenshot.thumbnail, file) file.close() os.unlink(tmp)
def handle(self, *args, **options): locale = options["locale"] content = generate_js(locale) file_name = staticfiles_storage.path(f"choices-{locale}.js") if staticfiles_storage.exists(file_name): staticfiles_storage.delete(file_name) staticfiles_storage.save(file_name, ContentFile(content)) self.stdout.write(f"{file_name} saved to static root!")
def testManagementCollectstatic(self): call_command("collectstatic", interactive=False, stdout=StringIO()) url = staticfiles_storage.url("foo.css") try: # The non-hashed name should have the default cache control. meta = staticfiles_storage.meta("foo.css") self.assertEqual(meta["CacheControl"], "public,max-age=3600") # The URL should not contain query string authentication. self.assertFalse(urlsplit(url).query) # The URL should contain an MD5 hash. assertRegex(self, url, "foo\.[0-9a-f]{12}\.css$") # The hashed name should be accessible and have a huge cache control. response = requests.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, b"* { display: none; }\n") self.assertEqual(response.headers["cache-control"], "public,max-age=31536000") finally: staticfiles_storage.delete("staticfiles.json") staticfiles_storage.delete("foo.css") staticfiles_storage.delete(posixpath.basename(url))