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