Workforce Script, how to get workers name

263
0
01-26-2023 01:10 PM
KhadijaSafi
New Contributor

I'm updating a python script for my organization that sends emails when an assignment is created in Workforce. I need to have the worker's name (First and Last) and details from the feature layer be part of the email. So far this is my script. The emails send but the worker name shows up as "None". I also don't know how to access information from the feature layer. I want information like "remarks and recommendations" in the email. Any help would be great.

id_to_user_lookup = {}
users_to_email_created = {}
users_to_email_due_in_week = {}
users_to_email_due_in_day = {}
script_start_datetime = datetime.now()
cutoff_time_minutes = 16 # If you change the frequency of how often the script is run then Replace! This should be set according to the 
                         # frequency at which this script runs. 31 minutes is meant for a script that runs every 30 minutes via the 
                         # Notebook Task scheduler.
myself = gis.users.me

for folder in myself.folders:
    # For all the user's folders
    for item in myself.items(folder, max_items=999999999):
        if item.type == "Feature Service":
            flc = FeatureLayerCollection.fromitem(item)

            # Loop over the layers to find the Workforce Assignments data layer
            for layer in flc.layers:
                layer_name = layer.properties["name"]
                ##
                ## Maybe Replace! "Assignments", say if you only want to look for a certain layer e.g. "Assignments1" 
                ## instead of all layers named "Assignments".
                ##
                if layer_name == "Assignments":
                    workerids_to_lookup = []
                    dispatcherids_to_lookup = []
                    
                    for feature in layer.query():

                        # If this feature was assigned less than variable cutoff_time_minutes ago,
                        # then add user to users_to_email_created.
                        assigneddate = feature.get_value("assigneddate")
                        
                        # Use CreationDate in case assigneddate isn't set, which seems to happen sometimes. 
                        if assigneddate is None:
                            assigneddate = feature.get_value("CreationDate")

                        assigneddate = datetime.fromtimestamp(assigneddate / 1e3 )
                        
                        recently_created = compare_time_difference( script_start_datetime,
                                                                    assigneddate, 
                                                                    cutoff_time_minutes )
                        
                        if recently_created:
                            prid = feature.get_value("priority")
                            dued = feature.get_value("duedate")
                            worker = feature.get_value("worker")
                            wid = feature.get_value("workerid")
                            did = feature.get_value("dispatcherid")
                            a_tuple = [(did,
                                       assigneddate,
                                       feature.get_value("location"),
                                       feature.get_value("description"),
                                       feature.get_value("priority"),
                                       feature.get_value("duedate"),
                                       feature.get_value("worker"))]
                            if wid in users_to_email_created:
                                users_to_email_created[wid] += a_tuple
                            else:                                       
                                users_to_email_created[wid] = a_tuple
                            workerids_to_lookup.append(wid)
                            dispatcherids_to_lookup.append(did)
                        
                        # If the Assignment is due in a day, then add user to users_to_email_due_in_day, 
                        # else check if it is due in a week, and then add user to users_to_email_due_in_week.
                        
                        duedate = feature.get_value("duedate")
                        
                        # duedate is not a mandatory field, so if it isn't set then we skip this Assignment. 
                        
                        if duedate is None:
                            continue 
                        
                        duedate = datetime.fromtimestamp(duedate / 1e3)
                        
                        if recently_created:
                            if compare_time_difference( duedate,
                                                        script_start_datetime,
                                                        1440 ):
                                a_tuple = [(did,
                                          duedate,
                                          feature.get_value("location"),                
                                          feature.get_value("description"),
                                          feature.get_value("priority"),
                                          feature.get_value("duedate"),
                                          feature.get_value("worker"))]
                                if wid in users_to_email_due_in_day:
                                    users_to_email_due_in_day[wid] += a_tuple
                                else:
                                    users_to_email_due_in_day[wid] = a_tuple
                            elif compare_time_difference( duedate,
                                                          script_start_datetime, 
                                                          10080 ):
                                a_tuple = [(did,
                                          duedate,
                                          feature.get_value("location"),
                                          feature.get_value("description"),
                                          feature.get_value("priority"),
                                          feature.get_value("duedate"),
                                          feature.get_value("worker"))]
                                if wid in users_to_email_due_in_week:
                                    users_to_email_due_in_week[wid] += a_tuple
                                else:
                                    users_to_email_due_in_week[wid] = a_tuple
                                    
                        else:
                            one_day_from_now = script_start_datetime + timedelta(days=1)
                            one_week_from_now = script_start_datetime + timedelta(days=7)
                            
                            if compare_time_difference( one_day_from_now, 
                                                        duedate,
                                                        cutoff_time_minutes ):
                                wid = feature.get_value("workerid")
                                did = feature.get_value("dispatcherid")
                                a_tuple = [(did,
                                          duedate,
                                          feature.get_value("location"),
                                          feature.get_value("description"),
                                          feature.get_value("priority"),
                                          feature.get_value("duedate"),
                                          feature.get_value("worker"))]
                                if wid in users_to_email_due_in_day:
                                    users_to_email_due_in_day[wid] += a_tuple
                                else:
                                    users_to_email_due_in_day[wid] = a_tuple
                                workerids_to_lookup.append(wid)
                                dispatcherids_to_lookup.append(did)
                            
                            elif compare_time_difference( one_week_from_now, 
                                                        duedate,
                                                        cutoff_time_minutes ):
                                wid = feature.get_value("workerid")
                                did = feature.get_value("dispatcherid")
                                a_tuple = [(did,
                                          duedate,
                                          feature.get_value("location"),
                                          feature.get_value("description"),
                                          feature.get_value("priority"),
                                          feature.get_value("duedate"),
                                          feature.get_value("worker"))]
                                if wid in users_to_email_due_in_week:
                                    users_to_email_due_in_week[wid] += a_tuple
                                else:
                                    users_to_email_due_in_week[wid] = a_tuple
                                workerids_to_lookup.append(wid)
                                dispatcherids_to_lookup.append(did)
    
            
            # Loop over the layers again to match found workerid values to users using the corresponding record's attributes
            for layer in flc.layers:
                layer_name = layer.properties["name"]
                if layer_name == "Workers":
                    for wid in workerids_to_lookup:
                        if wid in id_to_user_lookup:
                            continue 
                        wid_record = layer.query(GlobalID = wid).features[0]
                        id_to_user_lookup[wid] = wid_record.get_value("userid")
            
            # Loop over tables to match found dispatcherid values to users using the corresponding record's attributes
            for table in flc.tables:
                if table.properties["name"] == "Dispatchers":
                    for did in dispatcherids_to_lookup:
                        if did in id_to_user_lookup:
                            continue
                            
                        did_record = table.query(GlobalID = did).features[0]
                        id_to_user_lookup[did] = did_record.get_value("userid")

