How do I implement Post-redirect-get on Google App Engine with Python? - form-submit

I am developing an online bidding system on Google App Engine with Python. Regarding the post-redirect-get mechanism, I've been googling a while and still have no clear idea of how to implement it. Suppose:
HTML:
<form action="/test" method="post">
...
<input type="submit" value="Submit" />
</form>
Python:
# Collect data from the posted form
...
# Save data into datastore
...
# Prepare template values
tempalteValues = { ... }
path = os.path.join(os.path.dirname(__file__), 'templates/', 'responseMessage.html')
handler.response.out.write(template.render(path, templateValues))
# Then what?
I have two questions:
1) After rendering the response message file, what should I do next? That is, how to implement the 'GET'?
2) Another strategy I can think of is: If the post is supposed to happen only once (e.g., product purchasing with a unique order number), can I set a flag in the entity indicating that the form has been submitted and the following posts will be ignored if the flag is set? Is this feasible or even correct?
(Note: because the order number is generated by the system, the entity has to be saved before the form submission in order to get that number)
Thanks in advance.

What you are looking for is building a Restful service something like this:
class BiddingHandler(webapp2.RequestHandler):
def get(self):
#Get code goes here for this handler
def post(self):
#code that gets your posted data and processes it
def delete(self):
#code to delete something
app = webapp2.WSGIApplication([('/bidding', BiddingHandler)])
Looking at the above if you wanted to do a redirect after making a post in the last line of your post instead of rendering a template you would simply redirect the user to the get part of the handler with the following line:
self.response.redirect('/bidding')
What I have shown you above is the correct way to implement it. Writing to the datastore and reading from it for every request would mean more overhead and costs.

Related

SitePrism - Dynamically Define Element Based on Current Edit ID

Please note I'm new to SitePrism. I'm putting together a Capybara feature test case for editing data. The form's ID is "edit_bears_1", where you can imagine the #1 changes based on the current object's ID.
<form class="form-horizontal validate-form" id="edit_bears_1" action="admin/bears/1" accept-charset="UTF-8" method="post" novalidate="novalidate">
Here is the SitePrism file I created to define the form:
module Pages
module Admin
module Bears
class Edit < SitePrism::Page
set_url '/admin/bears/edit/:id'
set_url_matcher %r{/admin/bears/\d+/edit}
binding.pry
section :form, Form, "form#edit_bears_1"
end
end
end
end
As you can see I currently have the form's ID hard-coded to #1, but this is likely to change. How can I replace #1 with the some sort of variable that gets evaluated to and is linked to the bear I'm editing? All help is appreciated!
You could script in something that would re-create it. Or if you feel as if this is something we should be supporting out of the box you could create an issue and request a new feature for it.
GH Link: https://github.com/natritmeyer/site_prism/issues
Script example 5.times { |i| section "form#{i}", Form, "form#edit_bears_#{i}" }
Make sure to give each of the references an index-linked name otherwise they'll self-overwrite.

Following Test Automation best practise of "Methods return other PageObjects" in Ruby

