wtforms SelectMultipleField returning only 1 selected value - flask-wtforms

im following this tutorial to implement a multi checkbox field in flask with jinja
https://gist.github.com/doobeh/4668212
It works for the most part but when I go to access the value in my code (products is the field where I used the multicheckbox) like
class CreateBoardForm(Form):
products = MultiCheckboxField(
'Select Products:',
coerce=int,
choices=[(1, 'one'), (2, 'two'), (3, 'three')])
......
if request.method == 'POST':
products=request.form['products']
print name, description, tags
print "Selected products:", products
When I print selected products I only see one value being selected. When I printed the forms object I see this python data structure ImmutableMultiDict([('products', u'1'), ('products', u'3')])
Im not sure how to get all the values for the 'products' key

you will have to use the getlist method in order to return a list of other choices.
request.form.getlist('products')

Related

Modify query string on a form to add filter based on other fields

I have an enquiry form which works from a view with a custom query. The form has filters, which I use in the executeQuery method of the view on the form to add ranges on various fields.
A new requirement is to filter based on two fields in the query.
Example: The PurchLine table is one of the tables in the query.
A new range is needed :
if PurchLine.ItemId != “” then
filter by PurchLine.PurchStatus == None
but, if the Item has a SPECIFIC value,
then filter by PurchStatus == Received.
(Ok, this is just an example!).
I am unable to modify my query to add a range on the PurchStatus based on the Item field.
I know exactly how the string value of the query must look, but how can I modify the query string?
The current query string looks like this (if I breakpoint on super in executeQuery):
SELECT FIRSTFAST * FROM OpenPOLinesView(OpenPOLinesView) WHERE ((CreatedDateTime<='2016-11-30T23:59:59')) AND ((VendAccount = N'S000001048'))
I want to add this at the end:
AND (((ItemId = N'') AND (PurchStatus = 0)) OR ((ItemId = N'XXX123') AND (PurchStatus = 2)))
How can I modify the query string in code?
You can use query expression for this, e.g.
queryBuildRange.value(strFmt('((%1 == %2) || ((%1 == %3) && (%4 == "%5")))',
fieldStr(InventTable, ItemType),
any2int(ItemType::Service),
any2int(ItemType::Item),
fieldStr(InventTable, ProjCategoryId),
queryValue("Spares")));
Please refer to this link Using Expressions in Query Ranges [AX 2012] for details.

ASP.NET returns incorrect data from oData Controller

