Post PloneFormGen data to an external site in custom script adapter

768 Views Asked by At

Is there a way to pass PloneFormGen form data to an external site with post request in custom script adapter?

My external site is visible only by server

url for post is http://app.com/issues_form/issues

My script in custom stript adapter:

import urllib
import urllib2

url = "http://app.com/issues_form/issues"
values = {'issue' : request.form['issue'] }
data = urllib.urlencode(values)
urllib2.Request(url,data)

When submitting I get unauthorized access or something with permissions

http://ploneapp.com/acl_users/credentials_cookie_auth/require_login?came_from=http%3A/ploneapp.com/structure/test/issue_form

It seems the errors happens when I use urllib.urlencode.

It is the same when I try to use another way with 'requests' library. Maybe I cant access such libraries in such places.

2

There are 2 best solutions below

0
On BEST ANSWER

This is not possible via the custom script adapter unless you bypass some security — which is inadvisable. The problem is that the urllib and urllib2 libraries have not been marked as "safe" in restricted Python. "Restricted Python" is the subset of Python available for TTW scripting.

If you need to do something like this, you'll need to add a file system product, probably a browser view. You'll find examples of packages that do this with PFG input in the products section of Plone.Org. Most are concerned with external payment or membership systems. You'll find they deal with a lot of thorny issues, like threading and timeouts, to make sure that Plone is not tied up waiting for a post to process.

2
On

Actually, if you redirect the browser to the new site, the browser will automatically repost the form and all its data to that system. True that it is not sending this data behind the scenes, hidden from the user, but the site that now gets passed the data can now start showing other options/processing it. This is good, for example, if there is a pay-on-line type service that needs fields posted to them. Such as Credit Card Processing.

Yet, no so good for the "hidden" system. still this has some uses.

The following should work. I use a different version that gets a list of images in a folder and randomly selects one to show on the site.


# REDIRECTING to a specific URL
# NOTE: Form Submissions will cause the browser to resend the data to the new site.
# get the HTML request and response objects.
# redirect the page to the other site and end function at the same time
return context.REQUEST.RESPONSE.redirect("http://app.com/issues_form/issues")