[Twisted-web] Override File's DirectoryLister?
Philippe May
phil.twisted-web at hanjinet.org
Sun Nov 4 14:01:11 EST 2012
Hi Rich,
You can certainly do that using the same URL and resource.
The "triage" of the function depending on the method (render_GET, render_POST, etc) is done by the Request class.
Since DirectoryLister is a resource but has overriden its "render" function, it always renders the listing (twisted.web.static, ~ line 1010 in Twisted 12.2).
Your subclass can override it and process the POST method accordingly, here i added a feedback message for the user:
class MyDirectoryLister(DirectoryLister):
## ...
def render(self, request):
if request.method == "POST":
message = self.render_POST(request)
else:
message = ""
## Copied from the original method...
request.setHeader("content-type", "text/html; charset=utf-8")
if self.dirs is None:
directory = os.listdir(self.path)
directory.sort()
else:
directory = self.dirs
dirs, files = self._getFilesAndDirectories(directory)
tableContent = "".join(self._buildTableContent(dirs + files))
header = "Directory listing for %s" % (
cgi.escape(urllib.unquote(request.uri)),)
## ... until here. Now return the template with the message:
return self.template % {"header": header, "tableContent": tableContent, "message": message}
def render_POST(self, request):
## ** Process the form here, considering it's not blocking, ie not too long to write the file **
## Return a useful message, or error...
return "File uploaded"
Then add a "message" field in the template, eg. in a <div>.
You'll end up soon having you own DirectoryLister, as Glyph suggested :)
Hope that helps.
Philippe
Le 4 nov. 2012 à 02:35, Rich Brown a écrit :
> Folks,
>
> Thanks again for the answers to my earlier question about overriding the DirectoryLister class. They worked perfectly. I can tweak and style the appearance as desired. One more question:
>
> Within of the display of a directory's contents, I would like to have the ability to have a form that lets me add a file to that directory.
>
> To do this, I added a <form method="post"> to the template of the DirectoryLister with an <input type=file ...> to specify the file and created a render_POST() function within my subclass of File.
>
> But the render_POST() function doesn't seem to be called when I click Submit. I suspect that I'm misunderstanding the class hierarchy, but don't know what to change. Any advice?
>
> Many thanks!
>
> Rich Brown
> Hanover, NH
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
More information about the Twisted-web
mailing list