Getting the path of a handler method for a redirect

Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting the path of a handler method for a redirect

Jonathan Ludwig
It seems like I should be able to find my answer on the Internet, but maybe I don't what to search for. I have a simple form and I want to redirect to another page after is has been submitted. I want to be able to use a path relative to my handler object. This is an example:

#!/usr/bin/env python3

import os
import os.path

import cherrypy
from cherrypy.lib import static

localDir = os.path.dirname(__file__)
absDir = os.path.join(os.getcwd(), localDir)


class PhotoUploader(object):
UPLOAD_MSG_KEY = 'upload_message'
def __init__(self, upload_dir):
self._upload_dir = upload_dir

@cherrypy.expose
def index(self):
upload_message = cherrypy.session[self.UPLOAD_MSG_KEY] if self.UPLOAD_MSG_KEY in cherrypy.session else ''
msg = """
<html>
<body>
<p>%s</p>
<h2>Upload New Photos</h2>
<form action="upload" method="post" enctype="multipart/form-data">
<input value="Select Photos" type="file" name="myFiles" multiple/><br />
<input value="Upload" type="submit" />
</form>
</body>
</html>
""" % upload_message
if self.UPLOAD_MSG_KEY in cherrypy.session:
del cherrypy.session[self.UPLOAD_MSG_KEY]
return msg

@cherrypy.expose
def upload(self, myFiles):
files_sent = len(myFiles)
files_uploaded = 0

for myFile in myFiles:
upload_path = os.path.join(self._upload_dir, myFile.filename)
with open(upload_path, 'wb') as output_file:
size = 0
while True:
data = myFile.file.read(8192)
if not data:
break
size += len(data)
output_file.write(data)

files_uploaded += 1

cherrypy.session[self.UPLOAD_MSG_KEY] = '%u of %u files successfully uploaded' % (files_sent, files_uploaded)
print(dir(cherrypy))
raise cherrypy.HTTPRedirect('/files') <<== I don't want to have to specify this path. I want it to be relative to the
PhotoUploader instance so I can change it's mountpoint without having to change this

if __name__ == '__main__':
# CherryPy always starts with app.root when trying to map request URIs
# to objects, so we need to mount a request handler root. A request
# to '/' will be mapped to HelloWorld().index().
conf = {
'/': {
'tools.sessions.on': True
}
}
cherrypy.quickstart(PhotoUploader(absDir), '/files', conf)

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Getting the path of a handler method for a redirect

cyraxjoe
You can use the cherrypy.url function, this should do it:

raise cherrypy.HTTPRedirect(cherrypy.url('.'))

--
Joel Rivera


On Fri, Dec 20, 2019, at 2:05 PM, Jonathan Ludwig wrote:
It seems like I should be able to find my answer on the Internet, but maybe I don't what to search for. I have a simple form and I want to redirect to another page after is has been submitted. I want to be able to use a path relative to my handler object. This is an example:

#!/usr/bin/env python3

import os
import os.path

import cherrypy
from cherrypy.lib import static

localDir = os.path.dirname(__file__)
absDir = os.path.join(os.getcwd(), localDir)


class PhotoUploader(object):
UPLOAD_MSG_KEY = 'upload_message'
def __init__(self, upload_dir):
self._upload_dir = upload_dir

@cherrypy.expose
def index(self):
upload_message = cherrypy.session[self.UPLOAD_MSG_KEY] if self.UPLOAD_MSG_KEY in cherrypy.session else ''
msg = """
<html>
<body>
<p>%s</p>
<h2>Upload New Photos</h2>
<form action="upload" method="post" enctype="multipart/form-data">
<input value="Select Photos" type="file" name="myFiles" multiple/><br />
<input value="Upload" type="submit" />
</form>
</body>
</html>
""" % upload_message
if self.UPLOAD_MSG_KEY in cherrypy.session:
del cherrypy.session[self.UPLOAD_MSG_KEY]
return msg

@cherrypy.expose
def upload(self, myFiles):
files_sent = len(myFiles)
files_uploaded = 0

for myFile in myFiles:
upload_path = os.path.join(self._upload_dir, myFile.filename)
with open(upload_path, 'wb') as output_file:
size = 0
while True:
data = myFile.file.read(8192)
if not data:
break
size += len(data)
output_file.write(data)

