|
|
|
|
@ -1,7 +1,9 @@
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from fastapi import FastAPI, File, UploadFile, Depends
|
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
|
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware # CORS
|
|
|
|
|
|
|
|
|
|
from secrets import compare_digest
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from uuid import uuid4
|
|
|
|
|
import sqlite3
|
|
|
|
|
@ -9,15 +11,16 @@ import sqlite3
|
|
|
|
|
|
|
|
|
|
# use database residing here
|
|
|
|
|
DB_LOCATION = (
|
|
|
|
|
"db/photovoter.dblite" # Q: any allowances for this being not OUR database?
|
|
|
|
|
"../testbox/photovoter.dblite" # Q: any allowances for this being not OUR database?
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
security = HTTPBasic()
|
|
|
|
|
con = sqlite3.connect(DB_LOCATION)
|
|
|
|
|
con.row_factory = sqlite3.Row
|
|
|
|
|
cur = con.cursor() # NB! single is enough for now, we might require multiple later
|
|
|
|
|
|
|
|
|
|
origins = [ # CORS
|
|
|
|
|
origins = [ # CORS
|
|
|
|
|
"*",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
@ -144,3 +147,29 @@ async def rate_picture(cookie: str, picture_id: int, mark: int):
|
|
|
|
|
return JSONResponse(status_code=406)
|
|
|
|
|
|
|
|
|
|
return {"status": "success"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post(
|
|
|
|
|
"/upload_pictures/",
|
|
|
|
|
responses={
|
|
|
|
|
401: {"description": "Authentication is required to access this resource"},
|
|
|
|
|
415: {"description": "Cannot process uploaded archive"},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
async def upload_pictures(
|
|
|
|
|
credentials: HTTPBasicCredentials = Depends(security), file: UploadFile = File(...)
|
|
|
|
|
):
|
|
|
|
|
"""Интерфейс для загрузки фотографий"""
|
|
|
|
|
"""Условно кладём в браузер zip с фотографиями и он их потихоньку ест.
|
|
|
|
|
Доступ к этому интерфейсу, наверное, лучше ограничить паролем или как-нибудь ещё.
|
|
|
|
|
Пока исходим из предположения, что только я буду загружать фотографии."""
|
|
|
|
|
# check authenticity
|
|
|
|
|
correct_username = compare_digest(credentials.username, "1")
|
|
|
|
|
correct_password = compare_digest(credentials.password, "1")
|
|
|
|
|
if not (correct_username and correct_password):
|
|
|
|
|
return JSONResponse(status_code=401)
|
|
|
|
|
# slurp the zip
|
|
|
|
|
# *detach from the interface, if possible
|
|
|
|
|
# unpack zip
|
|
|
|
|
# feed the pictures to util/import_photos.py
|
|
|
|
|
return {"filename": file.filename}
|
|
|
|
|
|