Clean up create/update user schema

Pass True/False to indicate update/create
Delete extraneous () with del, del is an operator, not a function
When looking for field to delete, don't distract user with messages
saying field name doesn't match other fields; you get a message only if
you give and invalid field name
Error messages to user on invalid arguments say create/update as
appropriate, before it always said create which might be confusing if
you just did an update
It's an error to say deletefield when creating
This commit is contained in:
Ross Scroggs
2016-08-21 11:46:42 -07:00
parent 3b5b5be881
commit bbc9b8fb2b

View File

@@ -6415,24 +6415,26 @@ def doDelSchema():
callGAPI(cd.schemas(), u'delete', customerId=GC_Values[GC_CUSTOMER_ID], schemaKey=schemaKey) callGAPI(cd.schemas(), u'delete', customerId=GC_Values[GC_CUSTOMER_ID], schemaKey=schemaKey)
print u'Deleted schema %s' % schemaKey print u'Deleted schema %s' % schemaKey
def doCreateOrUpdateUserSchema(function): def doCreateOrUpdateUserSchema(updateCmd):
cd = buildGAPIObject(u'directory') cd = buildGAPIObject(u'directory')
schemaKey = sys.argv[3] schemaKey = sys.argv[3]
if function == u'update': if updateCmd:
cmd = u'update'
try: try:
body = callGAPI(cd.schemas(), u'get', throw_reasons=[u'notFound'], customerId=GC_Values[GC_CUSTOMER_ID], schemaKey=schemaKey) body = callGAPI(cd.schemas(), u'get', throw_reasons=[u'notFound'], customerId=GC_Values[GC_CUSTOMER_ID], schemaKey=schemaKey)
except googleapiclient.errors.HttpError: except googleapiclient.errors.HttpError:
print u'ERROR: Schema %s does not exist.' % schemaKey print u'ERROR: Schema %s does not exist.' % schemaKey
sys.exit(3) sys.exit(3)
else: # function == insert else: # create
cmd = u'create'
body = {u'schemaName': schemaKey, u'fields': []} body = {u'schemaName': schemaKey, u'fields': []}
i = 4 i = 4
while i < len(sys.argv): while i < len(sys.argv):
if sys.argv[i] in [u'field']: if sys.argv[i] in [u'field']:
if function != u'insert': # clear field if it exists on update if updateCmd: # clear field if it exists on update
for n, field in enumerate(body[u'fields']): for n, field in enumerate(body[u'fields']):
if field[u'fieldName'].lower() == sys.argv[i+1].lower(): if field[u'fieldName'].lower() == sys.argv[i+1].lower():
del(body[u'fields'][n]) del body[u'fields'][n]
break break
a_field = {u'fieldName': sys.argv[i+1]} a_field = {u'fieldName': sys.argv[i+1]}
i += 2 i += 2
@@ -6460,30 +6462,26 @@ def doCreateOrUpdateUserSchema(function):
i += 1 i += 1
break break
else: else:
print u'ERROR: %s is not a valid argument for "gam create schema"' % sys.argv[i] print u'ERROR: %s is not a valid argument for "gam %s schema"' % (sys.argv[i], cmd)
sys.exit(2) sys.exit(2)
elif sys.argv[i] in [u'deletefield']: elif updateCmd and sys.argv[i] in [u'deletefield']:
field_found = False
for n, field in enumerate(body[u'fields']): for n, field in enumerate(body[u'fields']):
if field[u'fieldName'].lower() == sys.argv[i+1].lower(): if field[u'fieldName'].lower() == sys.argv[i+1].lower():
del(body[u'fields'][n]) del body[u'fields'][n]
field_found = True
break break
else: else:
print "%s != %s" % (field[u'fieldName'].lower(), sys.argv[i+1].lower())
if not field_found:
print u'ERROR: field %s not found in schema %s' % (sys.argv[i+1], schemaKey) print u'ERROR: field %s not found in schema %s' % (sys.argv[i+1], schemaKey)
sys.exit(3) sys.exit(3)
i += 2 i += 2
else: else:
print u'ERROR: %s is not a valid argument for "gam create schema"' % sys.argv[i] print u'ERROR: %s is not a valid argument for "gam %s schema"' % (sys.argv[i], cmd)
sys.exit(2) sys.exit(2)
if function == u'insert': if updateCmd:
result = callGAPI(cd.schemas(), u'insert', customerId=GC_Values[GC_CUSTOMER_ID], body=body)
print u'Created user schema %s' % result[u'schemaName']
else: # function == update
result = callGAPI(cd.schemas(), u'update', customerId=GC_Values[GC_CUSTOMER_ID], body=body, schemaKey=schemaKey) result = callGAPI(cd.schemas(), u'update', customerId=GC_Values[GC_CUSTOMER_ID], body=body, schemaKey=schemaKey)
print u'Updated user schema %s' % result[u'schemaName'] print u'Updated user schema %s' % result[u'schemaName']
else:
result = callGAPI(cd.schemas(), u'insert', customerId=GC_Values[GC_CUSTOMER_ID], body=body)
print u'Created user schema %s' % result[u'schemaName']
def _showSchema(schema): def _showSchema(schema):
print u'Schema: %s' % schema[u'schemaName'] print u'Schema: %s' % schema[u'schemaName']
@@ -10588,7 +10586,7 @@ def ProcessGAMCommand(args):
elif argument in [u'verify', u'verification']: elif argument in [u'verify', u'verification']:
doSiteVerifyShow() doSiteVerifyShow()
elif argument == u'schema': elif argument == u'schema':
doCreateOrUpdateUserSchema(u'insert') doCreateOrUpdateUserSchema(False)
elif argument in [u'course', u'class']: elif argument in [u'course', u'class']:
doCreateCourse() doCreateCourse()
elif argument in [u'transfer', u'datatransfer']: elif argument in [u'transfer', u'datatransfer']:
@@ -10628,7 +10626,7 @@ def ProcessGAMCommand(args):
elif argument in [u'verify', u'verification']: elif argument in [u'verify', u'verification']:
doSiteVerifyAttempt() doSiteVerifyAttempt()
elif argument in [u'schema', u'schemas']: elif argument in [u'schema', u'schemas']:
doCreateOrUpdateUserSchema(u'update') doCreateOrUpdateUserSchema(True)
elif argument in [u'course', u'class']: elif argument in [u'course', u'class']:
doUpdateCourse() doUpdateCourse()
elif argument in [u'printer', u'print']: elif argument in [u'printer', u'print']: