Exemple #1
0
def upload_budget():
    import_type = request.form['importType']
    delimeter = request.form['delimeter']
    date_format = request.form['dateFormat']
    day_first = True if date_format == 'dd/mm/yyy' else False
    file = request.files['file']
    df = pd.read_csv(
        file,
        sep='{}'.format(delimeter),
        keep_default_na=False,
        parse_dates=True,
        dayfirst=day_first,
        encoding='utf-8') if file and allowed_file(file.filename) else None
    df['Start Date'] = pd.to_datetime(df['Start Date'],
                                      errors='coerce').dt.date.astype(object)
    df['End Date'] = pd.to_datetime(df['End Date'],
                                    errors='coerce').dt.date.astype(object)
    errors = []

    if not import_type:
        errors.append('Name is required.')
    if not file:
        errors.append('File is required.')

    if errors:
        [flash(error, category='upload') for error in errors]
        # return flashed messages
        return jsonify(status='error')
    else:
        if is_upload_valid:
            for index, row in df.iterrows():
                db = get_db()

                # testing exception rollback
                with db:
                    db.execute(
                        'REPLACE INTO position'
                        '('
                        'status_id, recruitment_status_id, number, pillar_id, company_id, department_id,'
                        'function_id, isBudget, title, functional_reporting_line, disciplinary_reporting_line, holder,'
                        'hours, start_date, end_date, salary, social_security_contribution,'
                        'fringe_benefit, performance_bonus, super_bonus, management_bonus'
                        ')'
                        ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
                        (get_position_status_by_name(row['Status'])['id'],
                         get_recruitment_status_by_name(
                             row['Recruitment Status'])['id'], row['Number'],
                         get_pillar_by_name(row['Pillar'])['id'],
                         get_company_by_name(row['Company'])['id'],
                         get_department_by_name(row['Department'])['id'],
                         get_function_by_name(row['Function'])['id'], 1,
                         row['Title'], row['Functional Reporting Line'],
                         row['Disciplinary Reporting Line'], row['Holder'],
                         row['Hours'], row['Start Date'], row['End Date'],
                         row['Salary'], row['Social Security Contribution'],
                         row['Fringe Benefit'], row['Performance Bonus'],
                         row['Super Bonus'], row['Management Bonus']))

            return jsonify(status='ok')
Exemple #2
0
def update_position(id):
    position = get_position_by_id(id)
    pillars = get_pillars()
    companies = [(company['id'], company['name'])
                 for company in get_companies()]
    departments = get_departments()
    functions = get_functions()
    position_statuses = get_position_statuses()
    recruitment_statuses = get_recruitment_statuses()

    data = {
        "position": dict(position),
        "pillars": dict(pillars),
        "companies": dict(companies),
        "departments": dict(departments),
        "functions": dict(functions),
        "positionStatuses": dict(position_statuses),
        "recruitmentStatuses": dict(recruitment_statuses)
    }

    if request.method == 'POST':
        status_id = get_position_status_by_name(
            request.form['positionStatuses'])['id']
        recruitment_status_id = get_recruitment_status_by_name(
            request.form['recruitmentStatuses'])['id']
        number = request.form['number']
        pillar_id = get_pillar_by_name(request.form['pillars'])['id']
        company_id = get_company_by_name(request.form['companies'])['id']
        department_id = get_department_by_name(
            request.form['departments'])['id']
        function_id = get_function_by_name(request.form['functions'])['id']
        title = request.form['title']
        functional_reporting_line = request.form['functionalReportingLine']
        disciplinary_reporting_line = request.form['disciplinaryReportingLine']
        holder = request.form['holder']
        hours = request.form['hours']
        start_date = request.form['startDate']
        end_date = request.form['endDate']
        salary = request.form['salary']
        social_security_contribution = request.form[
            'socialSecurityContribution']
        fringe_benefit = request.form['fringeBenefit']
        performance_bonus = request.form['performanceBonus']
        super_bonus = request.form['superBonus']
        management_bonus = request.form['managementBonus']
        user_ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
        user_agent = request.headers.get('User-Agent')
        domain = request.headers['Host']
        errors = []

        if not status_id:
            errors.append('Status is required.')

        if not recruitment_status_id:
            errors.append('Recruitment Status required.')

        if not number:
            errors.append('Number is required.')

        if not pillar_id:
            errors.append('Pillar is required.')

        if not department_id:
            errors.append('Department Status required.')

        if not function_id:
            errors.append('Function is required.')

        if not title:
            errors.append('Title is required.')

        if not functional_reporting_line:
            errors.append('Functional  is required.')

        if not disciplinary_reporting_line:
            errors.append('Disciplinary Reporting Line is required.')

        if errors:
            [flash([error, id], category='update') for error in errors]
            return jsonify(status='error')
        else:
            db = get_db()
            db.execute(
                'UPDATE position set status_id = ?, recruitment_status_id = ?, number = ?, pillar_id = ?, company_id = ?, department_id = ?,'
                'function_id = ?, title = ?, functional_reporting_line = ?, disciplinary_reporting_line = ?, holder = ?,'
                'hours = ?, start_date = ?, end_date = ?, salary = ?, social_security_contribution = ?,'
                'fringe_benefit = ?, performance_bonus = ?, super_bonus = ?, management_bonus = ?'
                'WHERE number = ?',
                (status_id, recruitment_status_id, number, pillar_id,
                 company_id, department_id, function_id, title,
                 functional_reporting_line, disciplinary_reporting_line,
                 holder, hours, start_date, end_date, salary,
                 social_security_contribution, fringe_benefit,
                 performance_bonus, super_bonus, management_bonus))

            db.commit()

            # create audit trail in db
            create_audit_log(user_agent,
                             user_ip,
                             domain,
                             action='Successful position update',
                             table='POSITION',
                             function='UPDATE',
                             user_id=g.user['id'])

            return jsonify(status='ok')

    return jsonify(data)