Adding support for using a subdirectory of MEDIA_ROOT for file fields (#475)

* Adding support for using a subdirectory of MEDIA_ROOT for file fields with CONSTANCE_FILE_ROOT setting

* Improving documentation for CONSTANCE_FILE_ROOT

* Updating PR branch to work with latest master
This commit is contained in:
James Tiplady 2023-07-29 17:35:38 +01:00 committed by GitHub
parent 554dac0473
commit 6a5052e9f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 5 deletions

View file

@ -208,4 +208,4 @@ class Config:
_meta = Meta()
admin.site.register([Config], ConstanceAdmin)
admin.site.register([Config], ConstanceAdmin)

View file

@ -1,6 +1,7 @@
import hashlib
from datetime import date, datetime, time, timedelta
from decimal import Decimal
from os.path import join
from django import conf, forms
from django.contrib import messages
@ -116,7 +117,7 @@ class ConstanceForm(forms.Form):
def save(self):
for file_field in self.files:
file = self.cleaned_data[file_field]
self.cleaned_data[file_field] = default_storage.save(file.name, file)
self.cleaned_data[file_field] = default_storage.save(join(settings.FILE_ROOT, file.name), file)
for name in settings.CONFIG:
current = getattr(config, name)

View file

@ -14,6 +14,8 @@ CONFIG_FIELDSETS = getattr(settings, 'CONSTANCE_CONFIG_FIELDSETS', {})
ADDITIONAL_FIELDS = getattr(settings, 'CONSTANCE_ADDITIONAL_FIELDS', {})
FILE_ROOT = getattr(settings, 'CONSTANCE_FILE_ROOT', '')
DATABASE_CACHE_BACKEND = getattr(
settings,
'CONSTANCE_DATABASE_CACHE_BACKEND',

View file

@ -132,7 +132,7 @@ Note: Use later evaluated strings instead of direct classes for the field and wi
'MY_SELECT_KEY': ('yes', 'select yes or no', 'yes_no_null_select'),
}
If you want to work with files you can use this configuration:
If you want to work with images or files you can use this configuration:
.. code-block:: python
@ -153,8 +153,14 @@ When used in a template you probably need to use:
{% get_media_prefix as MEDIA_URL %}
<img src="{{ MEDIA_URL }}{{ config.LOGO_IMAGE }}">
Images are uploaded to MEDIA_ROOT.
Images and files are uploaded to ``MEDIA_ROOT`` by default. You can specify a subdirectory of ``MEDIA_ROOT`` to use instead by adding the ``CONSTANCE_FILE_ROOT`` setting. E.g.:
.. code-block:: python
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CONSTANCE_FILE_ROOT = 'constance'
This will result in files being placed in ``media/constance`` within your ``BASE_DIR``. You can use deeper nesting in this setting (e.g. ``constance/images``) but other relative path components (e.g. ``../``) will be rejected.
Ordered Fields in Django Admin
------------------------------

View file

@ -99,7 +99,8 @@ CONSTANCE_ADDITIONAL_FIELDS = {
}
],
'email': ('django.forms.fields.EmailField',),
'json_field': ['cheeseshop.fields.JsonField']
'json_field': ['cheeseshop.fields.JsonField'],
'image_field': ['django.forms.ImageField', {}],
}
CONSTANCE_CONFIG = {
@ -115,6 +116,11 @@ CONSTANCE_CONFIG = {
'Some test data for json',
'json_field',
),
'LOGO': (
'',
'Logo image file',
'image_field',
),
}
CONSTANCE_CONFIG_FIELDSETS = {
@ -124,6 +130,7 @@ CONSTANCE_CONFIG_FIELDSETS = {
'OWNER_EMAIL',
'MUSICIANS',
'DATE_ESTABLISHED',
'LOGO',
],
'Awkward test settings': ['MY_SELECT_KEY', 'MULTILINE', 'JSON_DATA'],
}
@ -158,4 +165,10 @@ USE_TZ = True
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CONSTANCE_FILE_ROOT = 'constance'
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

View file

@ -1,2 +1,3 @@
Django>=3.2
Pillow
pymemcache