Skip to content

Commit

Permalink
Added Storage usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuluPro committed Apr 21, 2016
1 parent d71ee5b commit 0d3d1f1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
42 changes: 29 additions & 13 deletions favicon/management/commands/generate_favicon.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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')
5 changes: 2 additions & 3 deletions favicon/settings.py
Original file line number Diff line number Diff line change
@@ -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', {})

0 comments on commit 0d3d1f1

Please sign in to comment.