Esempio n. 1
0
def create_certification_by_emp(id):
    if not request.json:
        abort(400)
    #check if there is empty field comming up

    cert = {
        key: val.strip() if isinstance(val, str) else val
        for key, val in request.json.items()
    }
    #insert
    cert['employee_id'] = id
    ###################################
    #cert['expiry_date'] = '04-mar-2012'
    #cert['last_renewal_date'] = '03-mar-2017'
    ################################
    try:

        db_session.add(Certification(**cert))
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 2
0
def create_employee_type():

    if not request.json:
        abort(400)

    if not 'name' in request.json.keys():
        abort(401)

    if len(request.json['name'].strip()) < 2:
        abort(411)

    #clear up the values
    display_name = request.json['name'].strip()
    name = display_name.lower()

    try:
        e_type = EmployeeType(name=name, display_name=display_name)
        db_session.add(e_type)
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 3
0
def create_llg():
    print('This function was called')
    if not set(request.json.keys()) == {'name', 'llg_code', 'district_id'}:
        return jsonify({'message': 'missing keys'})

    if not len(request.json['name']) > 3:
        return jsonify({'message': 'not adequate length'})

    #lower case the facility name
    display_name = request.json['name'].strip()
    name = request.json['name'].replace(' ', '').lower().strip()
    llg_code = request.json['llg_code']
    district_id = request.json['district_id']
    #insert into the database
    try:
        dis = LLG(name=name,
                  display_name=display_name,
                  llg_code=llg_code,
                  district_id=district_id)
        db_session.add(dis)
        db_session.commit()
    except IntegrityError as ie:
        return record_exists_envelop()

    else:
        return record_created_envelop(request.json)
Esempio n. 4
0
def create_district():
    if not set(
            request.json.keys()) == {'name', 'district_code', 'province_id'}:
        return jsonify({'message': 'missing keys'})

    #lower case the facility name
    display_name = request.json['name'].strip()
    name = request.json['name'].replace(' ', '').lower().strip()
    district_code = request.json['district_code']
    province_id = request.json['province_id']

    #insert into the database
    try:
        dis = District(name=name,
                       display_name=display_name,
                       district_code=district_code,
                       province_id=province_id)
        db_session.add(dis)
        db_session.commit()
    except IntegrityError as ie:
        raise
        return record_exists_envelop()

    else:
        return record_created_envelop(request.json)
Esempio n. 5
0
def update_emp_category(id):
    if not request.json:
        abort(400)

    name = request.json.get('name', None)
    emp_cat_rank_id = request.json.get('emp_cat_rank_id', None)

    #now try to update the facilty name
    if name is not None:
        name = request.json['name'].lower().strip()
        display_name = request.json['name'].strip()

    try:
        cat = db_session.query(EmployeeCategory).filter(
            EmployeeCategory.id == id).one()
        if name is not None:
            cat.name = name
            cat.display_name = display_name
        if emp_cat_rank_id is not None:
            cat.emp_cat_rank_id = emp_cat_rank_id
        db_session.add(cat)
        db_session.commit()
    except NoResultFound as e:
        abort(404)
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:

        abort(500)
    else:
        return record_updated_envelop(request.json)
Esempio n. 6
0
def update_rank(id):
    if not request.json:
        abort(400)

    if 'name' not in request.json.keys():
        abort(401)

    #now try to update the facilty name
    name = request.json['name'].replace(' ', '').lower().strip()
    display_name = request.json['name'].strip()

    try:
        rank = db_session.query(EmployeeCategoryRank).filter(
            EmployeeCategoryRank.id == id).one()
        rank.name = name
        rank.display_name = display_name
        db_session.add(rank)
        db_session.commit()
    except NoResultFound as e:
        abort(404)
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        raise
        abort(500)

    else:
        return record_updated_envelop(request.json)
Esempio n. 7
0
def create_province():
    if not request.json:
        abort(400)
    if not set(request.json.keys()) == {'name', 'province_code', 'region_id'}:
        return jsonify({'message': 'missing keys'})

    #lower case the facility name
    display_name = request.json['name'].strip()
    name = request.json['name'].replace(' ', '').lower().strip()
    province_code = request.json['province_code'].upper().strip()
    region_id = request.json['region_id']
    #insert into the database
    try:
        dis = Province(name=name,
                       display_name=display_name,
                       province_code=province_code,
                       region_id=region_id)
        db_session.add(dis)
        db_session.commit()
    except IntegrityError as ie:
        return record_exists_envelop()
    except Exception as e:
        print(e)
        return fatal_error_envelop()

    else:
        return record_created_envelop(request.json)
