import arcgis from arcgis.gis import GIS from arcgis.apps import workforce #from arcgis.geometry import Geometry from arcgis.geocoding import reverse_geocode import requests, base64, json from datetime import datetime, timedelta, date workdefaults = { #Dictionary of job type parameters...[0] Assigned Worker [1] Priority [2] Days Until Due, could add more defaults in the future 'Crosswalk Maintenance' : ['BDPW_Mobile5',0,60], 'Culvert Repair/Maintenance' : ['BDPW_Mobile4',0,14], 'Catchbasin/Manhole Repair/Maintenance' : ['BDPW_Mobile4',0,14], 'Sign Maintenance' : ['BDPW_Mobile2',0,5], 'Sign Installation/Removal' : ['BDPW_Mobile2',0,5], 'Road Repair/Maintenance' : ['BDPW_Mobile3',0,30], 'Streetlight Maintenance' : ['BDPW_Mobile3',0,5], 'Special or Emergency Sign Placement' : ['BDPW_Mobile2',0,2], 'Damage Assessment' : ['No Default Worker',0,2], 'Storm Drain Maintenance' : ['BDPW_Mobile4',0,14], 'Snow Removal Operation' : ['No Default Worker',0,2], 'Debris Cleanup' : ['No Default Worker',0,5], 'Tree/Brush Removal' : ['No Default Worker',0,5], 'Pavement Marking' : ['BDPW_Mobile5',0,60], 'Stormwater Outfall/Drainageway Maintenance' : ['BDPW_Mobile4',0,14], 'Washout' : ['BDPW_Mobile3',0,30], 'Flood Control Measures' : ['BDPW_Mobile4',0,2], 'Other (See Description' : ['No Default Worker',0,30], 'Sidewalk/Traffic Island Maintenance' : ['BDPW_Mobile3',0,30], 'Special Holiday or Parks and Rec Assignment' : ['No Default Worker',0,5], 'Building/Facility Maintenance' : ['No Default Worker',0,14], 'Pothole Repair' : ['BDPW_Mobile3',0,5], 'Digsafe' : ['BDPW_Mobile2',0,2] } gis = arcgis.gis.GIS("home") token = gis._con.token item = gis.content.get("d4ce1eb9f3a544009ed1116a0ed8466d") #get item to pointer to Workforce dataset project = arcgis.apps.workforce.Project(item) #set the WorkForce project dpw_wc_pts = gis.content.search("DPWWorkCapture_Test")[1] #Get item pointer to QuickCapture project "workcapture" points flayer = dpw_wc_pts.layers[0] #make feature layer from it fset = flayer.query("Converted = 0 and WorkType <> 'Map Correction'") #WorkCapture features that aren't already converted or workcaptures that are only "map correction" notes for feature in fset: #iterate through WorkCapture points WorkType = feature.attributes['WorkType'] #WorkCature point types possess the same descriptor strings as assignment assignment-types fOID = feature.attributes['OBJECTID'] comments = feature.attributes['Comments'] address = (reverse_geocode(feature.geometry))['address']['Address'] #reverse-geocode WorkCapture point address to move to assignment #Get job type defaults today = datetime.today() if WorkType in workdefaults: if workdefaults[WorkType][0] == 'No Default Worker': assignworker = None jobstatus = 0 jobpriority = 0 date_due = today + timedelta(days=workdefaults[WorkType][2]) else: assignworker = project.workers.get(user_id=workdefaults[WorkType][0]) jobstatus = 1 jobpriority = workdefaults[WorkType][1] date_due = today + timedelta(days=workdefaults[WorkType][2]) else: assignworker = None jobstatus = 0 jobpriority = 0 date_due = today + timedelta(days=30) #Make the assignment assignment = project.assignments.add(assignment_type=WorkType, description='Converted from WorkCapture', due_date=date_due, location=address,geometry=feature.geometry, status=jobstatus, priority=jobpriority, worker=assignworker, notes=str(comments)) # get image attachment from WorkCapture point, if it exists urlAttachment = "https://services8.arcgis.com/<>/arcgis/rest/services/<>/FeatureServer/0/{}/attachments/{}?f=json&token={}".format(fOID, 1, token) response = requests.get(urlAttachment) attachjson = json.loads(response.text) if 'Attachment' in attachjson: #check to see if there is an attachment at all attachString = json.loads(response.text)["Attachment"] attachBinary = base64.b64decode(attachString) # upload image to other feature service params = {"f": "json", "token": token} files = {"attachments": ('quickcapture_rolled_over_image' + str(assignment.object_id) + '.jpg', attachBinary, 'image/jpeg')} urlAddAttachment = "https://services8.arcgis.com/<>/arcgis/rest/services/workforce_<>/FeatureServer/0/{}/addAttachment?token={}".format(assignment.object_id, token) response = requests.post(urlAddAttachment, params=params, files=files) result = json.loads(response.text) feature.attributes['Converted'] = 1 #flip to 1 to mark the point as already converted, separate clean-up script will delete these after a time print('Success!')