Fixed listing
[redakcja.git] / apps / filebrowser / base.py
1 # coding: utf-8
2
3 import locale
4
5 import os, re, datetime
6 from time import gmtime, strftime
7 from django.conf import settings
8 from django.utils.encoding import smart_unicode
9 import urllib
10
11 # filebrowser imports
12 from filebrowser.fb_settings import *
13 from filebrowser.functions import _get_file_type, _url_join, _is_selectable, _get_version_path
14
15 # PIL import
16 if STRICT_PIL:
17     from PIL import Image
18 else:
19     try:
20         from PIL import Image
21     except ImportError:
22         import Image
23
24
25 def filesystem_encoding(ucode):
26     ucode = ucode.encode('utf-8')
27     ucode = urllib.quote(ucode)
28     return ucode
29
30 class FileObject(object):
31     """
32     The FileObject represents a File on the Server.
33     
34     PATH has to be relative to MEDIA_ROOT.
35     """
36
37     def __init__(self, path):
38         self.path = path
39         self.head = os.path.split(path)[0]
40         self.filename = os.path.split(path)[1]
41         self.filename_lower = self.filename.lower() # important for sorting
42         self.filetype = _get_file_type(self.filename)
43
44     def _filesize(self):
45         """
46         Filesize.
47         """
48         if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
49             return os.path.getsize(os.path.join(MEDIA_ROOT, self.path))
50         return ""
51     filesize = property(_filesize)
52
53     def _date(self):
54         """
55         Date.
56         """
57         if os.path.isfile(os.path.join(MEDIA_ROOT, self.path)) or os.path.isdir(os.path.join(MEDIA_ROOT, self.path)):
58             return os.path.getmtime(os.path.join(MEDIA_ROOT, self.path))
59         # IMHO this should raise an exception
60         return None
61     date = property(_date)
62
63     def _datetime(self):
64         """
65         Datetime Object.
66         """
67         return datetime.datetime.fromtimestamp(self.date)
68     datetime = property(_datetime)
69
70     def _extension(self):
71         """
72         Extension.
73         """
74         return u"%s" % os.path.splitext(self.filename)[1]
75     extension = property(_extension)
76
77     def _filetype_checked(self):
78         if self.filetype == "Folder" and os.path.isdir(self.path_full):
79             return self.filetype
80         elif self.filetype != "Folder" and os.path.isfile(self.path_full):
81             return self.filetype
82         else:
83             return ""
84     filetype_checked = property(_filetype_checked)
85
86     def _path_full(self):
87         """
88         Full server PATH including MEDIA_ROOT.
89         """
90         return u"%s" % os.path.join(MEDIA_ROOT, self.path)
91     path_full = property(_path_full)
92
93     def _path_relative(self):
94         return self.path
95     path_relative = property(_path_relative)
96
97     def _path_relative_directory(self):
98         """
99         Path relative to initial directory.
100         """
101         directory_re = re.compile(r'^(%s)' % (DIRECTORY))
102         value = directory_re.sub('', self.path)
103         return u"%s" % value
104     path_relative_directory = property(_path_relative_directory)
105
106     def _url_relative(self):
107         return self.path
108     url_relative = property(_url_relative)
109
110     def _url_full(self):
111         """
112         Full URL including MEDIA_URL.
113         """
114         return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
115
116     url_full = property(_url_full)
117
118     def _url_save(self):
119         """
120         URL used for the filebrowsefield.
121         """
122         if SAVE_FULL_URL:
123             return self.url_full
124         else:
125             return filesystem_encoding(self.path)
126     url_save = property(_url_save)
127
128     def _url_thumbnail(self):
129         """
130         Thumbnail URL.
131         """
132         if self.filetype == "Image":
133             return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, _get_version_path(self.path, u'fb_thumb')))
134         else:
135             return ""
136     url_thumbnail = property(_url_thumbnail)
137
138     def url_admin(self):
139         if self.filetype_checked == "Folder":
140             directory_re = re.compile(r'^(%s)' % (DIRECTORY))
141             value = directory_re.sub('', self.path)
142             return filesystem_encoding(u"%s" % value)
143         else:
144             return filesystem_encoding(u"%s" % _url_join(MEDIA_URL, self.path))
145
146     def _dimensions(self):
147         """
148         Image Dimensions.
149         """
150         if self.filetype == 'Image':
151             try:
152                 im = Image.open(os.path.join(MEDIA_ROOT, self.path))
153                 return im.size
154             except:
155                 pass
156         else:
157             return False
158     dimensions = property(_dimensions)
159
160     def _width(self):
161         """
162         Image Width.
163         """
164         return self.dimensions[0]
165     width = property(_width)
166
167     def _height(self):
168         """
169         Image Height.
170         """
171         return self.dimensions[1]
172     height = property(_height)
173
174     def _orientation(self):
175         """
176         Image Orientation.
177         """
178         if self.dimensions:
179             if self.dimensions[0] >= self.dimensions[1]:
180                 return "Landscape"
181             else:
182                 return "Portrait"
183         else:
184             return None
185     orientation = property(_orientation)
186
187     def _is_empty(self):
188         """
189         True if Folder is empty, False if not.
190         """
191         if os.path.isdir(self.path_full):
192             if not os.listdir(self.path_full):
193                 return True
194             else:
195                 return False
196         else:
197             return None
198     is_empty = property(_is_empty)
199
200     def __repr__(self):
201         return u"%s" % self.url_save
202
203     def __str__(self):
204         return u"%s" % self.url_save
205
206     def __unicode__(self):
207         return u"%s" % self.url_save
208
209
210