Ticket #12323: 12323.1.diff

File 12323.1.diff, 13.6 KB (added by lukeplant, 5 years ago)

Alternative patch - introduce USER_MEDIA_ROOT and USER_MEDIA_URL

  • django/conf/global_settings.py

    diff -r 4dd0391a8f6b django/conf/global_settings.py
    a b  
    265265# Example: "http://media.lawrence.com"
    266266MEDIA_URL = ''
    267267
     268# Absolute path to the directory that holds user uploaded media.
     269# If None, this will default to MEDIA_ROOT
     270USER_MEDIA_ROOT = None
     271
     272# URL that handles the media serverd from USER_MEDIA_ROOT
     273# If None, this will default to MEDIA_URL
     274USER_MEDIA_URL = None
     275
    268276# List of upload handler classes to be applied in order.
    269277FILE_UPLOAD_HANDLERS = (
    270278    'django.core.files.uploadhandler.MemoryFileUploadHandler',
  • django/conf/project_template/settings.py

    diff -r 4dd0391a8f6b django/conf/project_template/settings.py
    a b  
    4343# calendars according to the current locale
    4444USE_L10N = True
    4545
    46 # Absolute path to the directory that holds media.
    47 # Example: "/home/media/media.lawrence.com/"
    48 MEDIA_ROOT = ''
    49 
    5046# URL that handles the media served from MEDIA_ROOT. Make sure to use a
    5147# trailing slash if there is a path component (optional in other cases).
    5248# Examples: "http://media.lawrence.com", "http://example.com/media/"
    5349MEDIA_URL = ''
    5450
     51# Absolute path to the directory that holds user uploaded media.
     52# Example: "/home/media/media.lawrence.com/"
     53USER_MEDIA_ROOT = ''
     54
     55# URL that handles the media served from USER_MEDIA_ROOT. Make sure to use a
     56# trailing slash if there is a path component (optional in other cases).
     57# Examples: "http://media.lawrence.com", "http://example.com/media/"
     58USER_MEDIA_URL = ''
     59
    5560# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
    5661# trailing slash.
    5762# Examples: "http://foo.com/media/", "/media/".
  • django/core/context_processors.py

    diff -r 4dd0391a8f6b django/core/context_processors.py
    a b  
    1010from django.conf import settings
    1111from django.middleware.csrf import get_token
    1212from django.utils.functional import lazy
     13from django.utils.media import get_user_media_url
    1314
    1415def auth(request):
    1516    """
  • django/core/files/storage.py

    diff -r 4dd0391a8f6b django/core/files/storage.py
    a b  
    1010from django.utils.encoding import force_unicode
    1111from django.utils.functional import LazyObject
    1212from django.utils.importlib import import_module
     13from django.utils.media import get_user_media_url, get_user_media_root
    1314from django.utils.text import get_valid_filename
    1415from django.utils._os import safe_join
    1516
     
    127128
    128129    def __init__(self, location=None, base_url=None):
    129130        if location is None:
    130             location = settings.MEDIA_ROOT
     131            location = get_user_media_root()
    131132        if base_url is None:
    132             base_url = settings.MEDIA_URL
     133            base_url = get_user_media_url()
    133134        self.location = os.path.abspath(location)
    134135        self.base_url = base_url
    135136
  • new file django/utils/media.py

    diff -r 4dd0391a8f6b django/utils/media.py
    - +  
     1
     2def get_user_media_url():
     3    from django.conf import settings
     4    url = settings.USER_MEDIA_URL
     5    if url is None:
     6        import warnings
     7        warnings.warn(
     8            'Please define the USER_MEDIA_URL setting (falling back to MEDIA_URL for now).',
     9            PendingDeprecationWarning
     10            )
     11        return settings.MEDIA_ROOT
     12        return settings.MEDIA_URL
     13    return url
     14
     15def get_user_media_root():
     16    from django.conf import settings
     17    root = settings.USER_MEDIA_ROOT
     18    if root is None:
     19        import warnings
     20        warnings.warn(
     21            'The MEDIA_ROOT setting has been deprecated in favor of USER_MEDIA_ROOT.',
     22            PendingDeprecationWarning
     23            )
     24        return settings.MEDIA_ROOT
     25    return root
  • docs/faq/usage.txt

    diff -r 4dd0391a8f6b docs/faq/usage.txt
    a b  
    4848Using a :class:`~django.db.models.FileField` or an
    4949:class:`~django.db.models.ImageField` in a model takes a few steps:
    5050
    51     #. In your settings file, you'll need to define :setting:`MEDIA_ROOT` as
    52        the full path to a directory where you'd like Django to store uploaded
     51    #. In your settings file, you'll need to define :setting:`USER_MEDIA_ROOT`
     52       as the full path to a directory where you'd like Django to store uploaded
    5353       files. (For performance, these files are not stored in the database.)
    54        Define :setting:`MEDIA_URL` as the base public URL of that directory.
    55        Make sure that this directory is writable by the Web server's user
    56        account.
     54       Define :setting:`USER_MEDIA_URL` as the base public URL of that
     55       directory.  Make sure that this directory is writable by the Web server's
     56       user account.
    5757
    5858    #. Add the :class:`~django.db.models.FileField` or
    5959       :class:`~django.db.models.ImageField` to your model, making sure to
    6060       define the :attr:`~django.db.models.FileField.upload_to` option to tell
    61        Django to which subdirectory of :setting:`MEDIA_ROOT` it should upload
    62        files.
     61       Django to which subdirectory of :setting:`USER_MEDIA_ROOT` it should
     62       upload files.
    6363
    64     #. All that will be stored in your database is a path to the file
    65        (relative to :setting:`MEDIA_ROOT`). You'll most likely want to use the
     64    #. All that will be stored in your database is a path to the file (relative
     65       to :setting:`USER_MEDIA_ROOT`). You'll most likely want to use the
    6666       convenience :attr:`~django.core.files.File.url` attribute provided by
    6767       Django. For example, if your :class:`~django.db.models.ImageField` is
    6868       called ``mug_shot``, you can get the absolute URL to your image in a
  • docs/ref/files/file.txt

    diff -r 4dd0391a8f6b docs/ref/files/file.txt
    a b  
    1414
    1515.. attribute:: File.name
    1616
    17     The name of file including the relative path from :setting:`MEDIA_ROOT`.
     17    The name of file including the relative path from :setting:`USER_MEDIA_ROOT`.
    1818
    1919.. attribute:: File.path
    2020
  • docs/ref/models/fields.txt

    diff -r 4dd0391a8f6b docs/ref/models/fields.txt
    a b  
    477477
    478478.. attribute:: FileField.upload_to
    479479
    480     A local filesystem path that will be appended to your :setting:`MEDIA_ROOT`
    481     setting to determine the value of the :attr:`~django.core.files.File.url`
    482     attribute.
     480    A local filesystem path that will be appended to your
     481    :setting:`USER_MEDIA_ROOT` setting to determine the value of the
     482    :attr:`~django.core.files.File.url` attribute.
    483483
    484484    This path may contain `strftime formatting`_, which will be replaced by the
    485485    date/time of the file upload (so that uploaded files don't fill up the given
     
    526526Using a :class:`FileField` or an :class:`ImageField` (see below) in a model
    527527takes a few steps:
    528528
    529     1. In your settings file, you'll need to define :setting:`MEDIA_ROOT` as the
    530        full path to a directory where you'd like Django to store uploaded files.
    531        (For performance, these files are not stored in the database.) Define
    532        :setting:`MEDIA_URL` as the base public URL of that directory. Make sure
    533        that this directory is writable by the Web server's user account.
     529    1. In your settings file, you'll need to define :setting:`USER_MEDIA_ROOT`
     530       as the full path to a directory where you'd like Django to store uploaded
     531       files.  (For performance, these files are not stored in the database.)
     532       Define :setting:`USER_MEDIA_URL` as the base public URL of that
     533       directory. Make sure that this directory is writable by the Web server's
     534       user account.
    534535
    535536    2. Add the :class:`FileField` or :class:`ImageField` to your model, making
    536537       sure to define the :attr:`~FileField.upload_to` option to tell Django
    537        to which subdirectory of :setting:`MEDIA_ROOT` it should upload files.
     538       to which subdirectory of :setting:`USER_MEDIA_ROOT` it should upload files.
    538539
    539540    3. All that will be stored in your database is a path to the file
    540        (relative to :setting:`MEDIA_ROOT`). You'll most likely want to use the
     541       (relative to :setting:`USER_MEDIA_ROOT`). You'll most likely want to use the
    541542       convenience :attr:`~django.core.files.File.url` function provided by
    542543       Django. For example, if your :class:`ImageField` is called ``mug_shot``,
    543544       you can get the absolute URL to your image in a template with
    544545       ``{{ object.mug_shot.url }}``.
    545546
    546 For example, say your :setting:`MEDIA_ROOT` is set to ``'/home/media'``, and
     547For example, say your :setting:`USER_MEDIA_ROOT` is set to ``'/home/media'``, and
    547548:attr:`~FileField.upload_to` is set to ``'photos/%Y/%m/%d'``. The ``'%Y/%m/%d'``
    548549part of :attr:`~FileField.upload_to` is `strftime formatting`_; ``'%Y'`` is the
    549550four-digit year, ``'%m'`` is the two-digit month and ``'%d'`` is the two-digit
     
    555556:attr:`~django.core.files.File.name`, :attr:`~django.core.files.File.url`
    556557and :attr:`~django.core.files.File.size` attributes; see :ref:`topics-files`.
    557558
     559.. versionchanged:: 1.3
     560   Up to Django 1.2, the ``MEDIA_ROOT`` and ``MEDIA_URL`` settings were used for
     561   file storage, instead of ``USER_MEDIA_ROOT`` and ``USER_MEDIA_URL``
     562
    558563Note that whenever you deal with uploaded files, you should pay close attention
    559564to where you're uploading them and what type of files they are, to avoid
    560565security holes. *Validate all uploaded files* so that you're sure the files are
  • docs/ref/settings.txt

    diff -r 4dd0391a8f6b docs/ref/settings.txt
    a b  
    10331033
    10341034Default: ``''`` (Empty string)
    10351035
    1036 Absolute path to the directory that holds media for this installation.
     1036Absolute path to the directory that holds static media for this installation.
    10371037Example: ``"/home/media/media.lawrence.com/"`` See also ``MEDIA_URL``.
    10381038
     1039.. deprecated:: 1.3
     1040   This is now used only for backwards compatibility, see ``USER_MEDIA_ROOT``
     1041
    10391042.. setting:: MEDIA_URL
    10401043
    10411044MEDIA_URL
     
    10431046
    10441047Default: ``''`` (Empty string)
    10451048
    1046 URL that handles the media served from ``MEDIA_ROOT``.
     1049URL that handles the static media stored in ``MEDIA_ROOT``.
    10471050Example: ``"http://media.lawrence.com"``
    10481051
    10491052Note that this should have a trailing slash if it has a path component.
     
    10511054Good: ``"http://www.example.com/static/"``
    10521055Bad: ``"http://www.example.com/static"``
    10531056
     1057See also ``USER_MEDIA_URL``
     1058
    10541059.. setting:: MIDDLEWARE_CLASSES
    10551060
    10561061MESSAGE_LEVEL
     
    17571762   This setting has been replaced by :setting:`TEST_NAME` in
    17581763   :setting:`DATABASES`.
    17591764
     1765.. setting:: USER_MEDIA_ROOT
     1766
     1767USER_MEDIA_ROOT
     1768---------------
     1769
     1770.. versionadded:: 1.3
     1771
     1772Default: ``None``
     1773
     1774Absolute path to the directory that holds user uploaded media for this installation.
     1775Example: ``"/home/media/media.lawrence.com/user/"`` See also ``USER_MEDIA_URL``.
     1776
     1777If ``None``, the value of ``MEDIA_ROOT`` is used as a fallback for this setting.
     1778
     1779.. setting:: USER_MEDIA_URL
     1780
     1781USER_MEDIA_URL
     1782--------------
     1783
     1784Default: ``None``
     1785
     1786URL that handles the media stored in ``USER_MEDIA_ROOT``.
     1787Example: ``"http://media.lawrence.com"``
     1788
     1789Note that this should have a trailing slash if it has a path component.
     1790
     1791Good: ``"http://www.example.com/usermedia/"``
     1792Bad: ``"http://www.example.com/usermedia"``
     1793
     1794See also ``MEDIA_URL``
  • docs/topics/files.txt

    diff -r 4dd0391a8f6b docs/topics/files.txt
    a b  
    88
    99This document describes Django's file access APIs.
    1010
    11 By default, Django stores files locally, using the :setting:`MEDIA_ROOT` and
    12 :setting:`MEDIA_URL` settings. The examples below assume that you're using these
    13 defaults.
     11.. versionchanged:: 1.3
     12   Up to Django 1.2, the ``MEDIA_ROOT`` and ``MEDIA_URL`` settings were used for
     13   file storage, instead of ``USER_MEDIA_ROOT`` and ``USER_MEDIA_URL``
     14
     15By default, Django stores files locally, using the :setting:`USER_MEDIA_ROOT`
     16and :setting:`USER_MEDIA_URL` settings. The examples below assume that you're
     17using these defaults.
    1418
    1519However, Django provides ways to write custom `file storage systems`_ that
    1620allow you to completely customize where and how Django stores files. The
     
    125129======================  ===================================================
    126130``location``            Optional. Absolute path to the directory that will
    127131                        hold the files. If omitted, it will be set to the
    128                         value of your ``MEDIA_ROOT`` setting.
     132                        value of your ``USER_MEDIA_ROOT`` setting.
    129133``base_url``            Optional. URL that serves the files stored at this
    130134                        location. If omitted, it will default to the value
    131                         of your ``MEDIA_URL`` setting.
     135                        of your ``USER_MEDIA_URL`` setting.
    132136======================  ===================================================
    133137
    134138For example, the following code will store uploaded files under
    135 ``/media/photos`` regardless of what your ``MEDIA_ROOT`` setting is::
     139``/media/photos`` regardless of what your ``USER_MEDIA_ROOT`` setting is::
    136140
    137141    from django.db import models
    138142    from django.core.files.storage import FileSystemStorage
Back to Top