Handling array as form name in flask - python

If I send a form like this:
<form method="POST" ...>
<input type="text" name="filters[price_from]">
<input type="text" name="filters[price_to]">
</form>
to PHP script it automatically creates an array and I can access variables like this:
$_POST['filters']['price_to']
and:
$_POST['filters']
is iterable
AND this is my question, how can I get same effect in FLASK?
request.form.getlist('filters')
return [] (empty list)
request.form.get('filters[price_from]')
return right value but this is not the result of which they expect (its not iterable).
Should i rebuild my form or use some other method?

You cannot access a python array/list using a key, if you want to access your data using a key, store your filters as a json object, then you will be able to access the data by using a key.
<form method="POST" enctype="application/JSON">
<input type="text" name="filters[price_from]" value="" >
<input type="text" name="filters[price_to]" value="" >
</form>
filters = request.get_json()
filters['price_from'] #returns price_from value
filters['price_to'] #returns price_to value
In php an array is several things.
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.
While in python an array is only a list accessed by index.
>>> filters = ['price_from', 'price_to']
>>> filters[0]
'price_from'
>>> filters[1]
'price_to'
And a dict is accessed by key.
>>> filters = {'price_from':'value', 'price_to':'another_value'}
>>> filters['price_from']
'value'
>>> filters['price_to']
'another_value'

Related

Python selenium - set value using data-qa attribute

I have the following object,
<div class="form-group">
<label data-qa="username-label"><span id="i18n-2">Username</span></label>
<input id="ember4124" class="ember-view ember-text-field form-control" data-qa="username-input" type="text" value="">
</div>
Here the input id "ember4124" changes every time. My requirement is find by data-qa "username-input" and pass the value, where value is the username.
Using below I can find the element,
driver.find_elements_by_css_selector("input[data-qa='username-input']")
Output: [<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="90144b65-8a86-4d54-90ae-4ccccab0cc9e", element="18e84265-46a7-41d8-a487-299e95ef7ae8")>]
But not sure how to send the value. Can anyone assist me?
Probably there is only one element so use element (without s at the end) instead of elements to get only first element and you will have
x = driver.find_element_by_css_selector("input[data-qa='username-input']")
x.send_keys("James Bond")
If you will use elements (with s at the end) then you get list and you will have to use [0] to get first element on list
x = driver.find_elements_by_css_selector("input[data-qa='username-input']")
x[0].send_keys("James Bond")

How to get value from date filter input field with Selenium

There is a date filter with input fields and drop-down datepicker like on below image
I want to get current value of, for example, "From" input field (expected output = "03/13/2013"). So for following element's code
<div class="input-group date" id="inputValidFrom">
<input name="validFrom" class="form-control" id="inputValidFromValue" required="" type="text"> </input>
I use Python's lines:
>>>from selenium import webdriver
>>>driver = webdriver.Ie()
>>>input = driver.get_element_by_xpath('//input[#name="validFrom"]')
>>>input.text # returns empty string
''
>>>input.get_attribute('value') # also returns ''
''
>>>input.value_of_css_property('text') # returns again just empty string
''
Who knows the way how to get this input field box property?
You are using the IE-Driver with XPath.
Try writing the attribute name in lower case.
http://www.seleniumhq.org/docs/03_webdriver.jsp#by-xpath

Python Pass List into Form and Retrieve as List

