Merge branch 'master' into with-dvcs
[redakcja.git] / apps / filebrowser / base.py
index e72b9ff..c124026 100644 (file)
@@ -1,12 +1,22 @@
-# coding: utf-8
+# -*- coding: utf-8
 
 
-import os, re, datetime
-from time import gmtime, strftime
 from django.conf import settings
 from django.conf import settings
+from django.utils.encoding import smart_unicode
+from filebrowser.fb_settings import *
+
+from filebrowser.functions import _get_file_type, _url_join, _is_selectable, \
+    _get_version_path
+
+from time import gmtime, strftime
+
+import locale
+import os
+import re
+import datetime
+import urllib
+
 
 # filebrowser imports
 
 # filebrowser imports
-from filebrowser.fb_settings import *
-from filebrowser.functions import _get_file_type, _url_join, _is_selectable, _get_version_path
 
 # PIL import
 if STRICT_PIL:
 
 # PIL import
 if STRICT_PIL:
@@ -16,22 +26,28 @@ else:
         from PIL import Image
     except ImportError:
         import Image
         from PIL import Image
     except ImportError:
         import Image
-    
+
+
+def filesystem_encoding(ucode):
+    ucode = ucode.encode('utf-8')
+    ucode = urllib.quote(ucode)
+    return ucode
+
 
 class FileObject(object):
     """
     The FileObject represents a File on the Server.
 
 class FileObject(object):
     """
     The FileObject represents a File on the Server.
-    
+
     PATH has to be relative to MEDIA_ROOT.
     """
     PATH has to be relative to MEDIA_ROOT.
     """
-    
+
     def __init__(self, path):
         self.path = path
         self.head = os.path.split(path)[0]
         self.filename = os.path.split(path)[1]
     def __init__(self, path):
         self.path = path
         self.head = os.path.split(path)[0]
         self.filename = os.path.split(path)[1]
-        self.filename_lower = self.filename.lower() # important for sorting
+        self.filename_lower = self.filename.lower()  # important for sorting
         self.filetype = _get_file_type(self.filename)
         self.filetype = _get_file_type(self.filename)
-    
+
     def _filesize(self):
         """
         Filesize.
     def _filesize(self):
         """
         Filesize.
@@ -40,30 +56,31 @@ class FileObject(object):
             return os.path.getsize(os.path.join(MEDIA_ROOT, self.path))
         return ""
     filesize = property(_filesize)
             return os.path.getsize(os.path.join(MEDIA_ROOT, self.path))
         return ""
     filesize = property(_filesize)
-        
+
     def _date(self):
         """
         Date.
         """
         if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
             return os.path.getmtime(os.path.join(MEDIA_ROOT, self.path))
     def _date(self):
         """
         Date.
         """
         if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
             return os.path.getmtime(os.path.join(MEDIA_ROOT, self.path))
-        return ""
+        # IMHO this should raise an exception
+        return None
     date = property(_date)
     date = property(_date)
-    
+
     def _datetime(self):
         """
         Datetime Object.
         """
         return datetime.datetime.fromtimestamp(self.date)
     datetime = property(_datetime)
     def _datetime(self):
         """
         Datetime Object.
         """
         return datetime.datetime.fromtimestamp(self.date)
     datetime = property(_datetime)
-    
+
     def _extension(self):
         """
         Extension.
         """
         return u"%s" % os.path.splitext(self.filename)[1]
     extension = property(_extension)
     def _extension(self):
         """
         Extension.
         """
         return u"%s" % os.path.splitext(self.filename)[1]
     extension = property(_extension)
-    
+
     def _filetype_checked(self):
         if self.filetype == "Folder" and os.path.isdir(self.path_full):
             return self.filetype
     def _filetype_checked(self):
         if self.filetype == "Folder" and os.path.isdir(self.path_full):
             return self.filetype
@@ -72,18 +89,18 @@ class FileObject(object):
         else:
             return ""
     filetype_checked = property(_filetype_checked)
         else:
             return ""
     filetype_checked = property(_filetype_checked)
-    
+
     def _path_full(self):
         """
         Full server PATH including MEDIA_ROOT.
         """
         return u"%s" % os.path.join(MEDIA_ROOT, self.path)
     path_full = property(_path_full)
     def _path_full(self):
         """
         Full server PATH including MEDIA_ROOT.
         """
         return u"%s" % os.path.join(MEDIA_ROOT, self.path)
     path_full = property(_path_full)
