From 29057b8b87c11baf4bd2becee4b8b3188417098b Mon Sep 17 00:00:00 2001 From: Andy Kittner Date: Wed, 30 May 2012 23:30:24 +0200 Subject: [PATCH] Fix exceptions if project path contains non-utf8 characters (#204) Django converts byte strings in various places to unicode by decoding them from utf-8, which will break if the filesystem encoding uses something else (e.g. fileystem encoding is latin1 and the path contains german umlauts). Avoid these problems by explicitly decoding all paths to unicode using an appropriate encoding. --- openslides/openslides_settings.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) mode change 100644 => 100755 openslides/openslides_settings.py diff --git a/openslides/openslides_settings.py b/openslides/openslides_settings.py old mode 100644 new mode 100755 index 1bc57e963..17714031a --- a/openslides/openslides_settings.py +++ b/openslides/openslides_settings.py @@ -11,8 +11,15 @@ """ import os +import sys from django.conf.global_settings import * +_fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() +def _fs2unicode(s): + if isinstance(s, unicode): + return s + return s.decode(_fs_encoding) + SITE_ROOT = os.path.realpath(os.path.dirname(__file__)) #SITE_ROOT = os.path.join(SITE_ROOT, '..') @@ -23,7 +30,7 @@ AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend', 'opensli LOGIN_URL = '/login/' LOGIN_REDIRECT_URL = '/' -DBPATH = os.path.join(os.path.join(SITE_ROOT, '..'), 'database.db') +DBPATH = _fs2unicode(os.path.join(os.path.join(SITE_ROOT, '..'), 'database.db')) DATABASES = { 'default': { @@ -64,7 +71,7 @@ MEDIA_URL = '' # Absolute path to the directory that holds static media from ``collectstatic`` # Example: "/home/media/static.lawrence.com/" -STATIC_ROOT = os.path.join(SITE_ROOT, '../site-static') +STATIC_ROOT = _fs2unicode(os.path.join(SITE_ROOT, '../site-static')) # URL that handles the media served from STATIC_ROOT. Make sure to use a # trailing slash if there is a path component (optional in other cases). @@ -74,7 +81,7 @@ STATIC_URL = '/static/' # Additional directories containing static files (not application specific) # Examples: "/home/media/lawrence.com/extra-static/" STATICFILES_DIRS = ( - os.path.join(SITE_ROOT, 'static'), + _fs2unicode(os.path.join(SITE_ROOT, 'static')), ) MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage' @@ -100,7 +107,7 @@ TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. - os.path.join(SITE_ROOT, 'templates'), + _fs2unicode(os.path.join(SITE_ROOT, 'templates')), ) INSTALLED_APPS = (