files_uploaded += 1

cherrypy.session[self.UPLOAD_MSG_KEY] = '%u of %u files successfully uploaded' % (files_sent, files_uploaded)
print(dir(cherrypy))
raise cherrypy.HTTPRedirect('/files') <<== I don't want to have to specify this path. I want it to be relative to the
PhotoUploader instance so I can change it's mountpoint without having to change this

if __name__ == '__main__':
# CherryPy always starts with app.root when trying to map request URIs
# to objects, so we need to mount a request handler root. A request
# to '/' will be mapped to HelloWorld().index().
conf = {
'/': {
'tools.sessions.on': True
}
}
cherrypy.quickstart(PhotoUploader(absDir), '/files', conf)


--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/2200bce5-a309-408b-88a4-0d1f40a1e9ad%40www.fastmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Getting the path of a handler method for a redirect

Jonathan Ludwig
Thanks. That's what I was looking for.

On Friday, December 20, 2019 at 4:54:41 PM UTC-7, Joel Rivera wrote:
You can use the<a href="https://docs.cherrypy.org/en/latest/pkg/cherrypy.html#cherrypy.url" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.cherrypy.org%2Fen%2Flatest%2Fpkg%2Fcherrypy.html%23cherrypy.url\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG053Stwn8cCQ-ULQXnX4LGoawzkw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.cherrypy.org%2Fen%2Flatest%2Fpkg%2Fcherrypy.html%23cherrypy.url\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG053Stwn8cCQ-ULQXnX4LGoawzkw&#39;;return true;"> cherrypy.url function, this should do it:

raise cherrypy.HTTPRedirect(cherrypy.url('.'))

--
Joel Rivera


On Fri, Dec 20, 2019, at 2:05 PM, Jonathan Ludwig wrote:
It seems like I should be able to find my answer on the Internet, but maybe I don't what to search for. I have a simple form and I want to redirect to another page after is has been submitted. I want to be able to use a path relative to my handler object. This is an example:

#!/usr/bin/env python3

import os
import os.path

import cherrypy
from cherrypy.lib import static

localDir = os.path.dirname(__file__)
absDir = os.path.join(os.getcwd(), localDir)


class PhotoUploader(object):
UPLOAD_MSG_KEY = 'upload_message'
def __init__(self, upload_dir):
self._upload_dir = upload_dir

@cherrypy.expose
def index(self):
upload_message = cherrypy.session[self.UPLOAD_MSG_KEY] if self.UPLOAD_MSG_KEY in cherrypy.session else ''
msg = """
<html>
<body>
<p>%s</p>
<h2>Upload New Photos</h2>
<form action="upload" method="post" enctype="multipart/form-data">
<input value="Select Photos" type="file" name="myFiles" multiple/><br />
<input value="Upload" type="submit" />
</form>
</body>
</html>
""" % upload_message
if self.UPLOAD_MSG_KEY in cherrypy.session:
del cherrypy.session[self.UPLOAD_MSG_KEY]
return msg

@cherrypy.expose
def upload(self, myFiles):
files_sent = len(myFiles)
files_uploaded = 0

for myFile in myFiles:
upload_path = os.path.join(self._upload_dir, myFile.filename)
with open(upload_path, 'wb') as output_file:
size = 0
while True:
data = myFile.file.read(8192)
if not data:
break
size += len(data)
output_file.write(data)

files_uploaded += 1

cherrypy.session[self.UPLOAD_MSG_KEY] = '%u of %u files successfully uploaded' % (files_sent, files_uploaded)
print(dir(cherrypy))
raise cherrypy.HTTPRedirect('/files') <<== I don't want to have to specify this path. I want it to be relative to the
PhotoUploader instance so I can change it's mountpoint without having to change this

if __name__ == '__main__':
# CherryPy always starts with app.root when trying to map request URIs
# to objects, so we need to mount a request handler root. A request
# to '/' will be mapped to HelloWorld().index().
conf = {
'/': {
'tools.sessions.on': True
}
}
cherrypy.quickstart(PhotoUploader(absDir), '/files', conf)


--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="F7pNkfxPBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">cherryp...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/29c41462-9ded-4c37-8925-c87ca3af3530%40googlegroups.com.