#!/usr/bin/env python3

import jargparse
import json
import requests

############
### MAIN ###
############
parser = jargparse.ArgParser('Test anon session lists to new user')

parser.add_argument('newUserName')
parser.add_argument('service', help='InterMine service URL.  e.g. http://synbiomine.org/synbiomine')
args = parser.parse_args()

rdata = { 'name' : args.newUserName, 'password' : 'passw0rd' }
r = requests.post('%s/users' % args.service, params = rdata)
print(json.loads(r.text))

@@ -17,16 +17,32 @@
 ython3%0A%0A
+import argparse%0A
 import j
@@ -147,16 +147,21 @@
 gParser(
+%0A    
 'Test an
@@ -193,174 +193,1157 @@
 ser'
-)%0A%0Aparser.add_argument('newUserName')%0Aparser.add_argument('service', help='InterMine service URL.  e.g. http://synbiomine.org/synbiomine')%0Aargs = parser.parse_args()%0A
+,%0A    formatter_class=argparse.RawDescriptionHelpFormatter,%0A    epilog='''QUERY EXAMPLE:%0A%3Cquery view=%22Gene.primaryIdentifier Gene.symbol%22 sortOrder=%22Gene.primaryIdentifier asc%22%3E%0A  %3Cconstraint path=%22Gene.organism.name%22 code=%22A%22 editable=%22true%22 op=%22=%22 value=%22Escherichia coli str. K-12 substr. MG1655%22/%3E%0A%3C/query%3E''')%0A%0Aparser.add_argument('newUserName')%0Aparser.add_argument('query', help='query XML')%0Aparser.add_argument('listname')%0A%0Aparser.add_argument('service', help='InterMine service URL.  e.g. http://synbiomine.org/synbiomine')%0Aargs = parser.parse_args()%0A%0Awith open(args.query) as f:%0A  xml = f.readlines()%0A%0Aprint('Requesting anonymous session')%0Ar = requests.get('%25s/session' %25 args.service)%0Arjson = json.loads(r.text)%0AanonSessionToken = rjson%5B'token'%5D%0Aprint('Got session token %5B%25s%5D' %25 anonSessionToken)%0A%0Aprint('Creating temporary list %5B%25s%5D' %25 args.listname)%0Ardata = %7B 'query' : xml, 'name' : args.listname %7D%0Ar = requests.post('%25s/query/tolist' %25 args.service, headers = %7B 'Authorization':'Token %25s' %25 anonSessionToken %7D, params = rdata)%0A%0Aprint('Creating new user account %5B%25s%5D feeding in anon session %5B%25s%5D' %25 (args.newUserName, anonSessionToken))
 %0Arda
@@ -1399,16 +1399,66 @@
 assw0rd'
+, 'existing-anon-session-token' : anonSessionToken
  %7D%0Ar = r
@@ -1539,8 +1539,81 @@
 .text))%0A
+%0A# Todo: check if now permanent list has the same id as the temporary one

extend test-anon-session-lists-to-new-user.py to create an anonymous list and feed the session token into new user creation