def update_user(id): user = get_user(id) functions = get_functions() user_function_rows = get_user_function(id) user_functions = [] for row in user_function_rows: user_functions.append(get_function(row['function_id'])['name']) data = { "user": dict(user), "functions": dict(functions), "userFunctions": user_functions } if request.method == 'POST': user_type = request.form['userType'] active = request.form['userActive'] user_functions = request.form.getlist('functions') errors = [] if not user_type: errors.append('Type is required.') if not active: errors.append('Active is required.') if user_type != 'ADMIN' and not user_functions: errors.append('User functions required.') if errors: [flash([error, id], category='update') for error in errors] # return flashed messages return jsonify(status='error') else: db = get_db() db.execute('UPDATE user SET type = ?, active = ? WHERE id = ?', (user_type, active, id)) if user_functions: if has_user_function(id): db.execute('DELETE from user_function WHERE user_id = ?', (id, )) for function in user_functions: db.execute( 'INSERT INTO user_function (user_id, function_id)' ' VALUES (?,?)', (id, get_function_by_name(function)['id'])) db.commit() return jsonify(status='ok') return jsonify(dict(data))
def index(): functions = get_user_function( g.user['id']) if g.user['type'] != 'ADMIN' else get_functions() data = { 'functions': dict([get_function(function['function_id']) for function in functions] if g.user['type'] != 'ADMIN' else functions) } return render_template('index.html', data=data)
def update_function(id): function = get_function(id) if request.method == 'POST': name = request.form['functionName'] errors = [] if not name: errors.append('Name is required.') if errors: [flash([error, id], category='update') for error in errors] return jsonify(status='error') else: db = get_db() db.execute('UPDATE function SET name = ? WHERE id = ?', (name, id)) db.commit() return jsonify(status='ok') return jsonify(dict(function))
def dashboard(id): user_functions = get_user_function(g.user['id']) function_ids = [function['function_id'] for function in user_functions] if g.user['type'] != 'ADMIN' and id != 0 and id not in function_ids: abort(403, 'You are assigned to the selected function') functions = get_functions() line_chart = get_line_chart( function=get_function(id)['name'] if id != 0 else 'All') positions = line_chart['positions'] budget = line_chart['budget'] positions_cost = line_chart['positions_cost'] budget_cost = line_chart['budget_cost'] data = { 'positions': positions, 'budget': budget, 'positions_cost': positions_cost, 'budget_cost': budget_cost, 'functions': dict(functions) } return jsonify(dict(data))
def get_line_chart(function=None): positions = get_positions(check_submitter=False) budget = get_budget() columns = [row.keys() for row in positions] positions = pd.DataFrame(positions, columns=columns[0]) budget = pd.DataFrame(budget, columns=columns[0]) if function: if function != 'All': positions = positions.loc[positions['function'] == function] budget = budget.loc[budget['function'] == function] if g.user['type'] != 'ADMIN' and function == 'All': functions = get_user_function(g.user['id']) function_names = [get_function(function['function_id'])['name'] for function in functions] positions = positions.loc[positions['function'].isin(function_names)] budget = budget.loc[budget['function'].isin(function_names)] positions['FTE'] = pd.to_numeric(positions['hours'], errors='coerce') / 40 budget['FTE'] = pd.to_numeric(budget['hours'], errors='coerce') / 40 positions['salary'] = pd.to_numeric(positions['salary'], errors='coerce') positions['fringe_benefit'] = pd.to_numeric(positions['fringe_benefit'], errors='coerce') positions['social_security_contribution'] = pd.to_numeric(positions['social_security_contribution'], errors='coerce') budget['salary'] = pd.to_numeric(budget['salary'], errors='coerce') budget['fringe_benefit'] = pd.to_numeric(budget['fringe_benefit'], errors='coerce') budget['social_security_contribution'] = pd.to_numeric(budget['social_security_contribution'], errors='coerce') positions['total_cost'] = positions['salary'].add(positions['fringe_benefit'], fill_value=0).add(positions['social_security_contribution'], fill_value=0) budget['total_cost'] = budget['salary'].add(budget['fringe_benefit'], fill_value=0).add(budget['social_security_contribution'], fill_value=0) positions['start_date'] = pd.to_datetime(positions['start_date'], errors='coerce') positions['end_date'] = pd.to_datetime(positions['end_date'], errors='coerce') budget['start_date'] = pd.to_datetime(budget['start_date'], errors='coerce') budget['end_date'] = pd.to_datetime(budget['end_date'], errors='coerce') year = datetime.now().year months = {1:'January', 2:'February', 3:'March', 4:'April', 5:'May', 6:'June', 7:'July', 8:'August', 9:'September', 10:'October', 11:'November', 12:'December'} index=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] headcount_2018 = pd.Series(index=index) budgeted_headcount_2018 = pd.Series(index=index) headcount_cost_2018 = pd.Series(index=index) budgeted_headcount_cost_2018 = pd.Series(index=index) total_proposed_increase = 0 proposed_monthly_increase = budget.loc[budget['recruitment_status'].isin(['Proposed', 'Approved'])]['FTE'].sum()/12 for month in range(1,13): total_proposed_increase += proposed_monthly_increase hc = budget.loc[budget['recruitment_status'].isin(['On-Board', 'Contracted'])]['FTE'].sum() budgeted_headcount_2018[months[month]] = pd.Series(hc + total_proposed_increase) for month in range(1,13): total_proposed_increase += proposed_monthly_increase hc = budget.loc[budget['recruitment_status'].isin(['On-Board', 'Contracted'])]['total_cost'].sum() budgeted_headcount_cost_2018[months[month]] = pd.Series(hc + total_proposed_increase) for month in range(1,13): hc = positions.loc[(positions['end_date'] >= datetime(year, month, 1)) & (positions['start_date'] <= datetime(year, month, monthrange(year, month)[1])) | (positions['end_date'].isnull()) & (positions['start_date'] <= datetime(year, month, monthrange(year,month)[1]))]['FTE'].sum() headcount_2018[months[month]] = pd.Series(hc) for month in range(1,13): hc = positions.loc[(positions['end_date'] >= datetime(year, month, 1)) & (positions['start_date'] <= datetime(year, month, monthrange(year, month)[1])) | (positions['end_date'].isnull()) & (positions['start_date'] <= datetime(year, month, monthrange(year,month)[1]))]['total_cost'].sum() headcount_cost_2018[months[month]] = pd.Series(hc) return { 'positions': headcount_2018.to_json(), 'budget': budgeted_headcount_2018.to_json(), 'positions_cost': headcount_cost_2018.to_json(), 'budget_cost': budgeted_headcount_cost_2018.to_json() }