Skip to content

Fixed #21219 -- Added a way to set different permission for static files... #1779

Closed
wants to merge 2 commits into from

2 participants

@vajrasky

....

Previously, when collecting static files, the files would receive permission
from FILE_UPLOAD_PERMISSIONS. Now, user has an option to give different
permission from uploaded files permission by subclassing StaticFilesStorage
and setting file_permissions_mode class attribute to the desired permission.

@vajrasky vajrasky Fixed #21219 -- Added a way to set different permission for static fi…
…les.

Previously, when collecting static files, the files would receive permission
from FILE_UPLOAD_PERMISSIONS. Now, user has an option to give different
permission from uploaded files permission by subclassing StaticFilesStorage
and setting file_permissions_mode class attribute to the desired permission.
993dd87
@vajrasky vajrasky commented on an outdated diff Oct 20, 2013
django/core/files/storage.py
@@ -171,7 +171,7 @@ def _save(self, name, content):
try:
if settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS is not None:
# os.makedirs applies the global umask, so we reset it,
- # for consistency with FILE_UPLOAD_PERMISSIONS behavior.
+ # for consistency with file_permissions_mode behavior.
@vajrasky
vajrasky added a note Oct 20, 2013

I should not have changed this, since the FileSystemStorage would never use file_permissions_mode class attribute. I'll wait another review before I update this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on the diff Oct 21, 2013
django/core/files/storage.py
@@ -232,8 +232,14 @@ def _save(self, name, content):
# OK, the file save worked. Break out of the loop.
break
- if settings.FILE_UPLOAD_PERMISSIONS is not None:
- os.chmod(full_path, settings.FILE_UPLOAD_PERMISSIONS)
+ if self.__class__ is not FileSystemStorage and \
@timgraham
Django member
timgraham added a note Oct 21, 2013

if hasattr(self, 'file_permissions_mode'): ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 21, 2013
django/core/files/storage.py
@@ -232,8 +232,14 @@ def _save(self, name, content):
# OK, the file save worked. Break out of the loop.
break
- if settings.FILE_UPLOAD_PERMISSIONS is not None:
- os.chmod(full_path, settings.FILE_UPLOAD_PERMISSIONS)
+ if self.__class__ is not FileSystemStorage and \
+ 'file_permissions_mode' in self.__class__.__dict__:
+ mode = self.__class__.__dict__['file_permissions_mode']
@timgraham
Django member
timgraham added a note Oct 21, 2013

mode = self.file_permissions_mode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 21, 2013
docs/ref/contrib/staticfiles.txt
@@ -61,6 +61,25 @@ receives all command line options of :djadmin:`collectstatic`. This is used
by the :class:`~django.contrib.staticfiles.storage.CachedStaticFilesStorage`
by default.
+Files will receive permission from :setting:`FILE_UPLOAD_PERMISSIONS`. In case
+you want to differentiate permission for collecting static files from the
+permission for uploading files, you can subclass
+:class:`~django.contrib.staticfiles.storage.StaticFilesStorage` and set
+the class attribute `file_permissions_mode` to the desired permission. For
+example:
+
+ .. code-block:: css+django
@timgraham
Django member
timgraham added a note Oct 21, 2013

css+django? just use a double colon after "example" and you should be able to omit the ".. code-block" directive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 21, 2013
docs/ref/contrib/staticfiles.txt
@@ -61,6 +61,25 @@ receives all command line options of :djadmin:`collectstatic`. This is used
by the :class:`~django.contrib.staticfiles.storage.CachedStaticFilesStorage`
by default.
+Files will receive permission from :setting:`FILE_UPLOAD_PERMISSIONS`. In case
+you want to differentiate permission for collecting static files from the
+permission for uploading files, you can subclass
+:class:`~django.contrib.staticfiles.storage.StaticFilesStorage` and set
+the class attribute `file_permissions_mode` to the desired permission. For
+example:
+
+ .. code-block:: css+django
+
+ class CustomStaticFilesStorage(storage.StaticFilesStorage):
+ file_permissions_mode = 0o600
+
+Then set :setting:`STATICFILES_STORAGE` to :class:`CustomStaticFilesStorage`.
@timgraham
Django member
timgraham added a note Oct 21, 2013

remove :class: (it won't create a link and will generate a error when building the docs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@vajrasky vajrasky Made changes asked in review by Tim Graham.
- Simplified checking and assigning the class attribute.
- Fixed documentation link.
bfb3e3a
@timgraham
Django member

Updated in #1803

@timgraham timgraham closed this Oct 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.