I have an ASP.NET Web API app using Oracle's Entity Framework driver. I have an entity defined for a view as follows:
CREATE OR REPLACE FORCE VIEW "PHASE_TWO"."EDIPRODUCT" ("ID", "STK_NUM", "TITLE", "ISBN", "UPC", "ITEMNO", "LONGFORMAT", "ABRIDGED", "WEB_TITLES_ID", "OCLC", "GENRE", "RELYEAR", "ORIG_REL", "LANG", "ORIG_STKNUM", "PUBLISHER", "PEOPLELIST", "SALES_ORG", "NOT_AVAIL") AS
SELECT sap_product.id,
sap_product.stk_num,
sap_product.longdesc AS title,
sap_product.isbn,
sap_product.upc,
sap_product.itemno,
sap_product.longformat,
sap_product.abridged,
mwt_product.web_titles_id,
mwt_product.oclc,
mwt_product.genre,
mwt_product.RELYEAR,
sap_product.orig_rel,
sap_product.lang,
sap_product.orig_stknum,
UPPER (publisher.name) publisher,
(SELECT LISTAGG (p.FULLNAME, ', ') WITHIN GROUP (
ORDER BY pp.rank) AS People
FROM people p
JOIN product_people pp
ON p.id = pp.peopleid
WHERE pp.stk_num = sap_product.stk_num
GROUP BY pp.STK_NUM
) PeopleList,
sppg.PRICING_TYPE as sales_org,
sap_product.not_avail
FROM sap_product
JOIN mwt_product ON sap_product.stk_num = mwt_product.stk_num
JOIN publisher ON mwt_product.publisherid = publisher.id
JOIN SAP_PRODUCT_PRICING_GROUP sppg on sppg.STK_NUM = mwt_product.stk_num and sppg.MARKED_FOR_DELETION = 0
WHERE mwt_product.WEB_PRODUCTS_ID > 0;
This view works as expected in SQL Developer. My getEDIPRODUCT function (yes, it's VB.NET) in my controller is as follows:
' GET: odata/EDIPRODUCTs
<EnableQuery>
Function GetEDIPRODUCT() As IQueryable(Of EDIPRODUCT)
Dim results As IQueryable
results = db.EDIPRODUCT
For Each _product In results
Console.Write(_product)
Next
Return results
End Function
I just added the for loop in order to inspect the results. What I see when I inspect the results is the same product record is returned for each row. The value for the ID is duplicate and the only other field that should have variant values (sppg.PRICING_TYPE as sales_org) also just repeats.
I have other views where this does not occur. The correct number of records are always returned, but the first record retrieved is always just repeated in each row of the result set. Any idea what could be going on here?
I never actually resolved this issue and am still interested in why this fails, but I rewrote the portion of the app that uses this view to use OData's $expand to retrieve the related data.

MS Access IIF Field Exists

I am creating a report from a query where a field ABC is displayed as CAT if yes and as MOUSE if no. But unfortunately, when there are instances where the table inside the query does not contain field ABC, the report generates a error pop-up. Is there any way to by-pass this and run the report with other fields excluding the missing field?
I heard that IIF Exist function could help, but I am really blank here. I wrote the access query like below:
Iif (fieldExists(iif([ABC]=5, 'CAT', 'MOUSE'),iif([ABC]=5, 'CAT', 'MOUSE'), '')) AS TOMnJERRY
This function is maybe the shortest one to test if a field exists in Access:
Function FieldExists(ByVal Table As String, ByVal Field As String) As Boolean
On Error Resume Next
FieldExists = (DCount(Field, Table) >= 0)
End Function
How it works:
If the field exists, the expression (DCount(Field, Table) >= 0) is obviously always true, because DCount never returns a negative value. If the field doesn't exist, an error will occur and the program will jump to the next line without setting the return variable FieldExist, so this one will keep at default and this is false.
So the solution for your problem should look like this:
Iif (FieldExists('YourTable','ABC'), iif([ABC]=5, 'CAT', 'MOUSE'), '')

Drupal - How to use Computed field in field collection item fields

I have custom content type type_a
Inside that custom type I have few titles
Title
Body
Group1 (field_group1) (Field Collection Item Can have multiple values)
Following are the details of fields inside Group1 (Field Collection Item)
Group Item 1 (field_item1) Can have one value
Group Item 2 (field_item2) (Computed field) Can have one value
I would like to copy value of "Group Item 1" field of same field collection item inside "Group Item 2"
Below is I am using in computed code:
$entity_field[0]['value'] = $entity->field_item1[LANGUAGE_NONE][0]['value'];
But it is not working. I am getting an error
Notice: Undefined index: value in eval() (line 1 of /homepages/13/d160804/htdocs/test/sites/all/modules/computed_field/computed_field.module(466) : eval()'d code).
Please help how to do this. Thanks
$entity->field_item1[LANGUAGE_NONE][0]['value'] contains the entity id of the field collection item (which contains the fields you want to copy).
You either need to load both field collection items and set their individual fields. Eg,
$source_fc = field_collection_item_load($entity->field_item1[LANGUAGE_NONE][0]['value']);
$dest_fc = field_collection_item_load($entity->field_item2[LANGUAGE_NONE][0]['value']);
// now set values of $dest_fc with values from $source_fc
Or do some sort of clone similar to this method: http://drupal.org/node/1233256#comment-5167316

Drupal CCK field select option names, where are they?

I have a custom content type which has a field called "location" which is a long select list (100 or so items). I want to get an array of all locations which have a piece of content associated with them. The numeric value of this field is stored in content_type_[my_content_type], but I can't find anywhere in the database where the name of the values are stored. I hope that's not too confusing - just to be clear, I want to do something like this:
SELECT DISTINCT(field_location_value) FROM content_type_[my_content_type]
and then
SELECT field_location_name_or_something FROM where_on_earth_are_the_names_stored
and then do something with the two arrays to find the names I want.
Can anyone help?
Thanks a lot. Drupal 6, by the way.
If you mean select list field of CCK:
Get all location associated with current node (piece of content?):
$node = node_load('YOUR content ID');
print_r($node->field_location); // $node->field_location - this will array of values.
Get all values of that field (defined in "Allowed values"):
$content_field = content_fields('field_location');
$allowed_values = content_allowed_values($content_field); // array of values
I found this, after much trial and tribulation, in the database table content_node_field_instance, under the field's widget settings field.
This Drupal 6 code snipped will retrieve your cck field value options and put them in the $allowed_values variable as an array. Replace 'YOUR_CCK_FIELD_NAME' with your cck field name (name, not label)
$cck_field_name = 'YOUR_CCK_FIELD_NAME';
$cck_field = db_fetch_object(db_query("SELECT global_settings FROM {content_node_field} WHERE field_name = '%s'", $cck_field_name));
$cck_field_global_settings = unserialize($cck_field->global_settings);
$allowed_values = explode("\r\n", trim($cck_field_global_settings['allowed_values'], "\r\n"));
In Drupal 7 if you have a field of type List (text) then I have written the following function to return an array of options:
function get_drupal_select_options($field_name) {
$options = unserialize(db_query("SELECT c.data
FROM field_config_instance i
INNER JOIN field_config c ON c.id = i.field_id
WHERE i.field_name = :fieldname", array(':fieldname'=>$field_name))->fetchField());
return $options['settings']['allowed_values'];
}

Resources