Added django-compress.
[redakcja.git] / apps / filebrowser / views.py
1 # coding: utf-8
2
3 # general imports
4 import os, re, locale
5 from time import gmtime, strftime
6
7 # django imports
8 from django.shortcuts import render_to_response, HttpResponse
9 from django.template import RequestContext as Context
10 from django.http import HttpResponseRedirect
11 from django.contrib.admin.views.decorators import staff_member_required
12 from django.views.decorators.cache import never_cache
13 from django.utils.translation import ugettext as _
14 from django.conf import settings
15 from django import forms
16 from django.core.urlresolvers import reverse
17 from django.core.exceptions import ImproperlyConfigured
18 from django.dispatch import Signal
19
20 from django.utils.encoding import smart_unicode, smart_str
21
22 # filebrowser imports
23 from filebrowser.fb_settings import *
24 from filebrowser.functions import _url_to_path, _path_to_url, _get_path, _get_file, _get_version_path, _get_breadcrumbs, _get_filterdate, _get_settings_var, _handle_file_upload, _get_file_type, _url_join, _convert_filename
25 from filebrowser.templatetags.fb_tags import query_helper
26 from filebrowser.base import FileObject
27 from filebrowser.decorators import flash_login_required
28
29 # Precompile regular expressions
30 filter_re = []
31 for exp in EXCLUDE:
32    filter_re.append(re.compile(exp))
33 for k, v in VERSIONS.iteritems():
34     exp = (r'_%s.(%s)') % (k, '|'.join(EXTENSION_LIST))
35     filter_re.append(re.compile(exp))
36
37
38 def browse(request):
39     """
40     Browse Files/Directories.
41     """
42
43     # QUERY / PATH CHECK
44     query = request.GET
45     path = _get_path(query.get('dir', ''))
46     directory = _get_path('')
47
48     if path is None:
49         msg = _('Directory/File does not exist.')
50         request.user.message_set.create(message = msg)
51         if directory is None:
52             # The DIRECTORY does not exist, raise an error to prevent eternal redirecting.
53             raise ImproperlyConfigured, _("Error finding upload directory. Maybe it does not exist?")
54         redirect_url = reverse("fb_browse") + query_helper(query, "", "dir")
55         return HttpResponseRedirect(redirect_url)
56     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
57
58     # INITIAL VARIABLES
59     results_var = {'results_total': 0, 'results_current': 0, 'delete_total': 0, 'images_total': 0, 'select_total': 0 }
60     counter = {}
61     for k, v in EXTENSIONS.iteritems():
62         counter[k] = 0
63
64     dir_list = os.listdir(abs_path)
65     files = []
66     # print "LISTING FILES: ", dir_list
67     for file in dir_list:
68         # print repr(file)
69         # EXCLUDE FILES MATCHING VERSIONS_PREFIX OR ANY OF THE EXCLUDE PATTERNS
70         filtered = file.startswith('.')
71         for re_prefix in filter_re:
72             if re_prefix.search(file):
73                 filtered = True
74         if filtered:
75             continue
76         results_var['results_total'] += 1
77
78         # CREATE FILEOBJECT
79         fileobject = FileObject(os.path.join(smart_str(DIRECTORY), smart_str(path), smart_str(file)))
80
81         # FILTER / SEARCH
82         append = False
83         if fileobject.filetype == request.GET.get('filter_type', fileobject.filetype) and _get_filterdate(request.GET.get('filter_date', ''), fileobject.date):
84             append = True
85         if request.GET.get('q') and not re.compile(request.GET.get('q').lower(), re.M).search(file.lower()):
86             append = False
87
88         # APPEND FILE_LIST
89         if append:
90             files.append(fileobject)
91             results_var['results_current'] += 1
92             # COUNTER/RESULTS
93             if fileobject.filetype == 'Image':
94                 results_var['images_total'] += 1
95             if fileobject.filetype != 'Folder':
96                 results_var['delete_total'] += 1
97             elif fileobject.filetype == 'Folder' and fileobject.is_empty:
98                 results_var['delete_total'] += 1
99             if query.get('type') and query.get('type') in SELECT_FORMATS and fileobject.filetype in SELECT_FORMATS[query.get('type')]:
100                 results_var['select_total'] += 1
101             elif not query.get('type'):
102                 results_var['select_total'] += 1
103
104         # COUNTER/RESULTS
105         if fileobject.filetype:
106             counter[fileobject.filetype] += 1
107
108     # SORTING
109     files.sort(key = lambda e: getattr(e, request.GET.get('o', DEFAULT_ORDER)))
110     if request.GET.get('ot') == "desc":
111         files.reverse()
112
113     return render_to_response('filebrowser/index.html', {
114         'dir': path,
115         'files': files,
116         'results_var': results_var,
117         'counter': counter,
118         'query': query,
119         'title': _(u'FileBrowser'),
120         'settings_var': _get_settings_var(),
121         'breadcrumbs': _get_breadcrumbs(query, path, ''),
122     }, context_instance = Context(request))
123 browse = staff_member_required(never_cache(browse))
124
125
126 # mkdir signals
127 filebrowser_pre_createdir = Signal(providing_args = ["path", "dirname"])
128 filebrowser_post_createdir = Signal(providing_args = ["path", "dirname"])
129
130 def mkdir(request):
131     """
132     Make Directory.
133     """
134
135     from filebrowser.forms import MakeDirForm
136
137     # QUERY / PATH CHECK
138     query = request.GET
139     path = _get_path(query.get('dir', ''))
140     if path is None:
141         msg = _('Directory/File does not exist.')
142         request.user.message_set.create(message = msg)
143         return HttpResponseRedirect(reverse("fb_browse"))
144     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
145
146     if request.method == 'POST':
147         form = MakeDirForm(abs_path, request.POST)
148         if form.is_valid():
149             server_path = os.path.join(abs_path, form.cleaned_data['dir_name'])
150             try:
151                 # PRE CREATE SIGNAL
152                 filebrowser_pre_createdir.send(sender = request, path = path, dirname = form.cleaned_data['dir_name'])
153                 # CREATE FOLDER
154                 os.mkdir(server_path)
155                 os.chmod(server_path, 0775)
156                 # POST CREATE SIGNAL
157                 filebrowser_post_createdir.send(sender = request, path = path, dirname = form.cleaned_data['dir_name'])
158                 # MESSAGE & REDIRECT
159                 msg = _('The Folder %s was successfully created.') % (form.cleaned_data['dir_name'])
160                 request.user.message_set.create(message = msg)
161                 # on redirect, sort by date desc to see the new directory on top of the list
162                 # remove filter in order to actually _see_ the new folder
163                 redirect_url = reverse("fb_browse") + query_helper(query, "ot=desc,o=date", "ot,o,filter_type,filter_date,q")
164                 return HttpResponseRedirect(redirect_url)
165             except OSError, (errno, strerror):
166                 if errno == 13:
167                     form.errors['dir_name'] = forms.util.ErrorList([_('Permission denied.')])
168                 else:
169                     form.errors['dir_name'] = forms.util.ErrorList([_('Error creating directory.')])
170     else:
171         form = MakeDirForm(abs_path)
172
173     return render_to_response('filebrowser/makedir.html', {
174         'form': form,
175         'query': query,
176         'title': _(u'New Folder'),
177         'settings_var': _get_settings_var(),
178         'breadcrumbs': _get_breadcrumbs(query, path, _(u'New Folder')),
179     }, context_instance = Context(request))
180 mkdir = staff_member_required(never_cache(mkdir))
181
182
183 def upload(request):
184     """
185     Multipe File Upload.
186     """
187
188     from django.http import parse_cookie
189
190     # QUERY / PATH CHECK
191     query = request.GET
192     path = _get_path(query.get('dir', ''))
193     if path is None:
194         msg = _('Directory/File does not exist.')
195         request.user.message_set.create(message = msg)
196         return HttpResponseRedirect(reverse("fb_browse"))
197     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
198
199     # SESSION (used for flash-uploading)
200     cookie_dict = parse_cookie(request.META.get('HTTP_COOKIE', ''))
201     engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
202     session_key = cookie_dict.get(settings.SESSION_COOKIE_NAME, None)
203
204     return render_to_response('filebrowser/upload.html', {
205         'query': query,
206         'title': _(u'Select files to upload'),
207         'settings_var': _get_settings_var(),
208         'breadcrumbs': _get_breadcrumbs(query, path, _(u'Upload')),
209         'session_key': session_key,
210     }, context_instance = Context(request))
211 upload = staff_member_required(never_cache(upload))
212
213
214 def _check_file(request):
215     """
216     Check if file already exists on the server.
217     """
218
219     from django.utils import simplejson
220
221     folder = request.POST.get('folder')
222     fb_uploadurl_re = re.compile(r'^(%s)' % reverse("fb_upload"))
223     folder = fb_uploadurl_re.sub('', folder)
224
225     fileArray = {}
226     if request.method == 'POST':
227         for k, v in request.POST.items():
228             if k != "folder":
229                 v = _convert_filename(v)
230                 if os.path.isfile(os.path.join(MEDIA_ROOT, DIRECTORY, folder, v)):
231                     fileArray[k] = v
232
233     return HttpResponse(simplejson.dumps(fileArray))
234
235
236 # upload signals
237 filebrowser_pre_upload = Signal(providing_args = ["path", "file"])
238 filebrowser_post_upload = Signal(providing_args = ["path", "file"])
239
240 def _upload_file(request):
241     """
242     Upload file to the server.
243     """
244
245     from django.core.files.move import file_move_safe
246
247     if request.method == 'POST':
248         folder = request.POST.get('folder')
249         fb_uploadurl_re = re.compile(r'^(%s)' % reverse("fb_upload"))
250         folder = fb_uploadurl_re.sub('', folder)
251         abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, folder)
252         if request.FILES:
253             filedata = request.FILES['Filedata']
254             filedata.name = _convert_filename(filedata.name)
255             # PRE UPLOAD SIGNAL
256             filebrowser_pre_upload.send(sender = request, path = request.POST.get('folder'), file = filedata)
257             # HANDLE UPLOAD
258             uploadedfile = _handle_file_upload(abs_path, filedata)
259             # MOVE UPLOADED FILE
260             # if file already exists
261             if os.path.isfile(os.path.join(MEDIA_ROOT, DIRECTORY, folder, filedata.name)):
262                 old_file = os.path.join(abs_path, filedata.name)
263                 new_file = os.path.join(abs_path, uploadedfile)
264                 file_move_safe(new_file, old_file)
265             # POST UPLOAD SIGNAL
266             filebrowser_post_upload.send(sender = request, path = request.POST.get('folder'), file = FileObject(os.path.join(DIRECTORY, folder, filedata.name)))
267     return HttpResponse('True')
268 _upload_file = flash_login_required(_upload_file)
269
270
271 # delete signals
272 filebrowser_pre_delete = Signal(providing_args = ["path", "filename"])
273 filebrowser_post_delete = Signal(providing_args = ["path", "filename"])
274
275 def delete(request):
276     """
277     Delete existing File/Directory.
278     
279     When trying to delete a Directory, the Directory has to be empty.
280     """
281
282     # QUERY / PATH CHECK
283     query = request.GET
284     path = _get_path(query.get('dir', ''))
285     filename = _get_file(query.get('dir', ''), query.get('filename', ''))
286     if path is None or filename is None:
287         msg = _('Directory/File does not exist.')
288         request.user.message_set.create(message = msg)
289         return HttpResponseRedirect(reverse("fb_browse"))
290     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
291
292     msg = ""
293     if request.GET:
294         if request.GET.get('filetype') != "Folder":
295             relative_server_path = os.path.join(DIRECTORY, path, filename)
296             try:
297                 # PRE DELETE SIGNAL
298                 filebrowser_pre_delete.send(sender = request, path = path, filename = filename)
299                 # DELETE IMAGE VERSIONS/THUMBNAILS
300                 for version in VERSIONS:
301                     try:
302                         os.unlink(os.path.join(MEDIA_ROOT, _get_version_path(relative_server_path, version)))
303                     except:
304                         pass
305                 # DELETE FILE
306                 os.unlink(os.path.join(abs_path, filename))
307                 # POST DELETE SIGNAL
308                 filebrowser_post_delete.send(sender = request, path = path, filename = filename)
309                 # MESSAGE & REDIRECT
310                 msg = _('The file %s was successfully deleted.') % (filename.lower())
311                 request.user.message_set.create(message = msg)
312                 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype")
313                 return HttpResponseRedirect(redirect_url)
314             except OSError:
315                 # todo: define error message
316                 msg = OSError
317         else:
318             try:
319                 # PRE DELETE SIGNAL
320                 filebrowser_pre_delete.send(sender = request, path = path, filename = filename)
321                 # DELETE FOLDER
322                 os.rmdir(os.path.join(abs_path, filename))
323                 # POST DELETE SIGNAL
324                 filebrowser_post_delete.send(sender = request, path = path, filename = filename)
325                 # MESSAGE & REDIRECT
326                 msg = _('The directory %s was successfully deleted.') % (filename.lower())
327                 request.user.message_set.create(message = msg)
328                 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype")
329                 return HttpResponseRedirect(redirect_url)
330             except OSError:
331                 # todo: define error message
332                 msg = OSError
333
334     if msg:
335         request.user.message_set.create(message = msg)
336
337     return render_to_response('filebrowser/index.html', {
338         'dir': dir_name,
339         'file': request.GET.get('filename', ''),
340         'query': query,
341         'settings_var': _get_settings_var(),
342         'breadcrumbs': _get_breadcrumbs(query, dir_name, ''),
343     }, context_instance = Context(request))
344 delete = staff_member_required(never_cache(delete))
345
346
347 # delete signals
348 filebrowser_pre_rename = Signal(providing_args = ["path", "filename"])
349 filebrowser_post_rename = Signal(providing_args = ["path", "filename"])
350
351 def rename(request):
352     """
353     Rename existing File/Directory.
354     
355     Includes renaming existing Image Versions/Thumbnails.
356     """
357
358     from filebrowser.forms import RenameForm
359
360     # QUERY / PATH CHECK
361     query = request.GET
362     path = _get_path(query.get('dir', ''))
363     filename = _get_file(query.get('dir', ''), query.get('filename', ''))
364     if path is None or filename is None:
365         msg = _('Directory/File does not exist.')
366         request.user.message_set.create(message = msg)
367         return HttpResponseRedirect(reverse("fb_browse"))
368     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
369     file_extension = os.path.splitext(filename)[1].lower()
370
371     if request.method == 'POST':
372         form = RenameForm(abs_path, file_extension, request.POST)
373         if form.is_valid():
374             relative_server_path = os.path.join(DIRECTORY, path, filename)
375             new_relative_server_path = os.path.join(DIRECTORY, path, form.cleaned_data['name'] + file_extension)
376             try:
377                 # PRE RENAME SIGNAL
378                 filebrowser_pre_delete.send(sender = request, path = path, filename = filename)
379                 # DELETE IMAGE VERSIONS/THUMBNAILS
380                 # regenerating versions/thumbs will be done automatically
381                 for version in VERSIONS:
382                     try:
383                         os.unlink(os.path.join(MEDIA_ROOT, _get_version_path(relative_server_path, version)))
384                     except:
385                         pass
386                 # RENAME ORIGINAL
387                 os.rename(os.path.join(MEDIA_ROOT, relative_server_path), os.path.join(MEDIA_ROOT, new_relative_server_path))
388                 # POST RENAME SIGNAL
389                 filebrowser_post_delete.send(sender = request, path = path, filename = filename)
390                 # MESSAGE & REDIRECT
391                 msg = _('Renaming was successful.')
392                 request.user.message_set.create(message = msg)
393                 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename")
394                 return HttpResponseRedirect(redirect_url)
395             except OSError, (errno, strerror):
396                 form.errors['name'] = forms.util.ErrorList([_('Error.')])
397     else:
398         form = RenameForm(abs_path, file_extension)
399
400     return render_to_response('filebrowser/rename.html', {
401         'form': form,
402         'query': query,
403         'file_extension': file_extension,
404         'title': _(u'Rename "%s"') % filename,
405         'settings_var': _get_settings_var(),
406         'breadcrumbs': _get_breadcrumbs(query, path, _(u'Rename')),
407     }, context_instance = Context(request))
408 rename = staff_member_required(never_cache(rename))
409
410
411 def versions(request):
412     """
413     Show all Versions for an Image according to ADMIN_VERSIONS.
414     """
415
416     # QUERY / PATH CHECK
417     query = request.GET
418     path = _get_path(query.get('dir', ''))
419     filename = _get_file(query.get('dir', ''), query.get('filename', ''))
420     if path is None or filename is None:
421         msg = _('Directory/File does not exist.')
422         request.user.message_set.create(message = msg)
423         return HttpResponseRedirect(reverse("fb_browse"))
424     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
425
426     return render_to_response('filebrowser/versions.html', {
427         'original': _path_to_url(os.path.join(DIRECTORY, path, filename)),
428         'query': query,
429         'title': _(u'Versions for "%s"') % filename,
430         'settings_var': _get_settings_var(),
431         'breadcrumbs': _get_breadcrumbs(query, path, _(u'Versions for "%s"') % filename),
432     }, context_instance = Context(request))
433 versions = staff_member_required(never_cache(versions))
434
435