Esempio n. 8
0
def create_emp_cat_ranks():
    if not request.json:
        abort(400)
    if not 'name' in request.json.keys():
        abort(401)
    
    if len(request.json['name'].strip()) < 2:
        abort(411)
    

    #if everything is fine
    name = request.json['name'].replace(' ', '').lower().strip()
    display_name = request.json['name'].strip()

    #put to db
    try:
        rank = EmployeeCategoryRank(name=name, display_name=display_name)
        db_session.add(rank)
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 9
0
def create_division_types():
    if not request.json:
        abort(400)
    #empty check
    if not all(
            len(val.strip()) >= 1
            for val in request.json.values() if isinstance(val, unicode)):
        return length_require_envelop()

    _result = {'name'} - set(request.json.keys())
    if _result:
        return keys_require_envelop('key : name is required')

    request.json['display_name'] = request.json['name'].strip()
    request.json['name'] = request.json['name'].strip().lower()

    try:
        db_session.add(DivisionTypeSetup(**request.json))
        db_session.commit()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 10
0
def create_certification_by_emp(id):
    if not request.json:
        abort(400)
    #check if there is empty field comming up
    if not all(
            len(str(val).strip()) >= 1 for key, val in request.json.items()):
        abort(411)

    #check if there is no registration number and registration body
    result = {'regulatory_body', 'registration_number'} - set(
        request.json.keys())
    if result:
        return keys_require_envelop(
            '"regulatory_body" and "regstration_number" is required')
    #clean up the values
    cert = {
        key: val.strip() if isinstance(val, str) else val
        for key, val in request.json.items()
    }
    #insert
    cert['employee_id'] = id
    ###################################
    cert['expiry_date'] = '04-mar-2012'
    cert['last_renewal_date'] = '03-mar-2017'
    ################################
    try:

        db_session.add(Certification(**cert))
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 11
0
def create_training_by_emp(id):
    if not request.json:
        abort(400)
    #check if there is empty field comming up
    if not all(
            len(str(val).strip()) >= 1 for key, val in request.json.items()):
        abort(411)

    #check if there is no registration number and registration body
    result = {'name'} - set(request.json.keys())
    if result:
        return keys_require_envelop('key : "name" is required')
    #clean up the values
    trs = {
        key: val.strip() if isinstance(val, str) else val
        for key, val in request.json.items()
    }
    #insert
    ###################################
    trs['start_date'] = '01-feb-2012'
    trs['end_date'] = '03-feb-2005'
    ##########################
    print(id)
    trs['employee_id'] = id
    try:
        print(id)
        db_session.add(Training(**trs))
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 12
0
def update_region(id):
    if not request.json:
        abort(400)

    if 'name' not in request.json.keys():
        abort(401)

    #now try to update the facilty name
    name = request.json['name'].replace(' ', '').lower().strip()
    display_name = request.json['name'].strip()

    try:
        facility = db_session.query(Region).filter(Region.id == id).one()
        facility.name = name
        facility.display_name = display_name
        db_session.add(facility)
        db_session.commit()
    except NoResultFound as e:
        abort(404)
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:

        abort(500)
    else:
        return record_updated_envelop(request.json)
Esempio n. 13
0
def create_qualification_by_emp(id):
    if not request.json:
        abort(400)
    #check if there is empty field comming up
    if not all(
            len(str(val).strip()) >= 1 for key, val in request.json.items()):
        abort(411)
    #clean up the values
    qual = {
        key: val.strip() if isinstance(val, str) else val
        for key, val in request.json.items()
    }
    #insert
    print(qual)
    qual['employee_id'] = id
    #######################
    qual['start_date'] = '03-jan-2018'
    qual['end_date'] = '05-jan-2020'
    ###############
    try:
        print(id)
        db_session.add(Qualification(**qual))
        db_session.commit()
    except IntegrityError as e:
        return fatal_error_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 14
0
def update_emp_type(id):
    if not request.json:
        abort(400)

    name = request.json.get('name', None)

    #now try to update the facilty name
    if name is not None:
        name = request.json['name'].lower().strip().replace(' ', '')
        display_name = request.json['name'].strip()

    try:
        typ = db_session.query(EmployeeType).filter(
            EmployeeType.id == id).one()
        if name is not None:
            typ.name = name
            typ.display_name = display_name

        db_session.add(typ)
        db_session.commit()
    except NoResultFound as e:
        abort(404)
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:

        abort(500)
    else:
        return record_updated_envelop(request.json)
