7 from django.shortcuts import render_to_response, HttpResponse
8 from django.template import RequestContext as Context
9 from django.http import HttpResponseRedirect
10 from django.contrib.admin.views.decorators import staff_member_required
11 from django.views.decorators.cache import never_cache
12 from django.utils.translation import ugettext as _
13 from django.conf import settings
14 from django import forms
15 from django.core.urlresolvers import reverse
16 from django.core.exceptions import ImproperlyConfigured
17 from django.dispatch import Signal
19 from django.utils.encoding import smart_unicode, smart_str
22 from filebrowser.fb_settings import *
24 from filebrowser.functions import (
25 _url_to_path, _path_to_url, _get_path, _get_file, _get_version_path,
26 _get_breadcrumbs, _get_filterdate, _get_settings_var, _handle_file_upload,
27 _get_file_type, _url_join, _convert_filename)
29 from filebrowser.templatetags.fb_tags import query_helper
30 from filebrowser.base import FileObject
31 from filebrowser.decorators import flash_login_required
33 # Precompile regular expressions
37 filter_re.append(re.compile(exp))
38 for k, v in VERSIONS.iteritems():
39 exp = (r'_%s.(%s)') % (k, '|'.join(EXTENSION_LIST))
40 filter_re.append(re.compile(exp))
45 Browse Files/Directories.
50 path = _get_path(query.get('dir', ''))
51 directory = _get_path('')
54 msg = _('Directory/File does not exist.')
55 request.user.message_set.create(message=msg)
57 # The DIRECTORY does not exist, raise an error to prevent eternal redirecting.
58 raise ImproperlyConfigured(_("Error finding upload directory: %s. Maybe it does not exist?" % os.path.join(MEDIA_ROOT, DIRECTORY)))
59 redirect_url = reverse("fb_browse") + query_helper(query, "", "dir")
60 return HttpResponseRedirect(redirect_url)
61 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
64 results_var = {'results_total': 0, 'results_current': 0, 'delete_total': 0, 'images_total': 0, 'select_total': 0}
66 for k, v in EXTENSIONS.iteritems():
69 dir_list = os.listdir(abs_path)
71 # print "LISTING FILES: ", dir_list
74 # EXCLUDE FILES MATCHING VERSIONS_PREFIX OR ANY OF THE EXCLUDE PATTERNS
75 filtered = file.startswith('.')
76 for re_prefix in filter_re:
77 if re_prefix.search(file):
81 results_var['results_total'] += 1
84 fileobject = FileObject(os.path.join(smart_str(DIRECTORY), smart_str(path), smart_str(file)))
88 if fileobject.filetype == request.GET.get('filter_type', fileobject.filetype) and _get_filterdate(request.GET.get('filter_date', ''), fileobject.date):
90 if request.GET.get('q') and not re.compile(request.GET.get('q').lower(), re.M).search(file.lower()):
95 files.append(fileobject)
96 results_var['results_current'] += 1
98 if fileobject.filetype == 'Image':
99 results_var['images_total'] += 1
100 if fileobject.filetype != 'Folder':
101 results_var['delete_total'] += 1
102 elif fileobject.filetype == 'Folder' and fileobject.is_empty:
103 results_var['delete_total'] += 1
104 if query.get('type') and query.get('type') in SELECT_FORMATS and fileobject.filetype in SELECT_FORMATS[query.get('type')]:
105 results_var['select_total'] += 1
106 elif not query.get('type'):
107 results_var['select_total'] += 1
110 if fileobject.filetype:
111 counter[fileobject.filetype] += 1
114 files.sort(key=lambda e: getattr(e, request.GET.get('o', DEFAULT_ORDER)))
115 if request.GET.get('ot') == "desc":
118 return render_to_response('filebrowser/index.html', {
121 'results_var': results_var,
124 'title': _(u'FileBrowser'),
125 'settings_var': _get_settings_var(),
126 'breadcrumbs': _get_breadcrumbs(query, path, ''),
127 }, context_instance=Context(request))
128 browse = staff_member_required(never_cache(browse))
132 filebrowser_pre_createdir = Signal(providing_args=["path", "dirname"])
133 filebrowser_post_createdir = Signal(providing_args=["path", "dirname"])
141 from filebrowser.forms import MakeDirForm
145 path = _get_path(query.get('dir', ''))
147 msg = _('Directory/File does not exist.')
148 request.user.message_set.create(message=msg)
149 return HttpResponseRedirect(reverse("fb_browse"))
150 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
152 if request.method == 'POST':
153 form = MakeDirForm(abs_path, request.POST)
155 server_path = os.path.join(abs_path, form.cleaned_data['dir_name'])
158 filebrowser_pre_createdir.send(sender=request, path=path, dirname=form.cleaned_data['dir_name'])
160 os.mkdir(server_path)
161 os.chmod(server_path, 0775)
163 filebrowser_post_createdir.send(sender=request, path=path, dirname=form.cleaned_data['dir_name'])
165 msg = _('The Folder %s was successfully created.') % (form.cleaned_data['dir_name'])
166 request.user.message_set.create(message=msg)
167 # on redirect, sort by date desc to see the new directory on top of the list
168 # remove filter in order to actually _see_ the new folder
169 redirect_url = reverse("fb_browse") + query_helper(query, "ot=desc,o=date", "ot,o,filter_type,filter_date,q")
170 return HttpResponseRedirect(redirect_url)
171 except OSError, (errno, strerror):
173 form.errors['dir_name'] = forms.util.ErrorList([_('Permission denied.')])
175 form.errors['dir_name'] = forms.util.ErrorList([_('Error creating directory.')])
177 form = MakeDirForm(abs_path)
179 return render_to_response('filebrowser/makedir.html', {
182 'title': _(u'New Folder'),
183 'settings_var': _get_settings_var(),
184 'breadcrumbs': _get_breadcrumbs(query, path, _(u'New Folder')),
185 }, context_instance=Context(request))
186 mkdir = staff_member_required(never_cache(mkdir))
194 from django.http import parse_cookie
198 path = _get_path(query.get('dir', ''))
200 msg = _('Directory/File does not exist.')
201 request.user.message_set.create(message=msg)
202 return HttpResponseRedirect(reverse("fb_browse"))
203 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
205 # SESSION (used for flash-uploading)
206 cookie_dict = parse_cookie(request.META.get('HTTP_COOKIE', ''))
207 engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
208 session_key = cookie_dict.get(settings.SESSION_COOKIE_NAME, None)
210 return render_to_response('filebrowser/upload.html', {
212 'title': _(u'Select files to upload'),
213 'settings_var': _get_settings_var(),
214 'breadcrumbs': _get_breadcrumbs(query, path, _(u'Upload')),
215 'session_key': session_key,
216 }, context_instance=Context(request))
217 upload = staff_member_required(never_cache(upload))
220 def _check_file(request):
222 Check if file already exists on the server.
225 from django.utils import simplejson
227 folder = request.POST.get('folder')
228 fb_uploadurl_re = re.compile(r'^(%s)' % reverse("fb_upload"))
229 folder = fb_uploadurl_re.sub('', folder)
232 if request.method == 'POST':
233 for k, v in request.POST.items():
235 v = _convert_filename(v)
236 if os.path.isfile(os.path.join(MEDIA_ROOT, DIRECTORY, folder, v)):
239 return HttpResponse(simplejson.dumps(fileArray))
243 filebrowser_pre_upload = Signal(providing_args=["path", "file"])
244 filebrowser_post_upload = Signal(providing_args=["path", "file"])
247 def _upload_file(request):
249 Upload file to the server.
252 from django.core.files.move import file_move_safe
254 if request.method == 'POST':
255 folder = request.POST.get('folder')
256 fb_uploadurl_re = re.compile(r'^(%s)' % reverse("fb_upload"))
257 folder = fb_uploadurl_re.sub('', folder)
258 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, folder)
260 filedata = request.FILES['Filedata']
261 filedata.name = _convert_filename(filedata.name)
263 filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata)
265 uploadedfile = _handle_file_upload(abs_path, filedata)
267 # if file already exists
268 if os.path.isfile(os.path.join(MEDIA_ROOT, DIRECTORY, folder, filedata.name)):
269 old_file = os.path.join(abs_path, filedata.name)
270 new_file = os.path.join(abs_path, uploadedfile)
271 file_move_safe(new_file, old_file)
273 filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject(os.path.join(DIRECTORY, folder, filedata.name)))
274 return HttpResponse('True')
275 _upload_file = flash_login_required(_upload_file)
279 filebrowser_pre_delete = Signal(providing_args=["path", "filename"])
280 filebrowser_post_delete = Signal(providing_args=["path", "filename"])
285 Delete existing File/Directory.
287 When trying to delete a Directory, the Directory has to be empty.
292 path = _get_path(query.get('dir', ''))
293 filename = _get_file(query.get('dir', ''), query.get('filename', ''))
294 if path is None or filename is None:
295 msg = _('Directory/File does not exist.')
296 request.user.message_set.create(message=msg)
297 return HttpResponseRedirect(reverse("fb_browse"))
298 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
302 if request.GET.get('filetype') != "Folder":
303 relative_server_path = os.path.join(DIRECTORY, path, filename)
306 filebrowser_pre_delete.send(sender=request, path=path, filename=filename)
307 # DELETE IMAGE VERSIONS/THUMBNAILS
308 for version in VERSIONS:
310 os.unlink(os.path.join(MEDIA_ROOT, _get_version_path(relative_server_path, version)))
314 os.unlink(os.path.join(abs_path, filename))
316 filebrowser_post_delete.send(sender=request, path=path, filename=filename)
318 msg = _('The file %s was successfully deleted.') % (filename.lower())
319 request.user.message_set.create(message=msg)
320 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype")
321 return HttpResponseRedirect(redirect_url)
323 # todo: define error message
328 filebrowser_pre_delete.send(sender=request, path=path, filename=filename)
330 os.rmdir(os.path.join(abs_path, filename))
332 filebrowser_post_delete.send(sender=request, path=path, filename=filename)
334 msg = _('The directory %s was successfully deleted.') % (filename.lower())
335 request.user.message_set.create(message=msg)
336 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype")
337 return HttpResponseRedirect(redirect_url)
339 # todo: define error message
343 request.user.message_set.create(message=msg)
345 return render_to_response('filebrowser/index.html', {
347 'file': request.GET.get('filename', ''),
349 'settings_var': _get_settings_var(),
350 'breadcrumbs': _get_breadcrumbs(query, dir_name, ''),
351 }, context_instance=Context(request))
352 delete = staff_member_required(never_cache(delete))
356 filebrowser_pre_rename = Signal(providing_args=["path", "filename"])
357 filebrowser_post_rename = Signal(providing_args=["path", "filename"])
362 Rename existing File/Directory.
364 Includes renaming existing Image Versions/Thumbnails.
367 from filebrowser.forms import RenameForm
371 path = _get_path(query.get('dir', ''))
372 filename = _get_file(query.get('dir', ''), query.get('filename', ''))
373 if path is None or filename is None:
374 msg = _('Directory/File does not exist.')
375 request.user.message_set.create(message=msg)
376 return HttpResponseRedirect(reverse("fb_browse"))
377 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
378 file_extension = os.path.splitext(filename)[1].lower()
380 if request.method == 'POST':
381 form = RenameForm(abs_path, file_extension, request.POST)
383 relative_server_path = os.path.join(DIRECTORY, path, filename)
384 new_relative_server_path = os.path.join(DIRECTORY, path, form.cleaned_data['name'] + file_extension)
387 filebrowser_pre_delete.send(sender=request, path=path, filename=filename)
388 # DELETE IMAGE VERSIONS/THUMBNAILS
389 # regenerating versions/thumbs will be done automatically
390 for version in VERSIONS:
392 os.unlink(os.path.join(MEDIA_ROOT, _get_version_path(relative_server_path, version)))
396 os.rename(os.path.join(MEDIA_ROOT, relative_server_path), os.path.join(MEDIA_ROOT, new_relative_server_path))
398 filebrowser_post_delete.send(sender=request, path=path, filename=filename)
400 msg = _('Renaming was successful.')
401 request.user.message_set.create(message=msg)
402 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename")
403 return HttpResponseRedirect(redirect_url)
404 except OSError, (errno, strerror):
405 form.errors['name'] = forms.util.ErrorList([_('Error.')])
407 form = RenameForm(abs_path, file_extension)
409 return render_to_response('filebrowser/rename.html', {
412 'file_extension': file_extension,
413 'title': _(u'Rename "%s"') % filename,
414 'settings_var': _get_settings_var(),
415 'breadcrumbs': _get_breadcrumbs(query, path, _(u'Rename')),
416 }, context_instance=Context(request))
417 rename = staff_member_required(never_cache(rename))
420 def versions(request):
422 Show all Versions for an Image according to ADMIN_VERSIONS.
427 path = _get_path(query.get('dir', ''))
428 filename = _get_file(query.get('dir', ''), query.get('filename', ''))
429 if path is None or filename is None:
430 msg = _('Directory/File does not exist.')
431 request.user.message_set.create(message=msg)
432 return HttpResponseRedirect(reverse("fb_browse"))
433 abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
435 return render_to_response('filebrowser/versions.html', {
436 'original': _path_to_url(os.path.join(DIRECTORY, path, filename)),
438 'title': _(u'Versions for "%s"') % filename,
439 'settings_var': _get_settings_var(),
440 'breadcrumbs': _get_breadcrumbs(query, path, _(u'Versions for "%s"') % filename),
441 }, context_instance=Context(request))
443 versions = staff_member_required(never_cache(versions))