def get_current_user(db: Session = Depends(get_db), Authorize: AuthJWT = Depends()) -> User: Authorize.jwt_required() user_id = Authorize.get_jwt_subject() dbuser = userCRUD.get(db, user_id) if not dbuser: return JSONResponse( content={'details': f'User {user_id} could not be queried'}, status_code=400) return dbuser
def get_user_posts(id: int, db: Session = Depends(get_db)) -> List[Post]: dbuser = userCRUD.get(db, id) if not dbuser: return JSONResponse( content={'details': f'User {id} could not be queried'}, status_code=400) posts = dbuser.posts if not posts: return JSONResponse( content={'details': f'User {id} does not have any posts'}, status_code=404) return posts
def delete_user_profile_picture(Authorize: AuthJWT = Depends(), db: Session = Depends(get_db)): Authorize.jwt_required() user_id = Authorize.get_jwt_subject() user_name = Authorize.get_raw_jwt()["username"] dbuser = userCRUD.get(db, user_id) if not user_name: raise HTTPException(500, "Failed to get username from JWT") profile_picture = get_default_profile_picture(user_name) dbuser = userCRUD.update_profile_picture(db, user_id=user_id, image=profile_picture) return JSONResponse({"success": True})
async def update_current_user_profile_picture( data: Request, Authorize: AuthJWT = Depends(), db: Session = Depends(get_db), ): Authorize.jwt_required() user_id = Authorize.get_jwt_subject() dbuser = userCRUD.get(db, user_id) if not dbuser: raise HTTPException(404, "User not found") if not dbuser.id == user_id: raise HTTPException(401) try: # Setting up variables binary_image_original = await data.body() binary_image_updated = io.BytesIO() # Getting the image, loading it into praw, and resizing it image = Image.open(io.BytesIO(binary_image_original)) width, height = image.size # Modifying the image, resizing it to 360 x 360 image.thumbnail((width, 360) if (width >= height) else (360, height)) image = crop_center(image, 360, 360) # Getting the filesize & saving the image into a buffer image = image.convert("RGB") image.save(binary_image_updated, format='JPEG') file_size = binary_image_updated.getbuffer().nbytes binary_image_updated = binary_image_updated.getvalue() except UnidentifiedImageError: # Throwing a 400 error if the file uploaded was not an image raise HTTPException(400, "File uploaded was not an image") if file_size > 250_000: raise HTTPException(400, "Image uploaded was too large in size.") # Inserting the profile picture into the database dbuser = userCRUD.update_profile_picture(db, user_id=user_id, image=binary_image_updated) return Response(dbuser.profile_picture, media_type='image/jpeg')
def refresh( Authorize: AuthJWT = Depends(), db: Session = Depends(get_db), ): Authorize.jwt_refresh_token_required() user_id = Authorize.get_jwt_subject() current_user = userCRUD.get(db, user_id) if not current_user: return HTTPException(404, "User with ID {} not found".format(user_id)) access_token = Authorize.create_access_token(subject=user_id, user_claims={ "username": current_user.username, "email": current_user.email, }) return {"access_token": access_token}
def get_profile_picture(id: int, db: Session = Depends(get_db)): dbuser = userCRUD.get(db, id) if not dbuser: raise HTTPException(404, "User with id {} not found".format(id)) return Response(dbuser.profile_picture, media_type='image/jpeg')