Django: Find element in list by value - django

I have a sorted list of objects that I want my users to select a range from. For this I added two select elements (called from and to)to my web page. from is filled directly from the list, like this:
<select id="from">
{% for el in el_list %}<option>{{el.number}}</option>{% endfor %}
</select>
Once a value has been selected in from I'd like to populate to with the values from the selected value until the last one.
The list is sorted by the number attribute on python's side so I guess all I need to do is find the selected element in the list, slice the list accordingly and use the rest to populate my second select.
I've searched the documentation and several snippets but so far I've only found filters giving me the last or the first element of a list. I also know I can check if the selected value is in my list using in, but how can I then get that object's index?
I know I could probably run along the whole list with a for ... if, but I am almost sure there's a more elegant method out there, that I simply haven't found yet.
If any of you could provide me with a few pointers on how to solve this more elegantly it'd be much appreciated.

I don't know why you're looking through template tags for this. They won't help: by the time the user sees the page to select something from the from select, the template has already been rendered. You need to use Javascript.

Related

How to delete duplicate constructed objects in a list while preserving order and returning a List in dart?

I have a list of constructed objects called RecentCard which is basically an object with user id, pic, and name. I have created a list arranged in order of most recent interaction based on timestamp. However i need to get rid of the second occurence and onwards of any duplicated object. I am comparing just the ids since users may have the same name or photo, and i cannot remove duplicates from a simple list of uids because then i could lose the order of corresponding photos and names.
For example the list would look something like this :
List<RecentCard> recentCards= [RecentCard(uid:as721dn18j2,name:mike,photourl:https://sadadasd1d1),RecentCard(.....]
I have searched for solutions but all of them are dealing with like primitive types like simple lists of strings and the solutions didn't work for me. For example this post: How to delete duplicates in a dart List? list.distinct()? Read below
The first answer's link is no longer available, the next answer with sets is something i tried but it simply doesnt work i have no idea why maybe because its not a primitive type. The next answer with the queries package didn't work because i was using the list to build a listview.builder and so when i called list.length it said i couldnt call that on an iterable or something. The final solution wouldn't work because it said String is not a subtype of RecentCard.
I tried using two forloops with the second just comparing first value to the next couple but that doesnt work because if a duplicate is found, the object is removed and the length is messed up so some elements get skipped.
Any ideas? I feel like its very simple but im not sure how to do it.
You have to use Set to store seen cards and then filter away those ones that are already in the set.
final seenCards = Set<String>();
final uniqueCards = recentCards.where((card) => seenCards.add(card.uid)).toList();

How to put formula in Data Validation List Excel?

I am creating an excel sheet with following Data Validation drop down list.
NA
Done
(add some formula here)
Basically, i will be able to select either plain text "NA"/ "Done" from the dropdown list. But sometimes, I want the user to be able to calculate some values based on the cell respective to the row selected so, I want to have one formula as a choice inside the data validation dropdown list. Is this possible?
Data Validation List Source
When I click on Formulae option, it should execute the formula with respect to the cells in that Row
But currently, the formula that i put in doesn't execute, instead it will just show the whole formula in the cell when activated.
1)How can i make it so that when i select the formula from data validation list, it will execute it instead of filling up the cell with it?
2)How do i set the formula so that it will be using the cell from the current Row? (for example, if i am using the data validation List in N60, the formula should adapt itself to use the cell (let's say A60?).
I may not be able to help with the second part, but I was seeking an answer to the first and discovered a solution/workaround using Name Manager.
First, in Formula > Name Manager, create a new reference (the "refers to" will contain whatever formula you are wishing to ultimately display in the validation list. For this example, we use the formula reference "=IF($H54=..." and Name it "UniqueName"
Now, we go into Data Validation, Select List, and input the three items we want displayed in the list, with an equals sign preceding our newly named reference: ie. "NA,Done,=UniqueName"
Note: You can't start with the =UniqueName or validation will try to read it all as a formula and fail.
This method will allow the user to display "NA", "Done", or "=UniqueName" in the cell; if "=UniqueName" is selected, the cell itself will interpret this as a formula and execute it accordingly, displaying the results of "=IF($H54=...", or whateverelse you have designated to use as a named formula.
If it's too late for yours, I hope this helps someone else who may face a similar problem.
While I think I know what you're trying to say. Why don't you just use an IF formula to evaluate everything instead of selecting a drop down for every row manually. You already had it partially solved using IF. Just need to add the criteria for a "Done" and an "NA"
=if(A1="date","Done",if(A1<"date","NA",if(something else until you have all your catergories))
Just going to piggyback off of Mark's response.
If you really needed your named formula to be the first selection in the list, you can setup your list with a leading comma like so:
,=UniqueName,NA,Done
That worked out for my use, and there was no null item listed in the Data Validation drop down. Hope that helps!

Does the order of items in a GET or POST dictionary ever change? django

I was just wondering if the order of items in a GET or POST dictionary ever changes?
if you have a list of 3 people, and you say :
template:
for x in listof3people:
<input type="hidden" name="x.name">
<input type="number" name="birthday">
in views when you do a getlist(name), the order of the information never changes correct?
so if the query pulled up mark , mindy, and sam in that order, the get list would show always show:
mark and his birthday , mindy and her birthday, and sam and his birthday in that order. The reason I'm askiong is because I'm going to have to link information based on position, so find the position of "bar" and pull the data in the nest list that is in the same position.
Behind the scenes, the QueryDict class used for GET and POST is built from the output of six.moves.urllib.parse.parse_qsl. This is based on the standard urllib.parse_qsl function, which you can see from the source code does maintain the order given in the URL:
https://github.com/python/cpython/blob/a54346b3a1232cdd503abc4d4e9e526ba65b26b3/Lib/urllib/parse.py
Such inputs are supposed to be put into the URL/encoded form data in the same order they appear in the document:
Do browsers preserve order of inputs with same name on GET/POST?
Note that I am talking only about multiple values for the same key, fetched using getlist. QueryDict does not guarantee the order of dict keys any more than any other dict-derived class.
So it's possible, but personally I'd pass around an unambiguous identifier rather than rely on it.
I think you are talking about a QueryDict. This is a customized Python dictionary. Normally there is no guarantee that the keys appear always in the order they are pushed in. So for your purpose you might use some extra weight values in your form so you can order the incoming data according to these values.

polymer repeat template "refresh"

Need help with a concept. Building a custom element that displays data from my database. Using a repeat template and it working great. The report is grouped and so each row in the report contains another level of data that is displayed once you drill into a row. So now I want to be able to "refresh" the report data to reflect sort or grouping changes. The report is based off an "indexing" array that points to the actual data displayed. I sort/group the data in the index array, and use this array as the array for the Template Repeat.
<div id="rptbody">
<template repeat="{{group in groups}}">
<pmg-group groupid={{group.id}} groupval={{group.groupval}} groupobj={{group.groupobj}}></pmg-group>
</template>
</div>
So in this example, the groups array is a list of keys to my data. In this example, the groups array is ordered properly so that [0] contains the first index to the data array, and [length-1] contains the last. I have a sort routine that changes the index value in groups, but it does not add or remove items from the groups array.
So my question is after my resort, how do I trigger the template to rescan the groups list? I have found that I can set groups = [], and then reload groups ( groups.push({idx} ) from my master index, and it will update the template, however that seems kind of wasteful to me.
Is there any way to trigger the template to refresh, or rescan the groups array and reorganize the DOM based on the changes in the values?
In the past I've gotten template repeats to refresh like this:
var template = document.querySelector('template'); // Use specific selector here
template.iterator_.updateIteratedValue(); // Force Polymer to refresh template
It's a hack, but it looks like it still works. Here's a jsbin.
Just stumbled on this and found out we have to use the .set method of the Polymer element data model. Not directly found any docs about this but this seems interesting https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#set-path. Would avoid those hacky things because they could change API over the time.

Writing value on the current textbox - Selenium

Well I'm facing an issue here:
I use Selenium to automate test on my application, and I use the following code to fill a textbox:
driver.find_element_by_id("form_widget_serial_number").send_keys("example", u'\u0009')
All the textboxes have the same id (form_widget_serial_number) so it's impossible to select a specific textbox by using selection by id (the code above selects the first textbox and u'\u0009' changes the cursor position to the next textbox).
Anyway, how can I send values to the "current" textbox without using find_element_by_id or another selector, given that the cursor is in the current textbox?
There are several selectors available to you - Xpath and css in particular are very powerful.
Xpath can even select by the text of an element and then access its second sibling1:
elem = driver.find_element(:xpath, "//div[contains(text(), 'match this text')]/following-sibling::*[2]")
If you need to find the current element that has focus:
focused_elem = driver.switch_to.active_element
And then to find the next one:
elem_sibling = focused_elem.find_element(:xpath, "following-sibling::*")
Using this example, you could easily traverse any sequence of sequential elements, which would appear to solve your problem.
Take a close look at the structure of the page and then see how you would navigate to the element you need from a reference you can get to. There is probably an Xpath or css selector that can make the traversal across the document.
1 FYI, I'm using an old version of Selenium in Ruby, in case your version's syntax is different
My approach would be:
Get a list of all contemplable elements by the known id
Loop over them until you find a focused element
Use send_keys to write into that element
To get the focused element something like
get_element_index('dom=document.activeElement')
might work. See this question for more information.
Edit: If the list you get from 1. is accidently sorted properly the index of an element in this list might even represent the number of u'\u0009' you have to send to reach that specific element.
Use ActionChains utility to do that. Use following code:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).send_keys("Hello").perform()