def build_resume_response(user_obj): ''' Builds the resume response for the specified user. Args: user_obj: The object representation for the specified user Returns: The Python dictionary that can be converted to JSON and sent as a response. ''' # create the attachment contents = {} contents['title'] = COMPANY_NAME contents['title_link'] = COMPANY_URL contents['color'] = "good" # Determine the text of the attachment contents['text'] = 'You have resumed your session. Welcome back!' # attach the footer _attach_footer(contents) # construct the response message with the attachment message = slack_api.Message() message.add_attachment(contents) return message.get_contents()
def build_logout_response(user_obj): ''' Builds the logout response for the specified user. Args: user_obj: The object representation for the specified user Returns: The Python dictionary that can be converted to JSON and sent as a response. ''' # create the attachment contents = {} contents['title'] = COMPANY_NAME contents['title_link'] = COMPANY_URL contents['color'] = "danger" # Determine the text of the attachment contents[ 'text'] = 'You are now logged out of Epoch, changing your state to OFFLINE.' # build the hours fields = [] f1 = {} f1['title'] = 'Total Hours (month)' f1['value'] = '%.2f' % (user_session.get_total_worked(user_obj.uuid)) f1['short'] = True f2 = {} f2['title'] = 'Goal Hours (month)' f2['value'] = user.get_goal_total(user_obj.uuid) f2['short'] = True fields.append(f1) fields.append(f2) contents['fields'] = fields # attach the footer _attach_footer(contents) # construct the response message with the attachment message = slack_api.Message() message.add_attachment(contents) return message.get_contents()
def send_user_report(user_obj, start_date, end_date, verified_hours, not_verified_hours, goal_hours): ''' Builds the user report for the specified user. Args: user_obj: The object representation for the specified user start_date: The starting part of the interval end_date: The ending part of the interval verified_hours: Number of hours that were verified not_verified_hours: Number of hours that weren't verified goal_hours: The goal hours for the user Returns: The Python dictionary that can be converted to JSON and sent as a post. ''' # format the time string representations to objects start = datetime.datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S') end = datetime.datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S') # format not verified hours not_ver_hrs = float(not_verified_hours) # construct an empty message message = slack_api.Message() # create the attachment contents = {} contents['color'] = "#082b63" contents['title'] = str(user_obj[1]) contents['title_link'] = 'http://isles.io' if not_ver_hrs > 0: contents['pretext'] = 'Greetings, ' + str( user_obj[1]) + '. Here is your monthly report, minus ' + str( not_verified_hours) + ' hours that were not verified.' else: contents['pretext'] = 'Greetings, ' + str( user_obj[1]) + '. Here is your monthly report.' contents['text'] = 'Report from ' + str( start.strftime('%b %d, %Y')) + ' to ' + str(end.strftime('%b %d, %Y')) # build the hours fields = [] f1 = {} f1['title'] = 'Verified Hours ' f1['value'] = verified_hours f1['short'] = True f2 = {} f2['title'] = 'Goal Hours' f2['value'] = goal_hours f2['short'] = True fields.append(f1) fields.append(f2) contents['fields'] = fields # general footer contents['footer'] = 'Epoch API' contents['footer_icon'] = ICON_URL # attach the timestamp contents['ts'] = int(time.time()) # add the attachment message.add_attachment(contents) message_data = message.get_contents() message_data['channel'] = str(user_obj[0]) message_data['username'] = '******' message_data['icon_emoji'] = ':bar_chart:' # send it off slack_server.send_json(message_data)
def handle_logout_payload(user_obj, start_date, end_date, worked_hours, goal_hours): ''' Builds the logout payload for the specified user. Args: user_obj: The object representation for the specified user start_date: The timestamp for when they logged in end_date: The timestamp for when they logged out worked_hours: The worked hours for the day goal_hours: The goal hours for the day Returns: The Python dictionary that can be converted to JSON and sent as a post. ''' commits = repo.get_commit_logs(user_obj.uuid, start_date, end_date) # construct an empty message message = slack_api.Message() # create the attachment contents = {} contents['color'] = "#082b63" contents['title'] = str(user_obj.username) contents['title_link'] = COMPANY_URL # build the text if commits is not None and len(commits) > 0: text_builder = '' for repo_name, commit_text, commit_url in commits: parts = commit_text.splitlines() m = '`<' + str(commit_url) + '|' + str(repo_name) + '>`: ' + str( parts[0]) + '\n' text_builder = text_builder + m contents['text'] = text_builder # build the hours fields = [] f1 = {} f1['title'] = 'Session Hours (today)' f1['value'] = worked_hours f1['short'] = True f2 = {} f2['title'] = 'Goal Hours (today)' f2['value'] = goal_hours f2['short'] = True fields.append(f1) fields.append(f2) contents['fields'] = fields # attach the footer _attach_footer(contents) # add the attachment message.add_attachment(contents) message_data = message.get_contents() message_data['channel'] = '#work-submit' message_data['username'] = '******' message_data['icon_emoji'] = ':bar_chart:' # send it off slack_server.send_json(message_data)
def build_status_response(user_obj): ''' Builds the status response for the specified user. Args: user_obj: The object representation for the specified user Returns: The Python dictionary that can be converted to JSON and sent as a response. ''' # USER --- STATE --- LAST LOGIN --- HOURS WORKED --- GOAL HOURS all_users = user.get_all_users() if all_users is not None and len(all_users) > 0: # construct the response message with the attachment message = slack_api.Message() for uuid, name in all_users: state = user_session.get_state(uuid) last_login = user_session.get_session_timestamp(uuid) total_hours = '%.2f' % user_session.get_total_worked(uuid) goal_hours = user.get_goal_total(uuid) data = (name, state, last_login, total_hours, goal_hours) # create the attachment contents = {} contents['title'] = str(name) # TODO maybe a link to our workers page? contents['title_link'] = COMPANY_URL # color the attachment based off of state if state == 'ONLINE': contents['color'] = "good" elif state == 'OFFLINE': contents['color'] = "danger" elif state == 'PAUSED': contents['color'] = "warning" # Determine the text of the attachment #contents['text'] = 'Last state change at ' + str(last_login) + ' EST.' # build the hours fields = [] f1 = {} f1['title'] = 'Total Hours (month)' f1['value'] = total_hours f1['short'] = True f2 = {} f2['title'] = 'Goal Hours (month)' f2['value'] = goal_hours f2['short'] = True fields.append(f1) fields.append(f2) contents['fields'] = fields if type(contents) is dict: # general footer contents['footer'] = 'Epoch API' contents['footer_icon'] = ICON_URL if last_login is not None: # attach the timestamp contents['ts'] = int(last_login.strftime('%s')) # add the attachment message.add_attachment(contents) return message.get_contents() else: return Response( 'Unexpected error occurred locally when parsing STATE request.' ), 200
def build_info_response(user_obj): ''' Builds the info response for the specified user. Args: user_obj: The object representation for the specified user Returns: The Python dictionary that can be converted to JSON and sent as a response. ''' # get the state of the user state = user_session.get_state(user_obj.uuid) # create the attachment contents = {} contents['title'] = COMPANY_NAME contents['title_link'] = COMPANY_URL # color the attachment based off of state if state == 'ONLINE': contents['color'] = "good" elif state == 'OFFLINE': contents['color'] = "danger" elif state == 'PAUSED': contents['color'] = "warning" # Determine the text of the attachment contents['text'] = 'Your current state is ' + str(state) + '.' if state == 'ONLINE' or state == 'PAUSED': work_time = user_session.get_work_time(user_obj.uuid) # build the hours fields = [] f1 = {} f1['title'] = 'Worked Time (session)' f1['value'] = '%.2f' % (work_time / 3600000.0) f1['short'] = True f2 = {} f2['title'] = 'Total Hours (month)' f2['value'] = user_session.get_total_worked(user_obj.uuid) f2['short'] = True fields.append(f1) fields.append(f2) contents['fields'] = fields elif state == 'OFFLINE': # build the hours fields = [] f1 = {} f1['title'] = 'Total Hours (month)' f1['value'] = user_session.get_total_worked(user_obj.uuid) f1['short'] = True f2 = {} f2['title'] = 'Goal Hours (month)' f2['value'] = user.get_goal_total(user_obj.uuid) f2['short'] = True fields.append(f1) fields.append(f2) contents['fields'] = fields # attach the footer _attach_footer(contents) # construct the response message with the attachment message = slack_api.Message() message.add_attachment(contents) return message.get_contents()