Changeset 1388

Show
Ignore:
Timestamp:
02/12/08 10:59:36 (2 years ago)
Author:
JensDiemer
Message:

-add a multiple base_path support, not tested well!
-Note: You must insert FILEMANAGER_BASEPATHS to your settings.py

Location:
trunk/pylucid/PyLucid
Files:
1 added
4 modified

Legend:

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

    r1387 r1388  
    3434from PyLucid.system.BasePlugin import PyLucidBasePlugin 
    3535 
    36  
    37 ABS_PATH = os.path.abspath(settings.MEDIA_ROOT) 
    38  
     36BASE_PATHS = [ 
     37    (str(no),path) for no,path in enumerate(settings.FILEMANAGER_BASEPATHS) 
     38] 
     39BASE_PATHS_DICT = dict(BASE_PATHS) 
    3940 
    4041def make_dirlist(path, result=[]): 
     
    7273 
    7374class EditFileForm(forms.Form): 
     75    """ Edit a text file form """ 
    7476    content = forms.CharField() 
    7577 
     78class SelectBasePathForm(forms.Form): 
     79    """ change the base path form """ 
     80    base_path = forms.ChoiceField(choices=BASE_PATHS) 
    7681 
    7782 
    7883class filemanager(PyLucidBasePlugin): 
    7984 
    80     def getFilesList(self, rel_dir): 
     85    def getFilesList(self, base_no, abs_path, rel_path): 
    8186        """ 
    8287        Returns all items in the given directory. 
     
    8691        files = [] 
    8792 
    88         if not rel_dir: 
     93        if rel_path == ".": 
    8994            # current dir it the media Root 
    9095            dirs=[] 
    9196        else: 
    9297            # Add the ".." dir item 
    93             updir = os.path.split(rel_dir)[0] 
     98            updir = os.path.split(rel_path)[0] 
    9499            dirs = [{ 
    95100                "name": "..", 
    96101                "link": self.URLs.methodLink( 
    97                     method_name="filelist", args=updir 
     102                    method_name="filelist", args=(base_no, updir) 
    98103                ), 
    99104                "is_dir": True, 
     
    101106            }] 
    102107 
    103         # convert the relative path to absolute filesystem path 
    104         abs_fs_path=os.path.join(ABS_PATH, rel_dir) 
    105  
    106         if not abs_fs_path.startswith(ABS_PATH): 
    107             raise WrongDirectory(abs_fs_path) 
    108  
    109         link_prefix = self.URLs.methodLink(method_name="filelist") 
    110  
    111         for item in sorted(os.listdir(abs_fs_path)): 
     108        link_prefix = self.URLs.methodLink(method_name="filelist", args=base_no) 
     109 
     110        for item in sorted(os.listdir(abs_path)): 
    112111            if item.startswith("."): 
    113112                # skip hidden files or directories 
    114113                continue 
    115114 
    116             item_abs_fs_path = os.path.join(abs_fs_path, item) 
    117             statinfo = os.stat(item_abs_fs_path) 
    118  
    119             link = os.path.join(link_prefix, rel_dir, item) 
     115            abs_item_path = os.path.join(abs_path, item) 
     116            statinfo = os.stat(abs_item_path) 
     117 
     118            link = os.path.join(link_prefix, rel_path, item) 
    120119 
    121120            if stat.S_ISDIR(statinfo[stat.ST_MODE]): 
     
    130129                "link": link, 
    131130                "is_dir": is_dir, 
    132                 "title": item_abs_fs_path, 
     131                "title": abs_item_path, 
    133132                "time": strftime("%Y:%m:%M",localtime(statinfo[stat.ST_MTIME])), 
    134133                "size": statinfo[stat.ST_SIZE], 
     
    148147 
    149148 
    150     def make_dir_links(self, path): 
    151         """ 
    152         path: /data/one/two 
    153  
    154         return [('/data/one/two','two'), ('/data/one','one'),('/data','data')] 
    155         """ 
     149    def make_dir_links(self, base_no, base_path, abs_path, rel_path): 
     150        """ 
     151        Build the context for the path link line. 
     152        Use the function make_dirlist(). 
     153        """ 
     154#        self.page_msg("base_no:", base_no, "base_path:", base_path) 
     155#        self.page_msg("abs_path:", abs_path, "rel_path:", rel_path) 
     156 
     157        # start with the first base_path entry: 
    156158        dir_links = [{ 
    157             # Display only the short relative path: 
    158             "name": os.path.normpath(settings.MEDIA_ROOT), 
    159             "title": ABS_PATH, 
    160             "link": self.URLs.methodLink(method_name="filelist"), 
     159            "name": base_path, # use only the short relative path 
     160            "title": abs_path, 
     161            "link": self.URLs.methodLink(method_name="filelist", args=base_no), 
    161162        }] 
    162         if path: 
     163        if rel_path != ".": 
    163164            # Not in the root 
    164             dirlist = make_dirlist(path, []) 
     165            dirlist = make_dirlist(rel_path, []) 
    165166            for path, name in dirlist: 
     167                # append every dir "steps" 
    166168                dir_links.append({ 
    167169                    "name": name, 
    168                     "title": os.path.join(ABS_PATH, path), 
     170                    "title": os.path.join(base_path, path), 
    169171                    "link": self.URLs.methodLink( 
    170                         method_name="filelist", args=path 
     172                        method_name="filelist", args=(base_no, path) 
    171173                    ), 
    172174                }) 
     
    201203 
    202204        if self.request.method == 'POST': 
    203             form = EditFileForm(request.POST) 
     205            form = EditFileForm(self.request.POST) 
    204206            if form.is_valid(): 
    205207                self.page_msg("Net implemented yet!!!") 
     
    297299        self._render_template("userinfo", context)#, debug=True) 
    298300 
    299     def filelist(self, rest=''): 
     301    def select_basepath(self): 
     302        """ 
     303        change the basepath, after send the form, we display the filelist 
     304        """ 
     305#        self.page_msg(self.request.POST) 
     306 
     307        if self.request.method == 'POST': 
     308            form = SelectBasePathForm(self.request.POST) 
     309            if form.is_valid(): 
     310                path_no = form.cleaned_data["base_path"] 
     311                new_path = BASE_PATHS_DICT[path_no] 
     312                if not os.path.isdir(new_path): 
     313                    self.page_msg.red("Error: Path '%s' doesn't exist" % new_path) 
     314                else: 
     315                    self.page_msg("changed to '%s'." % new_path) 
     316                    # Display the filelist: 
     317                    return self.filelist(path_no + "/") 
     318        else: 
     319            form = SelectBasePathForm() 
     320 
     321        self._render_template("select_basepath", {"form": form})#, debug=True) 
     322 
     323 
     324    def filelist(self, path_info=None): 
    300325        """ 
    301326        List dir and file. Some actions. 
    302327        rest: path to dir or file 
    303328        """ 
     329        if not path_info: 
     330            self.select_basepath() 
     331            return 
     332 
     333#        try: 
     334        base_no, rel_path = path_info.split("/", 1) 
     335        rel_path = os.path.normpath(rel_path) 
     336        base_path = os.path.normpath(BASE_PATHS_DICT[base_no]) 
     337#        except Exception, e: 
     338#            self.page_msg.red("Path error:", e) 
     339#            return 
     340 
     341        abs_base_path = os.path.abspath(base_path) 
     342 
     343#        self.page_msg("base_no:", base_no, "base_path:", base_path) 
     344#        self.page_msg("abs_base_path:", abs_base_path, "rel_path:", rel_path) 
     345 
    304346        # Change the global page title: 
    305347        self.context["PAGE"].title = _("File list") 
    306         context = {} 
    307         dir_list = [] 
    308         files_list = [] 
    309  
    310         dir_string='' 
    311  
    312         if rest!='': 
    313 #            self.page_msg("rest:", rest) 
    314             dir_string=os.path.normpath(rest) 
    315348 
    316349        if self.request.method == 'POST': 
    317350#            self.page_msg(self.request.POST) 
    318             dir_string=os.path.normpath(self.request.POST['dir_string']) 
     351#            dir_string=os.path.normpath(self.request.POST['dir_string']) 
    319352 
    320353            if self.request.has_key('action'): 
     
    349382                    ) 
    350383 
    351         abs_path = os.path.join(ABS_PATH, dir_string) 
    352  
    353 #        self.page_msg("read dir '%s'" % dir_string) 
    354         dir_list = self.getFilesList(dir_string) 
     384        abs_path = os.path.normpath(os.path.join(abs_base_path, rel_path)) 
     385        if not os.path.isdir(abs_path): 
     386            self.page_msg.red("Error: Path '%s' doesn't exist" % abs_path) 
     387            return 
     388 
     389        # build the directory+file list: 
     390        dir_list = self.getFilesList(base_no, abs_path, rel_path) 
     391 
     392        # Build the path link line: 
     393        dir_links = self.make_dir_links(base_no, base_path, abs_path, rel_path) 
    355394 
    356395        context = { 
    357396            "url": self.URLs.methodLink("filelist"), 
    358             "dir": dir_string, 
    359             "dir_links": self.make_dir_links(dir_string), 
     397            "dir": rel_path, 
     398            "dir_links": dir_links, 
    360399            "writeable": os.access(abs_path, os.W_OK), 
    361400            "dir_list": dir_list, 
    362             "abs_path": ABS_PATH, 
     401            "abs_path": abs_path, 
    363402            "messages": self.page_msg, 
    364403            "userinfo_link": self.URLs.methodLink( 
    365                 method_name="userinfo", args=dir_string 
     404                method_name="userinfo", args=rel_path 
    366405            ), 
    367406            "edit_link": self.URLs.methodLink( 
    368                 method_name="edit", args=dir_string 
     407                method_name="edit", args=rel_path 
     408            ), 
     409            "change_basepath_link": self.URLs.methodLink( 
     410                method_name="select_basepath" 
    369411            ), 
    370412        } 
  • trunk/pylucid/PyLucid/plugins_internal/filemanager/filemanager_cfg.py

    r1387 r1388  
    3636        }, 
    3737    }, 
     38    "select_basepath": { 
     39        "must_login"    : True, 
     40        "must_admin"    : True, 
     41        "internal_page_info" : { 
     42            "name"              : "select_basepath", 
     43            "description"       : "Select a base path (set in settings.py)", 
     44            "markup"            : None 
     45        }, 
     46    }, 
    3847    "edit": { 
    3948        "must_login"    : True, 
  • trunk/pylucid/PyLucid/plugins_internal/filemanager/internal_pages/filelist.html

    r1387 r1388  
    22{% for dir in dir_links %}<a href="{{ dir.link }}" title="{{ dir.title }}">{{ dir.name }}</a>/{% endfor %} 
    33</legend> 
     4 
     5<p><a href="{{ change_basepath_link }}">{% trans 'change basepath' %}</a></p> 
    46 
    57<table summary="Directory Listing" cellpadding="0" cellspacing="0"> 
  • trunk/pylucid/PyLucid/settings_example.py

    r1340 r1388  
    189189#     Examples-3: "http://django.media.your_domain.net/" 
    190190ADMIN_MEDIA_PREFIX = "/django/contrib/admin/media/" 
     191 
     192# Base path for the filemanager plugin 
     193# You can add more path for the plugin. 
     194FILEMANAGER_BASEPATHS = ( 
     195    MEDIA_ROOT, 
     196    #"./static/", 
     197) 
    191198 
    192199"""