-    
+
     def _path_relative(self):
         return self.path
     path_relative = property(_path_relative)
     def _path_relative(self):
         return self.path
     path_relative = property(_path_relative)
-    
+
     def _path_relative_directory(self):
         """
         Path relative to initial directory.
     def _path_relative_directory(self):
         """
         Path relative to initial directory.
@@ -92,18 +109,19 @@ class FileObject(object):
         value = directory_re.sub('', self.path)
         return u"%s" % value
     path_relative_directory = property(_path_relative_directory)
         value = directory_re.sub('', self.path)
         return u"%s" % value
     path_relative_directory = property(_path_relative_directory)
-    
+
     def _url_relative(self):
         return self.path
     url_relative = property(_url_relative)
     def _url_relative(self):
         return self.path
     url_relative = property(_url_relative)
-    
+
     def _url_full(self):
         """
         Full URL including MEDIA_URL.
         """
     def _url_full(self):
         """
         Full URL including MEDIA_URL.
         """
-        return u"%s" % _url_join(MEDIA_URL, self.path)
+        return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
+
     url_full = property(_url_full)
     url_full = property(_url_full)
-    
+
     def _url_save(self):
         """
         URL used for the filebrowsefield.
     def _url_save(self):
         """
         URL used for the filebrowsefield.
@@ -111,27 +129,27 @@ class FileObject(object):
         if SAVE_FULL_URL:
             return self.url_full
         else:
         if SAVE_FULL_URL:
             return self.url_full
         else:
-            return self.path
+            return filesystem_encoding(self.path)
     url_save = property(_url_save)
     url_save = property(_url_save)
-    
+
     def _url_thumbnail(self):
         """
         Thumbnail URL.
         """
         if self.filetype == "Image":
     def _url_thumbnail(self):
         """
         Thumbnail URL.
         """
         if self.filetype == "Image":
-            return u"%s" % _url_join(MEDIA_URL, _get_version_path(self.path, 'fb_thumb'))
+            return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, _get_version_path(self.path, u'fb_thumb')))
         else:
             return ""
     url_thumbnail = property(_url_thumbnail)
         else:
             return ""
     url_thumbnail = property(_url_thumbnail)
-    
+
     def url_admin(self):
         if self.filetype_checked == "Folder":
             directory_re = re.compile(r'^(%s)' % (DIRECTORY))
             value = directory_re.sub('', self.path)
     def url_admin(self):
         if self.filetype_checked == "Folder":
             directory_re = re.compile(r'^(%s)' % (DIRECTORY))
             value = directory_re.sub('', self.path)
-            return u"%s" % value
+            return filesystem_encoding(u"%s" % value)
         else:
         else:
-            return u"%s" % _url_join(MEDIA_URL, self.path)
-    
+            return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
+
     def _dimensions(self):
         """
         Image Dimensions.
     def _dimensions(self):
         """
         Image Dimensions.
@@ -145,21 +163,21 @@ class FileObject(object):
         else:
             return False
     dimensions = property(_dimensions)
         else:
             return False
     dimensions = property(_dimensions)
-    
+
     def _width(self):
         """
         Image Width.
         """
         return self.dimensions[0]
     width = property(_width)
     def _width(self):
         """
         Image Width.
         """
         return self.dimensions[0]
     width = property(_width)
-    
+
     def _height(self):
         """
         Image Height.
         """
         return self.dimensions[1]
     height = property(_height)
     def _height(self):
         """
         Image Height.
         """
         return self.dimensions[1]
     height = property(_height)
-    
+
     def _orientation(self):
         """
         Image Orientation.
     def _orientation(self):
         """
         Image Orientation.
@@ -172,7 +190,7 @@ class FileObject(object):
         else:
             return None
     orientation = property(_orientation)
         else:
             return None
     orientation = property(_orientation)
-    
+
     def _is_empty(self):
         """
         True if Folder is empty, False if not.
     def _is_empty(self):
         """
         True if Folder is empty, False if not.
@@ -185,15 +203,12 @@ class FileObject(object):
         else:
             return None
     is_empty = property(_is_empty)
         else:
             return None
     is_empty = property(_is_empty)
-    
+
     def __repr__(self):
         return u"%s" % self.url_save
     def __repr__(self):
         return u"%s" % self.url_save
-    
+
     def __str__(self):
         return u"%s" % self.url_save
     def __str__(self):
         return u"%s" % self.url_save
-    
+
     def __unicode__(self):
         return u"%s" % self.url_save
     def __unicode__(self):
         return u"%s" % self.url_save
-    
-
-