Giter Site home page Giter Site logo

Comments (1)

jezdez avatar jezdez commented on May 28, 2024

@saulshanabrook There multiple ways to implement this with django-configurations but since I see you don't only have one setting to change depending on your DJANGO_STORAGE environment variable, I would recommend to simply split those two cases into two different Configuration subclasses, e.g. Local and S3 for example. Each would have the appropriate settings as the if blocks indicate. Then you'd "switch" those configurations by simply setting the DJANGO_CONFIGURATION environment variable to either Local or S3.

In case there are some other settings that are in common to both storage configurations, simply create a base configuration class called Base that subclasses Configuration and use it when subclassing the Local and S3 configuration classes.

Here's an example:

from configurations import Configuration

class Base(Configuration):
    # some common settings here..


class Local(Base):

    STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.CachedStaticFilesStorage'

    STATIC_URL = '/static/'
    STATIC_ROOT = rel_path('tmp/static')

    MEDIA_URL = '/media/'
    MEDIA_ROOT = rel_path('tmp/media')


class S3(Base):

    DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
    STATICFILES_STORAGE = 'configs.storage_backends.S3HashedFilesStorage'

    INSTALLED_APPS = Base.INSTALLED_APPS + (
        'storages',
    )
    AWS_ACCESS_KEY_ID = values.SecretValue(environ_prefix=None)
    AWS_SECRET_ACCESS_KEY = values.SecretValue(environ_prefix=None)
    AWS_STORAGE_BUCKET_NAME = values.SecretValue(environ_name='AWS_BUCKET',
                                                 environ_prefix=None)
    AWS_S3_CUSTOM_DOMAIN = AWS_STORAGE_BUCKET_NAME
    AWS_HEADERS = {
        "Cache-Control": "public, max-age=31536000",
        'Expires': (datetime.utcnow() + timedelta(days=365)).strftime('%a, %d %b %Y %H:%M:%S UTC')
    }
    AWS_QUERYSTRING_AUTH = False
    AWS_QUERYSTRING_EXPIRE = 600
    AWS_S3_SECURE_URLS = False
    AWS_IS_GZIPPED = True
    AWS_PRELOAD_METADATA = True

    STATIC_URL = 'http://{}/'.format(AWS_S3_CUSTOM_DOMAIN)

from django-configurations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.