Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
0
def add_password_policy():
    if not request.json:
        return abort(400)

    db_fields = set(col.name for col in PasswordPolicy.__mapper__.columns)

    extra_keys = set(request.json.keys()) - (db_fields - {'id'})
    if extra_keys:
        return jsonify({
            'Message':
            'Extra Keys : %r' % ', '.join(col for col in extra_keys)
        })

    try:
        _bool_mapper = lambda s: 0 if s == 'false' else 1
        _json = {
            key: _bool_mapper(val) if val in ('true', 'false') else val
            for key, val in request.json.items()
        }
        db_session.query(PasswordPolicy).filter(
            PasswordPolicy.id == 1).update(_json)
        db_session.commit()
    except NoResultFound as e:
        abort(404)
    except Exception as e:
        abort(500)
    else:
        return record_updated_envelop(request.json)
Exemplo n.º 6
0
def update_emp_relativetype(id):
    if not request.json:
        abort(400)
    #check to see if there is any empty values
    if not all(
            len(str(val).strip()) >= 1
            for val in request.json.values() if isinstance(val, str)):
        abort(411)
    #check to see if the request has the right type of keys

    #clearn up the values for string
    #generator expression
    cleaned_json = dict(
        (key, val.strip()) if isinstance(val, str) else (key, val)
        for key, val in request.json.items())
    if 'name' in cleaned_json:
        cleaned_json['display_name'] = cleaned_json['name']
        cleaned_json['name'] = cleaned_json['name'].strip().lower()

    #try to executre

    try:
        db_session.query(EmployeeRelativeType).filter(
            EmployeeRelativeType.id == id).update(cleaned_json)
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_updated_envelop(request.json)
Exemplo n.º 7
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)
Exemplo n.º 8
0
def update_agencytype(id):
    if not request.json:
        abort(400)
    #check to see if ther eis empty field
    if not all(len(val.strip()) >= 1 for val in request.json.values()\
                                            if isinstance(val, str)):
        return length_require_envelop()

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

    if 'code' in request.json:
        request.json['code'] = request.json['code'].strip()

    try:
        db_session.query(AgencyType).filter(AgencyType.id == id).update(
            request.json)
        db_session.commit()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_updated_envelop(request.json)
Exemplo n.º 9
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)
Exemplo n.º 10
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')
Exemplo n.º 11
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)
Exemplo n.º 12
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)
Exemplo n.º 13
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'].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:

        abort(500)

    else:
        return record_updated_envelop(request.json)
Exemplo n.º 14
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)
Exemplo n.º 15
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) 
Exemplo n.º 16
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)
Exemplo n.º 17
0
def update_rank(id):
    if not request.json:
        abort(400)
    
    if 'name' in request.json:
        request.json['display_name'] = request.json['name']
        request.json['name'] = request.json['name'].lower().strip()
    
    #now try to update the facilty name
    

    try:
        db_session.query(EmployeeCategoryRank).filter(EmployeeCategoryRank.id==id).update(request.json)
        
        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)
Exemplo n.º 18
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)
Exemplo n.º 19
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)
Exemplo n.º 20
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)
Exemplo n.º 21
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()
        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)
Exemplo n.º 22
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)
Exemplo n.º 23
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)
Exemplo n.º 24
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)
Exemplo n.º 25
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)
Exemplo n.º 26
0
def delete_facility_type(f_id):
    if not request.json:
        abort(401)

    try:
        db_session.query(FacilityType).filter(FacilityType.id == f_id).update(
            {'del_flag': True})
        db_session.commit()
    except NoResultFound as e:
        return record_notfound_envelop()
    else:
        return record_deleted_envelop()
Exemplo n.º 27
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)
Exemplo n.º 28
0
def update_institute(id):
    if not request.json:
        abort(400)
    try:
        db_session.query(Institute).filter(Institute.id == id).update(
            request.json)
        db_session.commit()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_updated_envelop(request.json)
Exemplo n.º 29
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)
Exemplo n.º 30
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)