AnsweredAssumed Answered

Python: automate field based on editor and edit date

Question asked by dav22b on Jul 14, 2014
Latest reply on Jul 15, 2014 by dav22b

Hello all,

 

I am writing a script to automate a GIS data editing process:

 

I want two fields to be updated ("EDITOR" and "EDIT_DATE") every time a user edits a feature class within a geodatabase. I want the "EDITOR" field to be populated with the name of the user that is doing the editing and I want the "EDIT_DATE" field to be updated with date ("7/14/14") in which the editing was performed. Also, if the user has not created these fields ("EDITOR" and "EDIT_DATE"), I want the script to add the fields and update them accordingly.

 

Here is what I have so far (The script creates the "EDITOR field" but does not populate it with the username; The "EDIT_DATE" does the same but the date is wrong - it populates the field with rubbish):

 

I am not quite sure if I am on the right track here or not

 

# Developed by: David Bailey
# Contact: davidjonathanbailey@gmail.com
# Description: This tool updates the EDITOR and EDIT_DATE fields for all feature classes within a geodatabase
# based on the time the user makes the edits


# import arcpy site-package and os module
import arcpy, os, getpass, datetime


# Set geoprocessing environment:
# a) set the workspace environment 
aWS = r"R:\Projects\1258_CLTP\Data\Aspen\Other_geodatabases\AV_Clearview.gdb"
arcpy.env.workspace = aWS


# Set geoprocessing environment:
# b) the overwriteOutput parameter controls whether tools will
#    automatically overwrite any existing output 
arcpy.env.overwriteOutput = True


try:
    # Set Variables
    fcList = arcpy.ListFeatureClasses()
    Name_Expression = getpass.getuser()
    Date_Expression = datetime.datetime.date()
    #dropFields = ("EDIT_DATE", "EDITOR")


    # Iterate through feature classes - update EDITOR field
    for aFC in fcList:
        fieldList = arcpy.ListFields(aFC)
        for field in fieldList:   
            if field.name == "EDITOR":
                arcpy.CalculateField_management(aFC, "EDITOR", Name_Expression, "VB", "")
            else:
                arcpy.AddField_management(aFC, "EDITOR", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
                arcpy.CalculateField_management(aFC, "EDITOR", Name_Expression, "VB", "")
    
    # Iterate through feature classes - update EDIT_DATE field
    #for aFC in fcList:
        #fieldList = arcpy.ListFields(aFC)
        #for field in fieldList:   
            #if field.name == "EDIT_DATE":
               # arcpy.CalculateField_management(aFC, "EDIT_DATE", Date_Expression, "VB", "")
           # else:
             #   arcpy.AddField_management(aFC, "EDIT_DATE", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
               # arcpy.CalculateField_management(aFC, "EDIT_DATE", Date_Expression, "VB", "")
            
    # Delete Fields - this is only if you are working and editing the script - optional
    #for aFC in fcList:
        #arcpy.DeleteField_management(aFC, dropFields)


except:
    
    # Code to run when an error occured
    print "An ERROR Occured!"
    print "\n" + arcpy.GetMessages()


else:
    
    # Message when there was no error
    print "\nNo Error occurred"
    
# Script end message
print "\nEnd of the script!"  

 

I'd really appreciate some help - thanks!

Outcomes