Daniel Fetchinson schrieb:
>> What is the easiest way to get a list of files and display all their
>> names. This is to show the user the images they have uploaded to the
>> specific dir.
>
> There doesn't seem to be a canonical method, see
>
> http://www.mail-archive.com/[email protected]/msg37315.html
>
> Although it would be nice to have a controller in tg that does this in
> a purely tg way (i.e. not through apache) in the spirit of batteries
> included :)
It's not that hard to write:
import os
import fnmatch
from os.path import abspath, isdir, join
def get_dir_list(dir_, patterns=None):
files = []
dirs = []
for fn in sorted(os.listdir(dir_)):
if patterns:
for pat in patterns:
if fnmatch.fnmatch(fn, pat):
break
else:
continue
path = os.path.join(dir_, fn)
stat = os.stat(path)
if os.path.isdir(path):
dirs.append((fn, stat.st_mtime))
else:
files.append((fn, stat.st_mtime, stat.st_size))
return dirs, files
class DirListController(controllers.Controller):
root_dir = config.get('image_dir',
'%(top_level_dir)s/static/images')
root_url = '/static/images/'
@expose()
def index(self, dirname=''):
# XXX sanitize path here!
dirpath = abspath(join(self.root_dir, dirname))
dirs, files = get_dir_list(dirpath,
patterns=('*.gif', '*.jpg', '*.png'))
return dict(dirs=dir, files=files, root_url=self.root_url)
I leave it up to you as an excercise to write an appropriate template
for this.
If you want to protect access to these files with TG1 identity:
http://docs.turbogears.org/1.1/StaticFiles#protecting-static-files-via-identity
Chris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---