From b4f5acc09a90ac454e110cd69e649c0900b2b132 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 21 Jul 2011 03:41:57 -0400 Subject: [PATCH] Validate COMMON_TEMPORARY_DIRECTORY and fallback to use mkdtemp on failure --- apps/common/__init__.py | 8 +++++--- apps/common/utils.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/apps/common/__init__.py b/apps/common/__init__.py index 9d177e93ed..1172d62f79 100644 --- a/apps/common/__init__.py +++ b/apps/common/__init__.py @@ -2,11 +2,10 @@ import tempfile from django.utils.translation import ugettext_lazy as _ -from common.conf import settings as common_settings from navigation.api import register_links -TEMPORARY_DIRECTORY = common_settings.TEMPORARY_DIRECTORY \ - if common_settings.TEMPORARY_DIRECTORY else tempfile.mkdtemp() +from common.conf import settings as common_settings +from common.utils import validate_path def has_usable_password(context): @@ -17,3 +16,6 @@ current_user_details = {'text': _(u'user details'), 'view': 'current_user_detail current_user_edit = {'text': _(u'edit details'), 'view': 'current_user_edit', 'famfam': 'vcard_edit'} register_links(['current_user_details', 'current_user_edit', 'password_change_view'], [current_user_details, current_user_edit, password_change_view], menu_name='secondary_menu') + +if (validate_path(common_settings.TEMPORARY_DIRECTORY) == False) or (not common_settings.TEMPORARY_DIRECTORY): + setattr(common_settings, 'TEMPORARY_DIRECTORY', tempfile.mkdtemp()) diff --git a/apps/common/utils.py b/apps/common/utils.py index 5711ae3d4b..eacaba7923 100644 --- a/apps/common/utils.py +++ b/apps/common/utils.py @@ -2,6 +2,7 @@ import os import re import types +import tempfile from django.utils.http import urlquote as django_urlquote from django.utils.http import urlencode as django_urlencode @@ -374,3 +375,22 @@ def get_mimetype(filepath): file_mimetype, file_mime_encoding = mimetypes.guess_type(filename) return file_mimetype, file_mime_encoding + + +def validate_path(path): + if os.path.exists(path) != True: + # If doesn't exist try to create it + try: + os.mkdir(path) + except: + return False + + # Check if it is writable + try: + fd, test_filepath = tempfile.mkstemp(dir=path) + os.close(fd) + os.unlink(test_filepath) + except: + return False + + return True