def update_school_by_id(self, school_id: int, data: Dict) -> bool: """ update school by id :param school_id: :param data: :return: """ if not data: raise RequestDataEmpty("school data is empty") if not self.input_validate.validate_json(data, school_schema): self.logger.error("All school field input must be required.") raise ValidateFail("School update validation fail") try: self.logger.info( "update school info by school_id:{}".format(school_id)) return SchoolModel.update_school( school_id, SchoolModel(name=data["school_name"], contact_info=data["contact_info"], address_id=data["address_id"])) except SQLAlchemyError as error: self.logger.error("Error: {}".format(error)) raise SQLCustomError(description="Update school by ID SQL ERROR") except SQLCustomError as error: self.logger.error("Error: {}".format(error)) raise SQLCustomError(description="No record for requested school")
def get(self, school_id=None, page=None, per_page=None): if ( request.args.get("page") and request.args.get("per_page") and request.args.get("class_id") ): page = int(request.args.get("page")) name = request.args.get("username") per_page = int(request.args.get("per_page")) list_school = SchoolModel.find_list_by_name(name, page, per_page) if list_school is None: return {"messages": err_404.format("list_school")}, 404 return ( {"list": SchoolModel.to_json(list_school), "count ": len(list_school)}, 200, ) if school_id is None: list = SchoolModel.to_json( SchoolModel.query.paginate(page, per_page, False).items ) return {"list": list, "count": len(SchoolModel.query.all())}, 200 school = SchoolModel.find_by_school_id(school_id) if school: return school.json(), 200 return {"messages": err_404.format("school")}, 404
def delete(self, id): school = School.find(id) if school is None: return {'code': 400, 'message': 'School data not found'}, 400 School.delete(id) return { 'code': 200, 'message': 'School data deleted successfully' }, 200
def post(self): data = School.parser.parse_args() if SchoolModel.find_by_name(data["name"]): return {"messages": err_duplicate.format("school")}, 400 school = SchoolModel(**data) try: school.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def post(self): _input = SchoolsResource.parser.parse_args() school = School(id=None, name=_input['name']) school.save() return { 'code': 201, 'message': 'School data created successfully' }, 201
def create_school(self, data: Dict) -> bool: """ create school records :param data: :return: """ if not data: raise RequestDataEmpty("School data is empty") if not self.input_validate.validate_json(data, school_schema): self.logger.error("All school field input must be required.") raise ValidateFail("School validation fail") try: return SchoolModel.create_school(SchoolModel(**data)) except SQLAlchemyError as error: self.logger.error("School create fail. error %s", error) raise SQLCustomError("School create fail")
def get_schools_by_query( self, page: int, query: str, per_page: int = 20) -> Dict[str, Union[list, Any]]: """ get users by query (name, contact info) :params page :params query :return: school list of dict """ self.logger.info("Get users list by query %s", query) try: schools = SchoolModel.get_schools_by_query(page, query, per_page) return { "schools": self.__return_school_list(schools.items), "total_count": schools.total, "current_page": schools.page, "next_page": schools.next_num, "prev_page": schools.prev_num, "pages": schools.pages } except SQLAlchemyError: self.logger.error("Get users by name fail. query %s. error %s", query, traceback.format_exc()) raise SQLCustomError(description="GET schools by query SQL ERROR")
def get_all_school_address(): """ get all school address for get all address API """ return [ address.address_type_dict(school) for address, school in SchoolModel.get_all_school_address() ]
def delete(self, school_id): school = SchoolModel.find_by_school_id(school_id) if school is None: return {"messages": err_404.format("school")}, 404 try: school.delete_from_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def get_all_school_address(page: int = 1) -> (Dict, int): """ get all school address for get all address API :param page """ schools_addresses = SchoolModel.get_all_school_address(page) return [ address.address_type_dict(school) for address, school in schools_addresses.items ], schools_addresses.total
def get(self): user_id = get_jwt_identity() if user_id is None: return {'code': 200, 'schools': []}, 200 schools = School.all() return { 'code': 200, 'schools': [school._json() for school in schools] }, 200
def get_all_schools(self) -> (List, Any): """ get all school :return: school list of dict """ try: self.logger.info("Get school list") return self.__return_school_list(SchoolModel.get_all_schools()) except SQLAlchemyError as error: self.logger.error("Error: {}".format(error)) raise SQLCustomError(description="GET School SQL ERROR")
def get_schools_by_address_ids(ids: Tuple) -> Dict[int, SchoolModel]: """ get schools by address IDs :params ids :return: school list of dict """ try: schools = SchoolModel.get_schools_by_address_ids(ids) return {school.address_id: school for school in schools} except SQLAlchemyError: raise SQLCustomError( description="GET schools by ids query SQL ERROR")
def get(self): username = get_jwt_identity() schools = [school.json() for school in SchoolModel.find_all()] if username: return {"schools": schools}, 200 return ( { "schools": [school["name"] for school in schools], "message": constants.MORE_DATA_AVAILABLE, }, 200, )
def delete_school_by_id(self, school_id: int) -> bool: """ delete school by id :param school_id: :return: """ try: self.logger.info( "delete school info by school_id:{}".format(school_id)) return SchoolModel.delete_school_by_id(school_id) except SQLAlchemyError as error: self.logger.error("Error: {}".format(error)) raise SQLCustomError(description="Delete school by ID SQL ERROR")
def get_all_schools(self, page: int = 1) -> (List, Any): """ get all school :params:page : int :return: school list of dict """ try: self.logger.info("Get school list") schools = SchoolModel.get_all_schools(page) return self.__return_school_list(schools.items), schools.total except SQLAlchemyError as error: self.logger.error("Error: {}".format(error)) raise SQLCustomError(description="GET School SQL ERROR")
def put(self, id): _input = SchoolResource.parser.parse_args() school = School.find(id) if school is None: return {'code': 400, 'message': 'School data not found'}, 400 school.name = _input['name'] school.save() return { 'code': 200, 'message': 'School data updated successfully' }, 200
def get_school_by_id(self, school_id: int) -> Optional[List]: """ get school info by id :param school_id: :return: school list of dict """ try: self.logger.info( "get school info by school_id:{}".format(school_id)) return self.__return_school_list( SchoolModel.get_school_by_id(school_id)) except SQLAlchemyError as error: self.logger.error("Error: {}".format(error)) raise SQLCustomError(description="GET School by ID SQL ERROR")
def put(self, school_id): data = School.parser.parse_args() school = SchoolModel.find_by_school_id(school_id) if school is None: return {"messages": err_404.format("school")}, 404 if data["name"]: school.name = data["name"] if data["address"]: school.address = data["address"] try: school.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def post(self, name): if SchoolModel.find_by_name(name): return {'message': "Trường '{}' Đã tồn tại".format(name)}, 400 school = SchoolModel(name) try: school.save_to_db() except: return {'message': 'Xảy ra lỗi khi tạo 1 trường học'}, 500 return school.json(), 201
def attendance_dict(self, school: SchoolModel, student: StudentModel): """ Return object data for viewing easily serializable format :param school: :param student: :return: """ return { "id": self.id, "grade": self.grade, "year": self.year, "enrolled_date": self.enrolled_date.strftime("%d-%m-%Y"), "school": school.school_dict(), "student": student.student_dict() }
def put(self, class_id): data = Classs.parser.parse_args() classs = ClasssModel.find_by_class_id(class_id) if classs is None: return {"messages": err_404.format("class")}, 404 if data["name"]: classs.name = data["name"] if data["school_id"]: classs.school_id = data["school_id"] if SchoolModel.find_by_school_id(school_id=data["school_id"]) is None: return {"messages": err_404.format("school")}, 404 try: classs.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def post(self): data = Classs.parser.parse_args() if ClasssModel.find_by_name(data["name"]): return {"messages": err_duplicate.format("class")}, 400 # check khóa ngoại if SchoolModel.find_by_school_id(school_id=data["school_id"]) is None: return {"messages": err_404.format("school")}, 404 classs = ClasssModel(name=data["name"], school_id=data["school_id"], class_id=data["class_id"]) classs.save_to_db() try: classs.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def get_school_by_id(self, school_id: int) -> Optional[Dict]: """ get school info by id :param school_id: :return: school list of dict """ try: self.logger.info( "Get school info by school_id:{}".format(school_id)) school = SchoolModel.get_school_by_id(school_id) if not school: raise SQLCustomError( description="No data for requested school id: {}".format( school_id)) return school.school_dict() except SQLAlchemyError as error: self.logger.error("Error: {}".format(error)) raise SQLCustomError(description="GET School by ID SQL ERROR")
def post(self, name): if SchoolModel.find_by_name(name): return {'message': "School '{}' already exists".format(name)}, 400 school = SchoolModel(name) try: school.save_to_db() except: return { 'message': 'An error occurred while creating the school' }, 500 return school.json(), 201
def post(self, schoolName): if SchoolModel.find_by_name(schoolName): return { 'message': 'School with name {} already exists.'.format(schoolName) }, 400 school = SchoolModel(schoolName) try: school.save_to_db() except: return { 'message': 'An error occurred while saving the school. Database error.' }, 500 return school.json(), 201
def get_all_schools(self, page: int = 1, per_page: int = 20) -> Dict[str, Union[list, Any]]: """ get all school :params:page : int :params:per_page : int :return: school list of dict """ try: self.logger.info("Get school list") schools = SchoolModel.get_all_schools(page, per_page) return { "schools": self.__return_school_list(schools.items), "total_count": schools.total, "current_page": schools.page, "next_page": schools.next_num, "prev_page": schools.prev_num, "pages": schools.pages } except SQLAlchemyError as error: self.logger.error("Error: {}".format(error)) raise SQLCustomError(description="GET School SQL ERROR")
def delete(self, schoolName): school = SchoolModel.find_by_name(schoolName) if school: school.delete_from_db() return {'message': 'School deleted. Sorry to see it.'}
def delete(self, name): school = SchoolModel.find_by_name(name) if school: school.delete_from_db() return {'message': 'School deleted'}
def get(self, name): school = SchoolModel.find_by_name(name) if school: return school.json() return {'message': 'School not found'}, 404