예제 #1
0
def manipulate_plan(conn, cursor, fmr, plan_fmt):

    if plan_fmt['plan_name'] != fmr.plan_name:
        if is_duplicate_plan(mysql, fmr.plan_name):
            return short_error(err_list=['This plan is already exists'])

    if plan_fmt['is_active']:
        return short_error(err_list=['This plan is currently active'])

    if is_expired_plan(mysql, fmr.plan_name):
        return short_error(err_list=['This plan is expired and is read-only.'])

    p = Plan(cursor, conn=conn)
    p.update(fmr, plan_fmt['id'])

    q_del = """DELETE FROM employee_plan WHERE ep_employee_FK = %s AND ep_plan_FK = %s"""
    q_ins = """INSERT INTO employee_plan(ep_employee_FK, ep_plan_FK) VALUES (%s, %s)"""
    uq = get_unique_employees(plan_fmt['employees_list'], fmr.employees_list)

    if uq['operation'] == OperationType.DELETE_EMPLOYEE:
        [
            cursor.execute(q_del, (record['id'], plan_fmt['id']))
            for record in uq['diff_list']
        ]
    else:
        [
            cursor.execute(q_ins, (record['id'], plan_fmt['id']))
            for record in uq['diff_list']
        ]

    conn.commit()
    conn.close()
    return short_success(ManipulationType.UPDATED)
예제 #2
0
def create_plan_execution(conn, cursor, fmr: Forminator):
    if is_duplicate_plan(mysql, fmr.plan_name):
        return short_error(err_list=[
            'This plan already exists. Go to Manage Plan to learn more.'
        ])

    if fmr.disbursement_type == fmr.raw_form.DISB_ALL:
        employees = find_all_employees(cursor, session['manager_dept'])

        if employees is None:
            return short_error(
                err_list=['No employees were found in the department'])
        fmr.employees_list = employees['data']

    p = Plan(cursor, conn=conn)
    p.insert(fmr)
    conn.close()

    execute_orders()
    return short_success(ManipulationType.CREATED)
예제 #3
0
def create_plan():
    form = create_plan_form(session)

    if request.method == 'GET':
        return render_template('plans/create_plan/create_plan_partial.html',
                               form=form,
                               current_date=custom_strftime(
                                   SupportedTimeFormats.FMT_SIDE_UI,
                                   datetime.now()))
    else:
        if form.validate_on_submit():
            conn = mysql.connect()
            cursor = conn.cursor()
            return create_plan_execution(conn, cursor, Forminator(form))
        else:
            return short_error(form)
예제 #4
0
파일: routes.py 프로젝트: yijianma007/meet
def department_size():
    conn = mysql.connect()
    cursor = conn.cursor()

    dept_value = request.args.get('department')

    if not dept_value.strip():
        return short_error(err_list=[UNKNOWN_ERROR])

    q = """SELECT count(employee_dept_FK) as length 
    FROM employee e 
    JOIN department_lookup dl on e.employee_dept_FK = dl.token
    WHERE dl.department = %s;"""

    cursor.execute(q, dept_value)
    return jsonify(size=cursor.fetchall()[0][
        0]  # This will always return something, even 0
                   )
예제 #5
0
파일: routes.py 프로젝트: yijianma007/meet
def delete_plan():
    conn = mysql.connect()
    cursor = conn.cursor()

    q = """DELETE FROM employee_plan WHERE ep_plan_FK = %s"""
    q2 = """DELETE FROM plan WHERE id = %s"""

    try:
        cursor.execute(q, session['MANAGE_FORM']['id'])
        cursor.execute(q2, session['MANAGE_FORM']['id'])
        conn.commit()
        conn.close()
        return short_success(manip_type=ManipulationType.DELETED)

    except Exception:
        # An exception here shouldn't really occur, so log it
        conn.close()
        return short_error(err_list=[UNKNOWN_ERROR])
예제 #6
0
def manage_plan():

    form = get_plan_form(session)

    if request.method == 'GET':
        return render_template('plans/manage_plan/manage_plan_partial.html',
                               form=form,
                               current_date=custom_strftime(
                                   SupportedTimeFormats.FMT_SIDE_UI,
                                   datetime.now()))
    else:
        plan_fmt = session.get('MANAGE_FORM')

        if plan_fmt is None:
            return jsonify(response="A search query must be made to update it")

        form = get_plan_form(session)

        if form.validate_on_submit():
            conn = mysql.connect()
            cursor = conn.cursor()
            return manipulate_plan(conn, cursor, Forminator(form), plan_fmt)
        else:
            return short_error(form=form)
