def delete(self, class_id, report_id=None): """ delete a specific report from the class or all the reports. deleting a report will delete all the related tables under it: "zoom names", "session", "chat", "student_status" :param class_id: id of classroom to delete from :param report_id: id of report to delete - if "None" will delete all the reports of the class :return: 204 http code if succeeded """ if report_id is None: # Deleting all reports of class class_reports_id = db.session.query( ReportModel.id).filter_by(class_id=class_id).all() for report_data in class_reports_id: current_report = ReportModel.query.filter_by( id=report_data.id).first() db.session.delete(current_report) db.session.commit() return "", 204 current_report = ReportModel.query.filter_by(id=report_id).first( ) # Making sure the class belongs to the current user if current_report is None: abort(400, message=RestErrors.INVALID_REPORT) db.session.delete(current_report) db.session.commit() return "", 204 api.add_resource(ReportsResource, '/classrooms/<int:class_id>/reports', '/classrooms/<int:class_id>/reports/<int:report_id>')
class RegisterResource(Resource): def __init__(self): super().__init__() self._post_args = reqparse.RequestParser(bundle_errors=True) self._post_args.add_argument("username", type=custom_types.username, location='json', required=True) self._post_args.add_argument("email", type=custom_types.email, location='json', required=True) self._post_args.add_argument("password", type=custom_types.password, location='json', required=True) def post(self): args = self._post_args.parse_args() user = TeacherModel(username=args['username'], email=args['email'], password=args['password']) db.session.add(user) db.session.commit() return {'token': login_token_serializer.dumps(user.id)} api.add_resource(RegisterResource, "/register") api.add_resource(LoginResource, "/login")
:return: student object from db """ student = StudentModel.query.filter_by(id=student_id).first() if not student: abort(400, message=RestErrors.INVALID_STUDENT_ID) return student def put(self, class_id, student_id): """ update student data - given arguments of data to update from the json request - will update data in the db :param class_id: relevant classroom (checked in the "validate classroom decorator) :param student_id: id of student to update :params - more params passing from the RequestParser of the user - more info about these in the init of in the api-doc :return: """ student = StudentModel.query.filter_by(id=student_id) if not student.first(): abort(400, message=RestErrors.INVALID_STUDENT_ID) args = self._put_args.parse_args(strict=True) args_without_none = {k: v for k, v in args.items() if v is not None } # Dropping arguments which werent given student.update(args_without_none) db.session.commit() return "", 204 api.add_resource(StudentResource, '/classrooms/<int:class_id>/students/<int:student_id>')
self._put_args = reqparse.RequestParser(bundle_errors=True) self._put_args.add_argument( 'new_status', type=int, help= f"Bad choice, please pick one of this choices {STATUS_CHOICES}", required=True, location="json", choices=STATUS_CHOICES) def put(self, status_id): """ update student status in a classroom that is part of the teachers classroom in a specific report :param status_id: the status id - related to the student that will be updated :param new status: the new status for the user - must be part of the STATUS_CHOICES (check RequestParser) :return: 204 http code if succeeded """ args = self._put_args.parse_args() status = StudentStatus.query.get(status_id) if status is None or status.report.classroom.teacher != auth.current_user( ): abort(400, message=RestErrors.INVALID_STATUS) status.status = args["new_status"] db.session.commit() return "", 204 api.add_resource(StudentStatusResource, '/status/<int:status_id>')
def delete(self, class_id=None): """ The function will delete specific classroom or all classrooms of the user given from url: :param class_id: the class to delete, if None deletes all classes :return: 204 http code if succeeded """ if class_id is None: # Deleting all classes teacher_classes_id = db.session.query(ClassroomModel.id).filter_by( teacher=auth.current_user()).all() for class_data in teacher_classes_id: current_class = ClassroomModel.query.filter_by( id=class_data.id, teacher=auth.current_user()).first() db.session.delete(current_class) db.session.commit() return "", 204 current_class = ClassroomModel.query.filter_by( id=class_id, teacher=auth.current_user()).first( ) # Making sure the class belongs to the current user if current_class is None: abort(400, message=RestErrors.INVALID_CLASS) db.session.delete(current_class) db.session.commit() return "", 204 api.add_resource(ClassroomsResource, "/classrooms", "/classrooms/<int:class_id>")