Merge pull request #213 from taers232c/branch-3.66

Calendar add/update/show improvements
This commit is contained in:
Jay Lee
2016-04-03 12:40:43 -04:00

View File

@ -246,7 +246,9 @@ GC_VAR_INFO = {
GC_USER_BATCH_SIZE: {GC_VAR_TYPE: GC_TYPE_INTEGER, GC_VAR_LIMITS: (1, 1000)}, GC_USER_BATCH_SIZE: {GC_VAR_TYPE: GC_TYPE_INTEGER, GC_VAR_LIMITS: (1, 1000)},
GC_USER_MAX_RESULTS: {GC_VAR_TYPE: GC_TYPE_INTEGER, GC_VAR_LIMITS: (1, 500)}, GC_USER_MAX_RESULTS: {GC_VAR_TYPE: GC_TYPE_INTEGER, GC_VAR_LIMITS: (1, 500)},
} }
#
CLEAR_NONE_ARGUMENT = [u'clear', u'none',]
#
MESSAGE_CLIENT_API_ACCESS_DENIED = u'Access Denied. Please make sure the Client Name:\n\n{0}\n\nis authorized for the API Scope(s):\n\n{1}\n\nThis can be configured in your Control Panel under:\n\nSecurity -->\nAdvanced Settings -->\nManage API client access' MESSAGE_CLIENT_API_ACCESS_DENIED = u'Access Denied. Please make sure the Client Name:\n\n{0}\n\nis authorized for the API Scope(s):\n\n{1}\n\nThis can be configured in your Control Panel under:\n\nSecurity -->\nAdvanced Settings -->\nManage API client access'
MESSAGE_GAM_EXITING_FOR_UPDATE = u'GAM is now exiting so that you can overwrite this old version with the latest release' MESSAGE_GAM_EXITING_FOR_UPDATE = u'GAM is now exiting so that you can overwrite this old version with the latest release'
MESSAGE_GAM_OUT_OF_MEMORY = u'GAM has run out of memory. If this is a large Google Apps instance, you should use a 64-bit version of GAM on Windows or a 64-bit version of Python on other systems.' MESSAGE_GAM_OUT_OF_MEMORY = u'GAM has run out of memory. If this is a large Google Apps instance, you should use a 64-bit version of GAM on Windows or a 64-bit version of Python on other systems.'
@ -2307,7 +2309,7 @@ def changeCalendarAttendees(users):
break break
def deleteCalendar(users): def deleteCalendar(users):
cal = buildGAPIServiceObject(u'calendar', users[0]) buildGAPIObject(u'calendar')
calendarId = sys.argv[5] calendarId = sys.argv[5]
if calendarId.find(u'@') == -1: if calendarId.find(u'@') == -1:
calendarId = u'%s@%s' % (calendarId, GC_Values[GC_DOMAIN]) calendarId = u'%s@%s' % (calendarId, GC_Values[GC_DOMAIN])
@ -2317,19 +2319,21 @@ def deleteCalendar(users):
cal = buildGAPIServiceObject(u'calendar', user) cal = buildGAPIServiceObject(u'calendar', user)
callGAPI(service=cal.calendarList(), function=u'delete', calendarId=calendarId) callGAPI(service=cal.calendarList(), function=u'delete', calendarId=calendarId)
def addCalendar(users): CALENDAR_REMINDER_METHODS = [u'email', u'sms', u'popup',]
cal = buildGAPIServiceObject(u'calendar', users[0]) CALENDAR_NOTIFICATION_METHODS = [u'email', u'sms',]
body = dict() CALENDAR_NOTIFICATION_TYPES_MAP = {
body[u'defaultReminders'] = list() u'eventcreation': u'eventCreation',
body[u'id'] = sys.argv[5] u'eventchange': u'eventChange',
if body[u'id'].find(u'@') == -1: u'eventcancellation': u'eventCancellation',
body[u'id'] = u'%s@%s' % (body[u'id'], GC_Values[GC_DOMAIN]) u'eventresponse': u'eventResponse',
body[u'selected'] = True u'agenda': u'agenda',
body[u'hidden'] = False }
def getCalendarAttributes(i, body, function):
colorRgbFormat = False colorRgbFormat = False
i = 6
while i < len(sys.argv): while i < len(sys.argv):
if sys.argv[i].lower() == u'selected': myarg = sys.argv[i].lower().replace(u'_', u'')
if myarg == u'selected':
if sys.argv[i+1].lower() in true_values: if sys.argv[i+1].lower() in true_values:
body[u'selected'] = True body[u'selected'] = True
elif sys.argv[i+1].lower() in false_values: elif sys.argv[i+1].lower() in false_values:
@ -2338,7 +2342,7 @@ def addCalendar(users):
print u'ERROR: Value for selected must be true or false, not %s' % sys.argv[i+1] print u'ERROR: Value for selected must be true or false, not %s' % sys.argv[i+1]
sys.exit(2) sys.exit(2)
i += 2 i += 2
elif sys.argv[i].lower() == u'hidden': elif myarg == u'hidden':
if sys.argv[i+1].lower() in true_values: if sys.argv[i+1].lower() in true_values:
body[u'hidden'] = True body[u'hidden'] = True
elif sys.argv[i+1].lower() in false_values: elif sys.argv[i+1].lower() in false_values:
@ -2347,105 +2351,85 @@ def addCalendar(users):
print u'ERROR: Value for hidden must be true or false, not %s' % sys.argv[i+1] print u'ERROR: Value for hidden must be true or false, not %s' % sys.argv[i+1]
sys.exit(2) sys.exit(2)
i += 2 i += 2
elif sys.argv[i].lower() == u'reminder': elif myarg == u'summary':
method = sys.argv[i+1].lower()
try:
minutes = int(sys.argv[i+2])
except ValueError:
print u'ERROR: Reminder time must be specified in minutes, got %s' % sys.argv[i+2]
sys.exit(2)
if method != u'email' and method != u'sms' and method != u'popup':
print u'ERROR: Method must be email, sms or popup. Got %s' % method
sys.exit(2)
body[u'defaultReminders'].append({u'method': method, u'minutes': minutes})
i = i + 3
elif sys.argv[i].lower() == u'summary':
body[u'summaryOverride'] = sys.argv[i+1] body[u'summaryOverride'] = sys.argv[i+1]
i += 2 i += 2
elif sys.argv[i].lower() == u'colorindex': elif myarg == u'colorindex':
body[u'colorId'] = str(sys.argv[i+1]) body[u'colorId'] = str(sys.argv[i+1])
i += 2 i += 2
elif sys.argv[i].lower() == u'backgroundcolor': elif myarg == u'backgroundcolor':
body[u'backgroundColor'] = sys.argv[i+1] body[u'backgroundColor'] = sys.argv[i+1]
colorRgbFormat = True colorRgbFormat = True
i += 2 i += 2
elif sys.argv[i].lower() == u'foregroundcolor': elif myarg == u'foregroundcolor':
body[u'foregroundColor'] = sys.argv[i+1] body[u'foregroundColor'] = sys.argv[i+1]
colorRgbFormat = True colorRgbFormat = True
i += 2 i += 2
elif myarg == u'reminder':
body.setdefault(u'defaultReminders', [])
method = sys.argv[i+1].lower()
if method not in CLEAR_NONE_ARGUMENT:
if method not in CALENDAR_REMINDER_METHODS:
print u'ERROR: Method must be %s. Got %s' % (u','.join(CALENDAR_REMINDER_METHODS+CLEAR_NONE_ARGUMENT), method)
sys.exit(2)
try:
minutes = int(sys.argv[i+2])
except ValueError:
print u'ERROR: Reminder time must be specified in minutes, got %s' % sys.argv[i+2]
sys.exit(2)
body[u'defaultReminders'].append({u'method': method, u'minutes': minutes})
i += 3
else:
i += 2
elif myarg == u'notification':
body.setdefault(u'notificationSettings', {u'notifications': []})
method = sys.argv[i+1].lower()
if method not in CLEAR_NONE_ARGUMENT:
if method not in CALENDAR_NOTIFICATION_METHODS:
print u'ERROR: Method must be %s. Got %s' % (u','.join(CALENDAR_NOTIFICATION_METHODS+CLEAR_NONE_ARGUMENT), method)
sys.exit(2)
eventType = sys.argv[i+2].lower()
if eventType not in CALENDAR_NOTIFICATION_TYPES_MAP:
print u'ERROR: Event must be %s. Got %s' % (u','.join(CALENDAR_NOTIFICATION_TYPES_MAP), eventType)
sys.exit(2)
body[u'notificationSettings'][u'notifications'].append({u'method': method, u'type': CALENDAR_NOTIFICATION_TYPES_MAP[eventType]})
i += 3
else:
i += 2
else: else:
print u'ERROR: %s is not a valid argument for "gam add calendar"' % sys.argv[i] print u'ERROR: %s is not a valid argument for "gam %s calendar"' % (sys.argv[i], function)
sys.exit(2) sys.exit(2)
return colorRgbFormat
def addCalendar(users):
buildGAPIObject(u'calendar')
calendarId = sys.argv[5]
if calendarId.find(u'@') == -1:
calendarId = u'%s@%s' % (calendarId, GC_Values[GC_DOMAIN])
body = {u'id': calendarId, u'selected': True, u'hidden': False}
colorRgbFormat = getCalendarAttributes(6, body, u'add')
i = 1 i = 1
count = len(users) count = len(users)
for user in users: for user in users:
if user.find(u'@') == -1: if user.find(u'@') == -1:
user = u'%s@%s' % (user, GC_Values[GC_DOMAIN]) user = u'%s@%s' % (user, GC_Values[GC_DOMAIN])
print u"Subscribing %s to %s calendar (%s of %s)" % (user, body['id'], i, count) print u"Subscribing %s to %s calendar (%s of %s)" % (user, calendarId, i, count)
cal = buildGAPIServiceObject(u'calendar', user) cal = buildGAPIServiceObject(u'calendar', user)
callGAPI(service=cal.calendarList(), function=u'insert', body=body, colorRgbFormat=colorRgbFormat) callGAPI(service=cal.calendarList(), function=u'insert', body=body, colorRgbFormat=colorRgbFormat)
i += 1 i += 1
def updateCalendar(users): def updateCalendar(users):
buildGAPIObject(u'calendar')
calendarId = sys.argv[5] calendarId = sys.argv[5]
i = 6 if calendarId.find(u'@') == -1:
body = dict() calendarId = u'%s@%s' % (calendarId, GC_Values[GC_DOMAIN])
body[u'id'] = calendarId body = {}
colorRgbFormat = False colorRgbFormat = getCalendarAttributes(6, body, u'update')
while i < len(sys.argv):
if sys.argv[i].lower() == u'selected':
if sys.argv[i+1].lower() in true_values:
body[u'selected'] = True
elif sys.argv[i+1].lower() in false_values:
body[u'selected'] = False
else:
print u'ERROR: Value for selected must be true or false, not %s' % sys.argv[i+1]
sys.exit(2)
i += 2
elif sys.argv[i].lower() == u'hidden':
if sys.argv[i+1].lower() in true_values:
body[u'hidden'] = True
elif sys.argv[i+1].lower() in false_values:
body[u'hidden'] = False
else:
print u'ERROR: Value for hidden must be true or false, not %s' % sys.argv[i+1]
sys.exit(2)
i += 2
elif sys.argv[i].lower() == u'summary':
body[u'summaryOverride'] = sys.argv[i+1]
i += 2
elif sys.argv[i].lower() == u'colorindex':
body[u'colorId'] = str(sys.argv[i+1])
i += 2
elif sys.argv[i].lower() == u'backgroundcolor':
body[u'backgroundColor'] = sys.argv[i+1]
colorRgbFormat = True
i += 2
elif sys.argv[i].lower() == u'foregroundcolor':
body[u'foregroundColor'] = sys.argv[i+1]
colorRgbFormat = True
i += 2
elif sys.argv[i].lower() == u'reminder':
method = sys.argv[i+1].lower()
try:
minutes = int(sys.argv[i+2])
except ValueError:
print u'ERROR: Reminder time must be specified in minutes, got %s' % sys.argv[i+2]
sys.exit(2)
if method != u'email' and method != u'sms' and method != u'popup':
print u'ERROR: Method must be email, sms or popup. Got %s' % method
sys.exit(2)
try:
body[u'defaultReminders'].append({u'method': method, u'minutes': minutes})
except KeyError:
body[u'defaultReminders'] = [{u'method': method, u'minutes': minutes}]
i = i + 3
else:
print u'ERROR: %s is not a valid argument for "gam update calendar"' % sys.argv[i]
sys.exit(2)
i = 1 i = 1
count = len(users) count = len(users)
for user in users: for user in users:
if user.find(u'@') == -1:
user = u'%s@%s' % (user, GC_Values[GC_DOMAIN])
print u"Updating %s's subscription to calendar %s (%s of %s)" % (user, calendarId, i, count) print u"Updating %s's subscription to calendar %s (%s of %s)" % (user, calendarId, i, count)
cal = buildGAPIServiceObject(u'calendar', user) cal = buildGAPIServiceObject(u'calendar', user)
callGAPI(service=cal.calendarList(), function=u'update', calendarId=calendarId, body=body, colorRgbFormat=colorRgbFormat) callGAPI(service=cal.calendarList(), function=u'update', calendarId=calendarId, body=body, colorRgbFormat=colorRgbFormat)
@ -2827,6 +2811,8 @@ def doCalendarAddACL(calendarId=None, act_as=None, role=None, scope=None, entity
body[u'scope'] = dict() body[u'scope'] = dict()
if calendarId == None: if calendarId == None:
calendarId = sys.argv[2] calendarId = sys.argv[2]
if calendarId.find(u'@') == -1:
calendarId = u'%s@%s' % (calendarId, GC_Values[GC_DOMAIN])
if role != None: if role != None:
body[u'role'] = role body[u'role'] = role
else: else:
@ -3125,32 +3111,21 @@ def showCalendars(users):
cal = buildGAPIServiceObject(u'calendar', user) cal = buildGAPIServiceObject(u'calendar', user)
feed = callGAPI(service=cal.calendarList(), function=u'list') feed = callGAPI(service=cal.calendarList(), function=u'list')
for usercal in feed[u'items']: for usercal in feed[u'items']:
print u' Name: %s' % usercal['id'] print u' Name: %s' % usercal[u'id']
print convertUTF8(u' Summary: %s' % usercal['summary']) print convertUTF8(u' Summary: %s' % usercal[u'summary'])
try: print convertUTF8(u' Description: %s' % usercal.get(u'description', u''))
print convertUTF8(u' Description: %s' % usercal['description']) print u' Access Level: %s' % usercal[u'accessRole']
except KeyError: print u' Timezone: %s' % usercal[u'timeZone']
print u' Description: ' print convertUTF8(u' Location: %s' % usercal.get(u'location', u''))
print u' Access Level: %s' % usercal['accessRole'] print u' Hidden: %s' % usercal.get(u'hidden', u'False')
print u' Timezone: %s' % usercal['timeZone'] print u' Selected: %s' % usercal.get(u'selected', u'False')
try:
print convertUTF8(u' Location: %s' % usercal['location'])
except KeyError:
pass
try:
print u' Hidden: %s' % usercal['hidden']
except KeyError:
print u' Hidden: False'
try:
print u' Selected: %s' % usercal['selected']
except KeyError:
print u' Selected: False'
print u' Default Reminders:' print u' Default Reminders:'
try: for reminder in usercal.get(u'defaultReminders', []):
for reminder in usercal[u'defaultReminders']: print u' Type: %s Minutes: %s' % (reminder[u'method'], reminder[u'minutes'])
print u' Type: %s Minutes: %s' % (reminder['method'], reminder['minutes']) print u' Notifications:'
except KeyError: if u'notificationSettings' in usercal:
pass for notification in usercal[u'notificationSettings'].get(u'notifications', []):
print u' Method: %s Type: %s' % (notification[u'method'], notification[u'type'])
print u'' print u''
def showCalSettings(users): def showCalSettings(users):