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.')
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)
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)
def create_emp_apptype(): #lower case the facility name if not request.json: abort(400) if 'name' not in request.json: return missing_keys_envelop() if not all(len(val.strip()) >= 1 for key, val in request.json.items()): return length_require_envelop() request.json['display_name'] = request.json['name'] request.json['name'] = request.json['name'].strip().lower() #insert into the database try: rel = EmployeeAppraisalType(**request.json) db_session.add(rel) db_session.commit() except IntegrityError as ie: return record_exists_envelop() else: return record_created_envelop(request.json)
def update_user(u_id): if not request.json: abort(400) if request.args.get('action') == 'update_activation': try: db_session.query(User).filter(User.id == u_id).update(request.json) db_session.commit() except NoResultFound as e: return result_notfound_envelop() except Exception as e: return fatal_error_envelop() else: return record_updated_envelop(request.json) 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() result, err = validate_password(request.json.get('password'), user.user_name) print(result) if result == False: return jsonify({'message': err, 'status': 'fail'}) print('GOt here ----------') hashed_pass = hash_password( request.json['password'].strip().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 if request.args.get('by') == 'user': user.password_changed = True 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.')