<s> from __future__ import unicode_literals <EOL> import os <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . contrib . admin import widgets <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . http import Http404 , HttpResponseRedirect <EOL> from django . shortcuts import ( get_object_or_404 , redirect , render , <EOL> render_to_response ) <EOL> from django . template import RequestContext <EOL> from django . utils . encoding import uri_to_iri <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . management . commands . import_files import FileImporter <EOL> from . models import Clipboard , File , Folder , FolderRoot , Image , tools <EOL> class NewFolderForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Folder <EOL> fields = ( '<STR_LIT:name>' , ) <EOL> widgets = { <EOL> '<STR_LIT:name>' : widgets . AdminTextInputWidget , <EOL> } <EOL> class ScanFolderForm ( forms . Form ) : <EOL> folders = forms . CharField ( <EOL> label = '<STR_LIT>' , help_text = '<STR_LIT>' ) <EOL> class Meta : <EOL> exclude = ( ) <EOL> def popup_status ( request ) : <EOL> return ( '<STR_LIT>' in request . GET or '<STR_LIT>' in request . GET or <EOL> '<STR_LIT>' in request . POST or '<STR_LIT>' in request . POST ) <EOL> def selectfolder_status ( request ) : <EOL> return '<STR_LIT>' in request . GET or '<STR_LIT>' in request . POST <EOL> def popup_param ( request , separator = "<STR_LIT:?>" ) : <EOL> if popup_status ( request ) : <EOL> return "<STR_LIT>" % separator <EOL> else : <EOL> return "<STR_LIT>" <EOL> def selectfolder_param ( request , separator = "<STR_LIT:&>" ) : <EOL> if selectfolder_status ( request ) : <EOL> return "<STR_LIT>" % separator <EOL> else : <EOL> return "<STR_LIT>" <EOL> def _userperms ( item , request ) : <EOL> r = [ ] <EOL> ps = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for p in ps : <EOL> attr = "<STR_LIT>" % p <EOL> if hasattr ( item , attr ) : <EOL> x = getattr ( item , attr ) ( request ) <EOL> if x : <EOL> r . append ( p ) <EOL> return r <EOL> def canonical ( request , uploaded_at , file_id ) : <EOL> """<STR_LIT>""" <EOL> filer_file = get_object_or_404 ( File , pk = file_id , is_public = True ) <EOL> if ( uploaded_at != filer_file . uploaded_at . strftime ( '<STR_LIT:%s>' ) or <EOL> not filer_file . file ) : <EOL> raise Http404 ( '<STR_LIT>' % <EOL> File . _meta . object_name ) <EOL> return redirect ( filer_file . url ) <EOL> @ login_required <EOL> def edit_folder ( request , folder_id ) : <EOL> folder = None <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : folder , <EOL> '<STR_LIT>' : popup_status ( request ) , <EOL> '<STR_LIT>' : selectfolder_status ( request ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ login_required <EOL> def edit_image ( request , folder_id ) : <EOL> folder = None <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : folder , <EOL> '<STR_LIT>' : popup_status ( request ) , <EOL> '<STR_LIT>' : selectfolder_status ( request ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ login_required <EOL> def scan_folder ( request , folder_id = None ) : <EOL> if folder_id : <EOL> folder = Folder . objects . get ( id = folder_id ) <EOL> else : <EOL> folder = None <EOL> if request . user . is_superuser : <EOL> pass <EOL> elif folder is None : <EOL> if not settings . MEDIA_ALLOW_REGULAR_USERS_TO_ADD_ROOT_FOLDERS : <EOL> raise PermissionDenied <EOL> elif not folder . has_add_children_permission ( request ) : <EOL> raise PermissionDenied <EOL> if request . method == '<STR_LIT:POST>' : <EOL> scan_folder_form = ScanFolderForm ( request . POST ) <EOL> if scan_folder_form . is_valid ( ) : <EOL> kwargs = { <EOL> '<STR_LIT:path>' : os . path . join ( settings . MEDIA_ROOT , <EOL> scan_folder_form . cleaned_data [ '<STR_LIT>' ] ) , <EOL> } <EOL> if folder : <EOL> kwargs [ '<STR_LIT>' ] = folder . name <EOL> try : <EOL> importer = FileImporter ( ** kwargs ) <EOL> importer . walker ( ** kwargs ) <EOL> except Exception as e : <EOL> raise e <EOL> return render_to_response ( '<STR_LIT>' , <EOL> context_instance = RequestContext ( request ) ) <EOL> else : <EOL> new_folder_form = ScanFolderForm ( ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : new_folder_form , <EOL> '<STR_LIT>' : popup_status ( request ) , <EOL> '<STR_LIT>' : selectfolder_status ( request ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ login_required <EOL> def make_folder ( request , folder_id = None ) : <EOL> if not folder_id : <EOL> folder_id = request . GET . get ( '<STR_LIT>' ) <EOL> if not folder_id : <EOL> folder_id = request . POST . get ( '<STR_LIT>' ) <EOL> if folder_id : <EOL> try : <EOL> folder = Folder . objects . get ( id = folder_id ) <EOL> except Folder . DoesNotExist : <EOL> raise PermissionDenied <EOL> else : <EOL> folder = None <EOL> if request . user . is_superuser : <EOL> pass <EOL> elif folder is None : <EOL> if not settings . MEDIA_ALLOW_REGULAR_USERS_TO_ADD_ROOT_FOLDERS : <EOL> raise PermissionDenied <EOL> elif not folder . has_add_children_permission ( request ) : <EOL> raise PermissionDenied <EOL> if request . method == '<STR_LIT:POST>' : <EOL> new_folder_form = NewFolderForm ( request . POST ) <EOL> if new_folder_form . is_valid ( ) : <EOL> new_folder = new_folder_form . save ( commit = False ) <EOL> if ( folder or FolderRoot ( ) ) . contains_folder ( new_folder . name ) : <EOL> new_folder_form . _errors [ '<STR_LIT:name>' ] = new_folder_form . error_class ( <EOL> [ _ ( '<STR_LIT>' ) ] ) <EOL> else : <EOL> new_folder . parent = folder <EOL> new_folder . owner = request . user <EOL> new_folder . save ( ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> context_instance = RequestContext ( request ) ) <EOL> else : <EOL> new_folder_form = NewFolderForm ( ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : new_folder_form , <EOL> '<STR_LIT>' : popup_status ( request ) , <EOL> '<STR_LIT>' : selectfolder_status ( request ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> class UploadFileForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Image <EOL> exclude = ( ) <EOL> @ login_required <EOL> def upload ( request ) : <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : popup_status ( request ) , <EOL> '<STR_LIT>' : selectfolder_status ( request ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ login_required <EOL> def paste_clipboard_to_folder ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> folder = Folder . objects . get ( id = request . POST . get ( '<STR_LIT>' ) ) <EOL> clipboard = Clipboard . objects . get ( id = request . POST . get ( '<STR_LIT>' ) ) <EOL> if folder . has_add_children_permission ( request ) : <EOL> tools . move_files_from_clipboard_to_folder ( clipboard , folder ) <EOL> tools . discard_clipboard ( clipboard ) <EOL> else : <EOL> raise PermissionDenied <EOL> return HttpResponseRedirect ( '<STR_LIT>' % ( <EOL> request . REQUEST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> popup_param ( request , separator = '<STR_LIT:&>' ) , <EOL> selectfolder_param ( request ) ) ) <EOL> @ login_required <EOL> def discard_clipboard ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> clipboard = Clipboard . objects . get ( id = request . POST . get ( '<STR_LIT>' ) ) <EOL> tools . discard_clipboard ( clipboard ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' % ( <EOL> request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> popup_param ( request ) , <EOL> selectfolder_param ( request ) ) ) <EOL> @ login_required <EOL> def delete_clipboard ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> clipboard = Clipboard . objects . get ( id = request . POST . get ( '<STR_LIT>' ) ) <EOL> tools . delete_clipboard ( clipboard ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' % ( <EOL> request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> popup_param ( request ) , <EOL> selectfolder_param ( request ) ) ) <EOL> @ login_required <EOL> def clone_files_from_clipboard_to_folder ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> clipboard = Clipboard . objects . get ( id = request . POST . get ( '<STR_LIT>' ) ) <EOL> folder = Folder . objects . get ( id = request . POST . get ( '<STR_LIT>' ) ) <EOL> tools . clone_files_from_clipboard_to_folder ( clipboard , folder ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' % ( <EOL> request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> popup_param ( request ) , <EOL> selectfolder_param ( request ) ) ) <EOL> def directory_list ( request , directory_slug , category_parent_slug ) : <EOL> if directory_slug : <EOL> directory_slug = uri_to_iri ( directory_slug ) <EOL> if directory_slug is None : <EOL> object = None <EOL> root = getattr ( settings , '<STR_LIT>' , None ) <EOL> if root : <EOL> obj_root = Folder . objects . get ( name = root ) <EOL> object_list = Folder . objects . filter ( parent = obj_root ) <EOL> else : <EOL> object_list = Folder . objects . filter ( parent = None ) <EOL> else : <EOL> try : <EOL> object = Folder . objects . get ( id = directory_slug ) <EOL> except : <EOL> object = Folder . objects . get ( name = directory_slug ) <EOL> object_list = object . files . all ( ) <EOL> return render ( <EOL> request , <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : object_list , <EOL> } <EOL> ) <EOL> def directory_list_nested ( request , <EOL> directory_slug = None , <EOL> parent_directory_slug = None , <EOL> grandparent_directory_slug = None ) : <EOL> if directory_slug : <EOL> directory_slug = uri_to_iri ( directory_slug ) <EOL> if parent_directory_slug : <EOL> parent_directory_slug = uri_to_iri ( parent_directory_slug ) <EOL> if directory_slug is None : <EOL> object = None <EOL> root = getattr ( settings , '<STR_LIT>' , None ) <EOL> if root : <EOL> obj_root = Folder . objects . get ( name = root ) <EOL> object_list = Folder . objects . filter ( parent = obj_root ) <EOL> else : <EOL> object_list = Folder . objects . filter ( parent = None ) <EOL> else : <EOL> if parent_directory_slug is None : <EOL> try : <EOL> object = Folder . objects . get ( id = directory_slug ) <EOL> except : <EOL> object = Folder . objects . get ( name = directory_slug ) <EOL> object_list = object . files . all ( ) <EOL> else : <EOL> if grandparent_directory_slug is None : <EOL> object = Folder . objects . get ( <EOL> name = directory_slug , parent__name = parent_directory_slug ) <EOL> object_list = object . files . all ( ) <EOL> else : <EOL> object = Folder . objects . get ( <EOL> name = directory_slug , <EOL> parent__name = parent_directory_slug , <EOL> parent__parent__name = grandparent_directory_slug ) <EOL> object_list = object . files . all ( ) <EOL> return render ( <EOL> request , <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : object_list , <EOL> '<STR_LIT:object>' : object , <EOL> } <EOL> ) <EOL> def directory_detail_standalone ( request , category_id ) : <EOL> object = Folder . objects . get ( id = category_id ) <EOL> return render ( <EOL> request , <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT:object>' : object , <EOL> } , <EOL> ) </s>
