def delete_exam(id): if current_identity.is_superuser: Exam.query.filter_by(id=id).delete() db.session.commit() return custom_response({'message': 'Successfully deleted'}, 204) else: return custom_response({'error': 'Unauthorized'}, 403)
def add_exam(): if current_identity.is_superuser: data = request.get_json() exam = Exam(data['subject_id'], data['exam_period_id'], data['date']) exam.save() return custom_response({'message': 'Successfully created'}, 201) else: return custom_response({'error': 'Unauthorized'}, 403)
def cancelApplication(application_id): application = Application.query.get(application_id) if application and application.user_id == current_identity.id: print("Canceling application") print(application.serialize()) db.session.delete(application) db.session.commit() return custom_response( {'message': 'Application cancelation successful.'}) else: return custom_response({'message': 'User application not found.'}, 403)
def applyForExam(): examID = request.json['examID'] exam = Exam.query.filter(Exam.id == examID).first() if exam: print('User {} is trying to apply for exam id {}'.format( current_identity.username, exam.id)) application = Application(user_id=current_identity.id, exam_id=exam.id) application.save() return custom_response({'message': 'Application successful.'}) else: return custom_response({'message': 'Application failed.'}, 400)
def register(): req_data = request.get_json() username = req_data['username'] password = req_data['password'] user = User.get_by_username(username) if user: message = {'error': 'User already exist, please try again'} return custom_response(message, 400) user = User(username, password) user.save() return custom_response({'message': 'Successfully registered'}, 201)
def getAppliedExams(): userID = current_identity.id applied_exams = Application.query.filter(Application.user_id == userID) print(applied_exams) for exam in applied_exams: print(exam.serialize()) exams_json = [application.serialize() for application in applied_exams] return custom_response(exams_json)
def getSubjects(): # year query parametar year = request.args.get('year') if year: subjects = Subject.query.filter(Subject.year == year) else: subjects = Subject.query.all() subjects_json = [subject.serialize() for subject in subjects] return custom_response(subjects_json)
def update_exam(id): if current_identity.is_superuser: exam = Exam.query.filter_by(id=id) data = request.get_json() if 'date' in data: switch = {'date': data['date']} else: return custom_response({'error': 'Bad request'}, 400) exam.update(values=switch) db.session.commit() updated_exam = Exam.query.get(id) # updated_exam = Exam(updated_exam['id'], ) return custom_response(updated_exam.serialize(), 202) else: return custom_response({'error': 'Unauthorized'}, 403)
def get(self, user_id: int): try: user = User.find_by_id(user_id) if not user: return {"message": Errors.user_not_found}, 404 return user_schema.dump(user), 200 except Exception as Other_err: logger.exception("The exception is {}".format(Other_err)) out = {"message": Other_err.args} return custom_response(out), 500
def delete(self, user_id: int): """A fresh jwt token is required, if not logged in, log in and get the access token and pass it to the header with "Bearer" prefixed""" try: user = User.find_by_id(user_id) if not user: return {"message": Errors.user_not_found}, 404 user.delete_from_db() return {"message": Errors.user_deleted}, 200 except Exception as Other_err: db.session.rollback() logger.exception("The exception is {}".format(Other_err)) out = {"message": Other_err.args} return custom_response(out), 500
def get(self): try: if not github.authorized: return redirect(url_for('github.login')) account_info = github.get('/user') if account_info.ok: user_data = account_info.json() email = user_data.get("email", None) name = user_data.get("name", None) if User.find_by_mail_id(email): return {"message": Errors.user_username_exists}, 400 else: loaded_data = user_schema.load({"email":email,"name":name,"password":None}, session=db.session) user= User(loaded_data) user.save_user() dumped_data = user_schema.dump(user) return {"message":"CREATED", "user":dumped_data}, 200 return {"message": Errors.user_invalid_credentials}, 400 except ValidationError as vError: db.session.rollback() logger.exception("The exception is {}".format(vError.messages)) out = {"message": vError.messages} return custom_response(out), 400
def protected(): current_user = current_identity print('Current user is {}'.format(current_user.serialize())) return custom_response({'user': current_user.serialize()})
def getExamPeriod(): exam_periods = Exam_period.query.all() exam_periods_json = [period.serialize() for period in exam_periods] return custom_response(exam_periods_json)