This commit is contained in:
119
back/main.py
119
back/main.py
@@ -5,10 +5,11 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
import json
|
||||
from typing import Optional
|
||||
from crud import get_books, remove_book, edit_book, add_book, get_films, remove_film, edit_film, add_film, get_field_values, get_user
|
||||
from .database import get_db
|
||||
from .crud import get_books, remove_book, edit_book, add_book, get_films, remove_film, edit_film, add_film, get_field_values, get_user
|
||||
from pydantic import BaseModel
|
||||
from passlib.context import CryptContext
|
||||
from secret import SECRET_KEY, ALGORITHM, ACCESS_TOKEN_EXPIRE_MINUTES, origins
|
||||
from .secret import SECRET_KEY, ALGORITHM, ACCESS_TOKEN_EXPIRE_MINUTES, origins
|
||||
from datetime import datetime, timedelta
|
||||
from jose import JWTError, jwt
|
||||
|
||||
@@ -55,7 +56,7 @@ def get_password_hash(password):
|
||||
return pwd_context.hash(password)
|
||||
|
||||
def authenticate_user(name, password):
|
||||
user = get_user(name)
|
||||
user = get_user(db, name)
|
||||
if len(user) == 0:
|
||||
return False
|
||||
if verify_password(password, user[0]['password']):
|
||||
@@ -92,7 +93,10 @@ def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
||||
return encoded_jwt
|
||||
|
||||
@app.post("/api/token", response_model=Token)
|
||||
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
||||
async def login_for_access_token(
|
||||
form_data: OAuth2PasswordRequestForm = Depends(),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
user = authenticate_user(form_data.username, form_data.password)
|
||||
username = form_data.username
|
||||
if not user:
|
||||
@@ -108,60 +112,83 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
|
||||
return JSONResponse(content={"access_token": access_token, "token_type": "bearer"})
|
||||
|
||||
@app.post('/api/books')
|
||||
async def createBook(book: Book, current_user: dict = Depends(get_current_user)):
|
||||
async def createBook(
|
||||
book: Book,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not allowed")
|
||||
res = add_book({
|
||||
res = add_book(db, {
|
||||
'author': book.author,
|
||||
'title': book.title,
|
||||
'type': book.type,
|
||||
'editor': book.editor
|
||||
})
|
||||
if res == None:
|
||||
raise HTTPException(status_code=400, detail="Badly formated body")
|
||||
raise HTTPException(status_code=400, detail="Error with request, database may be offline")
|
||||
return res
|
||||
|
||||
@app.get('/api/books')
|
||||
async def readBook(page: int = 0, limit: int = 50, sort: str = "", search: str = "", current_user: dict = Depends(get_current_user)):
|
||||
async def readBook(
|
||||
page: int = 0,
|
||||
limit: int = 50,
|
||||
sort: str = "",
|
||||
search: str = "",
|
||||
current_user: dict = Depends(get_current_user),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not allowed")
|
||||
res, nb = get_books(page, limit=limit, search=search)
|
||||
res, nb = get_books(db, page, limit=limit, search=search)
|
||||
if res is False or not nb:
|
||||
raise HTTPException(status_code=400, detail="Badly formated")
|
||||
raise HTTPException(status_code=400, detail="Error with request, database may be offline")
|
||||
header = {'x-nbpage': str(int(nb[0]['count'] / limit))}
|
||||
content = {'result': [dict(r) for r in res]}
|
||||
return JSONResponse(content=content, headers=header)
|
||||
|
||||
@app.put('/api/book/{id}')
|
||||
async def updateBook(id: str, book: Book, current_user: dict = Depends(get_current_user)):
|
||||
async def updateBook(
|
||||
id: str,
|
||||
book: Book,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not allowed")
|
||||
res = edit_book(id, {
|
||||
res = edit_book(db, id, {
|
||||
'author': book.author,
|
||||
'title': book.title,
|
||||
'type': book.type,
|
||||
'editor': book.editor
|
||||
})
|
||||
if res == None:
|
||||
raise HTTPException(status_code=400, detail="Badly formated body")
|
||||
raise HTTPException(status_code=400, detail="Error with request, database may be offline")
|
||||
return res
|
||||
|
||||
@app.delete('/api/book/{id}')
|
||||
async def deleteBook(id: str, current_user: dict = Depends(get_current_user)):
|
||||
async def deleteBook(id: str,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not allowed")
|
||||
res = remove_book(id)
|
||||
_, nb = get_books(0)
|
||||
res = remove_book(db, id)
|
||||
_, nb = get_books(db, 0)
|
||||
header = {'x-nbpage': str(int(nb[0]['count'] / 50))}
|
||||
if res == None:
|
||||
raise HTTPException(status_code=400, detail="Badly formated body")
|
||||
raise HTTPException(status_code=400, detail="Error with request, database may be offline")
|
||||
return JSONResponse(content=res, headers=header)
|
||||
|
||||
@app.post('/api/films')
|
||||
async def createFilm(current_user: dict = Depends(get_current_user)):
|
||||
async def createFilm(
|
||||
film: Film,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not allowed")
|
||||
res = add_film({
|
||||
res = add_film(db, {
|
||||
'title': film.title,
|
||||
'director': film.director,
|
||||
'producer': film.producer,
|
||||
@@ -170,23 +197,33 @@ async def createFilm(current_user: dict = Depends(get_current_user)):
|
||||
'type': film.type
|
||||
})
|
||||
if res == None:
|
||||
raise HTTPException(status_code=400, detail="Badly formated body")
|
||||
raise HTTPException(status_code=400, detail="Error with request, database may be offline")
|
||||
return res
|
||||
|
||||
@app.get('/api/films')
|
||||
async def readFilm(page: int = 0, limit: int = 50, current_user: dict = Depends(get_current_user)):
|
||||
async def readFilm(
|
||||
page: int = 0,
|
||||
limit: int = 50,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not allowed")
|
||||
res = get_films(page, limit=limit)
|
||||
res = get_films(db, page, limit=limit)
|
||||
if res == None:
|
||||
raise HTTPException(status_code=400, detail="Badly formated body")
|
||||
raise HTTPException(status_code=400, detail="Error with request, database may be offline")
|
||||
return res
|
||||
|
||||
@app.put('/api/film/{id}')
|
||||
async def updateFilm(id: str, film: Film, current_user: dict = Depends(get_current_user)):
|
||||
async def updateFilm(
|
||||
id: str,
|
||||
film: Film,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not allowed")
|
||||
res = edit_film(id, {
|
||||
res = edit_film(db, id, {
|
||||
'title': film.title,
|
||||
'director': film.director,
|
||||
'producer': film.producer,
|
||||
@@ -195,32 +232,44 @@ async def updateFilm(id: str, film: Film, current_user: dict = Depends(get_curre
|
||||
'type': film.type
|
||||
})
|
||||
if res == None:
|
||||
raise HTTPException(status_code=400, detail="Badly formated body")
|
||||
raise HTTPException(status_code=400, detail="Error with request, database may be offline")
|
||||
return res
|
||||
|
||||
@app.delete('/api/film/{id}')
|
||||
async def deleteFilm(id: str, current_user: dict = Depends(get_current_user)):
|
||||
async def deleteFilm(
|
||||
id: str,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not allowed")
|
||||
res = remove_film(id)
|
||||
res = remove_film(db, id)
|
||||
if res == None:
|
||||
raise HTTPException(status_code=400, detail="Badly formated body")
|
||||
raise HTTPException(status_code=400, detail="Error with request, database may be offline")
|
||||
return res
|
||||
|
||||
@app.get('/api/books/{field}')
|
||||
async def getBookFields(field: str, current_user: dict = Depends(get_current_user)):
|
||||
async def getBookFields(
|
||||
field: str,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not allowed")
|
||||
if field == "":
|
||||
raise HTTPException(status_code=400, detail="Badly formated body")
|
||||
res = get_field_values('Books', field)
|
||||
raise HTTPException(status_code=400, detail="Error with request, database may be offline")
|
||||
res = get_field_values(db, 'Books', field)
|
||||
return res
|
||||
|
||||
@app.get('/api/films/{field}')
|
||||
async def getFilmFields(field: str, current_user: dict = Depends(get_current_user)):
|
||||
async def getFilmFields(
|
||||
field: str,
|
||||
current_user: dict = Depends(get_current_user),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not allowed")
|
||||
if field == "":
|
||||
raise HTTPException(status_code=400, detail="Badly formated body")
|
||||
res = get_field_values('Films', field)
|
||||
raise HTTPException(status_code=400, detail="Error with request, database may be offline")
|
||||
res = get_field_values(db, 'Films', field)
|
||||
return res
|
||||
Reference in New Issue
Block a user