Getting my current time from Aware UTC and updating the original row with it

What I’m trying to do:
I have 4 different buttons and I am using to clock in.

button1 = clock in
button2 = clock out
so on…

I used app_tables.clockinoutsystem.add_row(Clock_In_Start=Start_time_LA_time) for the 1st button, but when I use this same line of code (add_row), for the second button it adds a new row instead of updating the original row. I am sure there is a amend code right to search and find the row you were updating.

How do I update the same row?

Clone link:
https://anvil.works/build#clone:7PQA74TNYLACI2ZV=VEFIH2U4CGCD2Z5L4A7JB62V

Have you been through the news aggregator tutorial? Anvil | Build Database-Backed Apps

It covers the foundational aspects of working with data tables, including updating rows.

Alternatively, the docs on using data tables from Python cover updating rows, too: Anvil Docs | Using Data Tables from Python

1 Like

I read it, but obviously making a newbie mistake somewhere. Currently trying to update the row I added in def clock_in_button_click.

I keep getting this error and I cannot figure out what I am doing wrong. If I were to guess, def clock_out_button_click probably cannot reference the original row for some reason.

TypeError: ‘NoneType’ does not support item assignment at [Form1, line 74](javascript:void(0))

I am self-taught, but clearly, some of the basics eluded me.

def clock_in_button_click(self, **event_args):
current_clock_in_time = DT.datetime.now()
Start_time_UTC_current = DT.datetime.now(anvil.tz.tzutc())
Start_time_UTC_current = Start_time_UTC_current.strftime("%m-%d-%Y %H:%M")
Start_time_UTC_current2 = DT.datetime.strptime(Start_time_UTC_current,"%m-%d-%Y %H:%M")
Start_time_LA_time = Start_time_UTC_current2 - timedelta(hours=7)
app_tables.clockinoutsystem.add_row(Users=anvil.users.get_user()[‘email’],Clock_In_Start=Start_time_LA_time)

def clock_out_button_click(self, **event_args):
current_clock_in_time = DT.datetime.now()
Out_For_Lunch_UTC_current = DT.datetime.now(anvil.tz.tzutc())
Out_For_Lunch_UTC_current = Out_For_Lunch_UTC_current.strftime("%m-%d-%Y %H:%M")
Out_For_Lunch_UTC_current2 = DT.datetime.strptime(Out_For_Lunch_UTC_current,"%m-%d-%Y %H:%M")
Out_For_Lunch_LA_time = Out_For_Lunch_UTC_current2 - timedelta(hours=7)
x = None
clock_out_lunch = app_tables.clockinoutsystem.get(Clock_Out_Lunch=x)
clock_out_lunch[‘Clock_Out_Lunch’] = Out_For_Lunch_LA_time

It helps the readability of code if you post it between pairs of three backticks, e.g. ``` That’ll keep the formatting of the code intact.

I can’t really comment on the error message, because I don’t know which line is line 74.

In general, though, if you want to create a row in one function and update that row in another function, you should keep a reference to it around in a self. variable.

1 Like

So here is where I am stuck at…
I am trying to access the same row in **def clock_in_button_click function(self, event_args) so I can I update that row with the new clock out in def **clock_out_button_click(self, event_args) I made a few changes since we last messaged. The most updated version is the one below.

Currently getting this error

anvil.tables.TableError: More than one row matched this query

So my question is how do I get the same row in one function so I can update it in another function? I need clock in start shift, clock out, clock in, clock out for the day These are DateTime columns in the data table.

  def clock_in_button_click(self, **event_args):
    current_clock_in_time = DT.datetime.now()
    Start_time_UTC_current = DT.datetime.now(anvil.tz.tzutc())
    Start_time_UTC_current = Start_time_UTC_current.strftime("%m-%d-%Y %H:%M")
    Start_time_UTC_current2 = DT.datetime.strptime(Start_time_UTC_current,"%m-%d-%Y %H:%M")
    Start_time_LA_time = Start_time_UTC_current2 - timedelta(hours=7)
    Clocked_In = app_tables.clockinoutsystem.add_row(Users=anvil.users.get_user()['email'],Clock_In_Start=Start_time_LA_time)
    return Start_time_LA_time
   
    
  def clock_out_button_click(self, **event_args):
    current_clock_in_time = DT.datetime.now()
    Out_For_Lunch_UTC_current = DT.datetime.now(anvil.tz.tzutc())
    Out_For_Lunch_UTC_current = Out_For_Lunch_UTC_current.strftime("%m-%d-%Y %H:%M")
    Out_For_Lunch_UTC_current2 = DT.datetime.strptime(Out_For_Lunch_UTC_current,"%m-%d-%Y %H:%M")
    Out_For_Lunch_LA_time = Out_For_Lunch_UTC_current2 - timedelta(hours=7)
    clock_in = app_tables.clockinoutsystem.get(Clock_In_Start=self.clock_in_button_click())
    clock_in.update(Clock_Out_Lunch=Out_For_Lunch_LA_time)
    return Out_For_Lunch_LA_time

That’s the answer to your question. If that doesn’t make sense, you may want to go through the news aggregator tutorial again, since it’ll cover that and other aspects of building an Anvil app that works with data tables.

The larger context of the app is also missing, so it may be that what you’re trying to do isn’t the right way to go about it. Is this supposed to be a multi-user app? A single-user app? Is it used from a single PC that multiple users use, or do multiple users use their own PCs?

More context will help folks give you better general advice, but there’s no substitute for having the technical foundation the tutorials provide.

You may also want to edit the title of your post, since it was never really about getting the current time, it was about updating the original row. That’ll help people who later search the forum looking for current time related posts.

Clocked_In is a local variable, so it (and its value, a reference to the added database row) will be discarded as soon as the function ends. Did you want to retain this value for later use?

1 Like

I figured it out. Thank you for replying