Show
Ignore:
Timestamp:
02/16/09 16:50:17 (13 months ago)
Author:
JensDiemer
Message:

Work-in-progress: move some code from filemanager plugin into a shared destination. It's usefull for "filesystem based plugins" such as the gallery. Change the settings for the filemanager base path. Every fs-plugin should use this path information.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid_project/PyLucid/plugins_internal/filemanager/filemanager.py

    r1823 r1825  
    5252from datetime import datetime 
    5353 
     54from django import forms 
    5455from django.conf import settings 
    55 from django.http import Http404 
    56 from django import forms 
     56from django.http import HttpResponse 
    5757from django.forms import ValidationError 
    5858from django.utils.translation import ugettext as _ 
    59 from django.utils.encoding import force_unicode 
    60  
    61 from PyLucid.models import Page 
    62 from PyLucid.tools.utils import contains_char 
    63 from PyLucid.system.BasePlugin import PyLucidBasePlugin 
    64 from PyLucid.forms.filesystem import FilenameField, DirnameField, \ 
    65                                                 BAD_DIR_CHARS, BAD_FILE_CHARS 
    66  
    67 from PyLucid.tools.path_manager import BASE_PATHS, BASE_PATHS_DICT 
     59 
     60from PyLucid.system.BaseFilesystemPlugin import FilesystemPlugin 
     61from PyLucid.forms.filesystem import FilenameField, DirnameField 
    6862 
    6963#______________________________________________________________________________ 
     
    111105    ) 
    112106 
    113 class SelectBasePathForm(forms.Form): 
    114     """ change the base path form """ 
    115     base_path = forms.ChoiceField(choices=BASE_PATHS) 
    116107 
    117108#______________________________________________________________________________ 
     
    175166    item_name = FilenameField() 
    176167 
     168 
     169 
     170 
    177171#______________________________________________________________________________ 
    178172 
    179 class Path(dict): 
    180     """ 
    181     Helper class for analyse, check and store the html GET path information. 
    182     Used in filemanager() 
    183  
    184     base_no       = BASE_PATHS_DICT key (Note: it's a String!) 
    185     base_path     = relative base path 
    186     abs_base_path = absolute filesystem base path 
    187     rel_path      = relative path (from GET) 
    188     abs_path      = absolute filesystem path (abs_base_path + rel_path) 
    189     url_path      = base_no + rel_path for html links 
    190  
    191     - only with new_filename_path(): 
    192     filename      = contains only the filename 
    193     abs_file_path = absolute filesystem path incl. filename 
    194     """ 
    195     def __init__(self, context): 
    196         self.context     = context 
    197         self.request     = context["request"] 
    198         self.page_msg    = self.request.page_msg 
    199  
    200     def new_dir_path(self, path_info, must_exist=True): 
    201         """ 
    202         split the html-GET path information and build the absolute filesystem 
    203         path. 
    204         if must_exist==True: The given path must allready exists. 
    205         """ 
    206         if contains_char(path_info, BAD_DIR_CHARS): 
    207             raise Http404(_(u"Error: Bad character found!")) 
    208  
    209         path_info = os.path.normpath(path_info) 
    210  
    211         if len(path_info) == 1: 
    212             # e.g. edit a file in the base_path root 
    213             base_no = path_info 
    214             rel_path = "" 
    215         else: 
    216             try: 
    217                 base_no, rel_path = path_info.split("/", 1) 
    218             except ValueError: 
    219                 raise Http404(_("Wrong path!")) 
    220  
    221         try: 
    222             base_path = BASE_PATHS_DICT[base_no] 
    223         except KeyError: 
    224             raise Http404(_("Wrong basepath!")) 
    225  
    226         base_path = os.path.normpath(base_path) 
    227         abs_base_path = os.path.abspath(base_path) 
    228  
    229         abs_path = os.path.normpath(os.path.join(abs_base_path, rel_path)) 
    230         if must_exist and not os.path.exists(abs_path): 
    231             raise Http404(_("Error: Path '%s' doesn't exist.") % abs_path) 
    232  
    233         self["base_no"] = base_no 
    234         self["base_path"] = base_path 
    235         self["abs_base_path"] = abs_base_path 
    236         self["rel_path"] = rel_path 
    237         self["abs_path"] = abs_path 
    238         self["url_path"] = os.path.normpath(os.path.join(base_no, rel_path)) 
    239  
    240  
    241     def new_filename_path(self, file_path, must_exist=True): 
    242         """ 
    243         Split a html GET path information witch contains a filename. 
    244         if must_exist==True: The file must exist in the given path. 
    245         """ 
    246         path_info, filename = os.path.split(file_path) 
    247  
    248         self.new_dir_path(path_info, must_exist) 
    249  
    250         if contains_char(filename, BAD_FILE_CHARS): 
    251             raise Http404(_(u"Error: Bad character found!")) 
    252  
    253         abs_file_path = os.path.join(self["abs_path"], filename) 
    254  
    255         if must_exist and not os.path.isfile(abs_file_path): 
    256             raise Http404(_("Error: File '%s' doesn't exist.") % filename) 
    257  
    258         self["filename"] = filename 
    259         self["abs_file_path"] = abs_file_path 
    260  
    261     #-------------------------------------------------------------------------- 
    262  
    263     def get_abs_link(self, item=""): 
    264         """ 
    265         returns a absolute link to the given item. 
    266         """ 
    267         return os.path.join("/", self["base_path"], self["rel_path"], item) 
    268  
    269     #-------------------------------------------------------------------------- 
    270  
    271     def debug(self): 
    272         """ 
    273         write debug information into the page_msg 
    274         """ 
    275         self.page_msg("path debug:") 
    276         for k,v in self.items(): 
    277             self.page_msg(" - %15s: '%s'" % (k,v)) 
    278  
    279 #______________________________________________________________________________ 
    280  
    281 class filemanager(PyLucidBasePlugin): 
     173class filemanager(FilesystemPlugin): 
    282174    """ 
    283175    The PyLucid plugin class. 
    284176    """ 
    285     def __init__(self, context, response, plugin_name): 
    286         super(filemanager, self).__init__(context, response, plugin_name) 
    287         self.path = Path(context) 
    288  
    289     #-------------------------------------------------------------------------- 
    290  
    291177    def get_filelist(self): 
    292178        """ 
     
    306192                "name": "..", 
    307193                "link": self.URLs.methodLink( 
    308                     method_name="filelist", args=(self.path["base_no"], updir) 
     194                    method_name="filelist", args=(self.path["base_key"], updir) 
    309195                ), 
    310196                "is_dir": True, 
     
    370256        # start with the first base_path entry: 
    371257        dir_links = [{ 
    372             "name": self.path["base_path"], # use only the short relative path 
     258            "name": self.path["base_fs_path"], # use only the short relative path 
    373259            "title": self.path["abs_path"], 
    374260            "link": self.URLs.methodLink( 
    375                 method_name="filelist", args=self.path["base_no"] 
     261                method_name="filelist", args=self.path["base_key"] 
    376262            ), 
    377263        }] 
     
    383269                dir_links.append({ 
    384270                    "name": name, 
    385                     "title": os.path.join(self.path["base_path"], path), 
     271                    "title": os.path.join(self.path["base_fs_path"], path), 
    386272                    "link": self.URLs.methodLink( 
    387273                        method_name="filelist", 
    388                         args=(self.path["base_no"], path) 
     274                        args=(self.path["base_key"], path) 
    389275                    ), 
    390276                }) 
     
    571457        # Change the global page title: 
    572458        self.context["PAGE"].title = _("Filemanager - Change the basepath") 
    573 #        self.page_msg(self.request.POST) 
    574  
    575         if self.request.method == 'POST': 
    576             form = SelectBasePathForm(self.request.POST) 
    577             if form.is_valid(): 
    578                 path_no = form.cleaned_data["base_path"] 
    579                 new_path = BASE_PATHS_DICT[path_no] 
    580                 if not os.path.isdir(new_path): 
    581                     self.page_msg.red( 
    582                         "Error: Path '%s' doesn't exist" % new_path 
    583                     ) 
    584                 else: 
    585                     self.page_msg("change base path to '%s'." % new_path) 
    586                     # Display the filelist: 
    587                     return self.filelist(path_no + "/") 
     459         
     460        context = {} 
     461 
     462        path_key = self.basepath_form(context) 
     463        if not path_key: 
     464            self._render_template("select_basepath", context)#, debug=True) 
    588465        else: 
    589             form = SelectBasePathForm() 
    590  
    591         self._render_template("select_basepath", {"form": form})#, debug=True) 
     466            # POST with valide form data -> display the filelist 
     467            self.filelist(path_key + u"/")             
    592468 
    593469    #-------------------------------------------------------------------------- 
     
    597473        List dir and file. Some actions. 
    598474        rest: path to dir or file 
    599         """ 
     475        """         
    600476        if not path_info: 
    601477            self.select_basepath() 
     
    607483 
    608484        # Change the global page title: 
    609         path = os.path.join(self.path["base_path"], self.path["rel_path"]) 
     485        path = os.path.join(self.path["base_fs_path"], self.path["rel_path"]) 
    610486        self.context["PAGE"].title = _("File list - %s" % path) 
    611487 
     
    691567            ), 
    692568        } 
    693 #        self.page_msg(context) 
    694 #        self._render_template("filelist", context, debug=True) 
     569        #self.page_msg(context) 
    695570        self._render_template("filelist", context)#, debug=True) 
    696571