web2py pass parameters to controler and download a file - post

I have the following problem
On my page user can select date from and date to. This dates are to be send to controller on button click, which creates an excel file and user receives that file?
Any suggestions ??
Thank you

def excel_file():
form = SQLFORM.factory(Field('start_date', 'date'),
Field('end_date', 'date'))
if form.process(session=None).accepted:
excel_file = create_excel_file(form.vars.start_date, form.vars.end_date)
return response.stream(excel_file, filename='name_of_file.xlsx',
attachment=True)
return dict(form=form)
Note, the above sets session=None in the call to .process() in order to disable use of the _formkey hidden field (otherwise, you would only be able to submit the form once and would have to manually reload the page for a second submission). This means there is no CSRF protection, but that shouldn't be a problem here, as the form submission is just being used to request data rather than make any changes. If you need CSRF protection, you will have to implement it manually.
Also, note that excel_file can be a file-like object (such as StringIO), an open file object, or a string representing a full file path.
Alternatively, in the browser, you could add an event handler via Javascript to capture the button click and instead of allowing the form to post, call window.open() with a web2py URL that will create and serve the file (you would have to pass the values of the start and end dates from the form via the query string of the URL). You could optionally blank out the form fields after the submission.

Related

rails: enforce validation check with form with multiple submit buttons

I have a form with multiple submit buttons that trigger different functionality in controller action. One submit button allows user to save csv with format. Another submit button allows user to load csv file with specified format from dropdown. Then a final submit button allows user to save actual formatted records to database. In order to save records, the user must either save csv format first or load a csv format. How can I enforce this validation check? How can I determine one of the other two submit buttons were triggered before the create call?
The problem with using session variable will be if I add a boolean in session after one button submitted, then when they import another csv file, that value still will be in session and give false positive.

Add field for each file in fine-uploader

Trying to implement Fine-Uploader for first time.
There's a way I can add a text-field for each file pending uploading?
I want the user to comment the content of each file and than save these informations.
Thanks for explanations.
Dario.
If you want to add an input text field next to each file represented in the UI (I'm assuming you are using FineUploader mode), you can do the following:
Set the autoUpload option to false. This seems obvious, but I thought I should list it anyway. If you don't set this to false, files will be uploaded immediately after the user selects them. I'm guessing this would not fit into your workflow.
Define an onSubmitted callback handler. In this handler, you can use the id parameter to get a handler on the associated getItemByFileId API method.
Once you have the element that represents the file in the UI, you can add an input field next to it. Perhaps you should add a data attribute or a css class that you can later use to associate this field with the file.
I assume you intend to create a button that a user will click when they are ready to start uploading all selected files, presumably after they have filled out any text fields associated with these files. So, you can add a click handler to this button that calls the uploadStoredFiles API method.
You will also need to contribute an onUpload callback handler. When your handler is invoked (once for each file, before it is uploaded), grab the value from the associated text field and then use the setParams API method to send this value with the upload (POST) request for this file as a parameter. Don't forget to include the file ID as the last parameter when you call setParams.

Update param and call action in controller on click?

I have been banging my head over this for the past 3 days (No kidding!!!)....It seems like a very simple thing but I am just unable to do it. So I'm putting the question out here, and am open to any method which would work.
BACKGROUND : An advanced search form on submission used to generate an URL too large for the server. So the form had to be submitted via POST. So far, so good. Form submitted successfully, and the result was displayed. Only remaining problem was pagination.
As it's a POST call, it ruled out will pagination. While will-pagination merges param page to the existing params, but the problem is that it shows on url which results in the same error.
QUESTION: So is there any way, if the user clicks a link NEXT, I can
a) Update the page param
b) Keep the existing params
c) While not changing the URL
d) Transfer control back to the action in controller?
The only solution so far suggested was have a hidden form, repopulate it's value and submit again. The form's pretty complex, so if there is a cleaner way I'd like to know.
I see what you want from your comment. So editing my reply accordingly. You want the information as to which column is being selected in the sort to be available to the controller without having that information available in the url string, and you want to use GET for this not POST
The challenge you have is that you want to preserve state between requests and you need a mechanism for doing this. POST preserves that information by sending it in the body of the POST request. GET does this by allowing you to populate the query string. The other method for preserving state is to use a cookie.
You can manipulate the url with javascript on the page. There are tutorials online for that.
But if you just want a simple controller hack as you say in your comment then the simplest one I can think of is to allow the user to make the GET request to the url with the query params in it, then handle this request in two steps - step one saves the query information to the cookie, step two redirects them to the url with that query string data stripped, and you look on the cookie to see if it has those fields before building your data for that page. It's hacky but it works.
def sort
session[:sort] = params[:sort]
redirect_to url_without_the_query_string
end
There is also the new html 5 feature:
window.history.replaceState(“Replace”, “Page Title”, “/mypage”);
from this stackoverflow answer here: How to remove query string from url using javascript
However I'm not sure I'd recommend messing with the url after load like that (I don't know enough about that solution so I'd recommend you read that answer and see if that fits). So, if you MUST lose that data from the url string, because you need to somehow pass it between requests you either have to use POST, or use the session cookie/redirect method I describe above.
Does your html <form> have an action attribute? If not, add one that points to the page.
If you want to preserve the current GET params so that results from the POST can use , you will also need to modify the javascript triggered on the heading links so that as well as (or instead of) modifying the query string, they write the same data to hidden form fields (which of course then get posted in the body of the request). Your JS library may already include helpful functions for that.

how to get html tags of page without URL

I want to save the HTML tags of whole page that is going to (render) client side in the database table with a unique id so next time I can fetch that page directly from database with id and without any complicate logic execution. All this without any URL means I don't want to pass a URL to any method that will get the HTML tag. I want to get HTML tags before even rendering the page because I have some status system once status changed that URL will not work.
Sorry if my question is not clear, but I tried to mention all details. I simply want HTML tag of my view before sending it to client so that I can save those tags with a unique id in database table and next time I can just fetch those tags from the database table with that unique id. My need is that the view contains many complex queries that I don't want to run each time.
It's a kind of certificate in that once it is printed, it cannot be changed, it can only be viewed by that unique ID in a faster way.
What you want to do is called output caching, use that instead.
If you need to save the HTML output, you need to add an action filter and intercept the Result and save the text in database.

how to return url in form file in strut

hai all,
i have one problem regarding to return the form file value is validation fail. i have one form that have a few field to be fullfil by the user and one attachment field.if one of the field is blank the system will give the error when submit the form..my problem is, all the field will have the value that we entered before this but for form file it disappear.
Let's see if I understand your issue: You have a form with several input fields, and among them a <input type=file> for uploading some file content. In case of validation error, you return to that page (with struts2 result = INPUT, I guess) and the content of the previously filled fields appears, except for the FILE field.
This makes sense, if you understand how the other fields are "refilled" in Struts2 in this scenario (just from the action, which has typically mapped the parameters to properties). The server doesnt know the (clientside) fullpath of the file, it is not sent along the http request (it would be a privacy issue), it's the content of the file what is uploaded (and perhaps the filename without path). You can't escape that.
Anyway, I think this a case in which validation should signal an error early (in javascript, in the client side). Think about it: the user is uploading a file (potentially large) together with other info, and AFTER uploading it the server checks the fields and instructs the user to refill the fields and retry (including the upload). This can be unacceptable. My advise is, then, to include client-side validation (and if it pass, and the server validation fails, then the user must resign to refill the FILE field). If the file is large, one could split the input form in two pages, a first one to fill the several fields, and afterwards another to upload the file.

Resources