Skip to content

Commit

Permalink
Use MEDIA_ROOT instead of FILE_UPLOAD_DIR setting. Related to #160
Browse files Browse the repository at this point in the history
- serve these files locally in DEBUG mode
- serve the uploads directory via Apache in production
  • Loading branch information
atodorov committed Dec 30, 2017
1 parent ccd4e22 commit 192bed3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
24 changes: 24 additions & 0 deletions contrib/conf/kiwi-httpd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,27 @@ Alias /static /usr/share/kiwi/static
ExpiresActive On
ExpiresDefault "access plus 10 years"
</Location>

# user uploaded files
Alias /uploads /var/kiwi/uploads

<Location "/uploads">
SetHandler None
Options -Indexes

# Disable auth on the static content, so that we're aren't forced to
# use Kerberos. Doing so would remove "Expires" headers from the static
# content, which would lead to poor page-load times.
AuthType none
Satisfy Any
Allow from All

# Many file types are likely to benefit from compression
# Enable gzip compression on them:
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript application/x-javascript text/css

# Set far-future Expires headers on static content
# (trac 184):
ExpiresActive On
ExpiresDefault "access plus 10 years"
</Location>
10 changes: 5 additions & 5 deletions tcms/core/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def upload_file(request):
stored_name = '%s-%s-%s' % (request.user.username, now, upload_file.name)

stored_file_name = os.path.join(
settings.FILE_UPLOAD_DIR, stored_name).replace('\\', '/')
settings.MEDIA_ROOT, stored_name).replace('\\', '/')
stored_file_name = smart_str(stored_file_name)

if upload_file.size > settings.MAX_UPLOAD_SIZE:
Expand All @@ -63,8 +63,8 @@ def upload_file(request):
)

# Create the upload directory when it's not exist
if not os.path.exists(settings.FILE_UPLOAD_DIR):
os.mkdir(settings.FILE_UPLOAD_DIR)
if not os.path.exists(settings.MEDIA_ROOT):
os.mkdir(settings.MEDIA_ROOT)

if os.path.exists(stored_file_name):
return Prompt.render(
Expand Down Expand Up @@ -143,7 +143,7 @@ def check_file(request, file_id):
except TestAttachmentData.DoesNotExist:
if attachment.stored_name:
stored_file_name = os.path.join(
settings.FILE_UPLOAD_DIR, unquote(attachment.stored_name)
settings.MEDIA_ROOT, unquote(attachment.stored_name)
).replace('\\', '/')
stored_file_name = stored_file_name.encode('utf-8')
try:
Expand All @@ -153,7 +153,7 @@ def check_file(request, file_id):
raise Http404(error)
else:
stored_file_name = os.path.join(
settings.FILE_UPLOAD_DIR, unquote(attachment.file_name)
settings.MEDIA_ROOT, unquote(attachment.file_name)
).replace('\\', '/')
stored_file_name = stored_file_name.encode('utf-8')
try:
Expand Down
4 changes: 2 additions & 2 deletions tcms/core/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_refuse_if_file_is_too_big(self):
def test_upload_file_to_plan(self):
self.client.login(username=self.user.username, password=self.password)

with patch('tcms.core.files.settings.FILE_UPLOAD_DIR',
with patch('tcms.core.files.settings.MEDIA_ROOT',
new=self.file_upload_dir):
with open(self.upload_filename, 'r') as upload_file:
response = self.client.post(self.upload_file_url,
Expand All @@ -117,7 +117,7 @@ def test_upload_file_to_plan(self):
def test_upload_file_to_case(self):
self.client.login(username=self.user.username, password=self.password)

with patch('tcms.core.files.settings.FILE_UPLOAD_DIR',
with patch('tcms.core.files.settings.MEDIA_ROOT',
new=self.file_upload_dir):
with open(self.upload_filename, 'r') as upload_file:
response = self.client.post(self.upload_file_url,
Expand Down
9 changes: 2 additions & 7 deletions tcms/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@
MAX_UPLOAD_SIZE = 5242880


# Attachement file download path
FILE_UPLOAD_DIR = '/var/kiwi/uploads'


# TCMS email templates
PLAN_EMAIL_TEMPLATE = 'mail/change_plan.txt'
PLAN_DELELE_EMAIL_TEMPLATE = 'mail/delete_plan.txt'
Expand Down Expand Up @@ -135,13 +131,12 @@
TIME_ZONE = 'Etc/UTC'

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = ''
MEDIA_ROOT = '/var/kiwi/uploads'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = ''
MEDIA_URL = '/uploads/'

# URL prefix for admin absolute URL
ADMIN_PREFIX = '/admin'
Expand Down
2 changes: 1 addition & 1 deletion tcms/settings/devel.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

INSTALLED_APPS += ['debug_toolbar', 'django_extensions'] # noqa: F405

FILE_UPLOAD_DIR = os.path.join(TCMS_ROOT_PATH, '..', 'uploads') # noqa: F405
MEDIA_ROOT = os.path.join(TCMS_ROOT_PATH, '..', 'uploads') # noqa: F405

# Needed by django.template.context_processors.debug:
# See:
Expand Down
3 changes: 3 additions & 0 deletions tcms/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.admindocs import urls as admindocs_urls
from django.views.i18n import JavaScriptCatalog
Expand Down Expand Up @@ -112,6 +113,8 @@
# Debug zone

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

try:
import debug_toolbar

Expand Down

0 comments on commit 192bed3

Please sign in to comment.