From 0d3d1f1662fdec54748242dee03c9ab5b9d21433 Mon Sep 17 00:00:00 2001 From: ZuluPro Date: Thu, 21 Apr 2016 19:00:23 -0400 Subject: [PATCH] Added Storage usage --- .../management/commands/generate_favicon.py | 42 +++++++++++++------ favicon/settings.py | 5 +-- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/favicon/management/commands/generate_favicon.py b/favicon/management/commands/generate_favicon.py index 6f25dc8..3539cd8 100644 --- a/favicon/management/commands/generate_favicon.py +++ b/favicon/management/commands/generate_favicon.py @@ -1,6 +1,11 @@ import os + from django.core.management.base import BaseCommand, CommandError from django.template.loader import get_template +from django.core.files import File +from django.core.files.storage import get_storage_class +from django.utils.six import BytesIO + from PIL import Image from favicon import settings @@ -18,28 +23,39 @@ def add_arguments(self, parser): parser.add_argument('filename', nargs=1, type=str, help="Input file used to generate favicons") + def write_file(self, output_file, name): + storage = get_storage_class(settings.STORAGE)(**settings.STORAGE_OPTIONS) + content = File(output_file, name) + storage.save(content) + def handle(self, *args, **options): filename = options['filename'] if not os.path.exists(filename): - CommandError("File '%s' does not exist." % filename) + raise CommandError("File '%s' does not exist." % filename) + # Set storage # Save ICO img = Image.open(filename) - output = os.path.join(settings.STATIC_DIR, 'favicon.ico') - img.save(fp=output, sizes=[(16, 16), (32, 32), (48, 48), (64, 64)]) + output_file = BytesIO() + img.save(fp=output_file, sizes=[(16, 16), (32, 32), (48, 48), (64, 64)]) + self.write(output_file, 'favicon.ico') # Save PNG for size in PNG_SIZE: img = Image.open(filename) - output = os.path.join(settings.STATIC_DIR, 'favicon-%s.png' % size) + output_file = BytesIO() + output_name = 'favicon-%s.png' % size img.thumbnail(size=(size, size), resample=Image.ANTIALIAS) - img.save(output) - for size, filename in PNG_SIZE: + img.save(output_file) + self.write(output_file, output_name) + for size, output_name in PNG_SIZE: img = Image.open(filename) - output = os.path.join(settings.STATIC_DIR, 'filename') + output_file = BytesIO() img.thumbnail(size=size, resample=Image.ANTIALIAS) - img.save(output) + img.save(output_file) + self.write(output_file, output_name) # Create ieconfig.xml - output = os.path.join(settings.STATIC_DIR, 'ieconfig.xml') - with open(output, 'w') as output_file: - template = get_template('favicon/ieconfig.xml') - output_content = template.render({'tile_color': 'FFFFFF'}) - output_file.write(output_content) + output_name = 'ieconfig.xml' + output_file = BytesIO() + template = get_template('favicon/ieconfig.xml') + output_content = template.render({'tile_color': 'FFFFFF'}) + output_file.write(output_content) + self.write(output_file, 'ieconfig.xml') diff --git a/favicon/settings.py b/favicon/settings.py index 370d725..f9b1eb4 100644 --- a/favicon/settings.py +++ b/favicon/settings.py @@ -1,6 +1,5 @@ -import os from django.conf import settings -STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), - '/static/') +STORAGE = getattr(settings, 'FAVICON_STORAGE', settings.DEFAULT_FILE_STORAGE) +STORAGE_OPTIONS = getattr(settings, 'FAVICON_STORAGE_OPTIONS', {})