Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save esteele/dec8ebbbc1e34980a634515f987feff7 to your computer and use it in GitHub Desktop.
Save esteele/dec8ebbbc1e34980a634515f987feff7 to your computer and use it in GitHub Desktop.
import requests
SOURCE_URL_BASE = 'http://localhost:8080/Plone'
TARGET_URL_BASE = 'http://localhost:9090/Plone'
response = requests.post('%s/@login' % SOURCE_URL_BASE,
headers={'Accept': 'application/json', 'Content-Type': 'application/json',},
json={ 'login': 'admin', 'password': 'admin', })
token = response.json()['token']
source_headers = {'Accept': 'application/json', 'Authorization': 'Bearer %s' % token, 'Prefer': 'return=representation'}
response = requests.post('%s/@login' % TARGET_URL_BASE,
headers={'Accept': 'application/json', 'Content-Type': 'application/json',},
json={ 'login': 'admin', 'password': 'admin', })
token = response.json()['token']
target_headers = {'Accept': 'application/json', 'Authorization': 'Bearer %s' % token, 'Prefer': 'return=representation'}
root = requests.get('%s/test-folder' % SOURCE_URL_BASE, headers=source_headers).json()
objects = []
def recurse_folder(item):
objects.append(item['@id'])
for i in requests.get(item['@id'], headers=source_headers).json().get('items', []):
recurse_folder(i)
recurse_folder(root)
for oid in objects:
old_object = requests.get(oid, headers=source_headers).json()
parent_path = old_object['parent']['@id']
new_parent_path = parent_path.replace(SOURCE_URL_BASE, TARGET_URL_BASE)
print "Creating %s in %s" % (old_object['id'], new_parent_path)
print old_object
ok_fields = ['@type', 'id', 'title', 'creation_date', 'modification_date', 'text']
new_object = {}
for field in ok_fields:
if field in old_object:
new_object[field] = old_object[field]
new_object['created'] = u'2019-04-01T19:07:37+00:00'
new_object['modification_date'] = u'2019-04-01T19:07:37+00:00'
# new_object = {
# '@type': old_object['@type'],
# 'title': old_object['title'],
# 'id': old_object['id']
# }
response = requests.post(new_parent_path, headers=target_headers, json=new_object)
# import pdb; pdb.set_trace()
# response = requests.patch('http://localhost:8080/Plone/news', headers=headers, json={ 'title': 'My New Document Title', })
# response = requests.post('http://localhost:8080/Plone/', headers=headers, json={ '@type': 'Document', 'title': 'My Document', })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment