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