is there any way to shorten this?
im trying to make scheduler for my job letting them know what days they are working by using the app
i am rebuilding what i had so instead of updateing it day by day it can get updated week by week
p.s. i know a little about python coding but not alot
This is untested pseudo-ish code, but for the first block you can store the result of the search then add it to each of the (what I assume to be) drop downs. Assuming each of the components E1 to E14 are inside a container (eg column panel) which I will name “my_dropdowns_container” for convenience :
# Just do one data tables search.
values = [(r['name'],r) for r in app_tables.employees.search()]
# Find each drop down component inside a container
# and add the search data to it.
for c in self.my_dropdowns_container.get_components():
if type(c) is DropDown:
c.items = values
Something like that?
Expanding on what @david.wylie said, if your dropdowns were not in a container and you don’t want them to be, you can define a variable in the init to list them:
# Define this in your __init__ listing the components (not the .text properties)
self._drop_downs = [self.E1, self.E2, ... ]
After this you can do what @david.wylie said changing self.my_dropdowns_container.get_components()
for self._drop_downs
.
And then, in your button_3_click you can then run:
def button_3_click(self, **event_args):
app_tables.scheduling.delete_all_rows()
for dropdown in self._drop_downs:
app_tables.scheduling.add_row(RouteNum=dropdown.text, Employee=dropdown.selected_value)
alert(title="Submitted!")
pass
But just to add another thing:
You definitely don’t want to do all those searches to get the same thing. Everytime you do that is a new request and search to get, not just everything from your table, but also iterate and transform it into a list comprehension. You want to do this just once.
basically i have a bunch of drop downs that all need the same options then when the Submit button is clicked it will apply it to the data table that which then will be pulled from the table to a page the employees can see their schedule
Scheduler(seen only by managers > Data table > non-editable employee schedule page
seen only by management
seen by empoyees
If you do that, you’re querying the same database over and over. You could query it once to a variable and just use that for all of the dropdowns. Save the overhead.
self.repeating_panel_1.items=app_tables.feedback_request.search()
employees = [(r['Name'],r) for r in app_tables.employees.search()]
self.E1.items = employees
self.E2.items = employees
# or taking advice from above, you can try something like this,
dropdowns = ['E1', 'E2', 'E3', 'E4']
for dropdown_key in dropdowns:
dropdown = getattr(self, dropdown_key)
dropdown.items = employees