Getting the path of a handler method for a redirect
Jonathan Ludwig <[email protected]> Fri, 20 Dec 2019 12:05:21 -0800 (PST)
| Newsgroups | gmane.comp.python.cherrypy |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_282_383434541.1576872321148
Content-Type: multipart/alternative;
boundary="----=_Part_283_1945965646.1576872321148"
------=_Part_283_1945965646.1576872321148
Content-Type: text/plain; charset="UTF-8"
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 cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.com.
------=_Part_283_1945965646.1576872321148
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">It seems like I should be able to find my answer on the In=
ternet, 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 b=
e able to use a path relative to my handler object. This is an example:<br>=
<br><div style=3D"color: rgb(212, 212, 212); background-color: rgb(30, 30, =
30); font-family: "Droid Sans Mono", monospace, monospace, "=
Droid Sans Fallback"; font-size: 14px; line-height: 19px; white-space:=
pre;"><div><span style=3D"color: #6a9955;">#!/usr/bin/env python3</span></=
div><br><div><span style=3D"color: #c586c0;">import</span> os</div><div><sp=
an style=3D"color: #c586c0;">import</span> os.path</div><br><div><span styl=
e=3D"color: #c586c0;">import</span> cherrypy</div><div><span style=3D"color=
: #c586c0;">from</span> cherrypy.lib <span style=3D"color: #c586c0;">import=
</span> static</div><br><div>localDir =3D os.path.dirname(<span style=3D"co=
lor: #9cdcfe;">__file__</span>)</div><div>absDir =3D os.path.join(os.getcwd=
(), localDir)</div><br><br><div><span style=3D"color: #569cd6;">class</span=
> <span style=3D"color: #4ec9b0;">PhotoUploader</span>(<span style=3D"color=
: #4ec9b0;">object</span>):</div><div> UPLOAD_MSG_KEY =3D <span style=3D=
"color: #ce9178;">'upload_message'</span></div><div> <span style=
=3D"color: #569cd6;">def</span> <span style=3D"color: #dcdcaa;">__init__</s=
pan>(<span style=3D"color: #9cdcfe;">self</span>, <span style=3D"color: #9c=
dcfe;">upload_dir</span>):</div><div> <span style=3D"color: #569cd6;=
">self</span>._upload_dir =3D upload_dir</div><br><div> <span style=3D"c=
olor: #dcdcaa;">@cherrypy.expose</span></div><div> <span style=3D"color:=
#569cd6;">def</span> <span style=3D"color: #dcdcaa;">index</span>(<span st=
yle=3D"color: #9cdcfe;">self</span>):</div><div> upload_message =3D =
cherrypy.session[<span style=3D"color: #569cd6;">self</span>.UPLOAD_MSG_KEY=
] <span style=3D"color: #c586c0;">if</span> <span style=3D"color: #569cd6;"=
>self</span>.UPLOAD_MSG_KEY <span style=3D"color: #569cd6;">in</span> cherr=
ypy.session <span style=3D"color: #c586c0;">else</span> <span style=3D"colo=
r: #ce9178;">''</span></div><div> msg =3D <span style=3D"col=
or: #ce9178;">"""</span></div><div><span style=3D"color: #ce=
9178;"> <html></span></div><div><span style=3D"color: #ce9178;=
"> <body></span></div><div><span style=3D"color: #ce9178;"=
> <p></span><span style=3D"color: #569cd6;">%s</span><span=
style=3D"color: #ce9178;"></p></span></div><div><span style=3D"color=
: #ce9178;"> <h2>Upload New Photos</h2></span></div>=
<div><span style=3D"color: #ce9178;"> <form action=3D&quo=
t;upload" method=3D"post" enctype=3D"multipart/form-dat=
a"></span></div><div><span style=3D"color: #ce9178;"> =
<input value=3D"Select Photos" type=3D"file" n=
ame=3D"myFiles" multiple/><br /></span></div><div><span =
style=3D"color: #ce9178;"> <input value=3D"Uploa=
d" type=3D"submit" /></span></div><div><span style=3D"col=
or: #ce9178;"> </form></span></div><div><span style=3D=
"color: #ce9178;"> </body></span></div><div><span style=3D=
"color: #ce9178;"> </html></span></div><div><span style=3D"col=
or: #ce9178;"> """</span> % upload_message</div><div>=
<span style=3D"color: #c586c0;">if</span> <span style=3D"color: #56=
9cd6;">self</span>.UPLOAD_MSG_KEY <span style=3D"color: #569cd6;">in</span>=
cherrypy.session:</div><div> <span style=3D"color: #c586c0;">de=
l</span> cherrypy.session[<span style=3D"color: #569cd6;">self</span>.UPLOA=
D_MSG_KEY]</div><div> <span style=3D"color: #c586c0;">return</span> =
msg</div><br><div> <span style=3D"color: #dcdcaa;">@cherrypy.expose</spa=
n></div><div> <span style=3D"color: #569cd6;">def</span> <span style=3D"=
color: #dcdcaa;">upload</span>(<span style=3D"color: #9cdcfe;">self</span>,=
<span style=3D"color: #9cdcfe;">myFiles</span>):</div><div> files_s=
ent =3D <span style=3D"color: #dcdcaa;">len</span>(myFiles)</div><div> =
files_uploaded =3D <span style=3D"color: #b5cea8;">0</span></div><br><di=
v> <span style=3D"color: #c586c0;">for</span> myFile <span style=3D"=
color: #569cd6;">in</span> myFiles:</div><div> upload_path =3D o=
s.path.join(<span style=3D"color: #569cd6;">self</span>._upload_dir, myFile=
.filename)</div><div> <span style=3D"color: #c586c0;">with</span=
> <span style=3D"color: #dcdcaa;">open</span>(upload_path, <span style=3D"c=
olor: #ce9178;">'wb'</span>) <span style=3D"color: #c586c0;">as</sp=
an> output_file:</div><div> size =3D <span style=3D"color: #=
b5cea8;">0</span></div><div> <span style=3D"color: #c586c0;"=
>while</span> <span style=3D"color: #569cd6;">True</span>:</div><div> =
data =3D myFile.file.read(<span style=3D"color: #b5cea8;">819=
2</span>)</div><div> <span style=3D"color: #c586c0;">if<=
/span> <span style=3D"color: #569cd6;">not</span> data:</div><div> =
<span style=3D"color: #c586c0;">break</span></div><div> =
size +=3D <span style=3D"color: #dcdcaa;">len</span>(data)</=
div><div> output_file.write(data)</div><br><div> =
files_uploaded +=3D <span style=3D"color: #b5cea8;">1</span></div><br><=
div> cherrypy.session[<span style=3D"color: #569cd6;">self</span>.UP=
LOAD_MSG_KEY] =3D <span style=3D"color: #ce9178;">'</span><span style=
=3D"color: #569cd6;">%u</span><span style=3D"color: #ce9178;"> of </span><s=
pan style=3D"color: #569cd6;">%u</span><span style=3D"color: #ce9178;"> fil=
es successfully uploaded'</span> % (files_sent, files_uploaded)</div><d=
iv> <span style=3D"color: #dcdcaa;">print</span>(<span style=3D"colo=
r: #dcdcaa;">dir</span>(cherrypy))</div><div> <span style=3D"color: =
#c586c0;">raise</span> cherrypy.HTTPRedirect(<span style=3D"color: #ce9178;=
">'/files'</span>) <<=3D=3D I don't want to have to spec=
ify this path. I want it to be relative to the</div><div> =
P<span style=3D"color: rgb(78, 201, 176);=
">hotoUploader instance so I can change it's mountpoint without having =
to change this</span></div><br><div><span style=3D"color: #c586c0;">if</spa=
n> <span style=3D"color: #9cdcfe;">__name__</span> =3D=3D <span style=3D"co=
lor: #ce9178;">'__main__'</span>:</div><div> <span style=3D"colo=
r: #6a9955;"># CherryPy always starts with app.root when trying to map requ=
est URIs</span></div><div> <span style=3D"color: #6a9955;"># to objects,=
so we need to mount a request handler root. A request</span></div><div> =
<span style=3D"color: #6a9955;"># to '/' will be mapped to HelloWo=
rld().index().</span></div><div> conf =3D {</div><div> <span styl=
e=3D"color: #ce9178;">'/'</span>: {</div><div> <span sty=
le=3D"color: #ce9178;">'tools.sessions.on'</span>: <span style=3D"c=
olor: #569cd6;">True</span></div><div> }</div><div> }</div><div> =
cherrypy.quickstart(PhotoUploader(absDir), <span style=3D"color: #ce9178=
;">'/files'</span>, conf)</div></div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;cherrypy-users" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]">cher=
rypy-users+unsubscribe-/[email protected]</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/d/msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegrou=
ps.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/=
msgid/cherrypy-users/48c2b911-8ae8-4c0f-bab6-064356bc8655%40googlegroups.co=
m</a>.<br />
------=_Part_283_1945965646.1576872321148--
------=_Part_282_383434541.1576872321148--