Esempio n. 15
0
def create_training_by_emp(id):
    if not request.json:
        abort(400)
    #check if there is empty field comming up

    #check if there is no registration number and registration body

    trs = {
        key: val.strip() if isinstance(val, str) else val
        for key, val in request.json.items()
    }
    #insert
    ###################################
    #trs['start_date'] = '01-feb-2012'
    #trs['end_date'] = '03-feb-2005'
    ##########################
    print(id)
    trs['employee_id'] = id
    try:

        db_session.add(Training(**trs))
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 16
0
def create_emp_cat(rank_id):
    if not request.json:
        abort(400)
    if not 'name' in request.json.keys():
        abort(401)

    if len(request.json['name'].strip()) < 2:
        abort(411)

    #strip down the values
    display_name = request.json['name'].strip()
    name = display_name.lower()
    emp_cat_rank_id = rank_id

    #try to put onto database
    try:
        cat = EmployeeCategory(name=name,
                               display_name=display_name,
                               emp_cat_rank_id=emp_cat_rank_id)
        db_session.add(cat)
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 17
0
def add_company_detail():

    if not set(request.json.keys()) == {
            'name', 'currency_symbol', 'is_prefix', 'country', 'description'
    }:
        return missing_keys_envelop()
    if len(request.json['name']) < 4 or len(
            request.json['country']) < 3 or len(
                request.json['currency_symbol']) < 1:
        return length_require_envelop()

    #now shape up the fields
    name = request.json['name'].strip()
    currency_symbol = request.json['currency_symbol'].lower().strip()
    is_prefix = request.json['is_prefix']
    country = request.json['country'].strip()
    des = request.json['description'].strip()

    company = CompanyDetail(name=name,
                            currency_symbol=currency_symbol,
                            is_prefix=is_prefix,
                            country=country,
                            description=des)

    try:
        db_session.add(company)
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 18
0
def create_employee_extra(id):
    #cheek to see if ther is json
    if not request.json:
        abort(400)
    #if there is json,  if there are required fields
    required_keys = set(col.name for col in EmployeeExtra.__mapper__.columns) - {'id', 'employee_id'}
    
    
    #keys from the rquest object
    result = request.json.keys() - required_keys
    
    #if there is extra remaining keys , abort
    if result:
        return extra_keys_envelop('keys not accepeted %r' % result)
    
    #check to see if there are any valuekjdhflkjadsfaklsdhfkajsds less than 1
    if not all(len(str(val).strip())>=1 for val in request.json.values()):
        return length_require_envelop()
    #clearn up the dictionary
    json_vals = {key : val.strip() if isinstance(val, str) else val for key, val in request.json.items()}
    
    #try to insert to database
    try:
        db_session.add(EmployeeExtra(**json_vals, employee_id=id))
        db_session.commit()
    except IntegrityError as e:
        
        return record_exists_envelop()
    except Exception as e:
        raise e
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json) 
Esempio n. 19
0
def create_division(t_id):
    if not request.json:
        abort(400)
    if not all(
            len(val.strip()) >= 1
            for val in request.json.values() if isinstance(val, unicode)):
        return length_require_envelop()

    _result = {'name', 'code'} - set(request.json.keys())
    if _result:
        return keys_require_envelop('Key: %r is require' % ', '.join(_result))

    request.json['display_name'] = request.json['name'].strip()
    request.json['name'] = request.json['name'].strip().lower()

    #inject the id
    request.json['division_type_id'] = t_id
    try:
        db_session.add(Division(**request.json))
        db_session.commit()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 20
0
def create_emp_position():
    if not request.json:
        abort(401)
    required_fields = set(col.name for col in EmployeePosition.__mapper__.columns) - {'id', 'emp_pos_title_display_name', 'del_flag'}
    
    extra_fields = set(request.json) - required_fields
    if extra_fields:
        return extra_keys_envelop('Unknown Keys : %s' % ', '.join(key for key in extra_fields))
    
    _required = required_fields - set(request.json)

    if _required:
       return missing_keys_envelop('Required Keys : %s' % ', '.join(key for key in _required))
    
    #check to see if there is any blank 

    if not all(len(val.strip()) >= 1 for key, val in request.json.items() if isinstance(val, str)):
        return length_require_envelop()
    #inject 
    
    request.json['emp_pos_title_display_name'] = request.json['emp_pos_title']
    request.json['emp_pos_title'] = request.json['emp_pos_title'].lower().strip()
    #initiate the dession
    try:
        e_type = EmployeePosition(**request.json)
        db_session.add(e_type)
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 21
0
def update_user_password(u_id):
    if not request.json:
        abort(400)
    if 'password' not in request.json.keys():
        return missing_keys_envelop()
    try:
        user = db_session.query(User).filter(User.id == u_id).one()
        if user is None:
            return record_notfound_envelop()
        hashed_pass = hash_password(request.json['password'].encode())
        old_hashed_pass = user.password
        if old_hashed_pass == hashed_pass:
            return jsonify({
                'message': 'Please dont\'t use old password',
                'status': 'fail'
            })
        else:
            user.password = hashed_pass
            db_session.add(user)
            db_session.commit()

    except NoResultFound as e:
        return record_notfound_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_updated_envelop('Password updated Successfully')
