Code Monkey home page Code Monkey logo

Comments (8)

palewire avatar palewire commented on July 29, 2024

Related to #44

from django-bakery.

ghing avatar ghing commented on July 29, 2024

For what it's worth, this is the approach that I took to use Django Bakery to build the site in a subdirectory. It uses monkey patching, so be warned. I put this code inside a Fabric task. This might be a workable hack for those who need to do this until this issue gets resolved:

    from django.conf import settings
    old_build_dir = settings.BUILD_DIR
    old_static_url = settings.STATIC_URL
    old_initial_reuters_deflection = settings.INITIAL_REUTERS_DEFLECTION;
    settings.INITIAL_REUTERS_DEFLECTION = 'cache'
    settings.BUILD_DIR = os.path.join(settings.BUILD_DIR, settings.ARCHIVE_PATH)
    settings.STATIC_URL = posixpath.join('/', settings.ARCHIVE_PATH,
        settings.STATIC_URL.strip('/')) + '/'

    # HACK: Monkey patch django.core.urlresolvers.reverse to prefix generated URLs with
    # settings.ARCHIVE_PATH
    import django.core.urlresolvers
    old_reverse = django.core.urlresolvers.reverse
    def reverse_patched(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None):
        url = old_reverse(viewname, urlconf, args, kwargs, prefix, current_app)
        return '/' + settings.ARCHIVE_PATH + url
    setattr(django.core.urlresolvers, 'reverse', reverse_patched)

    # Call Django Bakery's build management command.  Skip copying the static
    # files because the way the management command calculates the target
    # directory based on settings.BUILD_DIR and settings.STATIC_URL doesn't
    # work with the way we've altered these things here.
    management.call_command('build', skip_static=True)

    # Copy static assets to the the build directory.  We do this explicitly
    # here because the ``build`` management command won't calculate the
    # correct directory with our settings.STATIC_URL
    target_dir = os.path.join(old_build_dir, settings.STATIC_URL[1:])

    if getattr(settings, 'BAKERY_GZIP', False):
        # The build management command class has a nice method copytree_and_gzip,
        # but we have to create call it directly if we want to specify the target directory
        cmd = Command()
        cmd.verbosity = 1
        cmd.copytree_and_gzip(settings.STATIC_ROOT, target_dir)
    # if gzip isn't enabled, just copy the tree straight over
    else:
        shutil.copytree(settings.STATIC_ROOT, target_dir)

    # Undo our monkey patches and patched settings
    # This gives the next task a clean slate in case we chain Fabric
    # tasks, e.g. fab chicago archive_build_local archive_publish_local
    settings.BUILD_DIR = old_build_dir
    settings.STATIC_URL = old_static_url
    setattr(django.core.urlresolvers, 'reverse', old_reverse)

from django-bakery.

palewire avatar palewire commented on July 29, 2024

Very interesting. Can you point me to the live example so I can see how it turns out?

from django-bakery.

ghing avatar ghing commented on July 29, 2024

Let me get back to you on that. I'm in the process of rolling a new elections.chicagotribune.com which will have the 2014-11-04 election results archived in a "subdirectory" of the S3 bucket.

from django-bakery.

ghing avatar ghing commented on July 29, 2024

Example of site baked by dynamically adjusting settings and monkey-patching django.core.urlresolvers.reverse as described in my previous comment: http://elections.chicagotribune.com/2014-11-04/

from django-bakery.

palewire avatar palewire commented on July 29, 2024

I think I'm going to stop pretending I will ever solve this.

from django-bakery.

palewire avatar palewire commented on July 29, 2024

There is the new --aws-bucket-prefix option on publish. I think that's as far as I will ever get unless someone else comes through.

from django-bakery.

aplayford avatar aplayford commented on July 29, 2024

I forgot this thread happened!

from django-bakery.

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.