Example #1
0
    def get(self, class_id=None, page=None, per_page=None):
        if (request.args.get("page") and request.args.get("per_page")
                and request.args.get("username")):
            page = int(request.args.get("page"))
            class_id = request.args.get("username")
            per_page = int(request.args.get("per_page"))
            list_class = ClasssModel.find_list_by_name(class_id, page,
                                                       per_page)
            if list_class is None:
                return {"messages": err_404.format("list_class")}, 404
            return (
                {
                    "list": ClasssModel.to_json(list_class),
                    "count ": len(list_class)
                },
                200,
            )

        if class_id is None:
            list = ClasssModel.to_json(
                ClasssModel.query.paginate(page, per_page, False).items)
            return {"list": list, "count": len(ClasssModel.query.all())}, 200

        classs = ClasssModel.find_by_class_id(class_id)
        if classs is None:
            return {"messages": err_404.format("class")}, 404
        return classs.json(), 200
    def put(self, class_id, subject_id):
        data = Subject_And_Class.parser.parse_args()
        row = Subject_And_ClassModel.find_row(class_id, subject_id)
        if row is None:
            return {"messages": err_404.format("row")}, 404
        else:
            if SubjectModel.find_by_subject_id(data["subject_id"]) is None:
                return {"messages": err_404.format("subject")}, 404
            if ClasssModel.find_by_class_id(data["class_id"]) is None:
                return {"messages": err_404.format("class")}, 404

            if datetime.datetime.now() > datetime.datetime.strptime(
                    data["exam_date"], "%Y-%m-%d"):
                return {"messages": "exam date you set is in the past"}, 400

            try:
                if data["class_id"]:
                    row.class_id = data["class_id"]
                if data["exam_date"]:
                    row.exam_date = datetime.datetime.strptime(
                        data["exam_date"], "%Y-%m-%d")
                if data["subject_id"]:
                    row.user_id = data["user_id"]
                row.save_to_db()
            except:
                return {"messages": err_500}, 500
        return {"messages": noti_201}, 201
    def post(self):
        data = Subject_And_Class.parser.parse_args()
        if Subject_And_ClassModel.find_row(data["class_id"],
                                           data["subject_id"]):
            return {"messages": err_duplicate.format("class")}, 400

        # check khóa ngoại
        if SubjectModel.find_by_subject_id(data["subject_id"]) is None:
            return {"messages": err_404.format("subject")}, 404
        if ClasssModel.find_by_class_id(data["class_id"]) is None:
            return {"messages": err_404.format("class")}, 404

        if datetime.datetime.now() > datetime.datetime.strptime(
                data["exam_date"], "%Y-%m-%d"):
            return {"messages": "exam date you set is in the past"}, 400

        row = Subject_And_ClassModel(
            class_id=data["class_id"],
            subject_id=data["subject_id"],
            exam_date=datetime.datetime.strptime(data["exam_date"],
                                                 "%Y-%m-%d"),
        )
        try:
            row.save_to_db()
        except:
            return {"messages": err_500}, 500
        return {"messages": noti_201}, 201
Example #4
0
 def delete(self, class_id):
     classs = ClasssModel.find_by_class_id(class_id)
     if classs is None:
         return {"messages": err_404.format("class")}, 404
     try:
         classs.delete_from_db()
     except:
         return {"messages": err_500}, 500
     return {"messages": noti_201}, 201
Example #5
0
 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 = Teacher_And_Class.parser.parse_args()
        if Teacher_And_ClassModel.find_row(data["class_id"], data["user_id"]):
            return {"messages": err_duplicate.format("class")}, 400
        row = Teacher_And_ClassModel(**data)

        ## check khóa ngoại
        if ClasssModel.find_by_class_id(class_id=data["class_id"]) is None:
            return {"messages": err_404.format("class")}, 404
        if UserModel.find_by_user_id(user_id=data["user_id"]) is None:
            return {"messages": err_404.format("user")}, 404
        if (UserModel.find_by_user_id(data["user_id"]).chuc_vu == 2
            ):  # lấy user rồi xem nó có là giáo viên không
            try:
                row.save_to_db()
            except:
                return {"messages": err_500}, 500
            return {"messages": noti_201}, 201
        return {"messages": "Không có quyền"}, 400
    def put(self, class_id, user_id):
        data = Teacher_And_Class.parser.parse_args()

        ## check khóa ngoại
        classs = ClasssModel.find_by_class_id(class_id=data["class_id"])
        if classs is None:
            return {"messages": err_404.format("class")}, 404
        user = UserModel.find_by_user_id(user_id=data["user_id"])
        if user is None:
            return {"messages": err_404.format("user")}, 404
        if (UserModel.find_by_user_id(data["user_id"]).chuc_vu == 2
            ):  # lấy user rồi xem nó có là giáo viên không
            row = Teacher_And_ClassModel.find_row(class_id, user_id)
            if row is None:
                return {"messages": err_404.format("row")}, 404
            else:
                try:
                    history = HistoryModel(
                        user_id=user.user_id,
                        class_id=classs.class_id,
                        school_id=classs.school_id,
                        name=user.name,
                        birth_date=user.birth_date,
                        phone_number=user.phone_number,
                        sex=user.sex,
                        address=user.address,
                        native_land=user.native_land,
                        email=user.email,
                        job=user.job,
                    )
                    if data["class_id"]:
                        row.class_id = data["class_id"]
                    if (data["user_id"] and UserModel.find_by_user_id(
                            data["user_id"]).chuc_vu == "2"):
                        row.user_id = data["user_id"]
                    else:
                        return {"messages": err_500}, 500
                    history.save_to_db()
                    row.save_to_db()
                except:
                    return {"messages": err_500}, 500
            return {"messages": noti_201}, 201
    def post(self):
        data = Student_And_Class.parser.parse_args()

        if Student_And_ClassModel.find_row(data["class_id"], data["user_id"]):
            return {"messages": err_duplicate.format("class")}, 400
        # check khóa ngoại
        if ClasssModel.find_by_class_id(class_id=data["class_id"]) is None:
            return {"messages": err_404.format("class")}, 404
        if UserModel.find_by_user_id(user_id=data["user_id"]) is None:
            return {"messages": err_404.format("user")}, 404

        row = Student_And_ClassModel(class_id=data["class_id"], user_id=data["user_id"])
        if (
            UserModel.find_by_user_id(data["user_id"]).job == 1
        ):  # lấy user rồi xem nó có là sinh viên không
            try:
                row.save_to_db()
            except:
                return {"messages": err_500}, 500
            return {"messages": noti_201}, 201
        return {"messages": "Only user is student can be add to the row"}, 400