def remove_user(income_id=Depends(Auth.wrapper), *, db: Session = Depends(db_session), id: str = id): AuthService.validate_admin_access(db, id=income_id) removed_user = UsersService.remove_user(db, id=id) return removed_user
def remove_class(income_id=Depends(Auth.wrapper), *, db: Session = Depends(db_session), id: str = id): AuthService.validate_admin_access(db, id=income_id) removed_course = ClassesService.remove_class(db, id=id) return removed_course
def remove_subject(income_id=Depends(Auth.wrapper), *, db: Session = Depends(db_session), id: str = id): AuthService.validate_admin_access(db, id=income_id) removed_subject = SubjectsService.remove_subject(db, id=id) return removed_subject
def create_subject(income_id=Depends(Auth.wrapper), *, db: Session = Depends(db_session), new_subject: SubjectBaseSchema) -> Any: AuthService.validate_admin_access(db, id=income_id) created_subject = SubjectsService.create_subject(db, object=new_subject) return created_subject
def create_attendances( income_id = Depends(Auth.wrapper), *, db: Session = Depends(db_session), new_attendances: AttendancesListSchema ) -> Any: AuthService.validate_professor_access(db, id=income_id) validated_attendances = AttendancesService.validate_attendances(db, attendances=new_attendances.attendances) print(validated_attendances) created_attendances = AttendancesService.create_attendances(db, attendances=validated_attendances) return created_attendances
def login(payload: AuthRequestSchema, *, db: Session = Depends(db_session)) -> Any: identity, password = dict(payload).values( ) # Desestruturando (Unpacking) os valores do Request Body payload user = UsersService.get_user(db, identity=identity) AuthService.validate_access(user, password) token = AuthService.get_token(user.id) return {'token': token, 'entity': user.entity}
def create_topic(income_id=Depends(Auth.wrapper), *, db: Session = Depends(db_session), new_topic: TopicBaseSchema) -> Any: AuthService.validate_admin_access(db, id=income_id) # ClassesService.validate_name(db, name=new_class.name) created_topic = TopicsService.create_topic(db, object=new_topic) return created_topic
def create_course(income_id=Depends(Auth.wrapper), *, db: Session = Depends(db_session), new_course: CourseBaseSchema) -> Any: AuthService.validate_admin_access(db, id=income_id) CoursesService.validate_name(db, name=new_course.name) created_course = CoursesService.create_course(db, object=new_course) return created_course
def update_topic(income_id=Depends(Auth.wrapper), *, db: Session = Depends(db_session), new_infos: Union[TopicBaseSchema, Dict[str, str]], id: str = id) -> Any: AuthService.validate_admin_access(db, id=income_id) current_topic = TopicsService.validate_id(db, id=id) updated_topic = TopicsService.update_topic( db, db_object=current_topic["db_object"], infos_object=new_infos) return updated_topic
def post(self): try: json_data = UserSchema().load(request.json) except ValidationError as err: return err.messages, 422 hashed_password = bcrypt.generate_password_hash( json_data['password']).decode('utf-8') user = User(username=json_data['username'], email=json_data['email'], password=hashed_password) AuthService.register(user) return {"msg": f"You have been registered. {user.username}"}, 201
def create_user(income_id=Depends(Auth.wrapper), *, db: Session = Depends(db_session), new_user: UserRequestSchema) -> Any: AuthService.validate_admin_access(db, id=income_id) UsersService.validate_username(db, username=new_user.username) UsersService.validate_email(db, email=new_user.email) # UsersService.validate_student( new_user ) created_user = UsersService.create_user(db, object=new_user) return created_user
def update_user(income_id=Depends(Auth.wrapper), *, db: Session = Depends(db_session), new_infos: Union[UserUpdateSchema, Dict[str, Union[str, StatusOptions]]], id: str = id) -> Any: AuthService.validate_admin_or_current_access(db, income_id=income_id, action_id=id) current_user = UsersService.validate_id(db, id=id) updated_user = UsersService.update_user( db, db_object=current_user["db_object"], infos_object=new_infos) return updated_user
def post(self): try: json_data = UserLoginSchema().load(request.json) except ValidationError as err: return err.messages, 422 user = User.query.filter_by(email=json_data['email']).first() if user and bcrypt.check_password_hash(user.password, json_data['password']): token = AuthService.login(user) else: return {'error': 'Email or password invalid'}, 401 return { "msg": f"Logged in as {json_data['email']}, {token['token']}." }, 200
def validate_token(payload: TokenSchema, *, db: Session = Depends(db_session)) -> Any: user_id = AuthService.validate_token(payload.token) user = UsersService.get_user(db, identity=user_id) return {'entity': user.entity}
def remove_attendance( income_id = Depends(Auth.wrapper), *, db: Session = Depends(db_session), id: str = id ): AuthService.validate_admin_access(db, id=income_id) removed_attendance = AttendancesService.remove_attendance(db, id=id) return removed_attendance