Django

Code

Changeset 8640

Show
Ignore:
Timestamp:
08/27/08 17:21:14 (3 months ago)
Author:
jacob
Message:

Fixed #8454: added a FILE_UPLOAD_PERMISSIONS setting to control the permissoin of files uploaded by the built-in file storage system. Thanks, dcwatson.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/conf/global_settings.py

    r8537 r8640  
    253253FILE_UPLOAD_TEMP_DIR = None 
    254254 
     255# The numeric mode to set newly-uploaded files to. The value should be a mode 
     256# you'd pass directly to os.chmod; see http://docs.python.org/lib/os-file-dir.html. 
     257FILE_UPLOAD_PERMISSIONS = None 
     258 
    255259# Default formatting for date objects. See all available format strings here: 
    256260# http://www.djangoproject.com/documentation/templates/#now 
  • django/trunk/django/core/files/storage.py

    r8639 r8640  
    173173                # OK, the file save worked. Break out of the loop. 
    174174                break 
    175                  
     175         
     176        if settings.FILE_UPLOAD_PERMISSIONS is not None: 
     177            os.chmod(full_path, settings.FILE_UPLOAD_PERMISSIONS) 
     178         
    176179        return name 
    177180 
  • django/trunk/docs/ref/settings.txt

    r8612 r8640  
    454454template files and initial SQL data files. 
    455455 
     456.. setting:: FILE_UPLOAD_HANDLERS 
     457 
    456458FILE_UPLOAD_HANDLERS 
    457459-------------------- 
     
    466468A tuple of handlers to use for uploading. See :ref:`topics-files` for details. 
    467469 
     470.. setting:: FILE_UPLOAD_MAX_MEMORY_SIZE 
     471 
    468472FILE_UPLOAD_MAX_MEMORY_SIZE 
    469473--------------------------- 
     
    475479The maximum size (in bytes) that an upload will be before it gets streamed to 
    476480the file system. See :ref:`topics-files` for details. 
     481 
     482.. setting:: FILE_UPLOAD_TEMP_DIR 
    477483 
    478484FILE_UPLOAD_TEMP_DIR 
     
    488494 
    489495See :ref:`topics-files` for details. 
     496 
     497.. setting:: FILE_UPLOAD_PERMISSIONS 
     498 
     499FILE_UPLOAD_PERMISSIONS 
     500----------------------- 
     501 
     502Default: ``None`` 
     503 
     504The numeric mode (i.e. ``0644``) to set newly uploaded files to. For 
     505more information about what these modes mean, see the `documentation for 
     506os.chmod`_ 
     507 
     508If this isn't given or is ``None``, you'll get operating-system 
     509dependent behavior. On most platforms, temporary files will have a mode 
     510of ``0600``, and files saved from memory will be saved using the 
     511system's standard umask. 
     512 
     513.. warning:: 
     514 
     515    **Always prefix the mode with a 0.** 
     516     
     517    If you're not familiar with file modes, please note that the leading 
     518    ``0`` is very important: it indicates an octal number, which is the 
     519    way that modes must be specified. If you try to use ``644``, you'll 
     520    get totally incorrect behavior. 
     521     
     522 
     523.. _documentation for os.chmod: http://docs.python.org/lib/os-file-dir.html  
    490524 
    491525.. setting:: FIXTURE_DIRS 
  • django/trunk/docs/topics/http/file-uploads.txt

    r8506 r8640  
    123123Three settings control Django's file upload behavior: 
    124124 
    125     ``FILE_UPLOAD_MAX_MEMORY_SIZE`
    126         The maximum size, in bytes, for files that will be uploaded 
    127         into memory. Files larger than ``FILE_UPLOAD_MAX_MEMORY_SIZE`` 
    128         will be streamed to disk. 
     125    :setting:`FILE_UPLOAD_MAX_MEMORY_SIZE
     126        The maximum size, in bytes, for files that will be uploaded into memory. 
     127        Files larger than :setting:`FILE_UPLOAD_MAX_MEMORY_SIZE` will be 
     128        streamed to disk. 
    129129 
    130130        Defaults to 2.5 megabytes. 
    131131 
    132     ``FILE_UPLOAD_TEMP_DIR`
    133         The directory where uploaded files larger than ``FILE_UPLOAD_TEMP_DIR`` 
    134         will be stored. 
     132    :setting:`FILE_UPLOAD_TEMP_DIR
     133        The directory where uploaded files larger than 
     134        :setting:`FILE_UPLOAD_TEMP_DIR` will be stored. 
    135135 
    136136        Defaults to your system's standard temporary directory (i.e. ``/tmp`` on 
    137137        most Unix-like systems). 
    138  
    139     ``FILE_UPLOAD_HANDLERS`` 
    140         The actual handlers for uploaded files. Changing this setting 
    141         allows complete customization -- even replacement -- of 
    142         Django's upload process. See `upload handlers`_, below, 
    143         for details. 
     138         
     139    :setting:`FILE_UPLOAD_PERMISSIONS` 
     140        The numeric mode (i.e. ``0644``) to set newly uploaded files to. For 
     141        more information about what these modes mean, see the `documentation for 
     142        os.chmod`_ 
     143         
     144        If this isn't given or is ``None``, you'll get operating-system 
     145        dependent behavior. On most platforms, temporary files will have a mode 
     146        of ``0600``, and files saved from memory will be saved using the 
     147        system's standard umask. 
     148         
     149        .. warning:: 
     150         
     151            If you're not familiar with file modes, please note that the leading 
     152            ``0`` is very important: it indicates an octal number, which is the 
     153            way that modes must be specified. If you try to use ``644``, you'll 
     154            get totally incorrect behavior. 
     155             
     156            **Always prefix the mode with a ``0``.** 
     157 
     158    :setting:`FILE_UPLOAD_HANDLERS` 
     159        The actual handlers for uploaded files. Changing this setting allows 
     160        complete customization -- even replacement -- of Django's upload 
     161        process. See `upload handlers`_, below, for details. 
    144162 
    145163        Defaults to:: 
     
    150168        Which means "try to upload to memory first, then fall back to temporary 
    151169        files." 
     170 
     171.. _documentation for os.chmod: http://docs.python.org/lib/os-file-dir.html  
    152172 
    153173``UploadedFile`` objects 
  • django/trunk/tests/regressiontests/file_storage/tests.py

    r8636 r8640  
    8787# This is written in such a way that it'll always pass on platforms  
    8888# without threading. 
    89  
     89import os 
    9090import time 
    9191from unittest import TestCase 
     92from django.conf import settings 
    9293from django.core.files.base import ContentFile 
    9394from models import temp_storage 
     
    118119        temp_storage.delete('conflict_') 
    119120 
     121class FileStoragePermissions(TestCase): 
     122    def setUp(self): 
     123        self.old_perms = settings.FILE_UPLOAD_PERMISSIONS 
     124        settings.FILE_UPLOAD_PERMISSIONS = 0666 
     125         
     126    def test_file_upload_permissions(self): 
     127        name = temp_storage.save("the_file", ContentFile("data")) 
     128        actual_mode = os.stat(temp_storage.path(name))[0] & 0777 
     129        self.assertEqual(actual_mode, 0666) 
     130         
     131    def tearDown(self): 
     132        settings.FILE_UPLOAD_PERMISSIONS = self.old_perms