From 7df1ff35030d52839586dfc9968aa78e11561c87 Mon Sep 17 00:00:00 2001 From: rrr-marble Date: Thu, 23 Sep 2021 04:23:23 +0300 Subject: [PATCH] add: upload_pictures() prototype --- main.py | 31 +++++++++++++++++++++++++++++-- requirements.txt | 2 ++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 80cccb0..efdd438 100644 --- a/main.py +++ b/main.py @@ -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,10 +11,11 @@ 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 @@ -172,3 +175,27 @@ async def photo_points(): } for point in points ] +@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} diff --git a/requirements.txt b/requirements.txt index 58ce903..8c2bdf8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,7 +11,9 @@ pathspec==0.8.1 pycodestyle==2.7.0 pydantic==1.8.2 pyflakes==2.3.1 +python-multipart==0.0.5 regex==2021.4.4 +six==1.16.0 starlette==0.14.2 toml==0.10.2 typing-extensions==3.10.0.0