Code

Ticket #12323: 12323.1.diff

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

Alternative patch - introduce USER_MEDIA_ROOT and USER_MEDIA_URL

Line 
1diff -r 4dd0391a8f6b django/conf/global_settings.py
2--- a/django/conf/global_settings.py    Mon May 24 19:10:30 2010 +0000
3+++ b/django/conf/global_settings.py    Fri May 28 13:14:10 2010 +0100
4@@ -265,6 +265,14 @@
5 # Example: "http://media.lawrence.com"
6 MEDIA_URL = ''
7 
8+# Absolute path to the directory that holds user uploaded media.
9+# If None, this will default to MEDIA_ROOT
10+USER_MEDIA_ROOT = None
11+
12+# URL that handles the media serverd from USER_MEDIA_ROOT
13+# If None, this will default to MEDIA_URL
14+USER_MEDIA_URL = None
15+
16 # List of upload handler classes to be applied in order.
17 FILE_UPLOAD_HANDLERS = (
18     'django.core.files.uploadhandler.MemoryFileUploadHandler',
19diff -r 4dd0391a8f6b django/conf/project_template/settings.py
20--- a/django/conf/project_template/settings.py  Mon May 24 19:10:30 2010 +0000
21+++ b/django/conf/project_template/settings.py  Fri May 28 13:14:10 2010 +0100
22@@ -43,15 +43,20 @@
23 # calendars according to the current locale
24 USE_L10N = True
25 
26-# Absolute path to the directory that holds media.
27-# Example: "/home/media/media.lawrence.com/"
28-MEDIA_ROOT = ''
29-
30 # URL that handles the media served from MEDIA_ROOT. Make sure to use a
31 # trailing slash if there is a path component (optional in other cases).
32 # Examples: "http://media.lawrence.com", "http://example.com/media/"
33 MEDIA_URL = ''
34 
35+# Absolute path to the directory that holds user uploaded media.
36+# Example: "/home/media/media.lawrence.com/"
37+USER_MEDIA_ROOT = ''
38+
39+# URL that handles the media served from USER_MEDIA_ROOT. Make sure to use a
40+# trailing slash if there is a path component (optional in other cases).
41+# Examples: "http://media.lawrence.com", "http://example.com/media/"
42+USER_MEDIA_URL = ''
43+
44 # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
45 # trailing slash.
46 # Examples: "http://foo.com/media/", "/media/".
47diff -r 4dd0391a8f6b django/core/context_processors.py
48--- a/django/core/context_processors.py Mon May 24 19:10:30 2010 +0000
49+++ b/django/core/context_processors.py Fri May 28 13:14:10 2010 +0100
50@@ -10,6 +10,7 @@
51 from django.conf import settings
52 from django.middleware.csrf import get_token
53 from django.utils.functional import lazy
54+from django.utils.media import get_user_media_url
55 
56 def auth(request):
57     """
58diff -r 4dd0391a8f6b django/core/files/storage.py
59--- a/django/core/files/storage.py      Mon May 24 19:10:30 2010 +0000
60+++ b/django/core/files/storage.py      Fri May 28 13:14:10 2010 +0100
61@@ -10,6 +10,7 @@
62 from django.utils.encoding import force_unicode
63 from django.utils.functional import LazyObject
64 from django.utils.importlib import import_module
65+from django.utils.media import get_user_media_url, get_user_media_root
66 from django.utils.text import get_valid_filename
67 from django.utils._os import safe_join
68 
69@@ -127,9 +128,9 @@
70 
71     def __init__(self, location=None, base_url=None):
72         if location is None:
73-            location = settings.MEDIA_ROOT
74+            location = get_user_media_root()
75         if base_url is None:
76-            base_url = settings.MEDIA_URL
77+            base_url = get_user_media_url()
78         self.location = os.path.abspath(location)
79         self.base_url = base_url
80 
81diff -r 4dd0391a8f6b django/utils/media.py
82--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
83+++ b/django/utils/media.py     Fri May 28 13:14:10 2010 +0100
84@@ -0,0 +1,25 @@
85+
86+def get_user_media_url():
87+    from django.conf import settings
88+    url = settings.USER_MEDIA_URL
89+    if url is None:
90+        import warnings
91+        warnings.warn(
92+            'Please define the USER_MEDIA_URL setting (falling back to MEDIA_URL for now).',
93+            PendingDeprecationWarning
94+            )
95+        return settings.MEDIA_ROOT
96+        return settings.MEDIA_URL
97+    return url
98+
99+def get_user_media_root():
100+    from django.conf import settings
101+    root = settings.USER_MEDIA_ROOT
102+    if root is None:
103+        import warnings
104+        warnings.warn(
105+            'The MEDIA_ROOT setting has been deprecated in favor of USER_MEDIA_ROOT.',
106+            PendingDeprecationWarning
107+            )
108+        return settings.MEDIA_ROOT
109+    return root
110diff -r 4dd0391a8f6b docs/faq/usage.txt
111--- a/docs/faq/usage.txt        Mon May 24 19:10:30 2010 +0000
112+++ b/docs/faq/usage.txt        Fri May 28 13:14:10 2010 +0100
113@@ -48,21 +48,21 @@
114 Using a :class:`~django.db.models.FileField` or an
115 :class:`~django.db.models.ImageField` in a model takes a few steps:
116 
117-    #. In your settings file, you'll need to define :setting:`MEDIA_ROOT` as
118-       the full path to a directory where you'd like Django to store uploaded
119+    #. In your settings file, you'll need to define :setting:`USER_MEDIA_ROOT`
120+       as the full path to a directory where you'd like Django to store uploaded
121        files. (For performance, these files are not stored in the database.)
122-       Define :setting:`MEDIA_URL` as the base public URL of that directory.
123-       Make sure that this directory is writable by the Web server's user
124-       account.
125+       Define :setting:`USER_MEDIA_URL` as the base public URL of that
126+       directory.  Make sure that this directory is writable by the Web server's
127+       user account.
128 
129     #. Add the :class:`~django.db.models.FileField` or
130        :class:`~django.db.models.ImageField` to your model, making sure to
131        define the :attr:`~django.db.models.FileField.upload_to` option to tell
132-       Django to which subdirectory of :setting:`MEDIA_ROOT` it should upload
133-       files.
134+       Django to which subdirectory of :setting:`USER_MEDIA_ROOT` it should
135+       upload files.
136 
137-    #. All that will be stored in your database is a path to the file
138-       (relative to :setting:`MEDIA_ROOT`). You'll most likely want to use the
139+    #. All that will be stored in your database is a path to the file (relative
140+       to :setting:`USER_MEDIA_ROOT`). You'll most likely want to use the
141        convenience :attr:`~django.core.files.File.url` attribute provided by
142        Django. For example, if your :class:`~django.db.models.ImageField` is
143        called ``mug_shot``, you can get the absolute URL to your image in a
144diff -r 4dd0391a8f6b docs/ref/files/file.txt
145--- a/docs/ref/files/file.txt   Mon May 24 19:10:30 2010 +0000
146+++ b/docs/ref/files/file.txt   Fri May 28 13:14:10 2010 +0100
147@@ -14,7 +14,7 @@
148 
149 .. attribute:: File.name
150 
151-    The name of file including the relative path from :setting:`MEDIA_ROOT`.
152+    The name of file including the relative path from :setting:`USER_MEDIA_ROOT`.
153 
154 .. attribute:: File.path
155 
156diff -r 4dd0391a8f6b docs/ref/models/fields.txt
157--- a/docs/ref/models/fields.txt        Mon May 24 19:10:30 2010 +0000
158+++ b/docs/ref/models/fields.txt        Fri May 28 13:14:10 2010 +0100
159@@ -477,9 +477,9 @@
160 
161 .. attribute:: FileField.upload_to
162 
163-    A local filesystem path that will be appended to your :setting:`MEDIA_ROOT`
164-    setting to determine the value of the :attr:`~django.core.files.File.url`
165-    attribute.
166+    A local filesystem path that will be appended to your
167+    :setting:`USER_MEDIA_ROOT` setting to determine the value of the
168+    :attr:`~django.core.files.File.url` attribute.
169 
170     This path may contain `strftime formatting`_, which will be replaced by the
171     date/time of the file upload (so that uploaded files don't fill up the given
172@@ -526,24 +526,25 @@
173 Using a :class:`FileField` or an :class:`ImageField` (see below) in a model
174 takes a few steps:
175 
176-    1. In your settings file, you'll need to define :setting:`MEDIA_ROOT` as the
177-       full path to a directory where you'd like Django to store uploaded files.
178-       (For performance, these files are not stored in the database.) Define
179-       :setting:`MEDIA_URL` as the base public URL of that directory. Make sure
180-       that this directory is writable by the Web server's user account.
181+    1. In your settings file, you'll need to define :setting:`USER_MEDIA_ROOT`
182+       as the full path to a directory where you'd like Django to store uploaded
183+       files.  (For performance, these files are not stored in the database.)
184+       Define :setting:`USER_MEDIA_URL` as the base public URL of that
185+       directory. Make sure that this directory is writable by the Web server's
186+       user account.
187 
188     2. Add the :class:`FileField` or :class:`ImageField` to your model, making
189        sure to define the :attr:`~FileField.upload_to` option to tell Django
190-       to which subdirectory of :setting:`MEDIA_ROOT` it should upload files.
191+       to which subdirectory of :setting:`USER_MEDIA_ROOT` it should upload files.
192 
193     3. All that will be stored in your database is a path to the file
194-       (relative to :setting:`MEDIA_ROOT`). You'll most likely want to use the
195+       (relative to :setting:`USER_MEDIA_ROOT`). You'll most likely want to use the
196        convenience :attr:`~django.core.files.File.url` function provided by
197        Django. For example, if your :class:`ImageField` is called ``mug_shot``,
198        you can get the absolute URL to your image in a template with
199        ``{{ object.mug_shot.url }}``.
200 
201-For example, say your :setting:`MEDIA_ROOT` is set to ``'/home/media'``, and
202+For example, say your :setting:`USER_MEDIA_ROOT` is set to ``'/home/media'``, and
203 :attr:`~FileField.upload_to` is set to ``'photos/%Y/%m/%d'``. The ``'%Y/%m/%d'``
204 part of :attr:`~FileField.upload_to` is `strftime formatting`_; ``'%Y'`` is the
205 four-digit year, ``'%m'`` is the two-digit month and ``'%d'`` is the two-digit
206@@ -555,6 +556,10 @@
207 :attr:`~django.core.files.File.name`, :attr:`~django.core.files.File.url`
208 and :attr:`~django.core.files.File.size` attributes; see :ref:`topics-files`.
209 
210+.. versionchanged:: 1.3
211+   Up to Django 1.2, the ``MEDIA_ROOT`` and ``MEDIA_URL`` settings were used for
212+   file storage, instead of ``USER_MEDIA_ROOT`` and ``USER_MEDIA_URL``
213+
214 Note that whenever you deal with uploaded files, you should pay close attention
215 to where you're uploading them and what type of files they are, to avoid
216 security holes. *Validate all uploaded files* so that you're sure the files are
217diff -r 4dd0391a8f6b docs/ref/settings.txt
218--- a/docs/ref/settings.txt     Mon May 24 19:10:30 2010 +0000
219+++ b/docs/ref/settings.txt     Fri May 28 13:14:10 2010 +0100
220@@ -1033,9 +1033,12 @@
221 
222 Default: ``''`` (Empty string)
223 
224-Absolute path to the directory that holds media for this installation.
225+Absolute path to the directory that holds static media for this installation.
226 Example: ``"/home/media/media.lawrence.com/"`` See also ``MEDIA_URL``.
227 
228+.. deprecated:: 1.3
229+   This is now used only for backwards compatibility, see ``USER_MEDIA_ROOT``
230+
231 .. setting:: MEDIA_URL
232 
233 MEDIA_URL
234@@ -1043,7 +1046,7 @@
235 
236 Default: ``''`` (Empty string)
237 
238-URL that handles the media served from ``MEDIA_ROOT``.
239+URL that handles the static media stored in ``MEDIA_ROOT``.
240 Example: ``"http://media.lawrence.com"``
241 
242 Note that this should have a trailing slash if it has a path component.
243@@ -1051,6 +1054,8 @@
244 Good: ``"http://www.example.com/static/"``
245 Bad: ``"http://www.example.com/static"``
246 
247+See also ``USER_MEDIA_URL``
248+
249 .. setting:: MIDDLEWARE_CLASSES
250 
251 MESSAGE_LEVEL
252@@ -1757,3 +1762,33 @@
253    This setting has been replaced by :setting:`TEST_NAME` in
254    :setting:`DATABASES`.
255 
256+.. setting:: USER_MEDIA_ROOT
257+
258+USER_MEDIA_ROOT
259+---------------
260+
261+.. versionadded:: 1.3
262+
263+Default: ``None``
264+
265+Absolute path to the directory that holds user uploaded media for this installation.
266+Example: ``"/home/media/media.lawrence.com/user/"`` See also ``USER_MEDIA_URL``.
267+
268+If ``None``, the value of ``MEDIA_ROOT`` is used as a fallback for this setting.
269+
270+.. setting:: USER_MEDIA_URL
271+
272+USER_MEDIA_URL
273+--------------
274+
275+Default: ``None``
276+
277+URL that handles the media stored in ``USER_MEDIA_ROOT``.
278+Example: ``"http://media.lawrence.com"``
279+
280+Note that this should have a trailing slash if it has a path component.
281+
282+Good: ``"http://www.example.com/usermedia/"``
283+Bad: ``"http://www.example.com/usermedia"``
284+
285+See also ``MEDIA_URL``
286diff -r 4dd0391a8f6b docs/topics/files.txt
287--- a/docs/topics/files.txt     Mon May 24 19:10:30 2010 +0000
288+++ b/docs/topics/files.txt     Fri May 28 13:14:10 2010 +0100
289@@ -8,9 +8,13 @@
290 
291 This document describes Django's file access APIs.
292 
293-By default, Django stores files locally, using the :setting:`MEDIA_ROOT` and
294-:setting:`MEDIA_URL` settings. The examples below assume that you're using these
295-defaults.
296+.. versionchanged:: 1.3
297+   Up to Django 1.2, the ``MEDIA_ROOT`` and ``MEDIA_URL`` settings were used for
298+   file storage, instead of ``USER_MEDIA_ROOT`` and ``USER_MEDIA_URL``
299+
300+By default, Django stores files locally, using the :setting:`USER_MEDIA_ROOT`
301+and :setting:`USER_MEDIA_URL` settings. The examples below assume that you're
302+using these defaults.
303 
304 However, Django provides ways to write custom `file storage systems`_ that
305 allow you to completely customize where and how Django stores files. The
306@@ -125,14 +129,14 @@
307 ======================  ===================================================
308 ``location``            Optional. Absolute path to the directory that will
309                         hold the files. If omitted, it will be set to the
310-                        value of your ``MEDIA_ROOT`` setting.
311+                        value of your ``USER_MEDIA_ROOT`` setting.
312 ``base_url``            Optional. URL that serves the files stored at this
313                         location. If omitted, it will default to the value
314-                        of your ``MEDIA_URL`` setting.
315+                        of your ``USER_MEDIA_URL`` setting.
316 ======================  ===================================================
317 
318 For example, the following code will store uploaded files under
319-``/media/photos`` regardless of what your ``MEDIA_ROOT`` setting is::
320+``/media/photos`` regardless of what your ``USER_MEDIA_ROOT`` setting is::
321 
322     from django.db import models
323     from django.core.files.storage import FileSystemStorage