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