for wid_lookup in id_to_user_lookup.keys():
    for a_dict in [users_to_email_created,
                 users_to_email_due_in_week,
                 users_to_email_due_in_day]:
        if wid_lookup in a_dict:
            a_dict[id_to_user_lookup[wid_lookup]] = a_dict[wid_lookup]
            del a_dict[wid_lookup]

for user in users_to_email_due_in_day:
    for search_user in gis.users.search():
        if search_user.username == user:
            worker_username = search_user.username
            worker_user = gis.users.get(worker_username)
            worker_user_email = worker_user.email

            for a_tuple in users_to_email_due_in_day[user]:

                description = a_tuple[3] 
                priority = a_tuple[4]
                worker = a_tuple[6]
                dispatcher_user_id = a_tuple[0]
                dispatcher_user_email = None
                if dispatcher_user_id in id_to_user_lookup:
                    dispatcher_user = gis.users.get(id_to_user_lookup[dispatcher_user_id])
                    dispatcher_user_email = dispatcher_user.email
                
                
                loc_str = " ".join(["Location:", a_tuple[2]])
                desc_str = " ".join(["Description:", description])
                pri_str = " ".join(["Priority:",str(priority)])
                due_str = " ".join(["Assignment Due Date:",str(duedate)])
                work_str = " ".join(["Hi", str(worker)])
                
                #hi_worker = " ".join(["Hi", worker_username])
                email_str = "\r".join(["\r", work_str, "This is a reminder that you have a work assignment due tomorrow.","Please see details about the work assignment below:", loc_str,
                                                                 desc_str, due_str, pri_str, " ", "If you have any questions about the assignment, please contact the project dispatcher or Gitga’at GIS Administrator."])
                send_email_smtp([worker_user_email, dispatcher_user_email], "RE: Gitga’at Asset Management: You have an Assignment due Tomorrow!", email_str)
    

 

This is a snip from the layer that I want to pull information from for the email.

KhadijaSafi_0-1674767400395.png

 

0 Kudos
0 Replies