What is the best way to pass List via form in python (cgi).
ListStr = ['State1', 'State2', 'State3']
TrVListStr = '##'.join(ListStr)
print """
<form method="post">
<input type=hidden name="state_carry" value="""+TrVListStr+"""><br />
<input type="submit" value="Submit" />
</form>
"""
After submission I should have list as it was before submission.
I can do split (based on ## rule) form['state_carry].value again to fetch that. but I think it is not good way.
Is there any way to pass Python List via form and retrieve them later.
Thanks.
You could use the python cgi module. The documentation specifically covers the case where you have multiple values for a certain field name.
The basic idea is that you can have multiple fields in your html form with the same name, and the value for each field is one of the values from the list. You can then use the getlist() method to retrieve all the values as a list. For example:
print "<form method=\"post\">"
for s in ListStr:
print "<input type=hidden name=\"state_carry\" value=\"" + s + "\"><br />"
print "<input type=\"submit\" value=\"Submit\" />"
print "</form>"
Then in your CGI script you'll have something like:
MyList = form.getlist("state_carry")
In python I would do.
###In the form page.
import cgi
#Convert list of values to string before passing them to action page.
ListStr=','.join(['State1', 'State2', 'State3'])
print "<input type=hidden name=\"state_carry\" value=\""+ListStr+"\"><br />"
###In the action page
import cgi
#Return the string passed in the user interaction page and transform it back to a list.
ListStr=cgi.FieldStorage().getvalue('state_carry').split(',')

Always the same results through ZCatalog? Everything is set by the book!

It is strange, but I can't get the ZCatalog working. Everything is done by the book and still the results are always the same full list.
You can see here:
http://nfp-bg.eionet.eu.int/waste/en/search-results?catalogTextIdx=selection
What I have done so far:
Created ZCatalog Object
Created ZCTextIndex Lexicon with the following items
HTMLWordSplitter
CaseNormalizer
StopWordRemover
Created catalogTextIdx Index - the parameters are:
Name(s) of attribute(s) indexed: PrincipiaSearchSource
Index type: Okapi BM25 Rank
ZCTextIndex Lexicon used: http://nfp-bg.eionet.eu.int/waste/catalog//catalogTextIdx/catalogLexicon
The MetaData collected is:
PrincipiaSearchSource
id
title
The Find Object is run only for Objects of Type: DTML Document
The list of items is created correctly for the catalog (I have removed only the css / js dtml files)
The following code is used for the Search form:
<form action="search-results" method="get">
<input type="text" name="catalogTextIdx" id="catalogTextIdx" value="<dtml-if catalogTextIdx>
<dtml-var catalogTextIdx><dtml-else>Search...</dtml-if>" class="search-field" />
<input type="submit" name="SUBMIT" value="Submit Query" class="button" />
</form>
Finally the following code is used for the Search Results Page:
<dtml-with common>&dtml.-Header;&dtml.-left-column;</dtml-with>
<td id="content" valign="top">
<h2>Search Results</h2>
<dtml-in expr="catalog(meta_type=['DTML Document'])">
<h3>Result founded: "><dtml-var title></h3>
<dtml-var "filterRenderedHTML(PrincipiaSearchSource)">
</dtml-in>
</td>
<dtml-with common>&dtml.-right-column;&dtml.-Footer;</dtml-with>
It should be working, but it's not. I believe it should be something small, but still not sure.
Thanks in advance for all your help.
The ZCatalog has a quirk which is that if your query includes an index that does not exist in the catalog, it will return all indexed objects. In this case, your query appears to involve a "meta_type" index which you did not set up. So the ZCatalog tries to apply that index, but doesn't find it, so it returns all the items.

Use getControl to control objects other than the name variable

I am using the Zope testbrowser which has been recommended in my last question. The problem that I am facing is that I can use the getControl function to control different objects like: password, username etc.
I am trying to submit the page to get to the next page but the submit button has no 'name' variable, just an 'id' variable. 'Submit' is written as follows:
<input type="submit" id="lgn_button" class="button" tabindex="3" accesskey="s" />
and the other objects are written as:
<input type="password" class="button" name="password" id="password" size="24" maxlength="20" accesskey="p" tabindex="2" value=""/></td>
I have no access to change this. The python zope code I am using to gain control of the 'password' object is:
browser.getControl(name='password')
The submit button doesn't have 'name' so I have written:
browser.getControl(id='lgn_button')
This prints out the error that 'id' is invalid:
TypeError: getControl() got an unexpected keyword argument 'id'
Is there any way to gain control of one of the other values in 'submit'.
Thanks for any help.
I assume that, for one reason or another, you can't add a 'name' attribute to your tag, but if it's only a 'name' that you can't add, you can explicitly set a 'value=Submit' (instead of relying on the default one, which is Submit) and then use browser.getControl('Submit')
Failing that, you can do something along the lines of
for form in browser.mech_browser.forms():
for control in form.controls:
if control.id == 'lgn_button':
return control
You might even want to extend Browser.getControl() with that and contribute it back to zope.testbrowser. ;)

Categories

Resources