def put(self): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'Admin privilege required.'}, 401 data = _teacher_parser.parse_args() for key in data.keys(): if str(data[key]).lower() in ('none', 'null', ''): data[key] = None teacher_id = data.get("teacher_id") teacher_name = data.get("teacher_name") teacher_code = data.get("teacher_code") if teacher_id: teacher = TeacherModel.find_by_teacher_id(teacher_id) elif teacher_code: teacher = TeacherModel.find_by_teacher_code(teacher_code) elif teacher_name: teacher = TeacherModel.find_by_teacher_name(teacher_name) else: return {"message": "Missing teacher key fields"}, 404 try: data['modified_date'] = datetime.now() if teacher: teacher.set_attribute(data) else: teacher = TeacherModel(**data) teacher.save_to_db() return teacher.json() except Exception as ex: return {"message": f"Error updating teacher data. {repr(ex)}"}, 403
def post(self): data = _teacher_parser.parse_args() claims = get_jwt_claims() if not claims.get('is_admin'): user_email = claims.get('email') return {'message': 'Admin privilege required.'}, 401 try: dt = datetime.now() data["creation_date"] = dt data["modified_date"] = dt print(f"data: {data}") teacher = TeacherModel(**data) teacher.save_to_db() except KeyError as ex: return {"message": f"Error saving teacher data. Missing key fields. {repr(ex)}"}, 403 except Exception as ex: return {"message": f"Error saving teacher data. {repr(ex)}"}, 403 return teacher.json()