I am a big advocate of the Page Object Pattern (POP) as defined by the experts at Selenium:
https://code.google.com/p/selenium/wiki/PageObjects
A key view of theirs that I have always followed when using Appium with Java is:
"Methods return other PageObjects"
e.g. LoginPage loginPage = homePage.gotoLoginPage();
I am now trying to following POP using Calabash with Ruby and so have been writing code like this:
e.g. #login_page = #home_page.goto_login_page
However, since Ruby doesn't know what type of object #login_page is or #home_page is, you dont get any of the benefits of intellisense showing what methods are available for a given page.
Anyone know a good way around this?
As much as I appreciate and apply PO design pattern, as much I disagree with returning page object by page object. Page object should be independent and don't need to know about other page objects. Look at two examples:
You test form validation. Click on submit button returns page object which is subsequent in the workflow, but in this case you remain on page with validation errors. Your page object won't know about it and will return the other page.
Page which you get to after clicking a button may differ depending on the context (e.g. from what other page you got to current page). It can lead to having multiple versions of actually same method, which will return different page objects depending on context. This is not good and overcomplicates simple thing.
If you want to return current page object, you can benefit from it e.g. in Java, when you return this at the end of the method. Then you can chain all methods you execute as long as you are on the same page. But when it comes to the question 'how to implement returning different page objects' - answer is simple - 'just don't'. Please note wiki entry you quoted has not been updated for a good while and best practices has evolved since it was originally published.
It seems like you already have your solution. However for others and perhaps also for you the x-platform approach to calabash uses page objects so you could check out that implementation https://github.com/calabash/x-platform-example
An alternative method would be as follows. Not as neat as I would like (given the need to manually create new instances of subsequent pages), but available as an alternative option:
When(/^I buy a movie from the movie page$/) do
movie_page = MoviePage.new
movie_page.buyMovie("Test Movie")
purchase_page = PurchasePage.new
purchase_page.confirmPurchase
end
Found a way of getting this to work after much research and applying well known Java/C#/Obj-c principles to Ruby:
Given(/^I am on the launch page$/) do
#launch_page ||= LaunchPage.new
end
When(/^I open the set alarm time page$/) do
#set_alarm_page = #launch_page.goto_set_alarm_page
end
When(/^I open our apps from the home page$/) do
#launch_page.navigation_toolbar.open_our_apps
end
Then(/^I should see the homepage alarm time is (\d+)$/) do |alarm_time|
alarm_time_actual = #launch_page.get_alarm_time
assert_equal(alarm_time, alarm_time_actual)
end
As long as somewhere on the step definition class you explicitly create a new page object (in the above example: LaunchPage.new), then all subsequent pages will provide intellisense method/property values, since the resulting page types returned will be known by RubyMine.

Coldfusion Coldbox - create and cache drop down options from queries

I am looking for opinions and alternate ideas, as what I am doing is working, but wanted to ask if it was optimal
I have a site that when the index handler is called, it populates the request collection with specific queries from database tables so that I can build drop downs for the user to select.
i am querying two models and putting their results in their respective variables, then loop thru them in the view to create the drop down
index Handler
function index(event, rc, prc){
event.paramValue("debug",0);
rc.stages = getmodel("tms_proposal_stage").select();
rc.plannedGiftTypes = getmodel("tms_funding_type").select();
event.setLayout('layout.bootstrap');
}
index view
<div class="form-group">
<label for="proposal_stage" class="control-label">Proposal Stage Code</label>
<select id="proposal_stage" name="proposal_stage" class="form-control">
<cfloop query="rc.stages">
<option value="#stage_code#">#short_desc#</option>
</cfloop>
</select>
</div>
I understand that two queries isnt that costly, but if I needed to run 100 of these that would have scalability issues. These query result sets do not change much, so I was thinking, shouldnt these be cached or stored and accessed a different way?
I thought about html5 local storage, which I have used but not in this regard.
I also considered making a new handler function that makes all of these database calls and is cached, then referenced by other functions
anyways, all thoughts are appreciated
You have several options available to you. Since you're using ColdBox, you have CacheBox readily available to you.
https://github.com/ColdBox/cbox-refcards/raw/master/CacheBox/CacheBox-Refcard.pdf
A very simple way to do inline caching of the data would be to inject a CacheBox provider at the top of your component:
component {
property name="cache" inject="cachebox:default";
}
Then use the cache API in your event to store and retrieve data. My favorite method is getOrSet() because it wraps it up in a single call.
rc.stages = cache.getOrSet(
objectKey="stages",
produce=function(){
return getmodel("tms_proposal_stage").select();
}
);
The closure is only executed if the key is not already in the cache.
http://wiki.coldbox.org/wiki/WhatsNew:CacheBox-1.6.cfm#Get_Or_Set_Baby
Another approach is to cache the full HTML. To do this, create a viewlet that only outputs the HTML for your form control. Create an event that returns the output of just that view like so:
function stagesFormInput(event, rc, prc) cache=true {
var stagesData = getmodel("tms_proposal_stage").select();
return renderView(view="viewlets/stages", args={ stagesData : stagesData } );
}
Note, I'm passing stageData directly into the view so it doesn't pollute the rc or prc. This data will be available in your viewlet as "args.stagesData".
Also note the "cache=true" in the method declaration. That's the magic that will tell ColdBox to cache this event (inside CacheBox's "template" provider"). You can specify a timeout, but this will use the default. Now, enabled eventCaching in your /config/ColdBox.cfc file.
coldbox={
eventCaching = true
}
http://wiki.coldbox.org/wiki/ConfigurationCFC.cfm#Application_Aspects
And finally, in your main view or layout, just run your new viewlet everywhere you want the cached HTML to be output.
#runEvent("viewlets.stagesFormInput")#
This is a little bit more setup, but is more powerful since it caches the full HTML snippet which is really ideal. I also have an entire sample app that demos this inside a working app. You can check it out here:
https://github.com/bdw429s/ColdBox-Viewlet-Sample

