Changeset 1408

Show
Ignore:
Timestamp:
02/14/08 10:52:20 (9 months ago)
Author:
JensDiemer
Message:

filemanager updates:
- display the file date using datetime and the django filter date:_("DATE_WITH_TIME_FULL") - But the time is wrong :(
- insert count of directory subitems
- change the page titles
- some code cleanup / add some DocStrings?

Location:
trunk/pylucid/PyLucid/plugins_internal/filemanager
Files:
3 modified

Legend:

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

    r1407 r1408  
    11#!/usr/bin/python 
    22# -*- coding: UTF-8 -*- 
    3  
    43 
    54""" 
     
    4847__version__= "$Rev: $" 
    4948 
    50 import os, cgi, sys, stat 
    51 from time import localtime, strftime 
     49import os, cgi, sys, stat, dircache 
     50from datetime import datetime 
    5251 
    5352from django.http import Http404 
     
    6059from PyLucid.system.BasePlugin import PyLucidBasePlugin 
    6160 
    62 # ----------------------------------------------------------------------------- 
     61#______________________________________________________________________________ 
     62# Build a list and a dict from the basepaths 
     63# The dict key is a string, not a integer. (GET/POST Data always returned 
     64# numbers as strings) 
    6365 
    6466BASE_PATHS = [ 
     
    6769BASE_PATHS_DICT = dict(BASE_PATHS) 
    6870 
    69 # ----------------------------------------------------------------------------- 
     71#______________________________________________________________________________ 
    7072# We use more than one html form in a filelist page. So we need some unique 
    7173# action values for a easier distinguish the POST data. 
     
    7577ACTION_DELETEFILE = "3" 
    7678 
    77 # ----------------------------------------------------------------------------- 
     79#______________________________________________________________________________ 
    7880 
    7981def make_dirlist(path, result=[]): 
    8082    """ 
     83    Helper function for building a directory link line. 
     84    used in filemanager.make_dir_links() 
     85 
    8186    >>> make_dirlist("/data/one/two") 
    8287    [('data/one/two', 'two'), ('data/one', 'one'), ('data', 'data')] 
     
    9499        return result 
    95100 
    96 # ----------------------------------------------------------------------------- 
    97  
    98 BAD_DIR_CHARS = ("..", "//", "\\") 
    99 BAD_FILE_CHARS = ("..", "/", "\\") 
     101#______________________________________________________________________________ 
     102 
     103BAD_DIR_CHARS = ("..", "//", "\\") # Bad characters in directories 
     104BAD_FILE_CHARS = ("..", "/", "\\") # Bad characters in a filename 
    100105 
    101106def contains_char(text, chars): 
     
    133138class DirnameField(BadCharField): 
    134139    """ 
    135     Verify a dirname 
     140    newforms field for verify a dirname. 
    136141    """ 
    137142    bad_chars = BAD_DIR_CHARS 
     
    139144class FilenameField(BadCharField): 
    140145    """ 
    141     Verify a filename 
     146    newforms field for verify a filename. 
    142147    """ 
    143148    bad_chars = BAD_FILE_CHARS 
    144149 
    145 # ----------------------------------------------------------------------------- 
     150#______________________________________________________________________________ 
    146151 
    147152class EditFileForm(forms.Form): 
    148153    """ Edit a text file form """ 
    149154    filename = FilenameField( 
    150         help_text="Change the filename, if you want to save the content into a new file." 
     155        help_text=_( 
     156            u"Change the filename," 
     157            " if you want to save the content into a new file." 
     158        ) 
    151159    ) 
    152160    content = forms.CharField( 
     
    158166    base_path = forms.ChoiceField(choices=BASE_PATHS) 
    159167 
    160 # ----------------------------------------------------------------------------- 
     168#______________________________________________________________________________ 
    161169 
    162170class ActionField(forms.CharField): 
     
    181189        return value 
    182190 
    183 # ----------------------------------------------------------------------------- 
     191#______________________________________________________________________________ 
    184192 
    185193class CreateDirForm(forms.Form): 
     
    218226    item_name = FilenameField() 
    219227 
    220 # ----------------------------------------------------------------------------- 
     228#______________________________________________________________________________ 
    221229 
    222230class Path(dict): 
    223231    """ 
    224     Analyse, check and store the html GET path information. 
     232    Helper class for analyse, check and store the html GET path information. 
     233    Used in filemanager() 
    225234 
    226235    base_no       = BASE_PATHS_DICT key (Note: it's a String!) 
     
    273282            raise Http404(_("Error: Path '%s' doesn't exist.") % abs_path) 
    274283 
    275 #        self.page_msg("base_no:", base_no, "base_path:", base_path) 
    276 #        self.page_msg("abs_base_path:", abs_base_path, "rel_path:", rel_path) 
    277  
    278284        self["base_no"] = base_no 
    279285        self["base_path"] = base_path 
     
    304310        self["abs_file_path"] = abs_file_path 
    305311 
    306     #__________________________________________________________________________ 
     312    #-------------------------------------------------------------------------- 
    307313 
    308314    def get_abs_link(self, item=""): 
     
    312318        return os.path.join("/", self["base_path"], self["rel_path"], item) 
    313319 
    314     #__________________________________________________________________________ 
     320    #-------------------------------------------------------------------------- 
    315321 
    316322    def debug(self): 
     
    322328            self.page_msg(" - %15s: '%s'" % (k,v)) 
    323329 
    324 # ----------------------------------------------------------------------------- 
     330#______________________________________________________________________________ 
    325331 
    326332class filemanager(PyLucidBasePlugin): 
    327  
     333    """ 
     334    The PyLucid plugin class. 
     335    """ 
    328336    def __init__(self, context, response): 
    329337        super(filemanager, self).__init__(context, response) 
    330338        self.path = Path(context) 
    331339 
    332     #__________________________________________________________________________ 
     340    #-------------------------------------------------------------------------- 
    333341 
    334342    def get_filelist(self): 
     
    341349 
    342350        if self.path["rel_path"] == "": 
    343             # current dir it the media Root 
     351            # current dir is the media root 
    344352            dirs=[] 
    345353        else: 
     
    367375            statinfo = os.stat(abs_item_path) 
    368376 
    369             if stat.S_ISDIR(statinfo[stat.ST_MODE]): 
     377            if stat.S_ISDIR(statinfo.st_mode): 
    370378                # Is a directory 
    371379                is_dir = True 
    372380                link = os.path.join(link_prefix, item) + "/" 
     381                size = len(dircache.listdir( 
     382                    os.path.join(self.path["abs_path"], item) 
     383                ))-1 
    373384            else: 
    374385                is_dir = False 
    375386                link = self.path.get_abs_link(item) 
     387                size = statinfo.st_size 
     388 
     389            print item, datetime.fromtimestamp(statinfo.st_mtime) 
    376390 
    377391            item_dict={ 
     
    380394                "is_dir": is_dir, 
    381395                "title": abs_item_path, 
    382                 "time": strftime("%Y:%m:%M",localtime(statinfo[stat.ST_MTIME])), 
    383                 "size": statinfo[stat.ST_SIZE], 
    384                 "mode": statinfo[stat.ST_MODE], 
    385                 "uid": statinfo[stat.ST_UID], 
    386                 "gid": statinfo[stat.ST_GID], 
     396                "time": datetime.fromtimestamp(statinfo.st_mtime), 
     397                "size": size, 
     398                "mode": statinfo.st_mode, 
     399                "uid": statinfo.st_uid, 
     400                "gid": statinfo.st_gid, 
    387401                "deletable": True, 
    388402            } 
     
    426440        return dir_links 
    427441 
    428     #__________________________________________________________________________ 
     442    #-------------------------------------------------------------------------- 
    429443    # html GET actions: 
    430444 
     
    476490        form_link += self.path["filename"] 
    477491 
     492        file_path = self.path.get_abs_link() 
     493 
     494        # Change the global page title: 
     495        self.context["PAGE"].title = _("Edit file - %s" % file_path) 
     496 
    478497        context = { 
    479498            "form_link": form_link, 
     
    481500                method_name="filelist", args=self.path["url_path"] 
    482501            ), 
    483             "file_path": self.path.get_abs_link(), 
     502            "file_path": file_path, 
    484503            "filename": self.path["filename"], 
    485504            "form": form, 
     
    488507        self._render_template("edit_file", context)#, debug=True) 
    489508 
    490     #__________________________________________________________________________ 
     509    #-------------------------------------------------------------------------- 
    491510    # html POST actions: 
    492511 
     
    541560            self.page_msg.green("File '%s' deleted successfull." % filename) 
    542561 
    543     #__________________________________________________________________________ 
     562    #-------------------------------------------------------------------------- 
    544563 
    545564    def userinfo(self, old_path=""): 
     
    547566        Display some user information related to the filemanager functionality. 
    548567        """ 
     568        # Change the global page title: 
     569        self.context["PAGE"].title = _( 
     570            "Filemanager - Display some user information" 
     571        ) 
     572 
    549573        import pwd, grp 
    550574 
     
    571595        change the basepath, after send the form, we display the filelist 
    572596        """ 
     597        # Change the global page title: 
     598        self.context["PAGE"].title = _("Filemanager - Change the basepath") 
    573599#        self.page_msg(self.request.POST) 
    574600 
     
    579605                new_path = BASE_PATHS_DICT[path_no] 
    580606                if not os.path.isdir(new_path): 
    581                     self.page_msg.red("Error: Path '%s' doesn't exist" % new_path) 
     607                    self.page_msg.red( 
     608                        "Error: Path '%s' doesn't exist" % new_path 
     609                    ) 
    582610                else: 
    583                     self.page_msg("changed to '%s'." % new_path) 
     611                    self.page_msg("change base path to '%s'." % new_path) 
    584612                    # Display the filelist: 
    585613                    return self.filelist(path_no + "/") 
     
    589617        self._render_template("select_basepath", {"form": form})#, debug=True) 
    590618 
    591     #__________________________________________________________________________ 
     619    #-------------------------------------------------------------------------- 
    592620 
    593621    def filelist(self, path_info=None): 
     
    605633 
    606634        # Change the global page title: 
    607         self.context["PAGE"].title = _("File list - %s" % self.path["rel_path"]) 
     635        path = os.path.join(self.path["base_path"], self.path["rel_path"]) 
     636        self.context["PAGE"].title = _("File list - %s" % path) 
    608637 
    609638        # We init all forms before we check the POST, because we only need 
  • trunk/pylucid/PyLucid/plugins_internal/filemanager/internal_pages/filelist.css

    r1407 r1408  
    5656    float:left; 
    5757} 
    58 .filemanager .o, .filemanager .u, .filemanager .g { 
    59     /* mode + uid + gid */ 
     58.filemanager .s, .filemanager .m, .filemanager .o, .filemanager .u, .filemanager .g { 
     59    /* size + mtime + mode + uid + gid */ 
    6060    font-size:0.8em; 
    6161    font-weight:lighter; 
  • trunk/pylucid/PyLucid/plugins_internal/filemanager/internal_pages/filelist.html

    r1407 r1408  
    2424    </td> 
    2525    {% if item.is_dir %} 
    26         <td class="s"><i>&lt;dir&gt;</i></td> 
     26        <td class="s"> 
     27            {% if item.size %} 
     28            <i>{{ item.size }} object{{ item.size|pluralize }}</i> 
     29            {% endif %} 
     30        </td> 
    2731    {% else %} 
    2832        <td class="s">{{ item.size|filesizeformat }}</td> 
    2933    {% endif %} 
    30     <td class="m">{{ item.time }}</td> 
     34    <td class="m">{{ item.time|date:_("DATE_WITH_TIME_FULL") }}</td> 
    3135    <td class="o" title="octal mode: {{ item.mode|get_oct }}">{{ item.mode|chmod_symbol }}</td> 
    3236    <td class="u">{{ item.uid }}</td>