Example #1
0
def update_user(u_id):
    if not request.json:
        abort(400)
    if not request.args.get('action') == 'update_role':

        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.')

    #update the role

    if 'role_id' not in request.json:
        return missing_keys_envelop()
    try:
        user = db_session.query(User).filter(User.id == u_id).one()
        if user is None:
            return record_notfound_envelop()
        user.role_id = int(request.json['role_id'])
        db_session.add(user)
        db_session.commit()
    except NoResultFound as e:
        return record_notfound_envelop()
    except Exception as e:
        raise
        return fatal_error_envelop()

    else:
        return record_updated_envelop('Role updated successfully.')
Example #2
0
def update_employee(id):
    '''This i iwill user the raw sql query because this would be easier to reason about'''

    if not request.json:
        abort(400)
    
    if not all(len(str(val).strip()) >= 1 for key, val in request.json.items()):
        abort(411)

    if 'age' not in request.json or 'retirement_age' not in request.json:
        return jsonify({'message' : 'please send both the age and retirement_age'})
        abort(400)
    age = request.json['age']
    retirement_age = request.json['retirement_age']
    #first check about the age
    if int(age) > int(retirement_age) or int(age) < 18:
        return record_not_updated_env('Age cannot be more than retirement age or less than 18')
    
    #clean up the data
    data = {key : val.strip() if isinstance(val, str) else val for key, val in request.json.items()}

    #prepate the sql query
    inner = ', '.join('{} = {!r}'.format(key, val) for key, val in data.items())
    query = '''UPDATE employees SET {}, updated_at=Now() where id={}'''.format(inner, id)

    #try to executre
    with engine.connect() as con:
        try:
            con.execute(query)
        except IntegrityError as e:
            return record_exists_envelop()
        except Exception as e:
            return fatal_error_envelop()
        else:
            return record_updated_envelop(request.json)
Example #3
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)
Example #4
0
def update_empextra_by_emp(emp_id, ex_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
    inner = ', '.join('{:s} = {!r}'.format(key, val)
                      for key, val in cleaned_json)
    query = '''UPDATE employee_extra SET {:s} WHERE id = {:d}'''.format(
        inner, ex_id)

    #try to executre
    with engine.connect() as con:
        try:
            con.execute(query)
        except IntegrityError as e:
            return record_exists_envelop()
        except Exception as e:
            return fatal_error_envelop()
        else:
            return record_updated_envelop(request.json)
Example #5
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)
Example #6
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)
Example #7
0
def update_division(d_id):

    json_request = dict(copy.deepcopy(request.json))

    facility_display_name = request.json.get('facility_name', None)
    if facility_display_name is not None:

        facility_name = facility_display_name.strip().replace(' ', '').lower()
        json_request['facility_name'] = facility_name
        json_request['facility_display_name'] = facility_display_name.strip()

    inner = ', '.join('{:s} = {!r}'.format(key, val)
                      for key, val in json_request.items())
    query = '''UPDATE branches SET {:s} where id = {:d}'''.format(inner, d_id)

    with engine.connect() as con:
        try:
            con.execute(query)
        except IntegrityError as e:
            return record_exists_envelop()
        except Exception as e:
            raise
            return fatal_error_envelop()
        else:
            return record_updated_envelop(json_request)
Example #8
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)
Example #9
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)
Example #10
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)
Example #11
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)
Example #12
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)
Example #13
0
def update_employee_education(e_id, id):
    if not request.json:
        abort(400)
    try:
        db_session.query(EmployeeEducation).filter(
            EmployeeEducation.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)
Example #14
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)
Example #15
0
def update_qualificationnames(id):
    if not request.json:
        abort(400)

    try:
        db_session.query(QualificationName).filter(
            QualificationName.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)
Example #16
0
def update_qualification_by_emp(emp_id, q_id):
    #this is me
    if not request.json:
        abort(400)
    try:
        db_session.query(Qualification).filter(
            Qualification.id == q_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)
Example #17
0
def update_lang_by_emp(id, l_id):
    if not request.json:
        abort(400)

    try:
        db_session.query(EmployeeLanguage).filter(
            EmployeeLanguage.id == l_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)
Example #18
0
def update_certification_by_emp(emp_id, c_id):
    if not request.json:
        abort(400)

    #try to executre

    try:
        db_session.query(Certification).filter(
            Certification.id == c_id).update(dict(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)
Example #19
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)
Example #20
0
def update_emp_appraisal(e_id, b_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)

    try:
        db_session.query(EmployeeAppraisal).filter(EmployeeAppraisal.id == b_id).\
                                                            update(request.json)
        db_session.commit()
    except IntegrityError:
        return record_notfound_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_updated_envelop(request.json)
