Changeset 1825
- Timestamp:
- 02/16/09 16:50:17 (13 months ago)
- Location:
- trunk/pylucid_project
- Files:
-
- 1 added
- 5 modified
-
media/PyLucid/internal_page/filemanager/select_basepath.html (modified) (1 diff)
-
PyLucid/forms/filesystem.py (modified) (2 diffs)
-
PyLucid/plugins_internal/filemanager/filemanager.py (modified) (10 diffs)
-
PyLucid/settings_example.py (modified) (1 diff)
-
PyLucid/system/BaseFilesystemPlugin.py (added)
-
PyLucid/tools/path_manager.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pylucid_project/media/PyLucid/internal_page/filemanager/select_basepath.html
r1449 r1825 1 1 <fieldset><legend>{% trans 'Select a base path' %}</legend> 2 2 <form action="." method="post"> 3 {{ form.as_p}}3 {{ select_basepath }} 4 4 <input type="submit" name="change" value="{% trans 'change' %}" /> 5 5 </form> -
trunk/pylucid_project/PyLucid/forms/filesystem.py
r1823 r1825 17 17 """ 18 18 19 20 19 from django import forms 20 from django.conf import settings 21 21 from django.utils.translation import ugettext as _ 22 22 … … 29 29 30 30 31 32 class BasePathField(forms.ChoiceField): 33 def __init__(self, required=True, label=None, initial=None, help_text=None, 34 *args, **kwargs): 35 36 widget = forms.Select 37 choices = [(i,i) for i in settings.FILESYSTEM_BASEPATHS.keys()] 38 39 super(BasePathField, self).__init__(choices, required, widget, label, 40 initial, help_text, *args, **kwargs) 41 42 def clean(self, value): 43 path_key = super(BasePathField, self).clean(value) 44 try: 45 fs_path, url = settings.FILESYSTEM_BASEPATHS[path_key] 46 47 except KeyError: 48 raise forms.ValidationError("Basepath doesn't exists!") 49 50 return path_key, fs_path, url 51 52 31 53 32 54 class BadCharField(forms.CharField): -
trunk/pylucid_project/PyLucid/plugins_internal/filemanager/filemanager.py
r1823 r1825 52 52 from datetime import datetime 53 53 54 from django import forms 54 55 from django.conf import settings 55 from django.http import Http404 56 from django import forms 56 from django.http import HttpResponse 57 57 from django.forms import ValidationError 58 58 from 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 60 from PyLucid.system.BaseFilesystemPlugin import FilesystemPlugin 61 from PyLucid.forms.filesystem import FilenameField, DirnameField 68 62 69 63 #______________________________________________________________________________ … … 111 105 ) 112 106 113 class SelectBasePathForm(forms.Form):114 """ change the base path form """115 base_path = forms.ChoiceField(choices=BASE_PATHS)116 107 117 108 #______________________________________________________________________________ … … 175 166 item_name = FilenameField() 176 167 168 169 170 177 171 #______________________________________________________________________________ 178 172 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): 173 class filemanager(FilesystemPlugin): 282 174 """ 283 175 The PyLucid plugin class. 284 176 """ 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 291 177 def get_filelist(self): 292 178 """ … … 306 192 "name": "..", 307 193 "link": self.URLs.methodLink( 308 method_name="filelist", args=(self.path["base_ no"], updir)194 method_name="filelist", args=(self.path["base_key"], updir) 309 195 ), 310 196 "is_dir": True, … … 370 256 # start with the first base_path entry: 371 257 dir_links = [{ 372 "name": self.path["base_ path"], # use only the short relative path258 "name": self.path["base_fs_path"], # use only the short relative path 373 259 "title": self.path["abs_path"], 374 260 "link": self.URLs.methodLink( 375 method_name="filelist", args=self.path["base_ no"]261 method_name="filelist", args=self.path["base_key"] 376 262 ), 377 263 }] … … 383 269 dir_links.append({ 384 270 "name": name, 385 "title": os.path.join(self.path["base_ path"], path),271 "title": os.path.join(self.path["base_fs_path"], path), 386 272 "link": self.URLs.methodLink( 387 273 method_name="filelist", 388 args=(self.path["base_ no"], path)274 args=(self.path["base_key"], path) 389 275 ), 390 276 }) … … 571 457 # Change the global page title: 572 458 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) 588 465 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"/") 592 468 593 469 #-------------------------------------------------------------------------- … … 597 473 List dir and file. Some actions. 598 474 rest: path to dir or file 599 """ 475 """ 600 476 if not path_info: 601 477 self.select_basepath() … … 607 483 608 484 # 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"]) 610 486 self.context["PAGE"].title = _("File list - %s" % path) 611 487 … … 691 567 ), 692 568 } 693 # self.page_msg(context) 694 # self._render_template("filelist", context, debug=True) 569 #self.page_msg(context) 695 570 self._render_template("filelist", context)#, debug=True) 696 571 -
trunk/pylucid_project/PyLucid/settings_example.py
r1800 r1825 177 177 ADMIN_MEDIA_PREFIX = "/django/contrib/admin/media/" 178 178 179 # Base path for the filemanager plugin 180 # You can add more path for the plugin. 181 FILEMANAGER_BASEPATHS = ( 182 MEDIA_ROOT, 183 #"./static/", 184 ) 179 # Base path for all filesystem bases plugins, e.g. Filemanager, Gallery 180 # The dict key would be inserted into the url. 181 # Every item is a tupel containing the filesystem path and the url. 182 # You can add more path, if you need them! 183 FILESYSTEM_BASEPATHS = { 184 "media": (MEDIA_ROOT, MEDIA_URL), 185 # "downloads": ("/var/www/media/downloads/", "/downloads/"), 186 # "my files": ("./my_static_files/", "http://static.your_domain.net/"), 187 # ... 188 } 185 189 186 190 """ -
trunk/pylucid_project/PyLucid/tools/path_manager.py
r1777 r1825 23 23 else: 24 24 from django.conf import settings 25 26 #______________________________________________________________________________27 # Build a list and a dict from the basepaths28 # The dict key is a string, not a integer. (GET/POST Data always returned29 # numbers as strings)30 31 BASE_PATHS = [32 (str(no), unicode(path)) for no, path in enumerate(33 settings.FILEMANAGER_BASEPATHS)34 ]35 BASE_PATHS_DICT = dict(BASE_PATHS)36 25 37 26 #______________________________________________________________________________