Skip to content

Commit

Permalink
Fix replace_file issue where some file metadata was incorrect or miss…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
jllong-usgs committed Jan 26, 2018
1 parent c7f2c99 commit c23e05a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
20 changes: 14 additions & 6 deletions pysb/SbSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def replace_file(self, filename, item):
facet['files'] = new_files
new_facets.append(facet)
item['facets'] = new_facets
self.update_item(item)
return self.update_item(item)

def _replace_file(self, filename, itemfile):
"""Upload a file to ScienceBase and update file json with new path on disk.
Expand All @@ -382,11 +382,19 @@ def _replace_file(self, filename, itemfile):
#
# Upload file and point file JSON at it
#
upld_json = self.upload_file(filename, itemfile['contentType'])
itemfile['pathOnDisk'] = upld_json[0]['fileKey']
itemfile['dateUploaded'] = upld_json[0]['dateUploaded']
itemfile['uploadedBy'] = upld_json[0]['uploadedBy']
return itemfile
new_itemfile = {}
new_itemfile["contentType"] = itemfile["contentType"] # ScienceBase does not allow this to be null, so guess that it's the same
new_itemfile["name"] = itemfile["name"]
new_itemfile["title"] = itemfile["title"]
new_itemfile["originalMetadata"] = itemfile["originalMetadata"]
new_itemfile["useForPreview"] = itemfile["useForPreview"]

upld_json = self.upload_file(filename)
new_itemfile['pathOnDisk'] = upld_json[0]['fileKey']
new_itemfile['dateUploaded'] = upld_json[0]['dateUploaded']
new_itemfile['uploadedBy'] = upld_json[0]['uploadedBy']

return new_itemfile

def get_item_files_zip(self, item, destination='.'):
"""Download all files from a ScienceBase Item as a zip. The zip is created server-side
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from distutils.core import setup
setup(name='pysb',
version='1.5.11',
version='1.5.12',
packages=['pysb',],
description="Python ScienceBase Utilities",
author="ScienceBase Development Team",
Expand Down
22 changes: 22 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from os import listdir
from os.path import isfile, join
from six.moves import input
import tempfile

class TestPysbMethods(unittest.TestCase):
SB_CATALOG_ITEM_ID = '4f4e4760e4b07f02db47df9c'
Expand Down Expand Up @@ -154,5 +155,26 @@ def test_relationships(self):
sb.delete_item(item1)
sb.delete_item(item2)

def test_replace_file(self):
sb = SbSession('beta').login(self.TEST_USER, self.TEST_PASSWORD)
item = sb.create_item({"title": "Replace File Test", "parentId": sb.get_my_items_id()})

my_file = tempfile.NamedTemporaryFile()
try:
my_file.write("This is a test file. ".encode('utf-8'))
my_file.flush()
item = sb.upload_file_to_item(item, my_file.name, scrape_file=False)
self.assertTrue("files" in item)
orig_size = item["files"][0]["size"]

my_file.write("Here is some more text -- update successful".encode('utf-8'))
my_file.flush()
item = sb.replace_file(my_file.name, item)
self.assertGreater(item["files"][0]["size"], orig_size)
finally:
my_file.close()
if item and "id" in item:
sb.delete_item(item)

if __name__ == '__main__':
unittest.main()

0 comments on commit c23e05a

Please sign in to comment.