Is it possible to pass argument from visualforce apex tag?

I have a function searchWorkByName that takes "key" as an argument and use SQOL to retrieve the data.
In visualforce side, I have a link that calls searchWorkByName but would like to be able to pass argument such as character 'a'
example, (this throws an error)
<apex:commandLink value="search!" action="{!searchWorkByName('aaa')}" />
Is it possible to do so if not what is the alternatives?
apex class
public class SearchWorkTest {
public PageReference searchWorkByName(String key) {
//find record of work names starting from provided key character
workNames = [select name from work__c where work__c.name like 'key%'];
return Page.searchResult;
}
}
visualforce
<apex:page standardController="work__c" extenstions="SearchWorkTest">
<!-- Is it possible to pass argument like 'foo' ? -->
<apex:commandLink value="search!" action="{!searchWorkByName}" />
</apex:page>
You can pass in parameters from a page into a function like this:
<apex:commandLink value="search!" action="{!searchWorkByName}">
<apex:param name="key" value="val"/>
</apex:commandLink>
Obviously, the value of the parameter in this case is fixed. If you want something dynamic (i.e. user types something and that is passed to the function), I'm not 100% sure how you'd do that, but I think it might be possible. However, the solution already posted skins the cat for you, but I thought I'd follow up with an alternative in case it's any use.
No, you cannot pass arguments to actions like that.
1 option is to make this variable a normal form field that user can type text/select from dropdown/whatever - if you'll use same name for a variable in Apex (and make it publicly visible by setters/getters), this will work without problems. Check out my answer at How do I integrate Salesforce with Google Maps? to get started.
Second option - if this search must be somehow done programatically without user having to click anything, if the data for example comes from page itself (i.e. is read in <apex:repeat> tag)... you could make a small helper page & controller and call them as components. There is no problem with passing data to components. Check documentation for <apex:component> and <apex:componentBody>. But I think first answer os most useful for you.
Good luck!

How do I fill in a specific field in Webrat when several share the same name and id?

I'm just getting started with Cucumber and Webrat, and am adding feature specifications to an existing Rails application. One page in the app has multiple forms on it, representing different ways to build a new Character object. Here's a simplified example:
<form id="adopt_character_form">
....
<input type="text" name="character[name]" id="character_name">
...
</form>
<form id="spawn_character_form">
....
<input type="text" name="character[name]" id="character_name">
...
</form>
As you can see, there are (at least) two fields that have the same name and id on the page, though they are in different forms, and I can't find a way in the Webrat rdoc or source code to specify a particular one.
I know that I could change the name or id on one of them, but I would have to deviate from the Rails naming conventions to do so, which I'd really rather not. I could also put the forms on different pages, but it would take more modification to the work flow than I want to do. I could also try to merge them in to a single form that was more modular and dynamic, but having to make structural changes to the UI just to support a given testing framework seems a little questionable and might not always be feasible, plus I would have to require javascript.
Is there any way to specify one of these fields in Webrat, or should I just give up and try Cucumber on a different project?
You can use webrats within method
For example in a cucumber steps file
#my_steps
Then /^I update character name with "([^\"]*)"$/ do |updated_character|
within '#adopt_character_form'
fill_in "character[name]", :with => updated_character
end
end
Hope that helps
Considering having multiple elements with the same ID is an explicit violation of HTML validity, I think you've already deviated from the path of Rails conventions. Recent versions of Rails generally make an effort to keep things standards-conformant. IIRC it's pretty straightforward to call the form helpers in such a way that the IDs are differentiated. If nothing else you can just supply an explicit ID with each call. For the purpose of Rails conventions the name field is the only one that matters, since that's the one Rails will have to map back to a field name on a model.

Resources