Ticket #21219: add_static_file_permission.diff

File add_static_file_permission.diff, 7.0 KB (added by Vajrasky Kok, 11 years ago)

Added STATIC_FILE_PERMISSIONS to collectstatic and tested it and documented it!

  • django/conf/global_settings.py

    diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
    index 6dd25e1..be39c5e 100644
    a b STATICFILES_FINDERS = (  
    610610#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    611611)
    612612
     613# The numeric mode to set newly-collected static files to. The value should be
     614# a mode you'd pass directly to os.chmod; see
     615# http://docs.python.org/lib/os-file-dir.html.
     616STATIC_FILE_PERMISSIONS = None
     617
    613618##############
    614619# MIGRATIONS #
    615620##############
  • django/contrib/staticfiles/storage.py

    diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py
    index a527379..15984c0 100644
    a b class StaticFilesStorage(FileSystemStorage):  
    2828    ``STATIC_ROOT`` and ``STATIC_URL``.
    2929    """
    3030    def __init__(self, location=None, base_url=None, *args, **kwargs):
     31        self.permissions_mode = settings.STATIC_FILE_PERMISSIONS
    3132        if location is None:
    3233            location = settings.STATIC_ROOT
    3334        if base_url is None:
  • django/core/files/storage.py

    diff --git a/django/core/files/storage.py b/django/core/files/storage.py
    index b42981c..295a634 100644
    a b class FileSystemStorage(Storage):  
    146146    """
    147147    Standard filesystem storage
    148148    """
     149    permissions_mode = None
    149150
    150151    def __init__(self, location=None, base_url=None):
    151152        if location is None:
    class FileSystemStorage(Storage):  
    171172            try:
    172173                if settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS is not None:
    173174                    # os.makedirs applies the global umask, so we reset it,
    174                     # for consistency with FILE_UPLOAD_PERMISSIONS behavior.
     175                    # for consistency with self.permissions_mode behavior.
    175176                    old_umask = os.umask(0)
    176177                    try:
    177178                        os.makedirs(directory, settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS)
    class FileSystemStorage(Storage):  
    232233                # OK, the file save worked. Break out of the loop.
    233234                break
    234235
    235         if settings.FILE_UPLOAD_PERMISSIONS is not None:
     236        if self.permissions_mode is not None:
     237            os.chmod(full_path, self.permissions_mode)
     238        elif settings.FILE_UPLOAD_PERMISSIONS:
    236239            os.chmod(full_path, settings.FILE_UPLOAD_PERMISSIONS)
    237240
    238241        return name
  • docs/ref/contrib/staticfiles.txt

    diff --git a/docs/ref/contrib/staticfiles.txt b/docs/ref/contrib/staticfiles.txt
    index 1a9fd25..4d8a954 100644
    a b following settings:  
    2828* :setting:`STATICFILES_DIRS`
    2929* :setting:`STATICFILES_STORAGE`
    3030* :setting:`STATICFILES_FINDERS`
     31* :setting:`STATIC_FILE_PERMISSIONS`
    3132
    3233Management Commands
    3334===================
    files:  
    322323
    323324.. _staticfiles-development-view:
    324325
     326Permissions
     327===========
     328
     329When running :djadmin:`collectstatic` management command, the newly-collected
     330static files will derive their permissions from
     331:setting:`STATIC_FILE_PERMISSIONS`. If it is ``None``, they will get the
     332permissions from :setting:`FILE_UPLOAD_PERMISSIONS`.
     333
    325334Static file development view
    326335----------------------------
    327336
  • docs/ref/settings.txt

    diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt
    index 4c46f3a..ffda162 100644
    a b setting.  
    26782678Static file finders are currently considered a private interface, and this
    26792679interface is thus undocumented.
    26802680
     2681STATIC_FILE_PERMISSIONS
     2682-----------------------
     2683
     2684Default: ``None``
     2685
     2686The numeric mode (i.e. ``0644``) to set newly-collected static files to. For
     2687more information about what these modes mean, see the documentation for
     2688:func:`os.chmod`.
     2689
     2690If this isn't given or is ``None``, you'll get FILE_UPLOAD_PERMISSIONS
     2691permission.
     2692
     2693.. warning::
     2694
     2695    **Always prefix the mode with a 0.**
     2696
     2697    If you're not familiar with file modes, please note that the leading
     2698    ``0`` is very important: it indicates an octal number, which is the
     2699    way that modes must be specified. If you try to use ``644``, you'll
     2700    get totally incorrect behavior.
     2701
    26812702Core Settings Topical Index
    26822703===========================
    26832704
  • docs/releases/1.7.txt

    diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt
    index 7a49f13..0b7d357 100644
    a b Signals  
    337337* The ``enter`` argument was added to the
    338338  :data:`~django.test.signals.setting_changed` signal.
    339339
     340Static File
     341^^^^^^^^^^^
     342
     343* The new :setting:`STATIC_FILE_PERMISSIONS` controls the file system
     344  permissions of newly-collected static files during deployment.
     345
    340346Templates
    341347^^^^^^^^^
    342348
  • tests/staticfiles_tests/tests.py

    diff --git a/tests/staticfiles_tests/tests.py b/tests/staticfiles_tests/tests.py
    index b73eea3..d1b852d 100644
    a b import posixpath  
    77import shutil
    88import sys
    99import tempfile
     10import unittest
    1011
    1112from django.template import loader, Context
    1213from django.conf import settings
    from django.utils._os import rmtree_errorhandler, upath  
    2122from django.utils import six
    2223
    2324from django.contrib.staticfiles import finders, storage
     25from django.contrib.staticfiles.management.commands import collectstatic
    2426
    2527TEST_ROOT = os.path.dirname(upath(__file__))
    2628TEST_SETTINGS = {
    class TestAppStaticStorage(TestCase):  
    804806            st.path('bar')
    805807        finally:
    806808            sys.getfilesystemencoding = old_enc_func
     809
     810
     811@unittest.skipIf(sys.platform.startswith('win'),
     812    "Windows only partially supports umasks and chmod.")
     813class TestStaticFilePermissions(BaseCollectionTestCase, StaticFilesTestCase):
     814
     815    @override_settings(STATIC_FILE_PERMISSIONS=0o654)
     816    def test_collect_static_file_permissions(self):
     817        command = collectstatic.Command()
     818        command.execute(**self.defaults)
     819        mode = os.stat(
     820                settings.STATIC_ROOT + os.path.sep + "test.txt")[0] & 0o777
     821        self.assertEqual(mode, 0o654)
     822
     823    @override_settings(STATIC_FILE_PERMISSIONS=None,
     824                       FILE_UPLOAD_PERMISSIONS=0o655)
     825    def test_collect_static_file_default_permissions(self):
     826        command = collectstatic.Command()
     827        command.execute(**self.defaults)
     828        mode = os.stat(
     829                settings.STATIC_ROOT + os.path.sep + "test.txt")[0] & 0o777
     830        self.assertEqual(mode, 0o655)
     831
     832    # It is very hard to pass settings to external process, so we use instance
     833    # of collectstatic command directly to collect static files.
     834    def run_collectstatic(self, **kwargs):
     835        self.defaults = {'interactive': False,
     836                         'post_process': True,
     837                         'verbosity': '0',
     838                         'ignore_patterns': ['*.ignoreme'],
     839                         'use_default_ignore_patterns': True,
     840                         'clear': False,
     841                         'link': False,
     842                         'dry_run': False}
Back to Top