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 handle_list_command(): ''' Handles the parsing of the list command. This allows the user to see what users we are tracking. ''' # 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: 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) print('[' + str(name) + '][' + str(state) + '] was last seen at ' + str(last_login) + '. They are at ' + str(total_hours) + ' / ' + str(goal_hours) + ' hours.')
def determine_goal_hours_today(uuid): ''' Determines the goal hours for today. Args: uuid: The uuid of the user Returns: The number of hours, as a string, that the user should try and work today. ''' current = user_session.get_total_worked(uuid) goal = get_goal_total(uuid) number_left = goal - current if number_left > 0: # get the int representation of today current_day = datetime.datetime.today().day # determine the days in THIS month now = datetime.datetime.now() days_in_month = calendar.monthrange(now.year, now.month)[1] days_left = days_in_month - current_day + 1 if days_left <= 0: if number_left > 24: return '>24' else: return '%.2f' % number_left else: goal = number_left / days_left return '%.2f' % goal return 'Goal Reached!'
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()