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, 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 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 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