Select to view content in your preferred language

Overwrite existing item fails

652
1
Jump to solution
06-01-2023 11:26 AM
JamesCrandall
MVP Frequent Contributor

Attempting to convert older python 2.7 script with Python API that overwrites and existing "Form" item on our org AGO.  

Context: a survey123 form item is downloaded, unzipped, media folder updated with new mmpk, re-zipped and then POST request was made to the same AGO item.  This worked just fine for py 2.7 but I've had to rewrite for the Pyhon API and running into this issue.

When i review the contents of the zip it appears everything is correct but the last step fails to upload/overwrite with: 

AttributeError: 'ZipFile' object has no attribute 'decode'
 

 

 

def updateZip(mmpk_file):

    mediaFolder = Path('./downloaded/esriinfo/media')
    targetFile = os.path.join(mediaFolder, 'quickMMPK.mmpk')

    #copy mmpk into medial folder
    shutil.copy(mmpk_file, targetFile)

    # compress the contents of esriinfo into a new zip file
    extractFolder = Path('./downloaded')
    esriInfoFolder = Path('./downloaded/esriinfo')
    outputFileName = extractFolder.joinpath('esriinfo')

    zip_path = extractFolder.joinpath('{}.zip'.format('FS_From_Connect'))
    #zip_file = zip_path #ZipFile(zip_path)
    zf = ZipFile(zip_path, "w")

    os.chdir(extractFolder)

    for dirname, subdirs, files in os.walk('esriinfo'):
      for filename in files:
        zf.write(os.path.join(dirname, filename))

    zf.close()

    # `ContentManager.get` will return `None` if there is no Item with ID `0ccabee15e76442483d50d82ae0c4053`
    data_item = gis.content.get(surveyFormItem)

    props = {
    "type":"form",
    "title":data_item.title,
    "overwrite":True
    }

    data_item.update(item_properties=props, data=zf) #AttributeError: 'ZipFile' object has no attribute 'decode'

 

 

0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor

Ok it looks like a simple misunderstanding... I think the data= property on .update is simply the full path/filename, not the ZipFile object.

 

extractFolder = r'\\Scripts\Survey123\mmpkUpdate\downloaded'
zip_path = os.path.join(extractFolder,'{}.zip'.format('FS_From_Connect'))
data_item.update(item_properties=props, data=zip_path)

View solution in original post

0 Kudos
1 Reply
JamesCrandall
MVP Frequent Contributor

Ok it looks like a simple misunderstanding... I think the data= property on .update is simply the full path/filename, not the ZipFile object.

 

extractFolder = r'\\Scripts\Survey123\mmpkUpdate\downloaded'
zip_path = os.path.join(extractFolder,'{}.zip'.format('FS_From_Connect'))
data_item.update(item_properties=props, data=zip_path)
0 Kudos