def check_requests(request_type, auth_file, worksheet_key): """Check for new approved requests""" sheet = Spreadsheet(keyfile=auth_file, sheet_id=worksheet_key) rows = sheet.get_all_rows('Form Responses 1') timestamp = datetime.now().strftime("%d %b %Y %H:%M:%S") processed_rows = [] # set som type-specific things if request_type == 'access': parse_function = parse_user_row csr_type = 'Access Request' elif request_type == 'quota': parse_function = parse_quota_row csr_type = 'Quota Request' else: raise Exception('Unknown request type: `{}`'.format(request_type)) for idx, row in enumerate(rows): if (idx == 0) or (row == []): # skip header row and blank rows continue elif (row[0].lower().strip() == 'approved') and (row[1] == ''): # process rows that are marked approved but not notified request_info = parse_function(row) notify_helpdesk(template=helpdesk_template, sender=helpdesk_email, receiver=helpdesk_email, csr_type=csr_type, priority='High', queue='Monitoring', **request_info) processed_rows.append(idx) if args.log: log_request(args.log, timestamp, request_info['user_email']) elif (row[0] == '') and (datetime.now() >= dateparser.parse(row[2]) + timedelta(hours=24)): # send reminder about rows that have been waiting for approval # for more than 24 hours request_info = parse_function(row) reminder(template=reminder_template, sender=reminder_email, receiver=reminder_email, request_type=request_type, **request_info) else: # skip over unapproved or already-notified rows continue # Google API returns an error if you send an empty request if processed_rows: timestamp_spreadsheet(sheet, timestamp, processed_rows)
def check_requests(request_type, auth_file, worksheet_key): """Check for new approved requests""" # Some definitions that should eventually be set in config TIMESTAMP_FORMAT = "%d %b %Y %H:%M:%S" # hours until first reminder sent reminder_start = timedelta(hours=int(config.get('reminder', 'start'))) # interval to send subsequent reminders reminder_interval = timedelta( hours=int(config.get('reminder', 'interval'))) sheet = Spreadsheet(keyfile=auth_file, sheet_id=worksheet_key) rows = sheet.get_all_rows('Form Responses 1') timestamp = datetime.now().strftime(TIMESTAMP_FORMAT) processed_rows = [] reminder_list = [] reminder_rows = [] now = datetime.now() # set some type-specific things if request_type == 'Access': parse_function = parse_user_row csr_type = 'Access Request' elif request_type == 'Quota': parse_function = parse_quota_row csr_type = 'Change Quota' else: raise Exception('Unknown request type: `{}`'.format(request_type)) for idx, row in enumerate(rows): if (idx == 0) or (row == []): # skip header row and blank rows continue elif (row[0].lower().strip() == 'approved') and (row[1] == ''): # process rows that are marked approved but not notified request_info = parse_function(row) notify_helpdesk(csr_type=csr_type, priority='High', queue='Monitoring', **request_info) processed_rows.append(idx) if args.log: log_request(args.log, timestamp, request_info['user_email']) # if request is not approved and is more than `reminder_start` # hours old, send a reminder elif row[0] == '' and (now >= dateparser.parse(row[3]) + reminder_start): # but only send if this is the first one, or if enough time # has passed since the last one if row[2]: last_sent = datetime.strptime(row[2], TIMESTAMP_FORMAT) else: last_sent = None if not last_sent or (now >= last_sent + reminder_interval): request_info = parse_function(row) reminder_list.append(request_info) reminder_rows.append(idx) else: # skip over unapproved rows <24 hours old, or already-notified rows continue # Skip sending empty requests to Google API because it is slow and returns # an error. Try/catch would handle the error but not avoid the time cost. if processed_rows: timestamp_spreadsheet(sheet, timestamp, processed_rows, column=1) if reminder_list: send_reminder(request_type=request_type, reminders=reminder_list, worksheet_key=worksheet_key) timestamp_spreadsheet(sheet, timestamp, reminder_rows, column=2)