예제 #7
0
파일: routes.py 프로젝트: yijianma007/meet
def manage_plan():
    print('entering route')
    conn = mysql.connect()
    cursor = conn.cursor()

    search_query = request.args.get('value')
    timezone = request.args.get('tz')

    if search_query is None or timezone is None:
        return short_error(err_list=[UNKNOWN_ERROR])

    search_query = Forminator.scrub_plan_name(search_query)

    cursor.execute('SELECT * FROM PLAN where plan_name = %s', search_query)
    plan_data = cursor.fetchall()

    if len(plan_data) == 0:
        return short_error(
            err_list=['A plan with this name could not be found.'])

    session['CURRENT_PLAN_EDIT'] = plan_data[0][0]

    headers = [desc[0] for desc in cursor.description]
    plan = zip(headers, plan_data[0])
    plan_fmt = {table_headers: val for (table_headers, val) in plan}

    cursor.execute('SELECT department FROM department_lookup WHERE id = %s',
                   plan_fmt['dest_fund_FK'])

    plan_fmt['dest_fund_FK'] = cursor.fetchall()[0][0]
    plan_fmt['funding_amount'] = float("{:.2f}".format(
        float(plan_fmt['funding_amount'])))
    plan_fmt['start_date'] = convert_time_zone(plan_fmt['start_date'],
                                               timezone)

    if plan_fmt['amount_limit'] is not None:
        plan_fmt['amount_limit'] = float("{:.2f}".format(
            float(plan_fmt['amount_limit'])))

    if plan_fmt['end_date'] is not None:
        plan_fmt['end_date'] = convert_time_zone(plan_fmt['end_date'],
                                                 timezone)

    plan_fmt['fund_individuals'] = True if int.from_bytes(
        plan_fmt['fund_individuals'], 'big') else False
    plan_fmt['fund_all_employees'] = True if int.from_bytes(
        plan_fmt['fund_all_employees'], 'big') else False
    plan_fmt['is_expired'] = is_expired_plan(mysql, plan_fmt['plan_name'])
    plan_fmt['is_active'] = is_active_plan(mysql, plan_fmt['plan_name'])

    employees_list = []
    if plan_fmt['fund_individuals']:
        get_employees_query = """
        SELECT id, CONCAT(first_name, ' ', last_name) AS name FROM employee 
        JOIN employee_plan ep on employee.id = ep.ep_employee_FK WHERE ep_plan_FK = %s
        """
        cursor.execute(get_employees_query, session['CURRENT_PLAN_EDIT'])
        cf = cursor.fetchall()

        employees_list = [{
            "id": employee[0],
            "name": employee[1]
        } for employee in cf]

    plan_fmt['employees_list'] = employees_list

    session['MANAGE_FORM'] = plan_fmt
    conn.close()

    return jsonify(
        response_status="success",
        status={
            "active": {
                "info":
                None if not plan_fmt['is_active'] else render_template(
                    "plans/plan_info_partial.html",
                    info_message="This plan is already "
                    "active "
                    "and cannot be modified. "
                    "It "
                    "can only "
                    "be deleted.")
            },
            "expired": {
                "info":
                None if not plan_fmt['is_expired'] else short_error(
                    err_list=['This plan is expired and is'
                              'read-only'])
            }
        },
        response={
            "plan_name":
            plan_fmt['plan_name'],
            "funding_amount":
            plan_fmt['funding_amount'],
            "justification":
            plan_fmt['plan_justification'],
            "memo":
            plan_fmt['memo'],
            "start_date":
            plan_fmt['start_date'],
            "dest_fund":
            plan_fmt['dest_fund_FK'],
            "has_employee_specific":
            True if plan_fmt['fund_individuals'] else False,
            "has_all_employees":
            True if plan_fmt['fund_all_employees'] else False,
            "employees_list":
            plan_fmt['employees_list'],
            "has_end_date":
            True if plan_fmt['end_date'] is not None else False,
            "end_date":
            plan_fmt['end_date'],
            "has_velocity_control":
            True if plan_fmt['control_name'] is not None else False,
            "control_name":
            plan_fmt['control_name'],
            "control_window":
            plan_fmt['control_window'],
            "amount_limit":
            plan_fmt['amount_limit'],
            "usage_limit":
            plan_fmt['usage_limit'],
            "priority":
            plan_fmt['priority']
        })