Example #21
0
def update_role(r_id):
    #check to see if they want to update the admin_role. refuse to change the admin_roel
    

    #clean up the json values
    cleaned_json = ((key, val.strip()) if isinstance(val, str) else (key, val) for key, val in request.json.items())

    query = update_query(Role.__tablename__, cleaned_json, r_id)

    with engine.connect() as con:
        try:
            con.execute(query)
        except IntegrityError as e:
            return record_exists_envelop()
        except Exception as e:
            return fatal_error_envelop()
        else:
            return record_updated_envelop(request.json)
Example #22
0
def update_training_by_emp(emp_id, t_id):
    if not request.json:
        abort(400)
    #check to see if there is any empty values

    #check to see if the request has the right type of keys

    try:
        db_session.query(Training).filter(Training.id == t_id).update(
            request.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)
Example #23
0
def update_emphistory_by_emp(id, e_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)
    try:
        db_session.query(EmployementHistory).filter(EmployementHistory.id == e_id).\
                                            update(request.json)
        db_session.commit()
    except NoResultFound:
        return record_notfound_envelop()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_updated_envelop(request.json)
Example #24
0
def update_division_position(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()

    try:
        db_session.query(DivisionPosition).filter(
            DivisionPosition.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)
Example #25
0
def update_emp_positions(p_id):
    #should do the error handling later
    if not request.json:
        abort(401)
    
    if not all(len(val.strip())>=1 for key, val in request.json.items() if isinstance(val, str)):
        abort(411)
        
    if 'emp_pos_title' in request.json:
        request.json['emp_pos_title_display_name'] = request.json['emp_pos_title']
        request.json['emp_pos_title'] = request.json['emp_pos_title'].lower().strip()
    try:
        db_session.query(EmployeePosition).filter(EmployeePosition.id == p_id).update(request.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)
Example #26
0
def update_relative_by_emp(id, rel_id):
    if not request.json:
        abort(400)

    if not all(
            len(val.strip()) >= 0
            for val in request.json.values() if isinstance(val, str)):
        abort(411)

    #request.json['id'] = rel_id
    try:
        db_session.query(EmployeeRelative).filter(
            EmployeeRelative.id == rel_id).update(request.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)
Example #27
0
def update_role(r_id):
    #check to see if they want to update the admin_role. refuse to change the admin_roel

    #clean up the json values
    cleaned_json = ((key, val.strip()) if isinstance(val, str) else (key, val)
                    for key, val in request.json.items())

    try:
        db_session.query(Role).filter(Role.id == r_id).update(
            dict(cleaned_json))
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        roles = db_session.query(Role).all()
        roles = [role.to_dict() for role in roles]
        for role in roles:
            current_app.config[role['id']] = role
        return record_updated_envelop(request.json)
Example #28
0
def update_region(id):
    if not request.json:
        abort(400)

    #clearn up the json_request
    _cleaner = lambda s: s.strip() if isinstance(s, str) else s
    _request = {key: _cleaner(val) for key, val in request.json.items()}
    if 'name' in _request:
        _request['display_name'] = _request['name']
        _request['name'] = _request['name'].lower().strip()

    #remove the id field
    if 'id' in request.json:
        del request['id']

    try:
        #for oracle
        #_bool_mapper = lambda s : 0 if s == 'false' else 1
        #for oracle
        _bool_mapper = lambda s: s
        _json = {
            key: _bool_mapper(val) if val in ('true', 'false') else val
            for key, val in _request.items()
        }

        db_session.query(Region).filter(Region.id == id).update(_json)
        #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:
        raise
        abort(500)
    else:
        return record_updated_envelop(request.json)
Example #29
0
def update_agency(a_id):

    #checck to see if there is json data
    if not request.json:
        abort(400)
    db_fields = set(
        col.name for col in Branch.__mapper__.columns) - {'id', 'is_branch'}
    result = set(request.json.keys()) - db_fields
    if result:
        return extra_keys_envelop('Keys not accepted %r' %
                                  (', '.join(key for key in result)))

    #now check if there are any missing values whose length is less than 2
    if not all(len(str(val).strip()) >= 1 for val in request.json.values()):
        return length_require_envelop()

    json_request = dict(copy.deepcopy(request.json))

    facility_display_name = request.json.get('facility_name', None)
    if facility_display_name is not None:

        facility_name = facility_display_name.strip().replace(' ', '').lower()
        json_request['facility_name'] = facility_name
        json_request['facility_display_name'] = facility_display_name.strip()

    inner = ', '.join('{:s} = {!r}'.format(key, val)
                      for key, val in json_request.items())
    query = '''UPDATE branches SET {:s} where id = {:d}'''.format(inner, a_id)
    print(query)

    with engine.connect() as con:
        try:
            con.execute(query)
        except IntegrityError as e:
            return record_exists_envelop()
        except Exception as e:
            raise
            return fatal_error_envelop()
        else:
            return record_updated_envelop(json_request)
Example #30
0
def update_divisions_by_type(t_id, 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()
    if 'name' in request.json:
        request.json['display_name'] = request.json['name'].strip()
        request.json['name'] = request.json['name'].strip().lower()

    try:
        db_session.query(Division).filter(Division.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)