From 18b47de71f15e9a4ade29a46936bfb5baee3fd86 Mon Sep 17 00:00:00 2001 From: Nick Sergeant Date: Sun, 23 Oct 2011 22:44:37 -0400 Subject: [PATCH] Horrible things for nice storage --- requirements.txt | 2 +- snipt/media/css/style.less | 4 ++++ snipt/settings.py | 40 ++++++++++++++++++++++++-------------- snipt/storage.py | 19 ++++++++++++++++++ snipt/templates/base.html | 30 +++++++++++----------------- 5 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 snipt/storage.py diff --git a/requirements.txt b/requirements.txt index f961872..c2bf824 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ boto Django django-annoying django-bcrypt -django-css +django-compressor django-grappelli django-pagination django-postmark diff --git a/snipt/media/css/style.less b/snipt/media/css/style.less index 20929f0..4bc9518 100644 --- a/snipt/media/css/style.less +++ b/snipt/media/css/style.less @@ -515,3 +515,7 @@ article.snipt { .hidden { display: none; } + +body { + background: blue; +} diff --git a/snipt/settings.py b/snipt/settings.py index d685567..b3f5914 100644 --- a/snipt/settings.py +++ b/snipt/settings.py @@ -4,6 +4,7 @@ import os DEBUG = True TEMPLATE_DEBUG = DEBUG +BASE_PATH = os.path.dirname(__file__) ADMINS = ( ('Nick Sergeant', 'nick@snipt.net'), @@ -49,20 +50,22 @@ USE_L10N = True # Absolute filesystem path to the directory that will hold user-uploaded files. # Example: "/home/media/media.lawrence.com/media/" -MEDIA_ROOT = '/Users/Nick/Code/snipt/snipt/media/' +MEDIA_ROOT = '' # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash. # Examples: "http://media.lawrence.com/media/", "http://example.com/media/" -MEDIA_URL = '/media/' +MEDIA_URL = '' # Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # Example: "/home/media/media.lawrence.com/static/" -STATIC_ROOT = '/Users/Nick/Code/snipt/snipt/static/' -STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage' +STATIC_ROOT = os.path.join(BASE_PATH, 'static') +STATICFILES_STORAGE = 'snipt.storage.CachedS3BotoStorage' STATIC_URL = 'https://dn2p0mzo970os.cloudfront.net/' + +# S3 Settings AWS_ACCESS_KEY_ID = 'AKIAJTFDHBCXHJLXINKQ' AWS_SECRET_ACCESS_KEY = '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' AWS_STORAGE_BUCKET_NAME = 'snipt' @@ -74,7 +77,7 @@ ADMIN_MEDIA_PREFIX = STATIC_URL + 'grappelli/' # Additional locations of static files STATICFILES_DIRS = ( - '/Users/Nick/Code/snipt/snipt/media', + os.path.join(BASE_PATH, 'media'), ) # List of finder classes that know how to find static files in @@ -82,7 +85,7 @@ STATICFILES_DIRS = ( STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', -# 'django.contrib.staticfiles.finders.DefaultStorageFinder', + 'compressor.finders.CompressorFinder', ) # Make this unique, and don't share it with anybody. @@ -94,12 +97,21 @@ TEMPLATE_LOADERS = ( 'django.template.loaders.app_directories.Loader', # 'django.template.loaders.eggs.Loader', ) +TEMPLATE_CONTEXT_PROCESSORS = ( + 'django.core.context_processors.auth', + 'django.core.context_processors.debug', + 'django.core.context_processors.i18n', + 'django.core.context_processors.media', + 'django.core.context_processors.request', + 'django.core.context_processors.static', +) MIDDLEWARE_CLASSES = ( 'snipt.middleware.www.WWWMiddleware', 'django.middleware.cache.CacheMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.csrf.CsrfResponseMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', @@ -169,20 +181,18 @@ GRAPPELLI_ADMIN_TITLE = 'Snipt' # Virtualenv VIRTUALENV_PATH = '/Users/Nick/.virtualenvs/snipt/lib/python2.7/site-packages/' +# Compressor +COMPRESS_ENABLED = True +COMPRESS_OUTPUT_DIR = 'cache' +COMPRESS_ROOT = STATIC_ROOT +COMPRESS_URL = STATIC_URL +COMPRESS_STORAGE = STATICFILES_STORAGE + # Account settings LOGIN_REDIRECT_URL = '/' LOGIN_URL = '/login/' LOGOUT_URL = '/logout/' -# CSS compression -COMPRESS_OUTPUT_DIR = 'cache' -COMPILER_FORMATS = { - '.less': { - 'binary_path':'lessc', - 'arguments': '*.less *.css' - }, -} - # User absolute URLs ABSOLUTE_URL_OVERRIDES = { 'auth.user': lambda u: "/%s/" % u.username, diff --git a/snipt/storage.py b/snipt/storage.py new file mode 100644 index 0000000..cb9e9a4 --- /dev/null +++ b/snipt/storage.py @@ -0,0 +1,19 @@ +from django.core.files.storage import get_storage_class +from storages.backends.s3boto import S3BotoStorage + +class CachedS3BotoStorage(S3BotoStorage): + """ + S3 storage backend that saves the files locally, too. + """ + def __init__(self, *args, **kwargs): + super(CachedS3BotoStorage, self).__init__(*args, **kwargs) + self.local_storage = get_storage_class( + "compressor.storage.CompressorFileStorage")() + + def save(self, name, content): + name = super(CachedS3BotoStorage, self).save(name, content) + self.local_storage._save(name, content) + return name + + def path(self, name): + return self.local_storage.path(name) diff --git a/snipt/templates/base.html b/snipt/templates/base.html index 7cf550a..24e74de 100644 --- a/snipt/templates/base.html +++ b/snipt/templates/base.html @@ -1,32 +1,24 @@ + + {% load compress %} + {% block page-title %}Snipt{% endblock %} - {% load compress %} - {% compress css %} - - - {% if not debug %} - - {% endif %} + + + {% endcompress %} - {% if debug %} - - {% endif %} - {% compress js %} - - - - - - {% if debug %} - - {% endif %} + + + + + {% endcompress %}