Esempio n. 22
0
def create_roles():
    '''This method will create a role and assign diffenet permissions'''
    try:
         role = Role(**request.json)
         db_session.add(role)
         db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 23
0
def create_institute():
    if not request.json:
        abort(400)

    try:
        db_session.add(Institute(**request.json))
        db_session.commit()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 24
0
def create_organizer_names():
    if not request.json:
        abort(400)

    try:
        db_session.add(OrganizerName(**request.json))
        db_session.commit()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 25
0
def create_region():
    if not request.json:
        abort(400)

    if 'name' in request.json:
        request.json['display_name'] = request.json['name'].strip()
        request.json['name'] = request.json['name'].lower().strip()
    try:
        dis = Region(**request.json)
        db_session.add(dis)
        db_session.commit()
    except IntegrityError as ie:
        return record_exists_envelop()

    else:
        return record_created_envelop(request.json)
Esempio n. 26
0
def create_roles():
    '''This method will create a role and assign diffenet permissions'''
    try:
        json_request = dict(copy.deepcopy(request.json))
        json_request['role_type_display_name'] = request.json.get('role_type')
        role = Role(**json_request)
        db_session.add(role)
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        raise
        return fatal_error_envelop()
    else:

        return record_created_envelop(request.json)
Esempio n. 27
0
def create_employee():

    if not request.json:
        abort(400)

    req_fields = {
        'first_name', 'last_name', 'current_sex', 'address_one', 'age',
        'retirement_age', 'employee_type_id', 'employee_category_id',
        'date_of_birth', 'employee_position_id', 'employement_number',
        'employee_agency_id'
    }
    result = req_fields - set(request.json.keys())

    #if there is some value then abort
    if result:

        return keys_require_envelop('Key required %s' % ', '.join(result))

    #if everything is included, check to see if there is any empty values
    # if not all(len(str(val).strip()) >= 1 for key, val in request.json.items()):
    #     abort(411)

    #now clean up the data to insert into database(onyl for strin)
    data = {
        key: val.strip() if isinstance(val, str) else val
        for key, val in request.json.items()
    }

    #for oracle
    #data['date_of_birth'] = '01-feb-2017'
    #data['date_of_commencement'] = '02-feb-2011'
    #now try to insert
    #for raocle

    try:

        emp = Employee(**data)
        db_session.add(emp)
        db_session.commit()
    except IntegrityError as e:

        return record_exists_envelop()
    except Exception as e:
        print(e)
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 28
0
def update_empextra_by_emp(emp_id):
    if not request.json:
        abort(400)
    #check to see if there is any empty values
    if not all(len(str(val).strip()) for val in request.json.values()):
        abort(411)
    #check to see if the request has the right type of keys
    result = set(request.json.keys()) - set(
        col.name for col in EmployeeExtra.__mapper__.columns)
    if result:

        return extra_keys_envelop('Keys: {!r} not accepted'.format(', '.join(
            r for r in result)))

    #clearn up the values for string
    #generator expression
    cleaned_json = ((key, val.strip()) if isinstance(val, str) else (key, val)
                    for key, val in request.json.items())
    #this means that it has extra set of keys that is not necessary
    #make the custom query

    cleaned_json = dict(cleaned_json)
    cleaned_json['employee_id'] = emp_id

    #try to executre

    try:

        nr = db_session.query(EmployeeExtra).filter(
            EmployeeExtra.employee_id == emp_id).update(dict(cleaned_json))
        print(nr)
        if nr == 0:
            #that means there is no data for that employee so add the data
            db_session.add(EmployeeExtra(**cleaned_json))
            #db_session.commit()

        db_session.commit()
    except NoResultFound as e:

        return record_notfound_envelop()
    except IntegrityError as e:
        raise
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_updated_envelop(request.json)
Esempio n. 29
0
def create_emphistory_by_emp(id):
    if not request.json:
        abort(400)
    if not all(
            len(val.strip()) >= 1
            for val in request.json.values() if isinstance(val, str)):
        abort(411)
    request.json['employee_id'] = id
    try:
        db_session.add(EmployementHistory(**request.json))
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Esempio n. 30
0
def create_langauge():
    if not request.json:
        abort(400)
    if not all(
            len(val.strip()) >= 1
            for val in request.json.values() if isinstance(val, unicode)):
        return length_require_envelop()

    try:
        db_session.add(Language(**request.json))
        db_session.commit()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)