|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | import time |
| 3 | +import uuid |
| 4 | +from distutils.util import strtobool |
| 5 | +from os import getenv |
3 | 6 | from typing import Optional |
4 | 7 |
|
| 8 | +from dotenv import load_dotenv |
5 | 9 | from fastapi import FastAPI, Header |
6 | 10 | from fastapi.responses import JSONResponse |
7 | 11 | from pydantic import BaseModel |
8 | | -from tinydb import TinyDB, Query |
| 12 | +from tinydb import Query, TinyDB |
9 | 13 |
|
10 | 14 | app = FastAPI(openapi_url=None, redoc_url=None) |
11 | | -db = TinyDB('data/db.json') |
12 | | -users = db.table('users') |
13 | | -documents = db.table('documents') |
14 | | - |
| 15 | +db = TinyDB("data/db.json") |
| 16 | +users = db.table("users") |
| 17 | +documents = db.table("documents") |
| 18 | +load_dotenv() |
15 | 19 |
|
16 | 20 | class KosyncUser(BaseModel): |
17 | 21 | username: Optional[str] = None |
@@ -110,10 +114,16 @@ def get_progress(document: Optional[str] = None, x_auth_user: Optional[str] = He |
110 | 114 | # get document progress if user has the document |
111 | 115 | result = documents.get((QDocument.username == x_auth_user) & (QDocument.document == document)) |
112 | 116 | if result: |
| 117 | + rrdi = bool(strtobool(getenv("RECEIVE_RANDOM_DEVICE_ID", "False"))) |
| 118 | + if rrdi == False: |
| 119 | + device_id = result["device_id"] |
| 120 | + else: |
| 121 | + device_id = uuid.uuid1() |
| 122 | + device_id = str(device_id.hex).upper() |
113 | 123 | return JSONResponse(status_code=200, |
114 | 124 | content={'username': x_auth_user, 'document': result["document"], |
115 | 125 | 'progress': result["progress"], 'percentage': result["percentage"], |
116 | | - 'device': result["device"], 'device_id': result["device_id"], |
| 126 | + 'device': result["device"], 'device_id': device_id, |
117 | 127 | 'timestamp': result["timestamp"]}) |
118 | 128 | else: |
119 | 129 | return JSONResponse(status_code=401, content={"message": f"Unauthorized"}) |
0 commit comments