def add(self): if not request.is_json: return jsonify({"msg": "missing json request"}), 400 first_name = request.json.get('first_name', None) last_name = request.json.get('last_name', None) expire_date = datetime.datetime.strptime( request.json.get('expire_date', None), '%Y-%m-%d') national_id = request.json.get('national_id', None) advisor = request.json.get('advisor', None) student_id = request.json.get( 'student_id', None) #TODO: generate a unique vlaue instead of None user_id = get_current_user().id exams = [] workbooks = [] classes = [] try: session = db_session() student = Student(first_name, last_name, expire_date, national_id, advisor, student_id, user_id, classes, exams, workbooks) session.add(student) session.commit() return jsonify(success=True), 201 except Exception as e: session.rollback() Logger.debug("could not create student") Logger.error(e.message) print e.message return jsonify(success=False), 400
def registration(self): if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 username = request.json.get('username', None) #TODO: hash the password password = request.json.get('password') first_name = request.json.get('first_name') last_name = request.json.get('last_name') email = request.json.get('email') national_id = request.json.get('national_id') classes = [] students = [] exams = [] account_activated = True # roles = [Role('holder')] session = db_session() try: user = User(first_name, last_name, email, national_id, username, password, account_activated, classes, students, exams) session.add(user) session.commit() #TODO: redirect and provide JWT token and return it return jsonify(success=True), 201 except Exception as e: session.rollback() Logger.debug("registration: Could not add new user to database") Logger.error(e.message) return jsonify(success=False), 400
def test_logger(logger: Logger) -> None: msg = "Any Message" logger.info(msg) logger.error() logger.critical(msg) logger.debug(msg) logger.warning(msg)
def post(self): if not request.is_json: return jsonify({"msg": "missing json request"}), 400 name = request.json.get('name', None) answers_key = request.json.get('answers_key', dict()) date = datetime.datetime.strptime(request.json.get('date', None), '%Y-%m-%d') description = request.json.get('description', None) lessons = request.json.get('lessons', dict()) setting = request.json.get('setting', dict()) marks = dict() ranks = dict() percentage_of_responses_to_questions = dict() user_id = get_current_user().id students = list() workbooks = list() classes = list() try: session = db_session() exam = Exam(name, answers_key, date, description, lessons, setting, marks, ranks, percentage_of_responses_to_questions, user_id, students, classes, workbooks) session.add(exam) session.commit() return jsonify(success=True), 201 except Exception as e: session.rollback() Logger.debug("could not create new exam") Logger.error(e.message) return jsonify(success=False), 400
def generate_sheets(self, exam_id): try: holder = Holder(Exam.query.filter_by(id=exam_id).first(), 340) holder.generate_all_sheets(tmp_addr=os.path.join(TEMP_Storage().get_png_temp_addr(), 'tmp.png'), template_addr=TEMP_Storage().get_sheet_template_addr(), save_addr=Private_Storage().get_sheets_dir_for(exam_id)) return jsonify(success=True), 200 except Exception as e: Logger.debug("could not create new exam") Logger.error(e.message) return jsonify({"success": False, "msg": e.message}), 400
def add(self): if not request.is_json: return jsonify({"msg": "missing json request"}), 400 name = request.json.get("name", None) desciption = request.json.get("description", None) students = [] user_id = get_current_user().id exams = [] try: session = db_session() new_class = Class(name, desciption, students, user_id, exams) session.add(new_class) session.commit() return jsonify(success=True), 201 except Exception as e: session.rollback() Logger.debug("could not create new class") Logger.error(e.message) return jsonify(success=False), 400
def add_exams(self, class_id): try: session = db_session() if not request.is_json: return jsonify({"msg": "missing json request"}), 400 exams = request.json.get('exams', None) the_class = Class.query.filter_by(id=class_id).first() for exam_id in exams: exam = Exam.query.filter_by(id=exam_id).first() exam.students += the_class.students the_class.exams.append(exam) exam.classes.append(the_class) session.add(the_class) session.add(exam) session.commit() return jsonify(success=True), 200 except Exception as e: session.rollback() Logger.debug("could not assign exams to class") Logger.error(e.message) return jsonify(success=False), 400
def patch(self, id): exam = Exam.query.filter_by(id=id).first() if not request.is_json: return jsonify({"msg": "missing json request"}), 400 exam.name = request.json.get('name', exam.name) exam.answers_key = request.json.get('answers_key', exam.answers_key) # exam.date = datetime.datetime.strptime(request.json.get('date', str(exam.date)),'%Y-%m-%d') exam.description = request.json.get('description', exam.description) exam.lessons = request.json.get('lessons', exam.lessons) exam.setting = request.json.get('setting', exam.setting) try: session = db_session() session.add(exam) session.commit() return jsonify(success=True), 200 except Exception as e: session.rollback() Logger.debug("could not update exam") Logger.error(e.message) return jsonify(success=False), 400
def login(self): if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 username = request.json.get('username', None) password = request.json.get('password', None) try: user = User.query.filter_by(username=username, password=password).first() if not user: return jsonify({"msg": "wrong username or password"}), 401 #generating access token access_token = create_access_token(identity=username) return jsonify(access_token=access_token), 200 except Exception as e: Logger.debug("login: user could not login, entered username: "******"msg": "server error"}), 500
def add_students(self, class_id): try: if not request.is_json: return jsonify({"msg": "missing json request"}), 400 students = request.json.get('students', None) the_class = Class.query.filter_by(id=class_id).first() session = db_session() for student_id in students: student = Student.query.filter_by(id=student_id).first() student.classes.append(the_class) the_class.students.append(student) if the_class.exams: for exam in the_class.exams: exam.students.append(student) session.add(exam) session.add(the_class) session.add(student) session.commit() return jsonify(success=True), 200 except Exception as e: session.rollback() Logger.debug("could not assign students to class") Logger.error(e.message) return jsonify(success=False), 400