def test_crud(self): with self.app_context(): user = UserModel('test', 'abcd') self.assertIsNone(UserModel.find_by_username('test')) self.assertIsNone(UserModel.find_by_id(1)) user.save_to_db() self.assertIsNotNone(UserModel.find_by_username('test')) self.assertIsNotNone(UserModel.find_by_id(1))
def get(self): user = UserModel.find_by_id(current_identity.id) if not user: abort(500, message="Some internal user fault.") employees = EmployeeModel.query.all() res = {"employees": []} if len(employees) == 0: return res, 200 for employee in employees: user = UserModel.find_by_id(employee.user_id) res["employees"].append(employee.get_employee(user)) return res, 200
def delete(cls, user_id): user = UserModel.find_by_id(user_id) if user: user.delete_from_db() return {'message': 'User deleted'}, 200 return {"message": "User not found"}, 404
def get(self, admin_id): admin = AdminModel.find_by_id(admin_id) if not admin: abort(404, message="no admin data exists.") user_id = current_identity.id user = UserModel.find_by_id(user_id) return admin.get_admin(user), 200
def get(self): admins = AdminModel.query_for_admin() if admins: admin = admins[0] user_id = admin.id user = UserModel.find_by_id(user_id) if user: return admin.get_admin(user), 200 abort(404, message="no super admin exist.")
def delete(self, employee_id): employee = EmployeeModel.find_by_id(employee_id) if not employee: abort(404, message="No Employee exists.") user = UserModel.find_by_id(employee.user_id) if not user: abort(404, message="No User exist") employee.delete_from_db() user.delete_from_db() return employee.get_employee(user), 200
def put(self, employee_id): data = SingleEmployeeController.parser.parse_args() employee = EmployeeModel.find_by_id(employee_id) if not employee: abort(404, message="No Employee exists.") user = UserModel.find_by_id(employee.user_id) user.firstname = data.firstname if data.firstname else user.firstname user.lastname = data.lastname if data.lastname else user.lastname user.save_to_db() return employee.get_employee(user), 201
def add_claims_to_jwt(identity): user = UserModel.find_by_id(identity) rights = {"right_id": 1, "is_admin": 0, "is_blocked": 1} if user: rights = { "right_id": user.right_id, "is_admin": user.right_id == 7, "is_blocked": user.right_id == 1 } return rights
def delete(self, company_id): user = UserModel.find_by_id(current_identity.id) if not user: abort(404, message="no user. Please use valid account") admin = AdminModel.find_by_id(user.admin_id.id) if not admin: abort(404, message="no admin data exists.") company = CompanyModel.find_by_id(company_id) if not company: abort(404, message="no company exist with the provided company_id.") company.delete_from_db() return company.json(), 200
def put(self, admin_id): data = SingleAdminController.parser.parse_args() admin = AdminModel.find_by_id(admin_id) user_id = current_identity.id if not admin: abort(404, message="no admin data exists.") if not admin.user_id == user_id: abort(402, message="Only admin can updtae itself") user = UserModel.find_by_id(user_id) user.firstname = data.firstname if data.firstname else user.firstname user.lastname = data.lastname if data.lastname else user.lastname user.save_to_db() return admin.get_admin(user), 200
def put(self, company_id): data = SingleCompanyController.parser.parse_args() user = UserModel.find_by_id(current_identity.id) if not user: abort(404, message="no user. Please use valid account") admin = AdminModel.find_by_id(user.admin_id.id) if not admin: abort(404, message="no admin data exists.") company = CompanyModel.find_by_id(company_id) if not company: abort(404, message="no company exist with the provided company_id.") company.description = data.description if data.description else company.description company.save_to_db() return company.json()
def get(self): user = UserModel.find_by_id(current_identity.id) if not user: abort(500, message="Some internal fault on user.") admin_user = AdminModel.find_by_user_id(current_identity.id) if not admin_user: abort(403, message="Please use admin or ask admin to crete company.") companies = CompanyModel.query.all() if not len(companies): return {"companies": companies} res = {"companies": []} for company in companies: res["companies"].append(company.json()) return res, 200
def test_crud(self): with self.app_context(): # to test user we need to create user right right = UserRightModel('test right') right.save_to_db() user = UserModel('*****@*****.**', 'abcd') self.assertIsNone( UserModel.find_by_email('*****@*****.**'), "Found a user with e-mail '*****@*****.**' before save_to_db.") self.assertIsNone(UserModel.find_by_id(1), "Found a user with id '1' before save_to_db.") user.save_to_db() self.assertIsNotNone( UserModel.find_by_email('*****@*****.**'), "Did not find a user with e-mail '*****@*****.**' after save_to_db" ) self.assertIsNot( UserModel.find_by_id(1), "Did not find a user with id '1' after save_to_db")
def put(self, user_id: int): try: first_name = request.form["first_name"] last_name = request.form["last_name"] phone_number = request.form["phone_number"] birth_date = request.form["birth_date"] print(birth_date) user = UserModel.find_by_id(user_id) if user: user.firstName = first_name or user.firstName user.lastName = last_name or user.lastName user.phoneNumber = phone_number or user.phoneNumber user.birthDate = birth_date or user.birthDate user.firstName = first_name or user.firstName user.update() return {"Message": "Updated"}, 204 except Exception as ex: print(ex)
def put(cls, user_id): data = User.parser.parse_args() item = UserModel.find_by_id(user_id) if not item: if data["password"]: item = UserModel(data["email"], data["password"]) else: return {"message": "User creation requires password."} if data['password'] is not None: item.password = data['password'] if data['right_id'] is not None: item.right_id = data['right_id'] if data['username'] is not None: item.username = data['username'] if data['hide'] is not None: item.hide = data['hide'] item.save_to_db() return item.json()
def test_user(client): ret = login(client) assert ret.status_code == 200 access_token = 'Bearer ' + json.loads( ret.get_data(as_text=True))['access_token'] uname = randomString(10) ret = client.post( '/user', data=dict(username=uname, password="******", first_name="Sadegh", last_name="Azarkaman", phone_number="12345", birth_date=datetime.now().strftime('%Y-%m-%dT%H:%M:%S')), headers={'Authorization': access_token}) user_id = json.loads(ret.get_data(as_text=True))["user_id"] assert int(user_id) > 0 ret = client.put( '/user/{}'.format(user_id), data=dict( first_name=uname, last_name="Azarkaman", phone_number="Pass123", birth_date=datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), ), headers={'Authorization': access_token}) assert ret.status_code == 204 name = UserModel.find_by_id(user_id).firstName assert name == uname ret = client.delete('/user/{}'.format(user_id), headers={'Authorization': access_token}) assert ret.status_code is 202
def get(cls, user_id): user = UserModel.find_by_id(user_id) if user: return user.json(), 200 return {'message': "User not found"}, 404
def get(self, employee_id): employee = EmployeeModel.find_by_id(employee_id) if not employee: abort(404, message="No Employee exists.") user = UserModel.find_by_id(employee.user_id) return employee.get_employee(user), 200
def identity(payload): user_id = payload['identity'] user = UserModel.find_by_id(user_id) return user.json()
def identity(payload): user_id = payload['identity'] return UserModel.find_by_id(user_id)
def get(self, user_id: int): user = UserModel.find_by_id(user_id) if user: return user_schema.dump(user), 200 return None, 404
def delete(self, user_id: int): user = UserModel.find_by_id(user_id) if user: user.delete_from_db() return {"Message": "Deleted"}, 202 return {"user not found"}, 404